mkdocs_hooks.py
This file
Path
scripts/mkdocs_hooks.py
38 lines 路 1.6 KiB 路 Python
Last changed by
Not known.
History of this fileTake it
1from typing import Any, List, Union23from mkdocs.config.defaults import MkDocsConfig4from mkdocs.structure.files import Files5from mkdocs.structure.nav import Link, Navigation, Section6from mkdocs.structure.pages import Page789def generate_renamed_section_items(10 items: List[Union[Page, Section, Link]], *, config: MkDocsConfig11) -> List[Union[Page, Section, Link]]:12 new_items: List[Union[Page, Section, Link]] = []13 for item in items:14 if isinstance(item, Section):15 new_title = item.title16 new_children = generate_renamed_section_items(item.children, config=config)17 first_child = new_children[0]18 if isinstance(first_child, Page):19 if first_child.file.src_path.endswith("index.md"):20 # Read the source so that the title is parsed and available21 first_child.read_source(config=config)22 new_title = first_child.title or new_title23 # Creating a new section makes it render it collapsed by default24 # no idea why, so, let's just modify the existing one25 # new_section = Section(title=new_title, children=new_children)26 item.title = new_title27 item.children = new_children28 new_items.append(item)29 else:30 new_items.append(item)31 return new_items323334def on_nav(35 nav: Navigation, *, config: MkDocsConfig, files: Files, **kwargs: Any36) -> Navigation:37 new_items = generate_renamed_section_items(nav.items, config=config)38 return Navigation(items=new_items, pages=nav.pages)