Setting up site search¶
Material for MkDocs provides an excellent client-side search implementation, omitting the need for the integration of third-party services, which might not be compliant with privacy regulations. Moreover, search even works offline, allowing users to download your documentation.
Configuration¶
Built-in search plugin¶
0.1.0 · Plugin
The built-in search plugin integrates seamlessly with Material for MkDocs, adding multilingual client-side search with lunr and lunr-languages. It's enabled by default, but must be re-added to mkdocs.yml
when other plugins are used:
The following configuration options are supported:
lang
-
Default: automatically set – This option allows to include the language-specific stemmers provided by lunr-languages. Note that Material for MkDocs will set this automatically based on the site language, but it may be overridden, e.g. to support multiple languages:
The following languages are supported by lunr-languages:
ar
– Arabicda
– Danishde
– Germandu
– Dutchen
– Englishes
– Spanishfi
– Finnishfr
– Frenchhu
– Hungarianit
– Italianja
– Japaneseno
– Norwegianpt
– Portuguesero
– Romanianru
– Russiansv
– Swedishth
– Thaitr
– Turkishvi
– Vietnamese
Material for MkDocs goes to great lengths to support languages that are not part of this list by automatically falling back to the stemmer yielding the best result.
separator
-
Default: automatically set – The separator for indexing and query tokenization can be customized, making it possible to index parts of words separated by other characters than whitespace and
-
, e.g. by including.
:With 9.0.0, a faster and more flexible tokenizer method is shipped, allowing for tokenizing with lookahead, which yields more influence on the way documents are indexed. As a result, we use the following separator setting for this site's search:
Broken into its parts, the separator induces the following behavior:
The first part of the expression inserts token boundaries for each document before and after whitespace, hyphens, commas, brackets and other special characters. If several of those special characters are adjacent, they are treated as one.
Many programming languages have naming conventions like
PascalCase
orcamelCase
. By adding this subexpression to the separator, words are split at case changes, tokenizing the wordPascalCase
intoPascal
andCase
.When adding
.
to the separator, version strings like1.2.3
are split into1
,2
and3
, which makes them undiscoverable via search. When using this subexpression, a small lookahead is introduced which will preserve version strings and keep them discoverable.If your documentation includes HTML/XML code examples, you may want to allow users to find specific tag names. Unfortunately, the
<
and>
control characters are encoded in code blocks as<
and>
. Adding this subexpression to the separator allows for just that.
Chinese language support¶
Sponsors only · insiders-4.14.0 · Experimental
Insiders adds search support for the Chinese language (see our blog article from May 2022) by integrating with the text segmentation library jieba, which can be installed with pip
.
If jieba is installed, the built-in search plugin automatically detects Chinese characters and runs them through the segmenter. The following configuration options are available:
jieba_dict
-
insiders-4.17.2 · Default: none – This option allows for specifying a custom dictionary to be used by jieba for segmenting text, replacing the default dictionary:
-
The following alternative dictionaries are provided by jieba:
- dict.txt.small – 占用内存较小的词典文件
- dict.txt.big – 支持繁体分词更好的词典文件
-
jieba_dict_user
-
insiders-4.17.2 · Default: none – This option allows for specifying an additional user dictionary to be used by jieba for segmenting text, augmenting the default dictionary:
User dictionaries can be used for tuning the segmenter to preserve technical terms.
Search suggestions¶
7.2.0 · Feature flag · Experimental
When search suggestions are enabled, the search will display the likeliest completion for the last word which can be accepted with the Right key. Add the following lines to mkdocs.yml
:
Searching for search su yields search suggestions as a suggestion.
Search highlighting¶
7.2.0 · Feature flag · Experimental
When search highlighting is enabled and a user clicks on a search result, Material for MkDocs will highlight all occurrences after following the link. Add the following lines to mkdocs.yml
:
Searching for code blocks highlights all occurrences of both terms.
Search sharing¶
7.2.0 · Feature flag
When search sharing is activated, a share button is rendered next to the reset button, which allows to deep link to the current search query and result. Add the following lines to mkdocs.yml
:
When a user clicks the share button, the URL is automatically copied to the clipboard.
Usage¶
Search boosting¶
Pages can be boosted in search with the front matter search.boost
property, which will make them rank higher. Add the following lines at the top of a Markdown file:
Search exclusion¶
9.0.0 · Experimental
Pages can be excluded from search with the front matter search.exclude
property, removing them from the index. Add the following lines at the top of a Markdown file:
Excluding sections¶
When Attribute Lists is enabled, specific sections of pages can be excluded from search by adding the data-search-exclude
pragma after a Markdown heading:
Excluding blocks¶
When Attribute Lists is enabled, specific sections of pages can be excluded from search by adding the data-search-exclude
pragma after a Markdown inline- or block-level element:
Created: April 14, 2023