|
| 1 | +--- |
| 2 | +template: overrides/main.html |
| 3 | +description: > |
| 4 | + How we rebuilt client-side search, delivering a better user experience while |
| 5 | + making it faster and smaller at the same time |
| 6 | +disqus: mkdocs-material |
| 7 | +search: |
| 8 | + exclude: true |
| 9 | +--- |
| 10 | + |
| 11 | +# Excluding content from search |
| 12 | + |
| 13 | +__The latest Insiders release brings three new simple ways to exclude |
| 14 | +dedicated parts of a document from the search index, allowing for more |
| 15 | +fine-grained control.__ |
| 16 | + |
| 17 | +<aside class="mdx-author" markdown="1"> |
| 18 | +![@squidfunk][1] |
| 19 | + |
| 20 | +<span>__Martin Donath__ · @squidfunk</span> |
| 21 | +<span> |
| 22 | +:octicons-calendar-24: September 26, 2021 · |
| 23 | +:octicons-clock-24: 5 min read · |
| 24 | +[:octicons-tag-24: 7.3.0+insiders-3.1.1](../../insiders/changelog.md#311-_-september-26-2021) |
| 25 | +</span> |
| 26 | +</aside> |
| 27 | + |
| 28 | + [1]: https://avatars.githubusercontent.com/u/932156 |
| 29 | + |
| 30 | +--- |
| 31 | + |
| 32 | +Two weeks ago, Material for MkDocs Insiders shipped a [brand new search |
| 33 | +plugin][2], yielding [massive improvements in usability][3], but also in [speed |
| 34 | +and size][4] of the search index. Interestingly, as discussed in the previous |
| 35 | +blog article, we only scratched the surface of what's now possible. This |
| 36 | +release brings some useful features that enhance the writing experience, |
| 37 | +allowing for more fine-grained control of what pages, sections and blocks of a |
| 38 | +Markdown file should be indexed by the built-in search functionality. |
| 39 | + |
| 40 | +_The following section discusses existing solutions for excluding pages and |
| 41 | +sections from the search index. If you immediately want to learn what's new, |
| 42 | +skip to the [section just after that][5]._ |
| 43 | + |
| 44 | + [2]: search-better-faster-smaller.md |
| 45 | + [3]: search-better-faster-smaller.md#whats-new |
| 46 | + [4]: search-better-faster-smaller.md#benchmarks |
| 47 | + [5]: #whats-new |
| 48 | + |
| 49 | +## Prior art |
| 50 | + |
| 51 | +MkDocs has a rich and thriving ecosystem of [plugins][6], and it comes as no |
| 52 | +surprise that there's already a fantastic plugin by @chrieke to exclude specific |
| 53 | +sections of a Markdown file – the [mkdocs-exclude-search][7] plugin. It can be |
| 54 | +installed with: |
| 55 | + |
| 56 | +``` |
| 57 | +pip install mkdocs-exclude-search |
| 58 | +``` |
| 59 | + |
| 60 | +__How it works__: the plugin post-processes the `search_index.json` file that |
| 61 | +is generated by the built-in search plugin, giving the author the ability to |
| 62 | +exclude certain pages and sections by adding a few lines of configuration to |
| 63 | +`mkdocs.yml`. An example: |
| 64 | + |
| 65 | +``` yaml |
| 66 | +plugins: |
| 67 | + - search |
| 68 | + - exclude-search: |
| 69 | + exclude: |
| 70 | + - page.md |
| 71 | + - page.md#section |
| 72 | + - directory/* |
| 73 | + - /*/page.md |
| 74 | +``` |
| 75 | +
|
| 76 | +It's easy to see that the plugin follows a configuration-centric approach, which |
| 77 | +adds support for advanced filtering techniques like infix- and suffix-filtering |
| 78 | +using wildcards. While this is a very powerful idea, it comes with some |
| 79 | +downsides: |
| 80 | +
|
| 81 | +1. __Exclusion patterns and content are not co-located__: exclusion patterns |
| 82 | + need to be defined in `mkdocs.yml`, and not as part of the respective |
| 83 | + document or section to be excluded. This might result in stale exclusion |
| 84 | + patterns, leading to unintended behavior: |
| 85 | + |
| 86 | + - When a headline is changed, its slug (permalink) also changes, which might |
| 87 | + suddenly match (or unmatch) a pattern, e.g., when an author fixes a typo |
| 88 | + in a headline. |
| 89 | + |
| 90 | + - As exclusion patterns support the use of wildcards, different authors |
| 91 | + might overwrite each other's patterns without any immediate feedback since |
| 92 | + the plugin does only report the number of excluded documents – not _what_ |
| 93 | + has been excluded.[^1] |
| 94 | + |
| 95 | + [^1]: |
| 96 | + When the log level is set to `DEBUG`, the plugin will report exactly which |
| 97 | + pages and sections have been excluded from the search index, but MkDocs will |
| 98 | + now flood the terminal with debug output from its core and other plugins. |
| 99 | + |
| 100 | +2. __Exclusion control might be too coarse__: The [mkdocs-exclude-search][7] |
| 101 | + plugin only allows for the exclusion of pages and sections. It's not possible |
| 102 | + to exclude parts of a section, e.g., content that is irrelevant to search but |
| 103 | + must be included as part of the documentation. |
| 104 | + |
| 105 | + [6]: https://github.com/mkdocs/mkdocs/wiki/MkDocs-Plugins |
| 106 | + [7]: https://github.com/chrieke/mkdocs-exclude-search |
| 107 | + |
| 108 | +## What's new? |
| 109 | + |
| 110 | +The latest Insiders release brings fine-grained control for [__excluding pages, |
| 111 | +sections, and blocks__][8] from the search index, implemented through front |
| 112 | +matter, as well as the [Attribute List][9] extension. Note that it doesn't |
| 113 | +replace the [mkdocs-exclude-search][7] plugin but _complements_ it. |
| 114 | + |
| 115 | + [8]: ../../setup/setting-up-site-search.md#search-exclusion |
| 116 | + [9]: https://python-markdown.github.io/extensions/attr_list/ |
| 117 | + |
| 118 | +### Excluding pages |
| 119 | + |
| 120 | +An entire page can be excluded from the search index by adding a simple |
| 121 | +directive to the front matter of the respective Markdown file. The good thing |
| 122 | +is that the author now only has to check the top of the document to learn |
| 123 | +whether it is excluded or not: |
| 124 | + |
| 125 | +``` bash |
| 126 | +--- |
| 127 | +search: |
| 128 | + exclude: true |
| 129 | +--- |
| 130 | +
|
| 131 | +# Document title |
| 132 | +... |
| 133 | +``` |
| 134 | + |
| 135 | +### Excluding sections |
| 136 | + |
| 137 | +If a section should be excluded, the author can use the [Attribute List][9] |
| 138 | +extension to add a __pragma__ called `{ data-search-exclude }` at the end of a |
| 139 | +heading. The pragma is not included in the final HTML, as search pragmas are |
| 140 | +filtered by the search plugin before the page is rendered: |
| 141 | + |
| 142 | +=== "`docs/page.md`" |
| 143 | + |
| 144 | + ``` markdown |
| 145 | + # Document title |
| 146 | +
|
| 147 | + ## Section 1 |
| 148 | +
|
| 149 | + The content of this section is included |
| 150 | +
|
| 151 | + ## Section 2 { data-search-exclude } |
| 152 | +
|
| 153 | + The content of this section is excluded |
| 154 | + ``` |
| 155 | + |
| 156 | +=== "`search_index.json`" |
| 157 | + |
| 158 | + ``` json |
| 159 | + { |
| 160 | + ... |
| 161 | + "docs": [ |
| 162 | + { |
| 163 | + "location":"page/", |
| 164 | + "text":"", |
| 165 | + "title":"Document title" |
| 166 | + }, |
| 167 | + { |
| 168 | + "location":"page/#section-1", |
| 169 | + "text":"<p>The content of this section is included</p>", |
| 170 | + "title":"Section 1" |
| 171 | + } |
| 172 | + ] |
| 173 | + } |
| 174 | + ``` |
| 175 | + |
| 176 | +### Excluding blocks |
| 177 | + |
| 178 | +If even more fine-grained control is desired, the __pragma__ can be added to |
| 179 | +any [block-level element][10] or [inline-level element][11] that is officially |
| 180 | +supported by the [Attribute List][9] extension: |
| 181 | + |
| 182 | +=== "`docs/page.md`" |
| 183 | + |
| 184 | + ``` markdown |
| 185 | + # Document title |
| 186 | +
|
| 187 | + The content of this block is included |
| 188 | +
|
| 189 | + The content of this block is excluded |
| 190 | + { data-search-exclude } |
| 191 | + ``` |
| 192 | + |
| 193 | +=== "`search_index.json`" |
| 194 | + |
| 195 | + ``` json |
| 196 | + { |
| 197 | + ... |
| 198 | + "docs": [ |
| 199 | + { |
| 200 | + "location":"page/", |
| 201 | + "text":"<p>The content of this block is included</p>", |
| 202 | + "title":"Document title" |
| 203 | + }, |
| 204 | + ] |
| 205 | + } |
| 206 | + ``` |
| 207 | + |
| 208 | + [10]: https://python-markdown.github.io/extensions/attr_list/#block-level |
| 209 | + [11]: https://python-markdown.github.io/extensions/attr_list/#inline-level |
| 210 | + |
| 211 | +## Conclusion |
| 212 | + |
| 213 | +The latest release brings three simple ways to control more precisely what goes |
| 214 | +into the search index and what doesn't. It complements the already very powerful |
| 215 | +[mkdocs-exclude-search][7] plugin, allowing for new methods of shaping the |
| 216 | +structure, size and content of the search index. |
0 commit comments