diff --git a/moli-html2md/src/writer.rs b/moli-html2md/src/writer.rs
index fc6a8397ad..6684a58f56 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;
}
- // Adjacent code elements are distinct inline values. A separator is
- // required in Markdown; otherwise their code-span delimiters touch and
- // the rendered text is indistinguishable from one joined value.
+ // 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();
- self.output.push(' ');
- self.line_digits = None;
+ self.flush_code_html();
}
if preformatted {
if !text.is_empty() {
@@ -460,6 +457,25 @@ impl<'a> Writer<'a> {
}
}
+ fn flush_code_html(&mut self) {
+ if let Some(code) = self.code.take() {
+ self.output.push_str("");
+ for ch in code.chars() {
+ match ch {
+ '&' => self.output.push_str("&"),
+ '<' => self.output.push_str("<"),
+ '>' => self.output.push_str(">"),
+ '\\' | '`' | '*' | '_' | '[' | ']' | '|' | '~' => {
+ self.output.push('\\');
+ self.output.push(ch);
+ }
+ _ => self.output.push(ch),
+ }
+ }
+ self.output.push_str("");
+ 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 7523826777..ad98e53ba2 100644
--- a/moli-html2md/tests/conversion.rs
+++ b/moli-html2md/tests/conversion.rs
@@ -156,7 +156,7 @@ fn keeps_adjacent_code_elements_distinct_and_chooses_safe_delimiters() {
dom.text(0, "after");
assert_eq!(
rendered_html(&convert(&dom, 0)),
- "
a` `b ` after
a``b ` after
namestringname string
namestring
firstsuffixsecond", false),
@@ -166,7 +165,7 @@ fn empty_code_between_values_does_not_join_or_invent_code_text() {
let result = markdown(&source, preformatted);
assert_eq!(
rendered_html(&result),
- "name string
namestring