- Notifications
You must be signed in to change notification settings - Fork 722
/
Copy pathmkdocs_hooks.py
38 lines (33 loc) · 1.56 KB
/
mkdocs_hooks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
fromtypingimportAny, List, Union
frommkdocs.config.defaultsimportMkDocsConfig
frommkdocs.structure.filesimportFiles
frommkdocs.structure.navimportLink, Navigation, Section
frommkdocs.structure.pagesimportPage
defgenerate_renamed_section_items(
items: List[Union[Page, Section, Link]], *, config: MkDocsConfig
) ->List[Union[Page, Section, Link]]:
new_items: List[Union[Page, Section, Link]] = []
foriteminitems:
ifisinstance(item, Section):
new_title=item.title
new_children=generate_renamed_section_items(item.children, config=config)
first_child=new_children[0]
ifisinstance(first_child, Page):
iffirst_child.file.src_path.endswith("index.md"):
# Read the source so that the title is parsed and available
first_child.read_source(config=config)
new_title=first_child.titleornew_title
# Creating a new section makes it render it collapsed by default
# no idea why, so, let's just modify the existing one
# new_section = Section(title=new_title, children=new_children)
item.title=new_title
item.children=new_children
new_items.append(item)
else:
new_items.append(item)
returnnew_items
defon_nav(
nav: Navigation, *, config: MkDocsConfig, files: Files, **kwargs: Any
) ->Navigation:
new_items=generate_renamed_section_items(nav.items, config=config)
returnNavigation(items=new_items, pages=nav.pages)