diff --git a/templates/modern/src/docfx.ts b/templates/modern/src/docfx.ts index c5505ad63cc..0864508636a 100644 --- a/templates/modern/src/docfx.ts +++ b/templates/modern/src/docfx.ts @@ -26,9 +26,9 @@ async function init() { window.docfx = window.docfx || {} initTheme() - enableSearch() await Promise.all([ + enableSearch(), renderInThisArticle(), renderMarkdown(), renderNav(), diff --git a/templates/modern/src/options.d.ts b/templates/modern/src/options.d.ts index b769a5b1ede..dd2df89771e 100644 --- a/templates/modern/src/options.d.ts +++ b/templates/modern/src/options.d.ts @@ -5,7 +5,6 @@ import BootstrapIcons from 'bootstrap-icons/font/bootstrap-icons.json' import { HLJSApi } from 'highlight.js' import { AnchorJSOptions } from 'anchor-js' import { MermaidConfig } from 'mermaid' -import lunr from 'lunr' export type Theme = 'light' | 'dark' | 'auto' @@ -41,7 +40,4 @@ export type DocfxOptions = { /** Configures [hightlight.js](https://highlightjs.org/) */ configureHljs?: (hljs: HLJSApi) => void, - - /** Configures [lunr](https://lunrjs.com/docs/index.html) */ - configureLunr?: (lunr: lunr.Builder) => void, } diff --git a/templates/modern/src/search-worker.ts b/templates/modern/src/search-worker.ts index 362bd8acb68..fe62c6dba9e 100644 --- a/templates/modern/src/search-worker.ts +++ b/templates/modern/src/search-worker.ts @@ -14,63 +14,59 @@ type SearchHit = { let search: (q: string) => SearchHit[] -async function loadIndex() { +async function loadIndex({ lunrLanguages }: { lunrLanguages?: string[] }) { const { index, data } = await loadIndexCore() search = q => index.search(q).map(({ ref }) => data[ref]) postMessage({ e: 'index-ready' }) -} - -async function loadIndexCore() { - const res = await fetch('../index.json') - const etag = res.headers.get('etag') - const data = await res.json() as { [key: string]: SearchHit } - const cache = createStore('docfx', 'lunr') - const { lunrLanguages, configureLunr } = await import('./main.js').then(m => m.default) as DocfxOptions + async function loadIndexCore() { + const res = await fetch('../index.json') + const etag = res.headers.get('etag') + const data = await res.json() as { [key: string]: SearchHit } + const cache = createStore('docfx', 'lunr') - if (lunrLanguages && lunrLanguages.length > 0) { - multi(lunr) - stemmer(lunr) - await Promise.all(lunrLanguages.map(initLanguage)) - } + if (lunrLanguages && lunrLanguages.length > 0) { + multi(lunr) + stemmer(lunr) + await Promise.all(lunrLanguages.map(initLanguage)) + } - if (etag) { - const value = JSON.parse(await get('index', cache) || '{}') - if (value && value.etag === etag) { - return { index: lunr.Index.load(value), data } + if (etag) { + const value = JSON.parse(await get('index', cache) || '{}') + if (value && value.etag === etag) { + return { index: lunr.Index.load(value), data } + } } - } - const index = lunr(function() { - lunr.tokenizer.separator = /[\s\-.()]+/ + const index = lunr(function() { + lunr.tokenizer.separator = /[\s\-.()]+/ - this.ref('href') - this.field('title', { boost: 50 }) - this.field('keywords', { boost: 20 }) + this.ref('href') + this.field('title', { boost: 50 }) + this.field('keywords', { boost: 20 }) - if (lunrLanguages && lunrLanguages.length > 0) { - this.use(lunr.multiLanguage(...lunrLanguages)) - } + if (lunrLanguages && lunrLanguages.length > 0) { + this.use(lunr.multiLanguage(...lunrLanguages)) + } - configureLunr?.(this) + for (const key in data) { + this.add(data[key]) + } + }) - for (const key in data) { - this.add(data[key]) + if (etag) { + await set('index', JSON.stringify(Object.assign(index.toJSON(), { etag })), cache) } - }) - if (etag) { - await set('index', JSON.stringify(Object.assign(index.toJSON(), { etag })), cache) + return { index, data } } - - return { index, data } } -loadIndex().catch(console.error) - onmessage = function(e) { - if (search) { + if (e.data.q && search) { postMessage({ e: 'query-ready', d: search(e.data.q) }) + } else if (e.data.init) { + loadIndex(e.data.init).catch(console.error) } } diff --git a/templates/modern/src/search.ts b/templates/modern/src/search.ts index 6fe527d8af1..aef1c540b61 100644 --- a/templates/modern/src/search.ts +++ b/templates/modern/src/search.ts @@ -1,7 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -import { loc, meta } from './helper' +import { loc, meta, options } from './helper' import { html, render, TemplateResult } from 'lit-html' import { classMap } from 'lit-html/directives/class-map.js' @@ -16,7 +16,7 @@ let query /** * Support full-text-search */ -export function enableSearch() { +export async function enableSearch() { const searchQuery = document.getElementById('search-query') as HTMLInputElement if (!searchQuery || !window.Worker) { return @@ -47,6 +47,9 @@ export function enableSearch() { } } + const { lunrLanguages } = await options() + worker.postMessage({ init: { lunrLanguages } }) + function onSearchQueryInput() { query = searchQuery.value @@ -100,7 +103,7 @@ export function enableSearch() { const curHits = hits.slice(start, start + numPerPage) const items = html` -