1
0
mirror of https://github.com/squidfunk/mkdocs-material.git synced 2024-12-01 02:27:17 +01:00

Added search separator customization option

This commit is contained in:
squidfunk 2017-07-27 22:48:47 +02:00 committed by Martin Donath
parent 147e5ef75d
commit d204b63ee4
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 %} {% endblock %}
</div> </div>
{% block scripts %} {% 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(",") %} {% set languages = lang.t("search.languages").split(",") %}
{% if languages | length and languages[0] != "" %} {% if languages | length and languages[0] != "" %}
{% set path = base_url + "/assets/javascripts/lunr" %} {% set path = base_url + "/assets/javascripts/lunr" %}

View File

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

View File

@ -9,7 +9,7 @@
</form> </form>
<div class="md-search__output"> <div class="md-search__output">
<div class="md-search__scrollwrap" data-md-scrollfix> <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') }}"> <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") }} {{ lang.t("search.result.placeholder") }}
</div> </div>

View File

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

View File

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

View File

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