Skip to content

Fix ancestor handling - #1646

Merged
waylan merged 2 commits into
Python-Markdown:masterfrom
facelessuser:fix-ancestors
Sep 30, 2026
Merged

waylan merged 2 commits into
Python-Markdown:masterfrom
facelessuser:fix-ancestors

Conversation

@facelessuser

Copy link
Copy Markdown
Collaborator

Ancestors used to continually add duplicate parents that were already present.

Call __build_ancestors once at the beginning and then manage ancestors as appropriate.

Description

AI Assistance Disclosure

  • No AI tools were used in preparing this PR.
  • If AI tools were used, I have disclosed which ones, and fully reviewed and verified their output.

Checklist

Ancestors used to continually add duplicate parents that were already
present.

Call `__build_ancestors` once at the beginning and then manage
ancestors as appropriate.
@facelessuser

Copy link
Copy Markdown
Collaborator Author

To clarify the motivation for this change. The ancestor handling used to duplicate parents that were already added. This could cause real performance issues for certain patterns:

Before, parent tags would get duplicated. Notice div and p constantly being duplicated.

>>> import markdown
>>> text = '**_test_* ******test*******'
>>> markdown.markdown(text, extensions=['pymdownx.betterem'])
['div']
['div', 'p', 'div', 'p', 'em']
['div', 'p', 'div', 'p', 'em', 'div', 'p', 'em', 'em']
['div', 'p', 'div', 'p', 'em', 'div', 'p', 'em', 'strong']
['div', 'p', 'div', 'p', 'em', 'div', 'p', 'em', 'strong', 'div', 'p', 'em', 'strong', 'strong']
'<p><em><em><em>test</em></em> <strong><strong><strong>test</strong></strong></strong></em></p>'

Now, they get handled more reasonably:

➜  markdown_fork git:(fix-ancestors) ✗ python3
Python 3.14.7 (v3.14.7:823f0323ee6, Aug  5 2026, 07:07:01) [Clang 21.0.0 (clang-2100.1.1.101)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import markdown
>>> text = '**_test_* ******test*******'
>>> markdown.markdown(text, extensions=['pymdownx.betterem'])
['div']
['div', 'p', 'em']
['div', 'p', 'em', 'em']
['div', 'p', 'em', 'strong']
['div', 'p', 'em', 'strong', 'strong']
'<p><em><em><em>test</em></em> <strong><strong><strong>test</strong></strong></strong></em></p>'

This reduces performance degradation for certain patterns.

@facelessuser
facelessuser marked this pull request as ready for review September 30, 2026 14:08
@waylan
waylan merged commit 3002575 into Python-Markdown:master Sep 30, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants