notes/docs/search.md

86 lines
2.7 KiB
Markdown
Raw Normal View History

2017-04-08 22:51:14 +00:00
---
layout: default
title: Search
2018-11-16 16:47:14 +00:00
nav_order: 7
2017-04-08 22:51:14 +00:00
---
# Search
2019-01-14 19:18:09 +00:00
{: .no_toc }
2018-12-16 19:47:41 +00:00
## Table of contents
{: .no_toc .text-delta }
1. TOC
{:toc}
---
2017-04-08 22:51:14 +00:00
2019-01-14 19:32:41 +00:00
Just the Docs uses [lunr.js](http://lunrjs.com) to add a client-side search interface powered by a JSON index that Jekyll generates. All search results are shown in an auto-complete style interface (there is no search results page). By default, all generated HTML pages are indexed using the following data points:
2017-04-08 22:51:14 +00:00
- Page title
- Page content
- Page URL
2018-11-16 16:47:30 +00:00
## Set up search
2018-12-16 19:47:41 +00:00
### Generate search index
2017-11-08 16:23:05 +00:00
2019-01-14 19:18:09 +00:00
Before you can use search, you must initialize the feature by running this `rake` command that comes with `just-the-docs`:
2017-11-08 16:23:05 +00:00
```bash
$ bundle exec just-the-docs rake search:init
```
2019-01-14 19:18:09 +00:00
This command creates the `search-data.json` file that Jekyll uses to create your search index. Alternatively, you can create the file manually in the `assets/js/` directory of your Jekyll site with this content:
2017-11-08 16:23:05 +00:00
2019-01-16 01:43:19 +00:00
```liquid
{% raw %}---
2017-11-08 16:23:05 +00:00
---
{
2019-09-10 15:26:14 +00:00
{% assign comma = false %}
{% for page in site.html_pages %}{% if page.search_exclude != true %}{% if comma == true%},{% endif %}"{{ forloop.index0 }}": {
"title": "{{ page.title | replace: '&', '&' }}",
2019-09-10 15:26:14 +00:00
"content": "{{ page.content | markdownify | replace: '</h', ' . </h' | replace: '<hr', ' . <hr' | replace: '</p', ' . </p' | replace: '</ul', ' . </ul' | replace: '</tr', ' . </tr' | replace: '</li', ' | </li' | replace: '</td', ' | </td' | strip_html | escape_once | remove: 'Table of contents' | remove: '```' | remove: '---' | replace: '\', ' ' | replace: ' . . . ', ' . ' | replace: ' . . ', ' . ' | normalize_whitespace }}",
"url": "{{ page.url | absolute_url }}",
"relUrl": "{{ page.url }}"
2019-09-10 15:26:14 +00:00
}{% assign comma = true %}
2017-11-08 16:23:05 +00:00
{% endif %}{% endfor %}
}{% endraw %}
```
2018-10-24 18:06:41 +00:00
_Note: If you don't run this rake command or create this file manually, search will not work (or it will use the search index data from this docs site, not your site's content)._
2018-11-16 16:47:30 +00:00
2018-12-16 19:47:41 +00:00
### Enable search in configuration
2018-11-16 16:47:30 +00:00
2019-01-14 19:32:41 +00:00
In your site's `_config.yml`, enable search:
2018-11-16 16:47:30 +00:00
2019-01-16 01:43:19 +00:00
```yaml
2018-11-16 16:47:30 +00:00
# Enable or disable the site search
search_enabled: true
```
2018-12-16 19:47:41 +00:00
The default is for hyphens to separate tokens in search terms:
`gem-based` is equivalent to `gem based`, matching either word.
To allow search for hyphenated words:
```yaml
# Set the search token separator
search_tokenizer_separator: /[\s/]+/
```
2018-12-16 19:47:41 +00:00
## Hiding pages from search
2019-01-14 19:32:41 +00:00
Sometimes you might have a page that you don't want to be indexed for the search nor to show up in search results, e.g, a 404 page. To exclude a page from search, add the `search_exclude: true` parameter to the page's YAML front matter:
2018-12-16 19:47:41 +00:00
#### Example
{: .no_toc }
2019-01-14 19:18:09 +00:00
2018-12-16 19:47:41 +00:00
```yaml
---
layout: default
title: Page not found
nav_exclude: true
search_exclude: true
---
```