Skip to content

Commit 9a44822

Browse files
committed
Add use-page config option
The option allows to use a different page for the section page instead of the first child.
1 parent 6e7b33e commit 9a44822

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,21 @@ To make writing this kind of `nav` more natural ([in YAML there's no better opti
6161
* [Foo](borgs/foo.md)
6262
```
6363

64+
### Specifying the page to use for the section
65+
66+
By default the first child is used as the section page, even if there is no
67+
`index.md`. If you want to change the page to use add the `use-page` config
68+
option:
69+
70+
```yaml
71+
plugins:
72+
- section-index:
73+
use-page: index
74+
```
75+
76+
The value is the name of the page to use, in this case `index.md`. If a child
77+
with that name does not exist no section page is generated.
78+
6479
[literate-nav]: https://oprypin.github.io/mkdocs-literate-nav/
6580

6681
## [Implementation](https://github.com/oprypin/mkdocs-section-index/blob/master/mkdocs_section_index/plugin.py)

mkdocs_section_index/plugin.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717

1818
class SectionIndexPlugin(BasePlugin):
19+
config_scheme = (
20+
('page', mkdocs.config.config_options.Type(str, default=None)),
21+
)
22+
1923
def on_nav(self, nav: Navigation, config, files) -> Navigation:
2024
todo = collections.deque((nav.items,))
2125
while todo:
@@ -24,7 +28,16 @@ def on_nav(self, nav: Navigation, config, files) -> Navigation:
2428
if not isinstance(section, Section) or not section.children:
2529
continue
2630
todo.append(section.children)
27-
page = section.children[0]
31+
name = self.config['use-page']
32+
if name is None:
33+
page = section.children[0]
34+
else:
35+
for child in section.children:
36+
if child.file.name == name:
37+
page = child
38+
break
39+
else:
40+
continue
2841
if not isinstance(page, Page):
2942
continue
3043
assert not page.children

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "mkdocs-section-index"
3-
version = "0.3.0"
3+
version = "0.4.0"
44
description = "MkDocs plugin to allow clickable sections that lead to an index page"
55
authors = ["Oleh Prypin <[email protected]>"]
66
license = "MIT"

0 commit comments

Comments
 (0)