Skip to content

Commit f02133d

Browse files
nav filter fix (#15)
1 parent 7350410 commit f02133d

9 files changed

Lines changed: 40 additions & 11 deletions

File tree

.github/workflows/workflow.ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ jobs:
4343
run: poetry install --verbose --only=dev
4444

4545
- name: Check imports sort
46-
run: poetry run isort --check-only .
46+
run: poetry run isort --profile black --check-only .
4747

4848
- name: Check formatting
4949
run: poetry run black --check .

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# MkDocs
22
site/**
33
docs/Contributions
4+
docs1/
5+
docs2/
46

57
# Byte-compiled / optimized / DLL files
68
__pycache__/

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ repos:
77
rev: 5.12.0
88
hooks:
99
- id: isort
10+
args: ["--profile", "black"]
1011
- repo: https://github.com/pycqa/flake8
1112
rev: 6.0.0
1213
hooks:

.vscode/settings.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
},
2323
"python.linting.enabled": true,
2424
"markdown.extension.toc.levels": "2..6",
25-
"python.analysis.extraPaths": [
26-
".venv/lib/site-packages"
27-
],
28-
"editor.formatOnSave": true
25+
"editor.formatOnSave": true,
26+
"python.formatting.provider": "black"
2927
}

dev.build.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
rm -rf dist && rm -rf build && rm -rf site && rm -rf *.egg-info
44

5-
poetry run isort .
5+
poetry run isort --profile black .
66
poetry run black .
77
poetry run flake8 --count .
88
# poetry run bandit --recursive .
@@ -14,3 +14,4 @@ pip uninstall -y mkdocs_file_filter_plugin
1414
pip install -e .
1515

1616
poetry run mkdocs serve --verbose
17+
#--dev-addr 127.0.0.1:9001

mkdocs.base.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,14 @@ site_name: Test Site
22

33
theme:
44
name: material
5+
features:
6+
- navigation.instant
7+
- navigation.indexes
8+
- navigation.tracking
9+
- navigation.top
10+
- content.tooltips
11+
- search.suggest
12+
- search.highlight
13+
- search.share
14+
- navigation.tabs
15+
- navigation.tabs.sticky

mkdocs_file_filter_plugin/judger.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import pathlib
44
import re
5+
from typing import Union
56
from urllib.parse import urlsplit
67

78
import igittigitt
@@ -14,6 +15,8 @@
1415
from . import util as LOG
1516
from .plugin_config import PluginConfig
1617

18+
NavigationItem = Union[MkDocsPage, MkDocsSection, MkDocsLink, None]
19+
1720

1821
class Judger:
1922
def __init__(self, plugin_config: PluginConfig, mkdocs_config: MkDocsConfig):
@@ -26,14 +29,15 @@ def __init__(self, plugin_config: PluginConfig, mkdocs_config: MkDocsConfig):
2629
pathlib.Path(self.plugin_config.mkdocsignore_file)
2730
)
2831

29-
def evaluate_nav(self, nav):
32+
def evaluate_nav(self, nav: NavigationItem) -> NavigationItem:
3033
if isinstance(nav, MkDocsSection):
3134
nev_section = [self.evaluate_nav(child) for child in nav.children]
3235
nev_section = list(filter(lambda item: item is not None, nev_section))
3336
if nev_section != []:
3437
return MkDocsSection(nav.title, nev_section)
3538
else:
3639
LOG.debug(f"remove navigation section: {nav.title}")
40+
return None
3741
else:
3842
scheme, netloc, path, query, fragment = urlsplit(nav.url)
3943
if (
@@ -42,7 +46,8 @@ def evaluate_nav(self, nav):
4246
and not scheme
4347
and not netloc
4448
):
45-
LOG.debug(f"remove navigation item: {nav.title} {nav.url}")
49+
LOG.debug(f"remove navigation link: {nav.title} {nav.url}")
50+
return None
4651
else:
4752
return nav
4853

mkdocs_file_filter_plugin/plugin.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
from mkdocs.plugins import BasePlugin as MkDocsPlugin
66
from mkdocs.structure.files import Files as MkDocsFiles
77
from mkdocs.structure.nav import Navigation as MkDocsNavigation
8+
from mkdocs.structure.nav import (
9+
_add_parent_links,
10+
_add_previous_and_next_links,
11+
_get_by_type,
12+
)
13+
from mkdocs.structure.pages import Page as MkDocsPage
814

915
from . import util as LOG
1016
from .external_config import ExternalConfig
@@ -99,11 +105,13 @@ def on_nav(self, nav: MkDocsNavigation, config: MkDocsConfig, files: MkDocsFiles
99105

100106
judger = Judger(self.config, config)
101107
nav_items_new = []
102-
for nav_item in nav:
108+
for nav_item in nav.items:
103109
result = judger.evaluate_nav(nav_item)
104110
if result is not None:
105111
nav_items_new.append(result)
106112

107-
nav_items_new = list(filter(lambda item: item is not None, nav_items_new))
113+
pages = _get_by_type(nav_items_new, MkDocsPage)
114+
_add_previous_and_next_links(pages)
115+
_add_parent_links(nav_items_new)
108116

109-
return MkDocsNavigation(nav_items_new, nav.pages)
117+
return MkDocsNavigation(nav_items_new, pages)

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ isort = "^5.12.0"
6666
flake8 = "^6.0.0"
6767
bandit = "^1.7.4"
6868

69+
[tool.isort]
70+
profile = "black"
71+
6972
[build-system]
7073
requires = ["poetry-core"]
7174
build-backend = "poetry.core.masonry.api"

0 commit comments

Comments
 (0)