Added search separator customization option

This commit is contained in:
squidfunk 2017-07-27 22:48:47 +02:00
parent 147e5ef75d
commit ef1c5a4043
7 changed files with 26 additions and 10 deletions

File diff suppressed because one or more lines are too long

View File

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

View File

@ -11,6 +11,7 @@
"search.result.none": "No matching documents",
"search.result.one": "1 matching document",
"search.result.other": "# matching documents",
"search.tokenizer": "",
"source.link.title": "Go to repository",
"toc.title": "Table of contents"
}[key] }}{% endmacro %}

View File

@ -9,7 +9,7 @@
</form>
<div class="md-search__output">
<div class="md-search__scrollwrap" data-md-scrollfix>
<div class="md-search-result" data-md-component="result" data-md-lang-search="{{ lang.t('search.languages') }}">
<div class="md-search-result" data-md-component="result" data-md-lang-search="{{ lang.t('search.languages') }}" data-md-lang-tokenizer="{{ lang.t('search.tokenizer') }}">
<div class="md-search-result__meta" data-md-lang-result-none="{{ lang.t('search.result.none') }}" data-md-lang-result-one="{{ lang.t('search.result.one') }}" data-md-lang-result-other="{{ lang.t('search.result.other') }}">
{{ lang.t("search.result.placeholder") }}
</div>

View File

@ -71,6 +71,10 @@ export default class Result {
other: this.meta_.dataset.mdLangResultOther
}
/* Override tokenizer separator, if given */
if (this.el_.dataset.mdLangTokenizer.length)
lunr.tokenizer.separator = this.el_.dataset.mdLangTokenizer
/* Load search languages */
this.lang_ = this.el_.dataset.mdLangSearch.split(",")
.filter(Boolean)
@ -139,13 +143,20 @@ export default class Result {
return docs
}, new Map)
/* eslint-disable no-invalid-this, lines-around-comment */
/* eslint-disable no-invalid-this */
const docs = this.docs_,
lang = this.lang_
/* Create index */
this.index_ = lunr(function() {
/* Remove stemmer, as it cripples search experience */
this.pipeline.reset()
this.pipeline.add(
lunr.trimmer,
lunr.stopWordFilter
)
/* Set up stemmers for search languages */
if (lang.length === 1) {
this.use(lunr[lang[0]])
@ -161,8 +172,8 @@ export default class Result {
/* Index documents */
docs.forEach(doc => this.add(doc))
})
/* eslint-enable no-invalid-this, lines-around-comment */
}
/* eslint-enable no-invalid-this */
/* Initialize index after short timeout to account for transition */
setTimeout(() => {
@ -217,10 +228,13 @@ export default class Result {
return items
}, new Map)
/* Assemble highlight regex from query string */
const match = new RegExp(
`(?:^|\\b)(${escape(this.value_.trim()).replace(" ", "|")})`, "img")
const highlight = string => `<em>${string}</em>`
/* Assemble regular expressions for matching */
const query = escape(this.value_.trim()).replace(
new RegExp(lunr.tokenizer.separator, "img"), "|")
const match =
new RegExp(`(^|${lunr.tokenizer.separator})(${query})`, "img")
const highlight = (_, separator, token) =>
`${separator}<em>${token}</em>`
/* Render results */
result.forEach((items, ref) => {

View File

@ -34,6 +34,7 @@
"search.result.none": "No matching documents",
"search.result.one": "1 matching document",
"search.result.other": "# matching documents",
"search.tokenizer": "",
"source.link.title": "Go to repository",
"toc.title": "Table of contents"
}[key] }}{% endmacro %}

View File

@ -38,7 +38,8 @@
<div class="md-search__output">
<div class="md-search__scrollwrap" data-md-scrollfix>
<div class="md-search-result" data-md-component="result"
data-md-lang-search="{{ lang.t('search.languages') }}">
data-md-lang-search="{{ lang.t('search.languages') }}"
data-md-lang-tokenizer="{{ lang.t('search.tokenizer') }}">
<div class="md-search-result__meta"
data-md-lang-result-none="{{ lang.t('search.result.none') }}"
data-md-lang-result-one="{{ lang.t('search.result.one') }}"