Skip to content

Commit

Permalink
fix #1067
Browse files Browse the repository at this point in the history
- add framework for custom content into
  Lunr indices
- update docs for this feature
  • Loading branch information
diablodale committed Jan 9, 2023
1 parent c296f91 commit cc9f37e
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 0 deletions.
Empty file added _includes/lunr/custom-data.json
Empty file.
Empty file added _includes/lunr/custom-index.js
Empty file.
1 change: 1 addition & 0 deletions assets/js/just-the-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ function initSearch() {
this.metadataWhitelist = ['position']

for (var i in docs) {
{% include lunr/custom-index.js %}
this.add({
id: i,
title: docs[i].title,
Expand Down
2 changes: 2 additions & 0 deletions assets/js/zzzz-search-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ permalink: /assets/js/search-data.json
"title": {{ title | jsonify }},
"content": {{ content | replace: '</h', ' . </h' | replace: '<hr', ' . <hr' | replace: '</p', ' . </p' | replace: '<ul', ' . <ul' | replace: '</ul', ' . </ul' | replace: '<ol', ' . <ol' | replace: '</ol', ' . </ol' | replace: '</tr', ' . </tr' | replace: '<li', ' | <li' | replace: '</li', ' | </li' | replace: '</td', ' | </td' | replace: '<td', ' | <td' | replace: '</th', ' | </th' | replace: '<th', ' | <th' | strip_html | remove: 'Table of contents' | normalize_whitespace | replace: '. . .', '.' | replace: '. .', '.' | replace: '| |', '|' | append: ' ' | jsonify }},
"url": "{{ url | relative_url }}",
{% include lunr/custom-data.json page=page %}
"relUrl": "{{ url }}"
}
{%- assign i = i | plus: 1 -%}
Expand All @@ -62,6 +63,7 @@ permalink: /assets/js/search-data.json
"title": {{ page.title | jsonify }},
"content": {{ parts[0] | replace: '</h', ' . </h' | replace: '<hr', ' . <hr' | replace: '</p', ' . </p' | replace: '<ul', ' . <ul' | replace: '</ul', ' . </ul' | replace: '<ol', ' . <ol' | replace: '</ol', ' . </ol' | replace: '</tr', ' . </tr' | replace: '<li', ' | <li' | replace: '</li', ' | </li' | replace: '</td', ' | </td' | replace: '<td', ' | <td' | replace: '</th', ' | </th' | replace: '<th', ' | <th' | strip_html | remove: 'Table of contents' | normalize_whitespace | replace: '. . .', '.' | replace: '. .', '.' | replace: '| |', '|' | append: ' ' | jsonify }},
"url": "{{ page.url | relative_url }}",
{% include lunr/custom-data.json page=page %}
"relUrl": "{{ page.url }}"
}
{%- assign i = i | plus: 1 -%}
Expand Down
39 changes: 39 additions & 0 deletions docs/search.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,42 @@ $ bundle exec just-the-docs rake search:init

This command creates the `assets/js/zzzz-search-data.json` file that Jekyll uses to create your search index.
Alternatively, you can create the file manually with [this content]({{ site.github.repository_url }}/blob/main/assets/js/zzzz-search-data.json).

## Custom content for search index

The standard text that is indexed is the page (or post) `.content`, `.title`, and sometimes headers which are already in the `.content`. Other text (Front Matter, data files, etc.) is not indexed. When you want additional text to be indexed, you can customize Just the Docs.

{: .warning }
> Customizing search indices is an advanced feature that requires Javascript and Liquid knowledge.
1. When your site used a previous version of Just the Docs, you must update the file
`assets/js/zzzz-search-data.json` using the [above methods](#generate-search-index-when-used-as-a-gem).
2. Add a new file named `_includes/lunr/custom-data.json` to your site. Insert your custom Liquid
code that reads the page object at `include.page` then generates custom Javascript
fields that hold the custom text you want to index. You can verify the fields you output at
the generated `assets/js/search-data.json`.
3. Add a new file named `_includes/lunr/custom-index.js` to your site. Insert your custom Javascript
code that reads your custom Javascript fields and inserts them into the search index.

#### Example

`_includes/lunr/custom-data.json` custom code reads the page's custom Front Matter `usage` and `examples`
fields, normalizes the text, and writes the text to custom Javascript `myusage` and `myexamples` fields.

{% raw %}
```liquid
{%- capture newline %}
{% endcapture -%}
"myusage": {{ include.page.usage | markdownify | replace:newline,' ' | strip_html | normalize_whitespace | strip | jsonify }},
"myexamples": {{ include.page.examples | markdownify | replace:newline,' ' | strip_html | normalize_whitespace | strip | jsonify }},
```
{% endraw %}

`_includes/lunr/custom-index.js` custom code is within a Javascript loop. All custom
Javascript fields are accessed as fields of `docs[i]` such as `docs[i].myusage`.
Finally, append your custom fields on to the already existing `docs[i].content`.

```javascript
const content_to_merge = [docs[i].content, docs[i].myusage, docs[i].myexamples];
docs[i].content = content_to_merge.join(' ');
```
2 changes: 2 additions & 0 deletions lib/tasks/search.rake
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ permalink: /assets/js/search-data.json
"title": {{ title | jsonify }},
"content": {{ content | replace: \'</h\', \' . </h\' | replace: \'<hr\', \' . <hr\' | replace: \'</p\', \' . </p\' | replace: \'<ul\', \' . <ul\' | replace: \'</ul\', \' . </ul\' | replace: \'<ol\', \' . <ol\' | replace: \'</ol\', \' . </ol\' | replace: \'</tr\', \' . </tr\' | replace: \'<li\', \' | <li\' | replace: \'</li\', \' | </li\' | replace: \'</td\', \' | </td\' | replace: \'<td\', \' | <td\' | replace: \'</th\', \' | </th\' | replace: \'<th\', \' | <th\' | strip_html | remove: \'Table of contents\' | normalize_whitespace | replace: \'. . .\', \'.\' | replace: \'. .\', \'.\' | replace: \'| |\', \'|\' | append: \' \' | jsonify }},
"url": "{{ url | relative_url }}",
{% include lunr/custom-data.json page=page %}
"relUrl": "{{ url }}"
}
{%- assign i = i | plus: 1 -%}
Expand All @@ -72,6 +73,7 @@ permalink: /assets/js/search-data.json
"title": {{ page.title | jsonify }},
"content": {{ parts[0] | replace: \'</h\', \' . </h\' | replace: \'<hr\', \' . <hr\' | replace: \'</p\', \' . </p\' | replace: \'<ul\', \' . <ul\' | replace: \'</ul\', \' . </ul\' | replace: \'<ol\', \' . <ol\' | replace: \'</ol\', \' . </ol\' | replace: \'</tr\', \' . </tr\' | replace: \'<li\', \' | <li\' | replace: \'</li\', \' | </li\' | replace: \'</td\', \' | </td\' | replace: \'<td\', \' | <td\' | replace: \'</th\', \' | </th\' | replace: \'<th\', \' | <th\' | strip_html | remove: \'Table of contents\' | normalize_whitespace | replace: \'. . .\', \'.\' | replace: \'. .\', \'.\' | replace: \'| |\', \'|\' | append: \' \' | jsonify }},
"url": "{{ page.url | relative_url }}",
{% include lunr/custom-data.json page=page %}
"relUrl": "{{ page.url }}"
}
{%- assign i = i | plus: 1 -%}
Expand Down

0 comments on commit cc9f37e

Please sign in to comment.