Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

Preserve empty pre blocks and adjacent code text

kevinead6facb8704
diff --git a/moli-html2md/src/machine.rs b/moli-html2md/src/machine.rs
index f488bde91..236beec17 100644
--- a/moli-html2md/src/machine.rs
+++ b/moli-html2md/src/machine.rs
@@ -132,7 +132,7 @@ impl<'a, D: Dom + ?Sized> Machine<'a, D> {
}
Task::RawChildren(Some(node), depth, inline) => self.raw_node(node, depth, inline),
Task::EndRawBlock => {
- if !self.raw.ends_with('\n') {
+ if !self.raw.is_empty() && !self.raw.ends_with('\n') {
self.raw.push('\n');
}
}
diff --git a/moli-html2md/src/writer.rs b/moli-html2md/src/writer.rs
index 057e177e9..6684a58f5 100644
--- a/moli-html2md/src/writer.rs
+++ b/moli-html2md/src/writer.rs
@@ -176,13 +176,10 @@ impl<'a> Writer<'a> {
if text.is_empty() {
return;
}
- // Each call represents a separate HTML code element. Keep adjacent
- // elements distinct even when the source has no whitespace between them.
+ // Adjacent code elements have separate HTML nodes but no text between
+ // them. Markdown code spans would merge or need an invented space.
if self.code.is_some() {
- self.flush_code();
- if !text.is_empty() {
- self.space = true;
- }
+ self.flush_code_html();
}
if preformatted {
if !text.is_empty() {
@@ -459,6 +456,26 @@ impl<'a> Writer<'a> {
self.line_digits = None;
}
}
+
+ fn flush_code_html(&mut self) {
+ if let Some(code) = self.code.take() {
+ self.output.push_str("<code>");
+ for ch in code.chars() {
+ match ch {
+ '&' => self.output.push_str("&amp;"),
+ '<' => self.output.push_str("&lt;"),
+ '>' => self.output.push_str("&gt;"),
+ '\\' | '`' | '*' | '_' | '[' | ']' | '|' | '~' => {
+ self.output.push('\\');
+ self.output.push(ch);
+ }
+ _ => self.output.push(ch),
+ }
+ }
+ self.output.push_str("</code>");
+ self.line_digits = None;
+ }
+ }
}
fn is_punctuation(ch: char) -> bool {
diff --git a/moli-html2md/tests/conversion.rs b/moli-html2md/tests/conversion.rs
index 46b855872..ad98e53ba 100644
--- a/moli-html2md/tests/conversion.rs
+++ b/moli-html2md/tests/conversion.rs
@@ -154,7 +154,10 @@ fn keeps_adjacent_code_elements_distinct_and_chooses_safe_delimiters() {
dom.text(0, " ");
dom.leaf(0, "code", " ` ");
dom.text(0, "after");
- assert_eq!(convert(&dom, 0), "`` a` `` `` `b `` `` ` `` after");
+ assert_eq!(
+ rendered_html(&convert(&dom, 0)),
+ "<p><code>a`</code><code>`b</code> <code>`</code> after</p>\n"
+ );
}
#[test]
diff --git a/moli-html2md/tests/inline_boundaries.rs b/moli-html2md/tests/inline_boundaries.rs
index f75981c77..51fe74785 100644
--- a/moli-html2md/tests/inline_boundaries.rs
+++ b/moli-html2md/tests/inline_boundaries.rs
@@ -76,7 +76,10 @@ fn inline_code_breaks_keep_separation_at_code_span_edges() {
..Options::default()
},
);
- assert_eq!(result, "before`git ` `status`after");
+ assert_eq!(
+ rendered_html(&result),
+ "<p>before<code>git </code><code>status</code>after</p>\n"
+ );
}
#[test]
@@ -132,6 +135,29 @@ fn preformatted_examples_keep_inline_values_and_separate_display_lines() {
}
}
+#[test]
+fn empty_wrappers_do_not_add_lines_to_preformatted_examples() {
+ for (source, expected) in [
+ ("<pre><div><!-- note --></div>print(42)</pre>", "print(42)"),
+ (
+ "<pre><section><div></div></section>print(42)</pre>",
+ "print(42)",
+ ),
+ (
+ "<pre>first<div>second</div>third</pre>",
+ "first\nsecond\nthird",
+ ),
+ ("<pre><span>\n</span>print(42)</pre>", "\nprint(42)"),
+ ] {
+ let result = markdown(source);
+ assert_eq!(
+ rendered_html(&result),
+ format!("<pre><code>{expected}\n</code></pre>\n"),
+ "{source}: {result}"
+ );
+ }
+}
+
#[test]
fn nested_emphasis_closes_with_markdown_before_a_word() {
for (html, expected, expected_html) in [
diff --git a/moli-html2md/tests/regressions.rs b/moli-html2md/tests/regressions.rs
index 17ca74023..9b6ce3e8d 100644
--- a/moli-html2md/tests/regressions.rs
+++ b/moli-html2md/tests/regressions.rs
@@ -147,10 +147,9 @@ fn normal_code_moves_unicode_boundary_spaces_outside_delimiters() {
#[test]
fn adjacent_code_elements_keep_distinct_values() {
let html = "<div><code>name</code><code>string</code></div>";
- assert_eq!(markdown(html, false), "`name` `string`");
assert_eq!(
rendered_html(&markdown(html, false)),
- "<p><code>name</code> <code>string</code></p>\n"
+ "<p><code>name</code><code>string</code></p>\n"
);
assert_eq!(
markdown("<code>first</code>suffix<code>second</code>", false),
@@ -166,13 +165,42 @@ fn empty_code_between_values_does_not_join_or_invent_code_text() {
let result = markdown(&source, preformatted);
assert_eq!(
rendered_html(&result),
- "<p><code>name</code> <code>string</code></p>\n",
+ "<p><code>name</code><code>string</code></p>\n",
"{source}, preformatted={preformatted}: {result}"
);
}
}
}
+#[test]
+fn neighboring_code_values_keep_their_text_and_node_boundaries() {
+ for (source, preformatted, expected) in [
+ (
+ "<code>git </code><code>status</code>",
+ true,
+ "<p><code>git </code><code>status</code></p>\n",
+ ),
+ (
+ "<code>--</code><code>help</code>",
+ false,
+ "<p><code>--</code><code>help</code></p>\n",
+ ),
+ (
+ "<code>*[x]|</code><code>name</code>",
+ false,
+ "<p><code>*[x]|</code><code>name</code></p>\n",
+ ),
+ (
+ "<code>first</code><code></code><code>second</code>",
+ true,
+ "<p><code>first</code><code>second</code></p>\n",
+ ),
+ ] {
+ let result = markdown(source, preformatted);
+ assert_eq!(rendered_html(&result), expected, "{source}: {result}");
+ }
+}
+
#[test]
fn preformatted_block_children_keep_text_boundaries() {
let html = "<pre><div>ts</div><div><code>function identity() {}</code></div></pre>";
diff --git a/moli-html2md/tests/turndown/expectations.json b/moli-html2md/tests/turndown/expectations.json
index 15a45d349..451003d61 100644
--- a/moli-html2md/tests/turndown/expectations.json
+++ b/moli-html2md/tests/turndown/expectations.json
@@ -4,8 +4,8 @@
"reason": "Moli fences bare pre elements as well as pre/code, preserving preformatted text. Turndown only recognizes a pre/code pair."
},
"extra_adjacent_code": {
- "markdown": "`a` `b`",
- "reason": "Separate HTML code elements remain separate Markdown code spans, preserving their semantic boundary. Turndown joins their delimiters into one code span whose text contains two extra backticks."
+ "markdown": "<code>a</code>`b`",
+ "reason": "Adjacent code elements retain their semantic boundary without an invented space. Inline HTML preserves the first boundary because adjacent Markdown code spans would merge. Turndown joins their delimiters into one code span whose text contains two extra backticks."
},
"extra_code_nested_format": {
"markdown": "`ab`",