Skip to content
Merged
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<!-- -*- markdown-extras: header-ids -*- -->` at the top
of a file. This option is disabled by default and is not an extra.
of a file. For example, `<!-- -*- markdown-extras: toc -*- -->` 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
Expand Down
21 changes: 12 additions & 9 deletions lib/markdown2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand All @@ -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
Expand Down
11 changes: 11 additions & 0 deletions test/test_markdown2.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,17 @@ def test_header_ids_from_file_vars(self):
self.assertIn('<h1 id="{}">Heading</h1>'.format(heading_id), html)
self.assertEqual(md.convert("# Heading\n"), "<h1>Heading</h1>\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 = "<!-- -*- markdown-extras: %s -*- -->\n" % option
html = md.convert(modeline + "# Heading\n")
self.assertIn('<h1 id="heading">Heading</h1>', html)
self.assertIn('<a href="#heading">Heading</a>', html.toc_html)
plain_html = md.convert("# Heading\n")
self.assertEqual(plain_html, "<h1>Heading</h1>\n")

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