Skip to content

Commit

Permalink
add options
Browse files Browse the repository at this point in the history
  • Loading branch information
shu307 committed Sep 14, 2022
1 parent c11efd7 commit d3b77b9
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
* text=auto
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Publish
publish.ps1

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down Expand Up @@ -149,4 +152,4 @@ cython_debug/
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
#.idea/
34 changes: 28 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
# mkdocs-nav-weight

> **A simple mkdocs plugin, enable to sort nav by setting "weight" in markdown metadata** </br>
> **Not sure if it works (at least it does in my local docs).**</br>
> **mkdocs get `nav` first, and then reads markdown resource to `page`. This plugin try to read markdown resource before the mkdocs do it, so using this plugin may introduce performance problems**
> **A simple mkdocs plugin, enable to sort nav by setting "weight" in markdown metadata**
>
> **Not sure if it works (at least it does in my local docs).**
>
> **This plugin tries to read markdown resources before mkdocs, which may add some performance overhead**
## How it works

Get the `weight` of each child of the folder (`section`):
- if it is a `page`, try to get its value, if the value is invalid, return 0;
- if it is a folder (`section`), then try to get the value from the child of its children wich is a `page` and `isindex=true`, if it is not found or the value is invalid, returns 0. </br>
- if it is a `page`, try to get its value, return 0 on failure.
- if it is a folder (`section`), then try to get the value from the child wich `isindex=true`, return 0 on failure.
- sort these children by `weight`.

Recursively all folders starting from `docs`
Recursively all folders starting from `docs`.

## Install


Expand All @@ -25,7 +29,25 @@ plugins:
- mkdocs-nav-weight
```
## Options
`mkdocs.yml`

```yaml
plugins:
- search
- mkdocs-nav-weight:
section_renamed: false
reverse: false
```

- `section_renamed`, default `false`:
- If `true`, section name will use the `title` of its `index` instead of the folder name. (For compatibility we have to name a folder like "c#" as "csharp", but waht we actually want to see in the page is "c#" , that's what this option does)
- `reverse`, default `false`:
- If `true`, sort `nav` by `weight` from largest to smallest.

## Example

Markdown metadata like this:
```csharp
foo.md
Expand Down
40 changes: 31 additions & 9 deletions mkdocs_nav_weight/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
class MkDocsNavWeight(mkdocs.plugins.BasePlugin):

config_scheme = (
('section_renamed', mkdocs.config.config_options.Type(bool, default=False)),
('reverse', mkdocs.config.config_options.Type(bool, default=False)),
)

_is_section_renamed = False
_is_reverse = False

# add markdown and meta to page
def _set_meta(self, element, config):
for item in element:
Expand All @@ -16,18 +21,25 @@ def _set_meta(self, element, config):
elif item.is_page:
item.read_source(config)

def _get_title_from_index(self, section, index):
try:
section.title = index.title
except:
return

def _get_page_weight(self, page):
try:
# exist 'weight'
# seems "isinstance" has an exception handling
# exist "weight"
if isinstance(page.meta['weight'], Number):
return page.meta['weight']
else:
print(f"[mkdocs-nav-weight]Warning: Invaild value for 'weight' in {page}, setting to 0")
print(
f"[mkdocs-nav-weight]Warning: Invaild value for 'weight' in {page}, setting to 0")
return 0
except:
# no `weight`
print(f"[mkdocs-nav-weight]Warning: No 'weight' in {page}, setting to 0")
# no "weight"
print(
f"[mkdocs-nav-weight]Warning: No 'weight' in {page}, setting to 0")
return 0

def _get_weight(self, element):
Expand All @@ -36,25 +48,35 @@ def _get_weight(self, element):
return self._get_page_weight(element)
# is a section
if element.is_section:
# try to find "index.md" and get weight
# try to find "index.md"
for child in element.children:
# exist "index.md"
if child.is_page and child.is_index:
if self._is_section_renamed:
# get title form index
self._get_title_from_index(element, child)
# return weight from index
return self._get_page_weight(child)
# a link or index or no index section
return 0

def _sort_by_weight(self, element):
element.children.sort(key=self._get_weight, reverse=False)
element.children.sort(key=self._get_weight, reverse=self._is_reverse)
for item in element.children:
if item.is_section:
self._sort_by_weight(item)

def on_nav(self, nav, config, files, **kwargs):
# get option
def on_pre_build(self, config, **kwargs):
self._is_section_renamed = self.config["section_renamed"]
self._is_reverse = self.config["reverse"]

# set nav
def on_nav(self, nav, config, files, **kwargs):
# set meta
self._set_meta(nav, config)
# sort nav
nav.items.sort(key=self._get_weight, reverse=False)
nav.items.sort(key=self._get_weight, reverse=self._is_reverse)
for item in nav.items:
if item.is_section:
self._sort_by_weight(item)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='mkdocs-nav-weight',
version='0.0.2',
version='0.0.3',
author='shu307',
author_email="[email protected]",
description='A simple mkdocs plugin, enable to sort nav by setting "weight" in markdown metadata',
Expand Down

0 comments on commit d3b77b9

Please sign in to comment.