@@ -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+ 
697717fn  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
702723fn  write_space ( _:  & crate :: pandoc:: Space ,  buf :  & mut  dyn  std:: io:: Write )  -> std:: io:: Result < ( ) >  { 
0 commit comments