mkdocs_hooks.py
This file
Path
scripts/mkdocs_hooks.py
38 lines · 1.6 KiB · Python
Last changed by
➖ Drop support for Python 3.8 (#1464)Sebastián Ramírezbefore adoptionfbff83e
History of this fileTake it
The walk stopped after 200 commits: lines older than dc07284 point at it.
➖ Drop support for Python 3.8 (#1464)Sebastián Ramírezbefore adoptionfbff83e
line 1why, one step back1from typing import Any, Union🔖 Release version 0.19.2Sebastián Ramírezbefore adoptiondc07284
lines 2–9why, one step back23from 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(➖ Drop support for Python 3.8 (#1464)Sebastián Ramírezbefore adoptionfbff83e
lines 10–12why, one step back10 items: list[Union[Page, Section, Link]], *, config: MkDocsConfig11) -> list[Union[Page, Section, Link]]:12 new_items: list[Union[Page, Section, Link]] = []🔖 Release version 0.19.2Sebastián Ramírezbefore adoptiondc07284
lines 13–38why, one step back13 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)