From 5bc8020714a9e81428efc4c264acacaccff48dec Mon Sep 17 00:00:00 2001 From: agammann Date: Tue, 13 Oct 2026 20:27:27 -0700 Subject: [PATCH] Fix TOC setup for file-variable extras --- README.md | 4 +++- lib/markdown2.py | 21 ++++++++++++--------- test/test_markdown2.py | 11 +++++++++++ 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index eeb9f109..0051a940 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,9 @@ $ markdown2 foo.md > foo.html Use `--use-file-vars` on the CLI, or `use_file_vars=True` with `markdown()`, `markdown_path()` or `Markdown()`, to enable extras declared in Emacs-style file variables such as `` at the top -of a file. This option is disabled by default and is not an extra. +of a file. For example, `` enables a table +of contents and heading IDs for that document. This option is disabled by +default and is not an extra. ```shell $ markdown2 --use-file-vars foo.md > foo.html diff --git a/lib/markdown2.py b/lib/markdown2.py index 18419ef0..9e712003 100755 --- a/lib/markdown2.py +++ b/lib/markdown2.py @@ -349,15 +349,7 @@ def __init__( self.extras.update(extras) assert isinstance(self.extras, dict) - if "toc" in self.extras: - if "header-ids" not in self.extras: - self.extras["header-ids"] = None # "toc" implies "header-ids" - - if self.extras["toc"] is None: - self._toc_depth = 6 - else: - self._toc_depth = self.extras["toc"].get("depth", 6) - + self._setup_toc() self._normalize_header_ids() if 'break-on-newline' in self.extras: @@ -404,6 +396,16 @@ def reset(self): self._setup_extras() self._toc = [] + def _setup_toc(self): + if "toc" in self.extras: + if "header-ids" not in self.extras: + self.extras["header-ids"] = None # "toc" implies "header-ids" + + if self.extras["toc"] is None: + self._toc_depth = 6 + else: + self._toc_depth = self.extras["toc"].get("depth", 6) + def _normalize_header_ids(self): if 'header-ids' in self.extras: if not isinstance(self.extras['header-ids'], dict): @@ -414,6 +416,7 @@ def _normalize_header_ids(self): } def _setup_extras(self): + self._setup_toc() self._normalize_header_ids() if "footnotes" in self.extras: # order of insertion matters for footnotes. Use ordered dict for Python < 3.7 diff --git a/test/test_markdown2.py b/test/test_markdown2.py index 21a66aa3..f4d105ac 100755 --- a/test/test_markdown2.py +++ b/test/test_markdown2.py @@ -230,6 +230,17 @@ def test_header_ids_from_file_vars(self): self.assertIn('

Heading

'.format(heading_id), html) self.assertEqual(md.convert("# Heading\n"), "

Heading

\n") + def test_toc_from_file_vars(self): + md = markdown2.Markdown(use_file_vars=True) + for option in ("toc", "header-ids, toc"): + with self.subTest(option=option): + modeline = "\n" % option + html = md.convert(modeline + "# Heading\n") + self.assertIn('

Heading

', html) + self.assertIn('Heading', html.toc_html) + plain_html = md.convert("# Heading\n") + self.assertEqual(plain_html, "

Heading

\n") + def test_footnote_numbers_reset_between_conversions(self): md = markdown2.Markdown(extras=["footnotes"]) md.convert("First[^first], second[^second].\n\n"