Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than a6266e6 point at it.

➖ Drop support for Python 3.8 (#1464)Sebastián Ramírezbefore adoptionfbff83e

line 1why, one step back
1from typing import Any, Union

✨ Allow setting an environment variable to `None` in `CliRunner.invoke` (#1303)arjenzorgdocbefore adoptiona6266e6

lines 2–9why, one step back
2
3from 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 back
10 items: list[Union[Page, Section, Link]], *, config: MkDocsConfig
11) -> list[Union[Page, Section, Link]]:12 new_items: list[Union[Page, Section, Link]] = []

✨ Allow setting an environment variable to `None` in `CliRunner.invoke` (#1303)arjenzorgdocbefore adoptiona6266e6

lines 13–38why, one step back
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)