mirror of
https://github.com/squidfunk/mkdocs-material.git
synced 2026-07-29 01:02:42 -04:00
Improved search experience in many languages
This commit is contained in:
@@ -58,9 +58,9 @@ export interface SearchIndexDocument {
|
||||
* Search index pipeline function
|
||||
*/
|
||||
export type SearchIndexPipelineFn =
|
||||
| "stemmer" /* Stemmer */
|
||||
| "stopWordFilter" /* Stop word filter */
|
||||
| "trimmer" /* Trimmer */
|
||||
| "stopWordFilter" /* Stop word filter */
|
||||
| "stemmer" /* Stemmer */
|
||||
|
||||
/**
|
||||
* Search index pipeline
|
||||
@@ -92,6 +92,25 @@ export interface SearchResult {
|
||||
sections: SectionDocument[] /* Section documents */
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Functions
|
||||
* ------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Compute the difference of two lists of strings
|
||||
*
|
||||
* @param a - 1st list of strings
|
||||
* @param b - 2nd list of strings
|
||||
*
|
||||
* @return Difference
|
||||
*/
|
||||
function difference(a: string[], b: string[]): string[] {
|
||||
const [x, y] = [new Set(a), new Set(b)]
|
||||
return [
|
||||
...new Set([...x].filter(value => !y.has(value)))
|
||||
]
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Class
|
||||
* ------------------------------------------------------------------------- */
|
||||
@@ -147,15 +166,20 @@ export class Search {
|
||||
this.use((lunr as any).multiLanguage(...config.lang))
|
||||
}
|
||||
|
||||
/* Set up pipeline according to configuration */
|
||||
this.pipeline.reset()
|
||||
for (const fn of pipeline!)
|
||||
this.pipeline.add(lunr[fn])
|
||||
/* Compute functions to be removed from the pipeline */
|
||||
const fns = difference([
|
||||
"trimmer", "stopWordFilter", "stemmer"
|
||||
], pipeline!)
|
||||
|
||||
/* Set up search pipeline according to configuration */
|
||||
this.searchPipeline.reset()
|
||||
for (const fn of pipeline!)
|
||||
this.searchPipeline.add(lunr[fn])
|
||||
/* Remove functions from the pipeline for every language */
|
||||
for (const lang of config.lang.map(language => (
|
||||
language === "en" ? lunr : (lunr as any)[language]
|
||||
))) {
|
||||
for (const fn of fns) {
|
||||
this.pipeline.remove(lang[fn])
|
||||
this.searchPipeline.remove(lang[fn])
|
||||
}
|
||||
}
|
||||
|
||||
/* Set up fields and reference */
|
||||
this.field("title", { boost: 1000 })
|
||||
|
||||
@@ -51,5 +51,5 @@ export function defaultTransform(value: string): string {
|
||||
return value
|
||||
.replace(/(?:^|\s+)[*+-:^~]+(?=\s+|$)/g, "")
|
||||
.trim()
|
||||
.replace(/\s+|\b$/g, "* ")
|
||||
.replace(/\s+|(?![^\x00-\x7F]|^)$|\b$/g, "* ")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user