From 4d20ff3382736498a5e0c50bf33d3a2ca76e7cd1 Mon Sep 17 00:00:00 2001 From: Tony Y Date: Mon, 16 Nov 2026 17:04:57 +0000 Subject: [PATCH] Do not recognize footnote definitions within a footnote An indented footnote definition nested in another footnote's content was extracted when the parent footnote was rendered, too late to be included in the footnotes div: the definition text was silently dropped and references to it produced links to a missing anchor. Matching PHP Markdown Extra, definitions are no longer recognized within a footnote definition: the FootnoteBlockProcessor is disabled while footnote content is being rendered, so a nested definition remains as text in the parent footnote and references to it stay unresolved. The ReferenceProcessor no longer treats a definition with an identifier starting with '^' as a link reference, as that is footnote definition syntax; this keeps the nested definition from being swallowed as a link reference instead. Fixes #1681 --- docs/changelog.md | 6 ++ markdown/blockprocessors.py | 2 +- markdown/extensions/footnotes.py | 17 +++++- .../test_syntax/extensions/test_footnotes.py | 61 +++++++++++++++++++ 4 files changed, 84 insertions(+), 2 deletions(-) diff --git a/docs/changelog.md b/docs/changelog.md index 043bfa2d7..b44d23f89 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -16,6 +16,12 @@ See the [Contributing Guide](contributing.md) for details. ### Fixed +* Footnote definitions are no longer recognized within a footnote definition, + matching PHP Markdown Extra: an indented definition nested in another + footnote's content now remains as text in the parent footnote instead of + being silently dropped and leaving references pointing at a missing + `#fn:` anchor (#1681). Link reference definitions can no longer use an + identifier starting with `^`, which is footnote definition syntax. * The `toc` extension no longer raises `TypeError` for a heading whose `id` attribute is present but valueless, as the `attr_list` extension's bare-attribute syntax (`{id}`) writes it: like a heading with no `id` at diff --git a/markdown/blockprocessors.py b/markdown/blockprocessors.py index 11ff0ddf0..1d740c954 100644 --- a/markdown/blockprocessors.py +++ b/markdown/blockprocessors.py @@ -588,7 +588,7 @@ def run(self, parent: etree.Element, blocks: list[str]) -> None: class ReferenceProcessor(BlockProcessor): """ Process link references. """ RE = re.compile( - r'^[ ]{0,3}\[([^\[\]]*)\]:[ ]*(?:\n[ ]*)?([^\s]+)[ ]*(?:\n[ ]*)?((["\'])(.*)\4[ ]*|\((.*)\)[ ]*)?$', + r'^[ ]{0,3}\[((?!\^)[^\[\]]*)\]:[ ]*(?:\n[ ]*)?([^\s]+)[ ]*(?:\n[ ]*)?((["\'])(.*)\4[ ]*|\((.*)\)[ ]*)?$', re.MULTILINE ) diff --git a/markdown/extensions/footnotes.py b/markdown/extensions/footnotes.py index 366f740ad..18940d543 100644 --- a/markdown/extensions/footnotes.py +++ b/markdown/extensions/footnotes.py @@ -132,6 +132,10 @@ def reset(self) -> None: self.unique_prefix += 1 self.found_refs = {} self.used_refs = set() + # While footnote content is being rendered, definitions found in it + # are not recognized (matching PHP Markdown Extra): a definition + # nested in another footnote's content remains part of that content. + self.parsing_footnotes = False def unique_ref(self, reference: str, found: bool = False) -> str: """ Get a unique reference if there are duplicates. """ @@ -219,7 +223,11 @@ def makeFootnotesDiv(self, root: etree.Element) -> etree.Element | None: # Parse footnote with surrogate parent as `li` cannot be used. # List block handlers have special logic to deal with `li`. # When we are done parsing, we will copy everything over to `li`. - self.parser.parseChunk(surrogate_parent, self.footnotes[id]) + self.parsing_footnotes = True + try: + self.parser.parseChunk(surrogate_parent, self.footnotes[id]) + finally: + self.parsing_footnotes = False for el in list(surrogate_parent): li.append(el) surrogate_parent.remove(el) @@ -259,6 +267,13 @@ def run(self, parent: etree.Element, blocks: list[str]) -> bool: """ Find, set, and remove footnote definitions. """ block = blocks.pop(0) + if self.footnotes.parsing_footnotes: + # Footnote definitions are not recognized within a footnote + # definition. Leave the block as content of the footnote + # currently being rendered. + blocks.insert(0, block) + return False + m = self.RE.search(block) if m: preceding = block[:m.start()] diff --git a/tests/test_syntax/extensions/test_footnotes.py b/tests/test_syntax/extensions/test_footnotes.py index 5cd1037d0..fd7da5fbf 100644 --- a/tests/test_syntax/extensions/test_footnotes.py +++ b/tests/test_syntax/extensions/test_footnotes.py @@ -629,6 +629,67 @@ def test_footnote_reference_within_footnote_definition(self): '' ) + def test_indented_footnote_definition_within_footnote_definition(self): + """Test an indented footnote definition within a footnote definition. + + Footnote definitions are not recognized within a footnote definition + (matching PHP Markdown Extra): the nested definition remains as text + in the parent footnote rather than being dropped or extracted. + """ + + self.assertMarkdownRenders( + self.dedent( + """ + Main[^1]. + + [^1]: First + + [^2]: Second + """ + ), + '

Main1.

\n' + '
\n' + '
\n' + '
    \n' + '
  1. \n' + '

    First

    \n' + '

    [^2]: Second ↩

    \n' + '
  2. \n' + '
\n' + '
' + ) + + def test_referenced_indented_footnote_definition_within_footnote_definition(self): + """Test a reference to an indented definition nested in a footnote. + + As the nested definition is not recognized, the reference is not + resolved either and remains as text; no dead `#fn:2` anchor is made. + """ + + self.assertMarkdownRenders( + self.dedent( + """ + Main[^1]. + + [^1]: First [^2] + + [^2]: Second + """ + ), + '

Main1.

\n' + '
\n' + '
\n' + '
    \n' + '
  1. \n' + '

    First [^2]

    \n' + '

    [^2]: Second ↩

    \n' + '
  2. \n' + '
\n' + '
' + ) + def test_footnote_reference_within_blockquote(self): """Test footnote reference within a blockquote."""