1
0
mirror of https://github.com/squidfunk/mkdocs-material.git synced 2024-11-30 18:24:35 +01:00
mkdocs-material/docs/setup/setting-up-site-search.md

230 lines
4.9 KiB
Markdown
Raw Normal View History

2020-07-20 15:18:09 +02:00
---
search:
boost: 1.05
2020-07-20 15:18:09 +02:00
---
# Setting up site search
2022-01-25 20:06:53 +01:00
Material for MkDocs provides an excellent client-side search implementation,
2020-07-22 19:11:22 +02:00
omitting the need for the integration of third-party services, which might
2022-01-25 20:06:53 +01:00
not be compliant with privacy regulations. Moreover, search even works
[offline], allowing users to download your documentation.
2020-07-20 15:18:09 +02:00
2022-02-27 17:07:10 +01:00
[offline]: building-for-offline-usage.md
2020-07-20 15:18:09 +02:00
## Configuration
2022-02-27 13:19:44 +01:00
### Built-in search plugin
2020-07-20 15:18:09 +02:00
2023-09-14 19:09:18 +02:00
<!-- md:version 0.1.0 -->
<!-- md:plugin -->
2021-09-13 18:14:28 +02:00
2021-10-11 13:38:03 +02:00
The built-in search plugin integrates seamlessly with Material for MkDocs,
2023-09-14 19:09:18 +02:00
adding multilingual client-side search with [lunr] and [lunr-languages]. It's
2021-10-11 13:38:03 +02:00
enabled by default, but must be re-added to `mkdocs.yml` when other plugins
2020-07-20 15:18:09 +02:00
are used:
``` yaml
plugins:
- search
```
2023-09-14 19:09:18 +02:00
For a list of all settings, please consult the [plugin documentation].
2020-07-20 15:18:09 +02:00
2023-09-14 19:09:18 +02:00
[plugin documentation]: ../plugins/search.md
2022-12-31 14:57:05 +01:00
2021-10-11 13:38:03 +02:00
[lunr]: https://lunrjs.com
[lunr-languages]: https://github.com/MihaiValentin/lunr-languages
2022-06-05 18:16:51 +02:00
### Search suggestions
2023-09-14 19:09:18 +02:00
<!-- md:version 7.2.0 -->
<!-- md:feature -->
<!-- md:flag experimental -->
2021-10-10 21:04:22 +02:00
When search suggestions are enabled, the search will display the likeliest
2021-10-11 13:38:03 +02:00
completion for the last word which can be accepted with the ++arrow-right++ key.
2021-01-31 19:23:28 +01:00
Add the following lines to `mkdocs.yml`:
``` yaml
theme:
features:
- search.suggest
```
2022-09-11 19:25:40 +02:00
Searching for [:octicons-search-24: search su][Search suggestions example]
yields ^^search suggestions^^ as a suggestion.
2022-09-11 19:25:40 +02:00
[Search suggestions example]: ?q=search+su
### Search highlighting
2023-09-14 19:09:18 +02:00
<!-- md:version 7.2.0 -->
<!-- md:feature -->
<!-- md:flag experimental -->
2021-10-10 21:04:22 +02:00
When search highlighting is enabled and a user clicks on a search result,
2020-08-02 22:09:44 +02:00
Material for MkDocs will highlight all occurrences after following the link.
2021-01-31 19:23:28 +01:00
Add the following lines to `mkdocs.yml`:
``` yaml
theme:
features:
- search.highlight
```
2022-09-11 19:25:40 +02:00
Searching for [:octicons-search-24: code blocks][Search highlighting example]
2021-10-11 13:38:03 +02:00
highlights all occurrences of both terms.
2022-09-11 19:25:40 +02:00
[Search highlighting example]: ../reference/code-blocks.md?h=code+blocks
2020-12-30 19:02:02 +01:00
### Search sharing
2023-09-14 19:09:18 +02:00
<!-- md:version 7.2.0 -->
<!-- md:feature -->
2020-12-30 19:02:02 +01:00
2021-10-10 21:04:22 +02:00
When search sharing is activated, a :material-share-variant: share button is
2020-12-30 19:02:02 +01:00
rendered next to the reset button, which allows to deep link to the current
2021-01-31 19:23:28 +01:00
search query and result. Add the following lines to `mkdocs.yml`:
2020-12-30 19:02:02 +01:00
``` yaml
theme:
features:
- search.share
```
When a user clicks the share button, the URL is automatically copied to the
clipboard.
2021-05-12 09:29:40 +02:00
## Usage
### Search boosting
2021-05-12 09:29:40 +02:00
2023-09-14 19:09:18 +02:00
<!-- md:version 8.3.0 -->
<!-- md:flag metadata -->
2021-05-12 09:29:40 +02:00
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:
2021-05-12 09:29:40 +02:00
2022-12-31 14:57:05 +01:00
=== ":material-arrow-up-circle: Rank up"
2021-05-12 09:29:40 +02:00
2022-12-31 14:57:05 +01:00
``` yaml
---
search:
boost: 2 # (1)!
---
2023-09-14 19:09:18 +02:00
# Page title
2022-12-31 14:57:05 +01:00
...
```
1. :woman_in_lotus_position: When boosting pages, be gentle and start with
__low values__.
=== ":material-arrow-down-circle: Rank down"
2021-05-12 09:29:40 +02:00
2022-12-31 14:57:05 +01:00
``` yaml
---
search:
boost: 0.5
---
2023-09-14 19:09:18 +02:00
# Page title
2022-12-31 14:57:05 +01:00
...
```
2021-10-11 13:38:03 +02:00
### Search exclusion
2023-09-14 19:09:18 +02:00
<!-- md:version 9.0.0 -->
<!-- md:flag metadata -->
<!-- md:flag experimental -->
Pages can be excluded from search with the front matter `search.exclude`
2023-09-14 19:09:18 +02:00
property, removing them from the index. Add the following lines at the top of a
2021-10-11 13:38:03 +02:00
Markdown file:
2022-09-11 19:25:40 +02:00
``` yaml
---
search:
exclude: true
---
2023-09-14 19:09:18 +02:00
# Page title
...
```
#### Excluding sections
2021-10-11 13:38:03 +02:00
When [Attribute Lists] is enabled, specific sections of pages can be excluded
2022-12-31 14:57:05 +01:00
from search by adding the `data-search-exclude` pragma after a Markdown
2021-10-11 13:38:03 +02:00
heading:
2022-09-11 19:25:40 +02:00
=== ":octicons-file-code-16: `docs/page.md`"
``` markdown
2023-09-14 19:09:18 +02:00
# Page title
## Section 1
The content of this section is included
## Section 2 { data-search-exclude }
The content of this section is excluded
```
2022-09-11 19:25:40 +02:00
=== ":octicons-codescan-16: `search_index.json`"
``` json
{
...
"docs": [
{
"location":"page/",
"text":"",
"title":"Document title"
},
{
"location":"page/#section-1",
"text":"<p>The content of this section is included</p>",
"title":"Section 1"
}
]
}
```
2021-10-11 13:38:03 +02:00
[Attribute Lists]: extensions/python-markdown.md#attribute-lists
#### Excluding blocks
2021-10-11 13:38:03 +02:00
When [Attribute Lists] is enabled, specific sections of pages can be excluded
2022-12-31 14:57:05 +01:00
from search by adding the `data-search-exclude` pragma after a Markdown
2021-10-11 13:38:03 +02:00
inline- or block-level element:
2022-09-11 19:25:40 +02:00
=== ":octicons-file-code-16: `docs/page.md`"
``` markdown
2023-09-14 19:09:18 +02:00
# Page title
The content of this block is included
The content of this block is excluded
{ data-search-exclude }
```
2022-09-11 19:25:40 +02:00
=== ":octicons-codescan-16: `search_index.json`"
``` json
{
...
"docs": [
{
"location":"page/",
"text":"<p>The content of this block is included</p>",
"title":"Document title"
}
]
}
```