Skip to content
Merged
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
2 changes: 2 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ See the [Contributing Guide](contributing.md) for details.

### Fixed

* The `footnotes` extension no longer takes quadratic time to assign ids
to many references to the same footnote.
* The `toc` extension no longer raises `TypeError` for a heading whose
`data-toc-label` attribute is present but valueless, as the `attr_list`
extension's bare-attribute syntax (`{: data-toc-label }`) writes it: like a
Expand Down
7 changes: 7 additions & 0 deletions markdown/extensions/footnotes.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def __init__(self, **kwargs):
self.unique_prefix = 0
self.found_refs: dict[str, int] = {}
self.used_refs: set[str] = set()
self.last_refs: dict[str, str] = {}

# Backward compatibility with old '%d' placeholder
self.setConfig('BACKLINK_TITLE', self.getConfig("BACKLINK_TITLE").replace("%d", "{}"))
Expand Down Expand Up @@ -132,13 +133,18 @@ def reset(self) -> None:
self.unique_prefix += 1
self.found_refs = {}
self.used_refs = set()
self.last_refs = {}

def unique_ref(self, reference: str, found: bool = False) -> str:
""" Get a unique reference if there are duplicates. """
if not found:
return reference

original_ref = reference
# Resume from the last reference issued for this footnote. Every reference
# before it is already in `used_refs`, so restarting from `original_ref` would
# only rebuild them all again, which is quadratic in the number of duplicates.
reference = self.last_refs.get(original_ref, original_ref)
while reference in self.used_refs:
ref, rest = reference.split(self.get_separator(), 1)
m = RE_REF_ID.match(ref)
Expand All @@ -148,6 +154,7 @@ def unique_ref(self, reference: str, found: bool = False) -> str:
reference = '%s%d%s%s' % (ref, 2, self.get_separator(), rest)

self.used_refs.add(reference)
self.last_refs[original_ref] = reference
if original_ref in self.found_refs:
self.found_refs[original_ref] += 1
else:
Expand Down
12 changes: 12 additions & 0 deletions tests/test_syntax/extensions/test_footnotes.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
License: BSD (see LICENSE.md for details).
"""

from markdown import Markdown
from markdown.extensions.footnotes import FootnoteExtension
from markdown.test_tools import TestCase


Expand Down Expand Up @@ -773,6 +775,16 @@ def test_duplicate_footnote_references(self):
'</div>'
)

def test_many_duplicate_footnote_references(self):
"""Test that many references to one footnote get sequential ids."""
ext = FootnoteExtension()
Markdown(extensions=[ext]).convert('[^a]' * 2000 + '\n\n[^a]: A footnote.')
self.assertEqual(ext.found_refs, {'fnref:a': 2000})
self.assertEqual(
ext.used_refs,
{'fnref:a'} | {'fnref{}:a'.format(i) for i in range(2, 2001)}
)

def test_footnote_reference_without_definition(self):
"""Test footnote reference without corresponding definition."""

Expand Down
Loading