diff --git a/CHANGES.md b/CHANGES.md index e1c9c958..0ebf4ea7 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -7,6 +7,7 @@ - [pull #644] Fix a number of em/strong issues (#641, #642, #643) - [pull #659] Fix a number of safemode issues (#647) - [pull #665] Rewrite emphasis and strong processing to be more GFM compliant +- [pull #672] Fix nested footnote references (#664) ## python-markdown2 2.5.4 diff --git a/lib/markdown2.py b/lib/markdown2.py index 68dced73..9364550a 100755 --- a/lib/markdown2.py +++ b/lib/markdown2.py @@ -514,12 +514,7 @@ def convert(self, text: str) -> 'UnicodeWithAttrs': text = self._run_block_gamut(text) if "footnotes" in self.extras: - def footnote_sub(match): - normed_id = match.group(1) - self.footnote_ids.append(normed_id) - return str(len(self.footnote_ids)) - - text = re.sub(r'%s-(.*?)(?=)' % self._footnote_marker, footnote_sub, text) + text = self._do_footnote_marker(text) text = self._add_footnotes(text) text = self.postprocess(text) @@ -572,6 +567,15 @@ def toc_sort(entry): rv.metadata = self.metadata return rv + def _do_footnote_marker(self, text): + def footnote_sub(match): + normed_id = match.group(1) + if normed_id not in self.footnote_ids: + self.footnote_ids.append(normed_id) + return str(len(self.footnote_ids)) + + return re.sub(r'%s-(.*?)(?=)' % self._footnote_marker, footnote_sub, text) + @mark_stage(Stage.POSTPROCESS) def postprocess(self, text: str) -> str: """A hook for subclasses to do some postprocessing of the html, if @@ -2170,7 +2174,12 @@ def _add_footnotes(self, text: str) -> str: if i != 0: footer.append('') footer.append('