Skip to content

Commit

Permalink
feat: support multiple search languages
Browse files Browse the repository at this point in the history
  • Loading branch information
yufeih committed Oct 20, 2023
1 parent 2fccdbd commit ca239fa
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
3 changes: 3 additions & 0 deletions templates/modern/src/options.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export type DocfxOptions = {
/** Configures mermaid diagram options */
mermaid?: MermaidConfig,

/** A list of [lunr languages](https://github.com/MihaiValentin/lunr-languages#readme) such as fr, es for full text search */
lunrLanguages?: string[],

/** Configures [hightlight.js](https://highlightjs.org/) */
configureHljs?: (hljs: HLJSApi) => void,

Expand Down
56 changes: 54 additions & 2 deletions templates/modern/src/search-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.

import lunr from 'lunr'
import stemmer from 'lunr-languages/lunr.stemmer.support'
import multi from 'lunr-languages/lunr.multi'
import { get, set, createStore } from 'idb-keyval'

type SearchHit = {
Expand Down Expand Up @@ -31,14 +33,22 @@ async function loadIndexCore() {
}
}

const { configureLunr } = await import('./main.js').then(m => m.default) as DocfxOptions
const { lunrLanguages, configureLunr } = await import('./main.js').then(m => m.default) as DocfxOptions

if (lunrLanguages) {
multi(lunr)
stemmer(lunr)
await Promise.all(lunrLanguages.map(initLanguage))
}

const index = lunr(function() {
lunr.tokenizer.separator = /[\s\-.()]+/

this.ref('href')
this.field('title', { boost: 50 })
this.field('keywords', { boost: 20 })
this.use(lunr.multiLanguage(...lunrLanguages))

lunr.tokenizer.separator = /[\s\-.()]+/
configureLunr?.(this)

for (const key in data) {
Expand All @@ -60,3 +70,45 @@ onmessage = function(e) {
postMessage({ e: 'query-ready', d: search(e.data.q) })
}
}

const langMap = {
ar: () => import('lunr-languages/lunr.ar.js'),
da: () => import('lunr-languages/lunr.da.js'),
de: () => import('lunr-languages/lunr.de.js'),
du: () => import('lunr-languages/lunr.du.js'),
el: () => import('lunr-languages/lunr.el.js'),
es: () => import('lunr-languages/lunr.es.js'),
fi: () => import('lunr-languages/lunr.fi.js'),
fr: () => import('lunr-languages/lunr.fr.js'),
he: () => import('lunr-languages/lunr.he.js'),
hi: () => import('lunr-languages/lunr.hi.js'),
hu: () => import('lunr-languages/lunr.hu.js'),
hy: () => import('lunr-languages/lunr.hy.js'),
it: () => import('lunr-languages/lunr.it.js'),
ja: () => import('lunr-languages/lunr.ja.js'),
jp: () => import('lunr-languages/lunr.jp.js'),
kn: () => import('lunr-languages/lunr.kn.js'),
ko: () => import('lunr-languages/lunr.ko.js'),
nl: () => import('lunr-languages/lunr.nl.js'),
no: () => import('lunr-languages/lunr.no.js'),
pt: () => import('lunr-languages/lunr.pt.js'),
ro: () => import('lunr-languages/lunr.ro.js'),
ru: () => import('lunr-languages/lunr.ru.js'),
sa: () => import('lunr-languages/lunr.sa.js'),
sv: () => import('lunr-languages/lunr.sv.js'),
ta: () => import('lunr-languages/lunr.ta.js'),
te: () => import('lunr-languages/lunr.te.js'),
th: () => import('lunr-languages/lunr.th.js'),
tr: () => import('lunr-languages/lunr.tr.js'),
vi: () => import('lunr-languages/lunr.vi.js')

// zh is currently not supported due to dependency on NodeJS.
// zh: () => import('lunr-languages/lunr.zh.js')
}

async function initLanguage(lang: string) {
if (lang !== 'en') {
const { default: init } = await langMap[lang]()
init(lunr)
}
}
11 changes: 11 additions & 0 deletions templates/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions templates/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"jquery": "3.7.0",
"lit-html": "^3.0.0",
"lunr": "2.3.9",
"lunr-languages": "^1.14.0",
"mathjax": "^3.2.2",
"mermaid": "^10.5.0"
},
Expand Down

0 comments on commit ca239fa

Please sign in to comment.