Skip to content

Commit

Permalink
Merge pull request google#275 from tekezo/fix/offline-search-html-ent…
Browse files Browse the repository at this point in the history
…ities

Fix a offline search issue that escaped HTML entities is shown
  • Loading branch information
LisaFC committed Jun 15, 2020
2 parents f75dcb3 + ef07806 commit b005fbb
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
5 changes: 4 additions & 1 deletion assets/js/offline-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@
});
});
})
.slice(0, 10);
.slice(
0,
$targetSearchInput.data('offlnie-search-max-results')
);

//
// Make result html
Expand Down
3 changes: 2 additions & 1 deletion assets/json/offline-search-index.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{{- $.Scratch.Add "offline-search-index" slice -}}
{{- range where .Site.AllPages ".Params.exclude_search" "!=" true -}}
{{- $.Scratch.Add "offline-search-index" (dict "title" .Title "ref" .RelPermalink "body" .Plain "excerpt" (.Summary | truncate 100)) -}}
{{/* We have to apply `htmlUnescape` again after `truncate` because `truncate` applies `html.EscapeString` if the argument is not HTML. */}}
{{- $.Scratch.Add "offline-search-index" (dict "title" .Title "ref" .RelPermalink "body" (.Plain | htmlUnescape) "excerpt" (.Plain | htmlUnescape | truncate (.Site.Params.offlineSearchSummaryLength | default 70) | htmlUnescape)) -}}
{{- end -}}
{{- $.Scratch.Get "offline-search-index" | jsonify -}}
1 change: 1 addition & 0 deletions layouts/partials/search-input.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
*/}}
data-offline-search-index-json-src="{{ $offlineSearchIndexFingerprint.RelPermalink }}"
data-offline-search-base-href="/"
data-offlnie-search-max-results="{{ .Site.Params.offlineSearchMaxResults | default 10 }}"
>
{{ end }}
4 changes: 2 additions & 2 deletions userguide/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ algolia_docsearch = false

#Enable offline search with Lunr.js
offlineSearch = false
offlineSearchSummaryLength = 70
offlineSearchMaxResults = 10

# User interface configuration
[params.ui]
Expand Down Expand Up @@ -188,5 +190,3 @@ enable = false
# url = "https://example.org/mail"
# icon = "fa fa-envelope"
# desc = "Discuss development issues around the project"


20 changes: 20 additions & 0 deletions userguide/content/en/docs/Adding content/navigation.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,26 @@ Once you've completed these steps, local search is enabled for your site and res
If you're [testing this locally](/docs/deployment/#serving-your-site-locally) using Hugo’s local server functionality, you need to build your `offline-search-index.xxx.json` file first by running `hugo`. If you have the Hugo server running while you build `offline-search-index.xxx.json`, you may need to stop the server and restart it in order to see your search results.
{{% /alert %}}

### Changing the summary length of the local search results

You can customize the summary length by setting `offlineSearchSummaryLength` in `config.toml`.

```
#Enable offline search with Lunr.js
offlineSearch = true
offlineSearchSummaryLength = 200
```

### Changing the maximum result count of the local search

You can customize the maximum result count by setting `offlineSearchMaxResults` in `config.toml`.

```
#Enable offline search with Lunr.js
offlineSearch = true
offlineSearchMaxResults = 25
```

### Changing the width of the local search results popover

The width of the search results popover will automatically widen according to the content.
Expand Down

0 comments on commit b005fbb

Please sign in to comment.