Skip to content

Commit e05b7dc

Browse files
authored
fix md escaping issue with >, #, etc (#37)
1 parent 4c71dbc commit e05b7dc

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

crates/quarto-markdown-pandoc/src/writers/qmd.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -694,9 +694,30 @@ fn reverse_smart_quotes(text: &str) -> String {
694694
text.replace('\u{2019}', "'")
695695
}
696696

697+
// Helper function to escape special markdown characters
698+
// This follows Pandoc's escaping rules for text strings
699+
fn escape_markdown(text: &str) -> String {
700+
let mut result = String::new();
701+
for ch in text.chars() {
702+
match ch {
703+
// Backslash must be escaped first (conceptually)
704+
// But since we're processing char by char, we just escape it when we see it
705+
'\\' => result.push_str("\\\\"),
706+
// Greater-than sign must be escaped to avoid blockquote interpretation
707+
'>' => result.push_str("\\>"),
708+
// Hash must be escaped to avoid header interpretation
709+
'#' => result.push_str("\\#"),
710+
// Other characters pass through unchanged
711+
_ => result.push(ch),
712+
}
713+
}
714+
result
715+
}
716+
697717
fn write_str(s: &Str, buf: &mut dyn std::io::Write) -> std::io::Result<()> {
698-
// FIXME what are the escaping rules that Pandoc uses?
699-
write!(buf, "{}", reverse_smart_quotes(&s.text))
718+
let text = reverse_smart_quotes(&s.text);
719+
let escaped = escape_markdown(&text);
720+
write!(buf, "{}", escaped)
700721
}
701722

702723
fn write_space(_: &crate::pandoc::Space, buf: &mut dyn std::io::Write) -> std::io::Result<()> {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
\> has to be preserved.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
> is greater than <

0 commit comments

Comments
 (0)