Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion markdown/blockprocessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
17 changes: 16 additions & 1 deletion markdown/extensions/footnotes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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. """
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()]
Expand Down
61 changes: 61 additions & 0 deletions tests/test_syntax/extensions/test_footnotes.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,67 @@ def test_footnote_reference_within_footnote_definition(self):
'</div>'
)

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
"""
),
'<p>Main<sup id="fnref:1"><a class="footnote-ref" href="#fn:1">1</a></sup>.</p>\n'
'<div class="footnote">\n'
'<hr />\n'
'<ol>\n'
'<li id="fn:1">\n'
'<p>First</p>\n'
'<p>[^2]: Second&#160;<a class="footnote-backref" href="#fnref:1" '
'title="Jump back to footnote 1 in the text">&#8617;</a></p>\n'
'</li>\n'
'</ol>\n'
'</div>'
)

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
"""
),
'<p>Main<sup id="fnref:1"><a class="footnote-ref" href="#fn:1">1</a></sup>.</p>\n'
'<div class="footnote">\n'
'<hr />\n'
'<ol>\n'
'<li id="fn:1">\n'
'<p>First [^2]</p>\n'
'<p>[^2]: Second&#160;<a class="footnote-backref" href="#fnref:1" '
'title="Jump back to footnote 1 in the text">&#8617;</a></p>\n'
'</li>\n'
'</ol>\n'
'</div>'
)

def test_footnote_reference_within_blockquote(self):
"""Test footnote reference within a blockquote."""

Expand Down
Loading