Skip to content

Commit e69bbdf

Browse files
Merge pull request #740 from agammann/fix/toc-from-file-vars
Initialize TOC when file variables enable the extra
2 parents 350a203 + 5bc8020 commit e69bbdf

3 files changed

Lines changed: 26 additions & 10 deletions

File tree

‎README.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ $ markdown2 foo.md > foo.html
7575
Use `--use-file-vars` on the CLI, or `use_file_vars=True` with `markdown()`,
7676
`markdown_path()` or `Markdown()`, to enable extras declared in Emacs-style
7777
file variables such as `<!-- -*- markdown-extras: header-ids -*- -->` at the top
78-
of a file. This option is disabled by default and is not an extra.
78+
of a file. For example, `<!-- -*- markdown-extras: toc -*- -->` enables a table
79+
of contents and heading IDs for that document. This option is disabled by
80+
default and is not an extra.
7981

8082
```shell
8183
$ markdown2 --use-file-vars foo.md > foo.html

‎lib/markdown2.py‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -349,15 +349,7 @@ def __init__(
349349
self.extras.update(extras)
350350
assert isinstance(self.extras, dict)
351351

352-
if "toc" in self.extras:
353-
if "header-ids" not in self.extras:
354-
self.extras["header-ids"] = None # "toc" implies "header-ids"
355-
356-
if self.extras["toc"] is None:
357-
self._toc_depth = 6
358-
else:
359-
self._toc_depth = self.extras["toc"].get("depth", 6)
360-
352+
self._setup_toc()
361353
self._normalize_header_ids()
362354

363355
if 'break-on-newline' in self.extras:
@@ -404,6 +396,16 @@ def reset(self):
404396
self._setup_extras()
405397
self._toc = []
406398

399+
def _setup_toc(self):
400+
if "toc" in self.extras:
401+
if "header-ids" not in self.extras:
402+
self.extras["header-ids"] = None # "toc" implies "header-ids"
403+
404+
if self.extras["toc"] is None:
405+
self._toc_depth = 6
406+
else:
407+
self._toc_depth = self.extras["toc"].get("depth", 6)
408+
407409
def _normalize_header_ids(self):
408410
if 'header-ids' in self.extras:
409411
if not isinstance(self.extras['header-ids'], dict):
@@ -414,6 +416,7 @@ def _normalize_header_ids(self):
414416
}
415417

416418
def _setup_extras(self):
419+
self._setup_toc()
417420
self._normalize_header_ids()
418421
if "footnotes" in self.extras:
419422
# order of insertion matters for footnotes. Use ordered dict for Python < 3.7

‎test/test_markdown2.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,17 @@ def test_header_ids_from_file_vars(self):
230230
self.assertIn('<h1 id="{}">Heading</h1>'.format(heading_id), html)
231231
self.assertEqual(md.convert("# Heading\n"), "<h1>Heading</h1>\n")
232232

233+
def test_toc_from_file_vars(self):
234+
md = markdown2.Markdown(use_file_vars=True)
235+
for option in ("toc", "header-ids, toc"):
236+
with self.subTest(option=option):
237+
modeline = "<!-- -*- markdown-extras: %s -*- -->\n" % option
238+
html = md.convert(modeline + "# Heading\n")
239+
self.assertIn('<h1 id="heading">Heading</h1>', html)
240+
self.assertIn('<a href="#heading">Heading</a>', html.toc_html)
241+
plain_html = md.convert("# Heading\n")
242+
self.assertEqual(plain_html, "<h1>Heading</h1>\n")
243+
233244
def test_footnote_numbers_reset_between_conversions(self):
234245
md = markdown2.Markdown(extras=["footnotes"])
235246
md.convert("First[^first], second[^second].\n\n"

0 commit comments

Comments
 (0)