|
1 | | -# mkdocs-filesfilter |
2 | | -A mkdocs plugin that lets you exclude/include files or trees from your output. |
| 1 | +# File exclude/include plugin for MkDocs |
| 2 | + |
| 3 | +[](https://pypi.org/project/mkdocs-file-filter-plugin) |
| 4 | +[](https://pypi.org/project/mkdocs-file-filter-plugin) |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +`mkdocs-file-filter` is a [mkdocs plugin](http://www.mkdocs.org/user-guide/plugins/) that allows you to exclude/include files from your input using Unix-style wildcards (globs), regular expressions (regexes) or .mkdocsignore file ([gitignore-style](https://git-scm.com/docs/gitignore) syntax). |
| 9 | + |
| 10 | +## Quick start |
| 11 | + |
| 12 | +1. Install the plugin using pip. |
| 13 | + |
| 14 | +```console |
| 15 | +pip install mkdocs-file-filter-plugin |
| 16 | +``` |
| 17 | + |
| 18 | +1. In your project, add a plugin configuration to `mkdocs.yml`: |
| 19 | + |
| 20 | + ```yaml |
| 21 | + plugins: |
| 22 | + - file-filter: |
| 23 | + mkdocsignore: true |
| 24 | + mkdocsignore_file: 'custom/path/.mkdocsignore' # relative to mkdocs.yml |
| 25 | + exclude_glob: |
| 26 | + - 'exclude/this/path/*' |
| 27 | + - 'exclude/this/file/draft.md' |
| 28 | + - '*.tmp' |
| 29 | + - '*.gz' |
| 30 | + exclude_regex: |
| 31 | + - '.*\.(tmp|bin|tar)$' |
| 32 | + include_glob: |
| 33 | + - 'include/this/path/*' |
| 34 | + - 'include/this/file/Code-of-Conduct.md' |
| 35 | + - '*.png' |
| 36 | + - 'assets/**' # the material theme requires this folder |
| 37 | + include_regex: |
| 38 | + - '.*\.(js|css)$' |
| 39 | + ``` |
| 40 | +
|
| 41 | +## External config file |
| 42 | +
|
| 43 | +The plugin supports external files for the plugin configuration. If the external config file is specified, then `*_glob:` and `*_regex:` are not taken `mkdocs.yml` - only config from the external file applies. |
| 44 | + |
| 45 | +```yaml |
| 46 | +plugins: |
| 47 | + - file-filter: |
| 48 | + # config: !ENV [MY_FILE_FILTER_CONFIG, 'mkdocs.file-filter.yml'] |
| 49 | + config: mkdocs.file-filter.yml |
| 50 | +``` |
| 51 | + |
| 52 | +External plugin config file example. |
| 53 | + |
| 54 | +```yaml |
| 55 | +mkdocsignore: false |
| 56 | +exclude_glob: |
| 57 | + - 'exclude/this/path/*' |
| 58 | + - 'exclude/this/file/draft.md' |
| 59 | + - '*.tmp' |
| 60 | + - '*.gz' |
| 61 | +exclude_regex: |
| 62 | + - '.*\.(tmp|bin|tar)$' |
| 63 | +include_glob: |
| 64 | + - 'include/this/path/*' |
| 65 | + - 'include/this/file/Code-of-Conduct.md' |
| 66 | + - '*.png' |
| 67 | + - 'assets/**' # the material theme requires this folder |
| 68 | +include_regex: |
| 69 | + - '.*\.(js|css)$' |
| 70 | +``` |
| 71 | + |
| 72 | +> **HINT** |
| 73 | +> |
| 74 | +> For external file config, you can use [MkDocs Environment Variables](https://www.mkdocs.org/user-guide/configuration/#environment-variables) to set the desired file dynamically. A useful case for serving the site with different content based on stage/environment. Works well with CI/CD automation. |
| 75 | + |
| 76 | +## .mkdocsignore |
| 77 | + |
| 78 | +Setting `mkdocsignore` to `true` will exclude the dirs/files specified in the `.mkdocsignore`. Use the same syntax as you use for gitignore. |
| 79 | + |
| 80 | +Optionally you can set `mkdocsignore_file` parameter with your path to `.mkdocsignore` file. By default, the plugin uses `.mkdocsignore` from the root of your MkDocs. |
| 81 | + |
| 82 | +You can combine mkdocsignore with globs/regex as well. The patterns from both will apply. |
| 83 | + |
| 84 | +External config for mkdocsignore. |
| 85 | + |
| 86 | +```yaml |
| 87 | +plugins: |
| 88 | + - file-filter: |
| 89 | + mkdocsignore: true # default: false |
| 90 | + mkdocsignore_file: 'custom/path/.myignore' # relative to mkdocs.yml, default: .mkdocsignore |
| 91 | +``` |
| 92 | + |
| 93 | +Example `.mkdocsignore` file. |
| 94 | + |
| 95 | +```bash |
| 96 | +# MkDocs |
| 97 | +docs/test/** |
| 98 | +docs/**/draft-*.md |
| 99 | +``` |
| 100 | + |
| 101 | +## Conflict behavior |
| 102 | + |
| 103 | +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. |
| 104 | + |
| 105 | +## Some useful stuff |
| 106 | + |
| 107 | +If you do not provide patterns, everything will stay the same - standard MkDocs behavior. |
| 108 | + |
| 109 | +Because of the YAML syntax specifics, patterns that start with a punctuation mark must be quoted. |
| 110 | + |
| 111 | +The preferred way for quotes is to use single quotes `'` rather than double quotes `"` - regex backslash escapes are preserved correctly without being doubled up. |
| 112 | + |
| 113 | +## License |
| 114 | + |
| 115 | +`mkdocs-file-filter-plugin` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license. |
0 commit comments