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('