Skip to content

Commit 830ae7c

Browse files
committed
Added blog article on search exclusion features
1 parent 3c4bc8a commit 830ae7c

8 files changed

+382
-28
lines changed

CHANGELOG

+9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
mkdocs-material-7.3.0+insiders-3.1.1 (2021-09-26)
2+
3+
* Fixed ordering bug in search exclusion logic
4+
5+
mkdocs-material-7.3.0+insiders-3.1.0 (2021-09-26)
6+
7+
* Added support for excluding pages, sections, and elements from search
8+
* Fixed #2803: Code block annotations not visible when printing
9+
110
mkdocs-material-7.3.0 (2021-09-23)
211

312
* Added support for sticky navigation tabs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
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.

docs/blog/2021/search-better-faster-smaller.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ smaller at the same time.__
2020
<span>__Martin Donath__ · @squidfunk</span>
2121
<span>
2222
:octicons-calendar-24: September 13, 2021 ·
23-
:octicons-clock-24: 15 min read
23+
:octicons-clock-24: 15 min read ·
24+
[:octicons-tag-24: 7.2.6+insiders-3.0.0](../../insiders/changelog.md#300-_-september-13-2021)
2425
</span>
2526
</aside>
2627

docs/blog/index.md

+19
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,25 @@ search:
66

77
# Blog
88

9+
<h2>Excluding content from search</h2>
10+
11+
__The latest Insiders release brings three new simple ways to exclude dedicated
12+
parts of a document from the search index, allowing for more fine-grained
13+
control.__
14+
15+
Two weeks ago, Material for MkDocs Insiders shipped a brand new search plugin,
16+
yielding massive improvements in usability, but also in speed and size of the
17+
search index. Interestingly, as discussed in the previous blog article, we only
18+
scratched the surface of what's now possible. This release brings some useful
19+
features that enhance the writing experience, allowing for more fine-grained
20+
control of what pages, sections and blocks of a Markdown file should be indexed
21+
by the built-in search functionality.
22+
23+
[Continue reading :octicons-arrow-right-24:][2]{ .md-button }
24+
25+
[2]: 2021/excluding-content-from-search.md
26+
27+
928
<h2>Search: better, faster, smaller</h2>
1029

1130
__This is the story of how we managed to completely rebuild client-side search,

docs/insiders/changelog.md

+9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ template: overrides/main.html
66

77
## Material for MkDocs Insiders
88

9+
### 3.1.1 <small>_ September 26, 2021</small>
10+
11+
- Fixed ordering bug in search exclusion logic
12+
13+
### 3.1.0 <small>_ September 26, 2021</small>
14+
15+
- Added support for excluding pages, sections, and elements from search
16+
- Fixed #2803: Code block annotations not visible when printing
17+
918
### 3.0.1 <small>_ September 19, 2021</small>
1019

1120
- Added support for using literal `h1-6` tags for search plugin

docs/insiders/index.md

+13-10
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ The following features are currently exclusively available to sponsors:
141141
- [x] [Rich search previews :material-new-box:][36]
142142
- [x] [Tokenizer with lookahead :material-new-box:][37]
143143
- [x] [Advanced search highlighting :material-new-box:][38]
144+
- [x] [Excluding content from search :material-new-box:][39]
144145
- [x] [Social cards][34]
145146
- [x] [Cookie consent][33]
146147
- [x] [Linking content tabs][32]
@@ -195,14 +196,14 @@ the public for general availability.
195196
- [x] [Custom admonition icons][31]
196197
- [x] [Linking content tabs][32]
197198

198-
[30]: ../setup/setting-up-site-search.md#boosting-a-page
199+
[30]: ../setup/setting-up-site-search.md#search-boosting
199200
[31]: ../reference/admonitions.md#changing-the-icons
200201
[32]: ../reference/content-tabs.md#linking-content-tabs
201202

202203
#### $ 7,000 – Royal Gold
203204

204205
- [x] [Cookie consent][33]
205-
- [ ] Exclude pages from search
206+
- [ ] Was this page helpful?
206207
- [ ] Link cards
207208

208209
[33]: ../setup/setting-up-site-analytics.md#cookie-consent
@@ -221,11 +222,13 @@ the public for general availability.
221222
- [x] [Rich search previews][36]
222223
- [x] [Tokenizer with lookahead][37]
223224
- [x] [Advanced search highlighting][38]
225+
- [x] [Excluding content from search][39]
224226

225227
[35]: ../blog/2021/search-better-faster-smaller.md
226228
[36]: ../blog/2021/search-better-faster-smaller.md#rich-search-previews
227229
[37]: ../blog/2021/search-better-faster-smaller.md#tokenizer-lookahead
228230
[38]: ../blog/2021/search-better-faster-smaller.md#accurate-highlighting
231+
[39]: ../setup/setting-up-site-search.md#search-exclusion
229232

230233
### Goals completed
231234

@@ -300,25 +303,25 @@ implemented behind feature flags; all configuration changes are
300303
backward-compatible. This means that your users will be able to build the
301304
documentation locally with Material for MkDocs and when they push their changes,
302305
it can be built with Insiders (e.g. as part of GitHub Actions). Thus, it's
303-
recommended to [install Insiders][39] only in CI, as you don't want to expose
306+
recommended to [install Insiders][40] only in CI, as you don't want to expose
304307
your `GH_TOKEN` to users.
305308

306-
[39]: ../publishing-your-site.md#github-pages
309+
[40]: ../publishing-your-site.md#github-pages
307310

308311
### Payment
309312

310313
_We don't want to pay for sponsorship every month. Are there any other options?_
311314

312315
Yes. You can sponsor on a yearly basis by [switching your GitHub account to a
313-
yearly billing cycle][40]. If for some reason you cannot do that, you could
316+
yearly billing cycle][41]. If for some reason you cannot do that, you could
314317
also create a dedicated GitHub account with a yearly billing cycle, which you
315318
only use for sponsoring (some sponsors already do that).
316319

317320
If you have any problems or further questions, don't hesitate to contact me at
318321
[email protected]. Note that one-time payments are not eligible for
319322
Insiders, but of course, very appreciated.
320323

321-
[40]: https://docs.github.com/en/github/setting-up-and-managing-billing-and-payments-on-github/changing-the-duration-of-your-billing-cycle
324+
[41]: https://docs.github.com/en/github/setting-up-and-managing-billing-and-payments-on-github/changing-the-duration-of-your-billing-cycle
322325

323326
### Terms
324327

@@ -327,7 +330,7 @@ commercial project. Can we use Insiders under the same terms and conditions?_
327330

328331
Yes. Whether you're an individual or a company, you may use _Material for MkDocs
329332
Insiders_ precisely under the same terms as Material for MkDocs, which are given
330-
by the [MIT license][41]. However, we kindly ask you to respect the following
333+
by the [MIT license][42]. However, we kindly ask you to respect the following
331334
guidelines:
332335

333336
- Please __don't distribute the source code__ of Insiders. You may freely use
@@ -338,7 +341,7 @@ guidelines:
338341
- If you cancel your subscription, you're removed as a collaborator and will
339342
miss out on future updates of Insiders. However, you may __use the latest
340343
version__ that's available to you __as long as you like__. Just remember that
341-
[GitHub deletes private forks][42].
344+
[GitHub deletes private forks][43].
342345

343-
[41]: ../license.md
344-
[42]: https://docs.github.com/en/github/setting-up-and-managing-your-github-user-account/removing-a-collaborator-from-a-personal-repository
346+
[42]: ../license.md
347+
[43]: https://docs.github.com/en/github/setting-up-and-managing-your-github-user-account/removing-a-collaborator-from-a-personal-repository

0 commit comments

Comments
 (0)