Improved search matching and highlighting

This commit is contained in:
squidfunk 2017-07-25 16:46:51 +02:00 committed by Martin Donath
parent 4887a7e062
commit 357c6f4122
3 changed files with 14 additions and 4 deletions

View File

@ -150,7 +150,7 @@
{% endblock %}
</div>
{% block scripts %}
<script src="{{ base_url }}/assets/javascripts/application-b4eb7a00cd.js"></script>
<script src="{{ base_url }}/assets/javascripts/application-8ec1c9e2d8.js"></script>
{% set languages = lang.t("search.languages").split(",") %}
{% if languages | length and languages[0] != "" %}
{% set path = base_url + "/assets/javascripts/lunr" %}

View File

@ -194,7 +194,17 @@ export default class Result {
/* Perform search on index and group sections by document */
const result = this.index_
.search(this.value_)
/* Append trailing wildcard to all terms for prefix querying */
.query(query => {
this.value_.split(" ")
.filter(Boolean)
.forEach(term => {
query.term(term, { wildcard: lunr.Query.wildcard.TRAILING })
})
})
/* Process query results */
.reduce((items, item) => {
const doc = this.docs_.get(item.ref)
if (doc.parent) {
@ -209,7 +219,7 @@ export default class Result {
/* Assemble highlight regex from query string */
const match = new RegExp(
`(?:^|\\s)(${escape(this.value_.trim()).replace(" ", "|")})`, "img")
`(?:^|\\b)(${escape(this.value_.trim()).replace(" ", "|")})`, "img")
const highlight = string => `<em>${string}</em>`
/* Render results */