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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- [pull #701] Allow boolean attribute syntax in `markdown-in-html` extra
- [pull #704] Fix XSS from smuggling spans into image attributes (#702, #703)
- [pull #710] Add emoji support (#709)
- Fix `header-ids` extra generating duplicate ids when a suffixed id collides with another header (#661)


## python-markdown2 2.5.5
Expand Down
16 changes: 13 additions & 3 deletions lib/markdown2.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,8 @@ def _setup_extras(self):
if "header-ids" in self.extras:
if not hasattr(self, '_count_from_header_id') or self.extras['header-ids'].get('reset-count', False):
self._count_from_header_id = defaultdict(int)
if not hasattr(self, '_header_ids_seen') or self.extras['header-ids'].get('reset-count', False):
self._header_ids_seen = set()
if "metadata" in self.extras:
self.metadata: dict[str, Any] = {}

Expand Down Expand Up @@ -1582,9 +1584,17 @@ def header_id_from_text(self,
if prefix and isinstance(prefix, str):
header_id = prefix + '-' + header_id

self._count_from_header_id[header_id] += 1
if 0 == len(header_id) or self._count_from_header_id[header_id] > 1:
header_id += '-%s' % self._count_from_header_id[header_id]
base_id = header_id
self._count_from_header_id[base_id] += 1
if 0 == len(base_id) or self._count_from_header_id[base_id] > 1:
header_id = '%s-%s' % (base_id, self._count_from_header_id[base_id])
# A suffixed id may still collide with a differently-named header
# (e.g. "# Chapter" twice yields "chapter-2", which clashes with
# "# Chapter 2"). Keep bumping until the id is genuinely unique.
while header_id in self._header_ids_seen:
self._count_from_header_id[base_id] += 1
header_id = '%s-%s' % (base_id, self._count_from_header_id[base_id])
self._header_ids_seen.add(header_id)

return header_id

Expand Down
11 changes: 11 additions & 0 deletions test/tm-cases/header_ids_duplicate.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<h1 id="chapter">Chapter</h1>

<p>Test</p>

<h1 id="chapter-2">Chapter</h1>

<p>Test</p>

<h1 id="chapter-2-2">Chapter 2</h1>

<p>Test</p>
1 change: 1 addition & 0 deletions test/tm-cases/header_ids_duplicate.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"extras": ["header-ids"]}
1 change: 1 addition & 0 deletions test/tm-cases/header_ids_duplicate.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extra header-ids issue661
11 changes: 11 additions & 0 deletions test/tm-cases/header_ids_duplicate.text
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Chapter

Test

# Chapter

Test

# Chapter 2

Test
Loading