Skip to content

Commit 046e042

Browse files
Navigation filter (#12)
close #9
1 parent 05f54c0 commit 046e042

19 files changed

+285
-58
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ indent_style = tab
1010
[*.{cmd,bat}]
1111
end_of_line = crlf
1212

13-
[*.{yml,yaml,md}]
13+
[*.{yml,yaml,md,pages}]
1414
indent_style = space
1515
indent_size = 2
1616

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
enabled: !ENV [CI, true]
21
mkdocsignore: false
32
include_glob:
43
- 'test/test.md'
54
- '**/draft-*.md'
5+
exclude_glob:
6+
- 'Contributions/**'
67
include_tag:
78
- foo
89
- '#bar'
9-
enabled_on_serve: !ENV [CI, true]
1010
exclude_tag:
1111
- test

.github/workflows/workflow.pr.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ jobs:
4545
strategy:
4646
fail-fast: false
4747
matrix:
48-
python-version: ["3.8", "3.9", "3.10", "pypy-3.8"]
48+
python-version: ["3.8", "3.9", "3.10"]
4949
os: [ubuntu-latest, macos-latest, windows-latest]
5050
runs-on: ${{ matrix.os }}
5151
steps:
@@ -67,4 +67,10 @@ jobs:
6767
- name: Test Build
6868
run: |
6969
poetry version
70-
poetry build
70+
poetry build --verbose
71+
72+
- name: Test MkDocs build
73+
run: |
74+
pip uninstall -y mkdocs_file_filter_plugin
75+
pip install dist/mkdocs_file_filter_plugin-0.0.1.tar.gz
76+
poetry run mkdocs build --verbose

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# MkDocs
22
site/**
3+
docs/Contributions
34

45
# Byte-compiled / optimized / DLL files
56
__pycache__/

.pre-commit-config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ repos:
1111
rev: 6.0.0
1212
hooks:
1313
- id: flake8
14+
- repo: https://github.com/PyCQA/bandit
15+
rev: 1.7.4
16+
hooks:
17+
- id: bandit

.vscode/settings.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,8 @@
44
"mkdocs.yml",
55
"mkdocs.base.yml"
66
],
7-
// "https://json.schemastore.org/mkdocs-1.0.json": [
8-
// "mkdocs.yml",
9-
// "mkdocs.base.yml"
10-
// ],
11-
"schema-config.json": [
12-
"mkdocs.file-filter.yml"
7+
"schema-file-filter-config.json": [
8+
".file-filter.yml"
139
]
1410
},
1511
"files.trimTrailingWhitespace": true,

README.md

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# mkdocs-file-filter-plugin
1+
# MkDocs File Filter Plugin
22

33
[![PyPI - Version][pypi-v-image]][pypi-v-link]
44
[![PyPI - Python Version][pypi-pyversions-image]][pypi-pyversions-link]
@@ -23,6 +23,7 @@
2323
- [Tags metadata property](#tags-metadata-property)
2424
- [Custom metadata list](#custom-metadata-list)
2525
- [.mkdocsignore](#mkdocsignore)
26+
- [Navigation filtering](#navigation-filtering)
2627
- [Conflict behavior](#conflict-behavior)
2728
- [License](#license)
2829

@@ -70,8 +71,9 @@ Add a plugin configuration to `mkdocs.yml` - below example contains only example
7071
plugins:
7172
- search # if you include another plugin, and want search you have to add it again
7273
- file-filter:
74+
filter_nav: true # default value
7375
exclude_glob:
74-
- 'exclude/this/path/*'
76+
- 'exclude/this/path/**'
7577
- 'exclude/this/file/draft.md'
7678
- '*.tmp'
7779
exclude_regex:
@@ -80,7 +82,7 @@ plugins:
8082
- draft
8183
- preview
8284
include_glob:
83-
- 'include/this/path/*'
85+
- 'include/this/path/**'
8486
- 'include/this/file/Code-of-Conduct.md'
8587
- '*.png'
8688
- 'assets/**' # the material theme requires this folder
@@ -105,7 +107,8 @@ The plugin supports external files for the plugin configuration. If the external
105107
plugins:
106108
- search # if you include another plugin, and want search you have to add it again
107109
- file-filter:
108-
config: !ENV [MY_FILE_FILTER_CONFIG, 'mkdocs.file-filter.yml']
110+
config: !ENV [MY_FILE_FILTER_CONFIG, '.file-filter.yml']
111+
# config: path/to/config/file/.file-filter.yml
109112
```
110113

111114
> :bulb: **HINT**
@@ -118,6 +121,7 @@ External plugin config file example:
118121
# mkdocs.file-filter.yml
119122
enabled: !ENV [CI, true]
120123
enabled_on_serve: true
124+
filter_nav: true
121125
mkdocsignore: false
122126
exclude_glob:
123127
- 'exclude/this/path/*'
@@ -153,9 +157,10 @@ The below table shows all supported options by the plugin.
153157
| `config` | string | *none* | Path to external plugin's configuration file |
154158
| `enabled` | bool | `true` | Turn on/off plugin without removing/adding plugin's config from `mkdocs.yml` |
155159
| `enabled_on_serve` | bool | `false` | Turn on/off plugin on `serve` command |
160+
| `filter_nav` | bool | `true` | Remove `nav` items pointed to excluded files |
156161
| `mkdocsignore` | bool | `false` | Use gitignore-style file for patterns |
157162
| `mkdocsignore_file` | string | `.mkdocsignore` | Path to gitignore-style file with patterns |
158-
| `metadata_property` | string | `tags` | What Markdown/FrontMatter metadata property will be used for checking tags |
163+
| `metadata_property` | string | `tags` | What Markdown/FrontMatter metadata property list will be used for checking keywords |
159164
| `exclude_tag` | [string] | *none* | List of excluded tags |
160165
| `include_tag` | [string] | *none* | List of included tags |
161166
| `exclude_glob` | [string] | *none* | Exclude glob patterns |
@@ -181,6 +186,7 @@ The below table shows all supported options by the plugin.
181186
# mkdocs.yml
182187
plugins:
183188
- file-filter:
189+
filter_nav: true # default value
184190
exclude_glob:
185191
- 'drafts/**'
186192
include_glob:
@@ -199,6 +205,7 @@ plugins:
199205
# mkdocs.yml
200206
plugins:
201207
- file-filter:
208+
filter_nav: true # default value
202209
exclude_regex:
203210
- '.*\.(tmp|bin|tar)$'
204211
include_regex:
@@ -223,6 +230,7 @@ By default plugin filter files using `tags` property of your Markdown metadata.
223230
# mkdocs.yml
224231
plugins:
225232
- file-filter:
233+
filter_nav: true # default value
226234
exclude_tag:
227235
- abc
228236
- draft
@@ -275,6 +283,7 @@ Set `metadata_property` with your custom list property, e.g., `labels` for this
275283
# mkdocs.yml
276284
plugins:
277285
- file-filter:
286+
filter_nav: true # default value
278287
metadata_property: labels
279288
exclude_tag:
280289
- foo
@@ -336,8 +345,9 @@ Example config for mkdocsignore.
336345
# mkdocs.yml
337346
plugins:
338347
- file-filter:
348+
filter_nav: true # default value
339349
mkdocsignore: true # default: false
340-
mkdocsignore_file: 'custom/path/.myignore' # optional, relative to mkdocs.yml, default: .mkdocsignore
350+
mkdocsignore_file: 'custom/path/.mkdocsignore' # optional, relative to mkdocs.yml, default: .mkdocsignore
341351
```
342352

343353
Example `.mkdocsignore` file.
@@ -354,6 +364,59 @@ docs/**/draft-*.md
354364
>
355365
> **.mkdocsignore** patterns relative to your root.
356366

367+
### Navigation filtering
368+
369+
Suppose you customized [MkDocs navigation configuration][mkdocs-nav], and your `nav` contains elements defined in exclude patterns. In that case, the default MkDocs behavior is to render navigation to a non-existing file, and generated site gives 404.
370+
371+
By default, the plugin filters those cases and removes not working navigation items.
372+
373+
You can control the plugin's behavior to explicitly disable that option by setting `filter_nav: false`.
374+
375+
Example `mkdocs.yml` config.
376+
377+
```yaml
378+
# mkdocs.yml
379+
nav:
380+
- Foo: exclude/this/path
381+
- Bar: exclude/this/file/draft.md
382+
- Abc:
383+
- About: exclude/this/path/about.md
384+
- Contact: include/this/file/contact.md
385+
- Xyz: path/xyz.md
386+
387+
plugins:
388+
- file-filter:
389+
filter_nav: true # default value
390+
exclude_glob:
391+
- 'exclude/this/path/**'
392+
- 'exclude/this/file/draft.md'
393+
```
394+
395+
**Nav** results with `filter_nav: false`:
396+
397+
```yaml
398+
- Foo: exclude/this/path # -> 404
399+
- Bar: exclude/this/file/draft.md # -> 404
400+
- Abc:
401+
- About: exclude/this/path/about.md # -> 404
402+
- Contact: include/this/file/contact.md
403+
- Xyz: path/xyz.md
404+
```
405+
406+
**Nav** results with `filter_nav: true`:
407+
408+
```yaml
409+
- Abc:
410+
- Contact: include/this/file/contact.md
411+
- Xyz: path/xyz.md
412+
```
413+
414+
> :warning: **NOTE**
415+
>
416+
> If you use any other 3rd-party plugins that modify navigation (e.g., [mkdocs-awesome-pages-plugin][mkdocs-awesome-pages-plugin-pypi]) - first, test and evaluate expected behavior. Plugin configuration order may impact results.
417+
418+
> In most cases is recommended to transform navigation first and next filter with the file-filter plugin.
419+
357420
### Conflict behavior
358421

359422
It is possible to exclude and include will have conflict. For example, you could exclude `drafts/**` but include `*.md`. In that case, **include** has higher priority over exclude. So all `*.md` files from the drafts folder will be **included**.
@@ -370,7 +433,9 @@ It is possible to exclude and include will have conflict. For example, you could
370433
[mkdocs-envs]: https://www.mkdocs.org/user-guide/configuration/#environment-variables
371434
[mkdocs-metadata]: https://www.mkdocs.org/user-guide/writing-your-docs/#meta-data
372435
[mkdocs-docs-dir]: https://www.mkdocs.org/user-guide/configuration/#docs_dir
436+
[mkdocs-nav]: https://www.mkdocs.org/user-guide/writing-your-docs/#configure-pages-and-navigation
373437
[poetry]: https://python-poetry.org
374438
[pip]: https://pip.pypa.io
375439
[gitignore]: https://git-scm.com/docs/gitignore
376-
[mit]: https://spdx.org/licenses/MIT.html
440+
[mit]: https://opensource.org/licenses/MIT
441+
[mkdocs-awesome-pages-plugin-pypi]: https://pypi.org/project/mkdocs-awesome-pages-plugin

dev.build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ rm -rf dist && rm -rf build && rm -rf site && rm -rf *.egg-info
55
poetry run isort .
66
poetry run black .
77
poetry run flake8 --count .
8+
# poetry run bandit --recursive .
89

910
poetry build
1011

docs/NAV.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
title: nav
33
---
44

5-
* [Frob](index.md)
5+
<!-- * [Frob](index.md)
66
* [Baz](baz.md)
77
* [Borgs](borgs/index.md)
88
* [Bar](borgs/bar.md)
9-
* [Foo](borgs/foo.md)
9+
* [Foo](borgs/foo.md) -->

mkdocs.base.yml

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

33
theme:
44
name: material
5-
6-
plugins:
7-
- monorepo
8-
- awesome-pages
9-
# - literate-nav:
10-
# nav_file: NAV.md
11-

0 commit comments

Comments
 (0)