@@ -32,7 +32,7 @@ def render_referrences(self, state: BlockState) -> Iterable[str]:
3232 text = "[" + attrs ["label" ] + "]: " + attrs ["url" ]
3333 title = attrs .get ("title" )
3434 if title :
35- text += ' "' + title + '"'
35+ text += ' "' + _escape_title ( title ) + '"'
3636 yield text
3737
3838 def render_children (self , token : Dict [str , Any ], state : BlockState ) -> str :
@@ -71,7 +71,7 @@ def link(self, token: Dict[str, Any], state: BlockState) -> str:
7171 else :
7272 out += url
7373 if title :
74- out += ' "' + title + '"'
74+ out += ' "' + _escape_title ( title ) + '"'
7575 return out + ")"
7676
7777 def image (self , token : Dict [str , Any ], state : BlockState ) -> str :
@@ -188,6 +188,13 @@ def table_cell(self, token: Dict[str, Any], state: BlockState) -> str:
188188 return _render_table_cell (self , token , state )
189189
190190
191+ def _escape_title (title : str ) -> str :
192+ """Escape a link/image title for emission inside double quotes. The closing
193+ quote would otherwise end the title early on a re-parse; a backslash is
194+ escaped first so it can't combine with the following character."""
195+ return title .replace ("\\ " , "\\ \\ " ).replace ('"' , '\\ "' )
196+
197+
191198def _escape_block_prefix (text : str ) -> str :
192199 """Backslash-escape a leading block marker on each line so that literal
193200 text is not re-parsed as a list, heading or block quote."""
0 commit comments