Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions crates/quarto-markdown-pandoc/src/writers/qmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()> {
Expand Down Expand Up @@ -968,8 +989,8 @@ fn write_rawinline(
if raw.format == "markdown" {
write!(buf, "{}", raw.text)
} else {
// For other formats, use raw span notation
write!(buf, "`{}`{{{}}}", raw.text, raw.format)
// For other formats, use raw span notation with = prefix
write!(buf, "`{}`{{={}}}", raw.text, raw.format)
}
}
fn write_note(note: &crate::pandoc::Note, buf: &mut dyn std::io::Write) -> std::io::Result<()> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\> has to be preserved.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
> is greater than <
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<sub>hello</sub>