mirror of
https://github.com/lexmount/moli.git
synced 2026-11-17 08:08:43 +00:00
fix(html2md): retain boundaries of expanded block elements
This commit is contained in:
@@ -216,17 +216,11 @@ pub(crate) fn render<D: Dom + ?Sized>(
|
||||
continue;
|
||||
}
|
||||
if let NodeKind::Element(tag) = dom.node_kind(node) {
|
||||
if !code
|
||||
&& dom.has_block_layout(node)
|
||||
&& !crate::structure::is_block(tag)
|
||||
&& !crate::structure::owns_boundaries(tag)
|
||||
{
|
||||
let owns_boundaries = allowed(tag) && crate::structure::owns_boundaries(tag);
|
||||
if !code && dom.has_block_layout(node) && !owns_boundaries {
|
||||
output.push_str("<div>");
|
||||
tasks.push(Task::Close("div".to_owned()));
|
||||
} else if !code
|
||||
&& dom.has_text_boundary(node)
|
||||
&& !crate::structure::owns_boundaries(tag)
|
||||
{
|
||||
} else if !code && dom.has_text_boundary(node) && !owns_boundaries {
|
||||
output.push(' ');
|
||||
tasks.push(Task::Literal(" "));
|
||||
}
|
||||
|
||||
@@ -1084,6 +1084,40 @@ fn fallback_keeps_code_boxes_and_ignores_column_boxes() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stripped_block_elements_keep_their_computed_boundaries_in_tables() {
|
||||
let content =
|
||||
"<output style='display:block'>A</output><output style='display:block'>B</output>";
|
||||
for source in [
|
||||
format!("<div>{content}</div>"),
|
||||
format!("<table><tr><td>{content}</td><td>Other</td></tr></table>"),
|
||||
format!("<table><tr><td colspan='2'>{content}</td></tr></table>"),
|
||||
] {
|
||||
let dom = Counted {
|
||||
tree: Tree::parse(&source),
|
||||
visits: Cell::new(0),
|
||||
};
|
||||
let markdown = convert(&dom, dom.tree.root);
|
||||
let output = Tree::parse(&rendered_html(&markdown));
|
||||
let mut text = String::new();
|
||||
for node in &output.nodes {
|
||||
if matches!(node.tag.as_deref(), Some("p" | "div" | "td" | "br")) {
|
||||
text.push(' ');
|
||||
}
|
||||
if let Some(value) = &node.text {
|
||||
text.push_str(value);
|
||||
}
|
||||
}
|
||||
assert!(
|
||||
text.split_whitespace()
|
||||
.collect::<Vec<_>>()
|
||||
.windows(2)
|
||||
.any(|words| words == ["A", "B"]),
|
||||
"{source}: {markdown}\n{text}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn content_after_a_nested_list_can_make_the_outer_list_loose() {
|
||||
assert_eq!(
|
||||
|
||||
Reference in New Issue
Block a user