Skip to content

Initialize TOC when file variables enable the extra - #740

Merged
nicholasserra merged 1 commit into
trentm:masterfrom
agammann:fix/toc-from-file-vars
Oct 16, 2026
Merged

nicholasserra merged 1 commit into
trentm:masterfrom
agammann:fix/toc-from-file-vars

Conversation

@agammann

Copy link
Copy Markdown
Contributor

Problem

With use_file_vars=True, a document beginning with <!-- -*- markdown-extras: toc -*- --> crashes while rendering its first heading (UnboundLocalError: header_id). Declaring header-ids, toc in that file variable instead crashes because _toc_depth was never set. TOC setup currently runs only for constructor extras, before file variables are read.

Change

Run the existing TOC setup when extras are refreshed, so file variables get the same implied heading IDs and depth initialization as constructor extras. Add a regression covering both file-variable forms and repeated conversions with the same Markdown instance. Document the TOC file-variable form in the README.

Verification

  • Reproduced both crashes on upstream d030ca7f9ef55b73f143450c5835e48f4b5d4043 before the fix.
  • py -3.14 testall.py from test/: 289 passed.
  • py test.py -- -knownfailure from test/: 289 passed.
  • --use-file-vars CLI rendering emits a TOC and heading ID for the example modeline.
  • git diff --check and changed-line PEP8 check passed.

@nicholasserra

Copy link
Copy Markdown
Collaborator

Thank you!

@nicholasserra
nicholasserra merged commit e69bbdf into trentm:master Oct 16, 2026
18 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