diff --git a/crates/quarto-markdown-pandoc/src/writers/qmd.rs b/crates/quarto-markdown-pandoc/src/writers/qmd.rs index 95becee..4be67e0 100644 --- a/crates/quarto-markdown-pandoc/src/writers/qmd.rs +++ b/crates/quarto-markdown-pandoc/src/writers/qmd.rs @@ -694,9 +694,30 @@ fn reverse_smart_quotes(text: &str) -> String { text.replace('\u{2019}', "'") } +// Helper function to escape special markdown characters +// This follows Pandoc's escaping rules for text strings +fn escape_markdown(text: &str) -> String { + let mut result = String::new(); + for ch in text.chars() { + match ch { + // Backslash must be escaped first (conceptually) + // But since we're processing char by char, we just escape it when we see it + '\\' => result.push_str("\\\\"), + // Greater-than sign must be escaped to avoid blockquote interpretation + '>' => result.push_str("\\>"), + // Hash must be escaped to avoid header interpretation + '#' => result.push_str("\\#"), + // Other characters pass through unchanged + _ => result.push(ch), + } + } + result +} + fn write_str(s: &Str, buf: &mut dyn std::io::Write) -> std::io::Result<()> { - // FIXME what are the escaping rules that Pandoc uses? - write!(buf, "{}", reverse_smart_quotes(&s.text)) + let text = reverse_smart_quotes(&s.text); + let escaped = escape_markdown(&text); + write!(buf, "{}", escaped) } fn write_space(_: &crate::pandoc::Space, buf: &mut dyn std::io::Write) -> std::io::Result<()> { diff --git a/crates/quarto-markdown-pandoc/tests/roundtrip_tests/qmd-json-qmd/escape_greater_than.qmd b/crates/quarto-markdown-pandoc/tests/roundtrip_tests/qmd-json-qmd/escape_greater_than.qmd new file mode 100644 index 0000000..a4f2aee --- /dev/null +++ b/crates/quarto-markdown-pandoc/tests/roundtrip_tests/qmd-json-qmd/escape_greater_than.qmd @@ -0,0 +1 @@ +\> has to be preserved. diff --git a/crates/quarto-markdown-pandoc/tests/roundtrip_tests/qmd-json-qmd/greater_than_escape.qmd b/crates/quarto-markdown-pandoc/tests/roundtrip_tests/qmd-json-qmd/greater_than_escape.qmd new file mode 100644 index 0000000..a90812d --- /dev/null +++ b/crates/quarto-markdown-pandoc/tests/roundtrip_tests/qmd-json-qmd/greater_than_escape.qmd @@ -0,0 +1 @@ +> is greater than <