-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move deduplicate_toc to a separate Markdown extension
- Loading branch information
Showing
5 changed files
with
50 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import xml.etree.ElementTree as etree | ||
from typing import List | ||
|
||
from markdown import Markdown # type: ignore | ||
from markdown.extensions import Extension, fenced_code # type: ignore | ||
from markdown.treeprocessors import Treeprocessor | ||
|
||
|
||
def _deduplicate_toc(toc: List[dict]) -> None: | ||
i = 0 | ||
while i < len(toc): | ||
el = toc[i] | ||
if el.get("children"): | ||
_deduplicate_toc(el["children"]) | ||
elif i > 0 and el["name"] == toc[i - 1]["name"]: | ||
del toc[i] | ||
continue | ||
i += 1 | ||
|
||
|
||
class _TocDeduplicatingTreeprocessor(Treeprocessor): | ||
def run(self, root: etree.Element): | ||
try: | ||
toc = self.md.toc_tokens | ||
except AttributeError: | ||
return | ||
_deduplicate_toc(toc) | ||
|
||
|
||
class DeduplicateTocExtension(Extension): | ||
def extendMarkdown(self, md: Markdown) -> None: | ||
md.treeprocessors.register( | ||
_TocDeduplicatingTreeprocessor(md), "mkdocstrings_crystal_deduplicate_toc", 4 | ||
) | ||
|
||
|
||
makeExtension = DeduplicateTocExtension |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import pytest | ||
|
||
from mkdocstrings.handlers.crystal import renderer | ||
from mkdocstrings.handlers.crystal import deduplicate_toc | ||
|
||
|
||
@pytest.mark.golden_test("deduplicate_toc/**/*.yml") | ||
def test_deduplicate_toc(golden): | ||
toc = list(golden["input"]) | ||
renderer._deduplicate_toc(toc) | ||
deduplicate_toc._deduplicate_toc(toc) | ||
assert toc == golden.out.get("output") |