From 4f253ff1bfaa586aa6484b511ce195cfe23ae713 Mon Sep 17 00:00:00 2001 From: Tony Y Date: Mon, 16 Nov 2026 17:04:57 +0000 Subject: [PATCH 1/4] 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 49de6cc01..fbffca516 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -26,6 +26,12 @@ See the [Contributing Guide](contributing.md) for details. `data-toc-label` attribute is present but valueless, as the `attr_list` extension's bare-attribute syntax (`{: data-toc-label }`) writes it: like a heading with no `data-toc-label`, its text is used as the TOC name (#1712). +* 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 580cf5946..3219cc44e 100644 --- a/markdown/extensions/footnotes.py +++ b/markdown/extensions/footnotes.py @@ -134,6 +134,10 @@ def reset(self) -> None: self.found_refs = {} self.used_refs = set() self.last_refs = {} + # 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. """ @@ -226,7 +230,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) @@ -266,6 +274,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 0b372e51c..ddb343190 100644 --- a/tests/test_syntax/extensions/test_footnotes.py +++ b/tests/test_syntax/extensions/test_footnotes.py @@ -631,6 +631,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.""" From 2061e426d2b8582caf66678dff6135476a41f628 Mon Sep 17 00:00:00 2001 From: tony1233200 <127579663+tony1233200@users.noreply.github.com> Date: Thu, 26 Nov 2026 06:33:43 +0000 Subject: [PATCH 2/4] Move the caret exclusion out of the core reference processor The core ReferenceProcessor must not know about footnotes: the '^' exclusion now lives in a FootnoteReferenceProcessor in the footnotes extension, registered under the same name and priority only while the extension is loaded. Without the extension, '[^a]: ...' is still a link reference. Fixes #1681 --- docs/changelog.md | 6 ++-- markdown/blockprocessors.py | 2 +- markdown/extensions/footnotes.py | 31 ++++++++++++++++++- .../blocks/test_reference_links.py | 12 +++++++ 4 files changed, 47 insertions(+), 4 deletions(-) diff --git a/docs/changelog.md b/docs/changelog.md index fbffca516..e0e7bef34 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -30,8 +30,10 @@ See the [Contributing Guide](contributing.md) for details. 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. + `#fn:` anchor (#1681). When the `footnotes` extension is enabled, link + reference definitions can no longer use an identifier starting with `^`, + which is footnote definition syntax; without the extension the core + behavior is unchanged. * 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 1d740c954..11ff0ddf0 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 3219cc44e..e381cf5cc 100644 --- a/markdown/extensions/footnotes.py +++ b/markdown/extensions/footnotes.py @@ -20,7 +20,7 @@ from __future__ import annotations from . import Extension -from ..blockprocessors import BlockProcessor +from ..blockprocessors import BlockProcessor, ReferenceProcessor from ..inlinepatterns import InlineProcessor from ..treeprocessors import Treeprocessor from ..postprocessors import Postprocessor @@ -89,6 +89,7 @@ def extendMarkdown(self, md): | Class Instance | Registry | Name | Priority | | --------------------------------------------------------------------------- | ---------------------------------------------------------------- | ------ | :------: | | [`FootnoteBlockProcessor`][markdown.extensions.footnotes.FootnoteBlockProcessor] | [`blockprocessors`][markdown.blockprocessors.build_block_parser] | `footnote` | `17` | + | [`FootnoteReferenceProcessor`][markdown.extensions.footnotes.FootnoteReferenceProcessor] | [`blockprocessors`][markdown.blockprocessors.build_block_parser] | `reference` | `15` | | [`FootnoteInlineProcessor`][markdown.extensions.footnotes.FootnoteInlineProcessor] | [`inlinepatterns`][markdown.inlinepatterns.build_inlinepatterns] | `footnote` | `175` | | [`FootnoteTreeprocessor`][markdown.extensions.footnotes.FootnoteTreeprocessor] | [`treeprocessors`][markdown.treeprocessors.build_treeprocessors] | `footnote` | `50` | | [`FootnoteReorderingProcessor`][markdown.extensions.footnotes.FootnoteReorderingProcessor] | [`treeprocessors`][markdown.treeprocessors.build_treeprocessors] | `footnote-reorder` | `19` | @@ -103,6 +104,12 @@ def extendMarkdown(self, md): # Insert a `blockprocessor` before `ReferencePreprocessor` md.parser.blockprocessors.register(FootnoteBlockProcessor(self), 'footnote', 17) + # Replace the core `ReferenceProcessor` (same name and priority) + # while this extension is loaded, so a definition starting with + # `^` is never taken as a link reference. Without the extension + # the core processor -- and its behavior -- is untouched. + md.parser.blockprocessors.register(FootnoteReferenceProcessor(md.parser), 'reference', 15) + # Insert an inline pattern before `ImageReferencePattern` FOOTNOTE_RE = r'\[\^([^\]]*)\]' # blah blah [^1] blah md.inlinePatterns.register(FootnoteInlineProcessor(FOOTNOTE_RE, self), 'footnote', 175) @@ -258,6 +265,28 @@ def makeFootnotesDiv(self, root: etree.Element) -> etree.Element | None: return div +class FootnoteReferenceProcessor(ReferenceProcessor): + """ Process link references, except footnote definitions. + + The core knows nothing about extensions, so the core + `ReferenceProcessor` is replaced -- only while this extension is + loaded -- with one that does not treat a definition whose + identifier starts with `^` as a link reference: that is footnote + definition syntax. This matters inside footnote content, where + the `FootnoteBlockProcessor` is disabled: without this, a nested + definition would be swallowed as a link reference instead of + remaining as text in the parent footnote. + + The pattern is `ReferenceProcessor.RE` with a negative lookahead + for a leading `^` on the identifier; keep the two in sync. + """ + + RE = re.compile( + r'^[ ]{0,3}\[((?!\^)[^\[\]]*)\]:[ ]*(?:\n[ ]*)?([^\s]+)[ ]*(?:\n[ ]*)?((["\'])(.*)\4[ ]*|\((.*)\)[ ]*)?$', + re.MULTILINE + ) + + class FootnoteBlockProcessor(BlockProcessor): """ Find footnote definitions and store for later use. """ diff --git a/tests/test_syntax/blocks/test_reference_links.py b/tests/test_syntax/blocks/test_reference_links.py index 5c9884b09..ccdeec52f 100644 --- a/tests/test_syntax/blocks/test_reference_links.py +++ b/tests/test_syntax/blocks/test_reference_links.py @@ -30,6 +30,18 @@ def test_reference_link(self): '

Text

' ) + def test_reference_link_with_caret_id(self): + """A `^`-leading id is still a link reference without extensions. + + The `footnotes` extension (see #1681) stops treating such ids as + link references while it is loaded, but the core must keep + working as normal on its own. + """ + self.assertMarkdownRenders( + '[Text][^a]\n\n[^a]: http://example.com', + '

Text

' + ) + def test_reference_link_split_across_lines(self): self.assertMarkdownRenders( '[Text][id]\n\n[id]:\nhttp://example.com', From 609bb6624e2899fe7dfa36acceedb506d4d2c80f Mon Sep 17 00:00:00 2001 From: Waylan Limberg Date: Thu, 26 Nov 2026 15:37:31 -0500 Subject: [PATCH 3/4] fix spelling --- markdown/extensions/footnotes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/markdown/extensions/footnotes.py b/markdown/extensions/footnotes.py index e381cf5cc..be3b3137b 100644 --- a/markdown/extensions/footnotes.py +++ b/markdown/extensions/footnotes.py @@ -277,7 +277,7 @@ class FootnoteReferenceProcessor(ReferenceProcessor): definition would be swallowed as a link reference instead of remaining as text in the parent footnote. - The pattern is `ReferenceProcessor.RE` with a negative lookahead + The pattern is `ReferenceProcessor.RE` with a negative look-ahead for a leading `^` on the identifier; keep the two in sync. """ From a435dbb5b76ead2bd8814aeceb2a9a53cde00ecd Mon Sep 17 00:00:00 2001 From: tony1233200 Date: Thu, 26 Nov 2026 22:37:05 +0000 Subject: [PATCH 4/4] Move changelog entry to unreleased The 3.11.4 release landed while this PR was in review; the #1681 entry moves back under a new Unreleased section. --- docs/changelog.md | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/docs/changelog.md b/docs/changelog.md index e0e7bef34..dca67c464 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -12,6 +12,19 @@ and this project adheres to the [Python Version Specification](https://packaging.python.org/en/latest/specifications/version-specifiers/). See the [Contributing Guide](contributing.md) for details. +## Unreleased + +### 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). When the `footnotes` extension is enabled, link + reference definitions can no longer use an identifier starting with `^`, + which is footnote definition syntax; without the extension the core + behavior is unchanged. + ## [3.11.4] - 2026-11-25 ### Fixed @@ -26,14 +39,6 @@ See the [Contributing Guide](contributing.md) for details. `data-toc-label` attribute is present but valueless, as the `attr_list` extension's bare-attribute syntax (`{: data-toc-label }`) writes it: like a heading with no `data-toc-label`, its text is used as the TOC name (#1712). -* 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). When the `footnotes` extension is enabled, link - reference definitions can no longer use an identifier starting with `^`, - which is footnote definition syntax; without the extension the core - behavior is unchanged. * 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