mirror of
https://github.com/thangisme/notes.git
synced 2024-11-10 15:26:05 -05:00
60c82cb518
Because `page.title` (and `page.url`) is already escaped and page titles served by lunrjs do not need escaping, ampersands need to be unescaped to display properly in search results.
77 lines
2.2 KiB
Markdown
77 lines
2.2 KiB
Markdown
---
|
|
layout: default
|
|
title: Search
|
|
nav_order: 7
|
|
---
|
|
|
|
# Search
|
|
{: .no_toc }
|
|
|
|
## Table of contents
|
|
{: .no_toc .text-delta }
|
|
|
|
1. TOC
|
|
{:toc}
|
|
|
|
---
|
|
|
|
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:
|
|
|
|
- Page title
|
|
- Page content
|
|
- Page URL
|
|
|
|
## Set up search
|
|
|
|
### Generate search index
|
|
|
|
Before you can use search, you must initialize the feature by running this `rake` command that comes with `just-the-docs`:
|
|
|
|
```bash
|
|
$ bundle exec just-the-docs rake search:init
|
|
```
|
|
|
|
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:
|
|
|
|
```liquid
|
|
{% raw %}---
|
|
---
|
|
{
|
|
{% for page in site.html_pages %}{% if page.search_exclude != true %}"{{ forloop.index0 }}": {
|
|
"id": "{{ forloop.index0 }}",
|
|
"title": "{{ page.title | replace: '&', '&' }}",
|
|
"content": "{{ page.content | markdownify | strip_html | escape_once | remove: 'Table of contents' | remove: '```' | remove: '---' | replace: '\', ' ' | normalize_whitespace }}",
|
|
"url": "{{ page.url | absolute_url }}",
|
|
"relUrl": "{{ page.url }}"
|
|
}{% unless forloop.last %},{% endunless %}
|
|
{% endif %}{% endfor %}
|
|
}{% endraw %}
|
|
```
|
|
|
|
_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)._
|
|
|
|
### Enable search in configuration
|
|
|
|
In your site's `_config.yml`, enable search:
|
|
|
|
```yaml
|
|
# Enable or disable the site search
|
|
search_enabled: true
|
|
```
|
|
|
|
## Hiding pages from search
|
|
|
|
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:
|
|
|
|
#### Example
|
|
{: .no_toc }
|
|
|
|
```yaml
|
|
---
|
|
layout: default
|
|
title: Page not found
|
|
nav_exclude: true
|
|
search_exclude: true
|
|
---
|
|
```
|