Skip to content

Commit 833716d

Browse files
authored
BREAKING CHANGE: drop configureLunr (#9342)
chore: drop configureLunr
1 parent d1844e1 commit 833716d

File tree

4 files changed

+41
-46
lines changed

4 files changed

+41
-46
lines changed

templates/modern/src/docfx.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ async function init() {
2626
window.docfx = window.docfx || {}
2727

2828
initTheme()
29-
enableSearch()
3029

3130
await Promise.all([
31+
enableSearch(),
3232
renderInThisArticle(),
3333
renderMarkdown(),
3434
renderNav(),

templates/modern/src/options.d.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import BootstrapIcons from 'bootstrap-icons/font/bootstrap-icons.json'
55
import { HLJSApi } from 'highlight.js'
66
import { AnchorJSOptions } from 'anchor-js'
77
import { MermaidConfig } from 'mermaid'
8-
import lunr from 'lunr'
98

109
export type Theme = 'light' | 'dark' | 'auto'
1110

@@ -41,7 +40,4 @@ export type DocfxOptions = {
4140

4241
/** Configures [hightlight.js](https://highlightjs.org/) */
4342
configureHljs?: (hljs: HLJSApi) => void,
44-
45-
/** Configures [lunr](https://lunrjs.com/docs/index.html) */
46-
configureLunr?: (lunr: lunr.Builder) => void,
4743
}

templates/modern/src/search-worker.ts

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,63 +14,59 @@ type SearchHit = {
1414

1515
let search: (q: string) => SearchHit[]
1616

17-
async function loadIndex() {
17+
async function loadIndex({ lunrLanguages }: { lunrLanguages?: string[] }) {
1818
const { index, data } = await loadIndexCore()
1919
search = q => index.search(q).map(({ ref }) => data[ref])
2020
postMessage({ e: 'index-ready' })
21-
}
22-
23-
async function loadIndexCore() {
24-
const res = await fetch('../index.json')
25-
const etag = res.headers.get('etag')
26-
const data = await res.json() as { [key: string]: SearchHit }
27-
const cache = createStore('docfx', 'lunr')
2821

29-
const { lunrLanguages, configureLunr } = await import('./main.js').then(m => m.default) as DocfxOptions
22+
async function loadIndexCore() {
23+
const res = await fetch('../index.json')
24+
const etag = res.headers.get('etag')
25+
const data = await res.json() as { [key: string]: SearchHit }
26+
const cache = createStore('docfx', 'lunr')
3027

31-
if (lunrLanguages && lunrLanguages.length > 0) {
32-
multi(lunr)
33-
stemmer(lunr)
34-
await Promise.all(lunrLanguages.map(initLanguage))
35-
}
28+
if (lunrLanguages && lunrLanguages.length > 0) {
29+
multi(lunr)
30+
stemmer(lunr)
31+
await Promise.all(lunrLanguages.map(initLanguage))
32+
}
3633

37-
if (etag) {
38-
const value = JSON.parse(await get('index', cache) || '{}')
39-
if (value && value.etag === etag) {
40-
return { index: lunr.Index.load(value), data }
34+
if (etag) {
35+
const value = JSON.parse(await get('index', cache) || '{}')
36+
if (value && value.etag === etag) {
37+
return { index: lunr.Index.load(value), data }
38+
}
4139
}
42-
}
4340

44-
const index = lunr(function() {
45-
lunr.tokenizer.separator = /[\s\-.()]+/
41+
const index = lunr(function() {
42+
lunr.tokenizer.separator = /[\s\-.()]+/
4643

47-
this.ref('href')
48-
this.field('title', { boost: 50 })
49-
this.field('keywords', { boost: 20 })
44+
this.ref('href')
45+
this.field('title', { boost: 50 })
46+
this.field('keywords', { boost: 20 })
5047

51-
if (lunrLanguages && lunrLanguages.length > 0) {
52-
this.use(lunr.multiLanguage(...lunrLanguages))
53-
}
48+
if (lunrLanguages && lunrLanguages.length > 0) {
49+
this.use(lunr.multiLanguage(...lunrLanguages))
50+
}
5451

55-
configureLunr?.(this)
52+
for (const key in data) {
53+
this.add(data[key])
54+
}
55+
})
5656

57-
for (const key in data) {
58-
this.add(data[key])
57+
if (etag) {
58+
await set('index', JSON.stringify(Object.assign(index.toJSON(), { etag })), cache)
5959
}
60-
})
6160

62-
if (etag) {
63-
await set('index', JSON.stringify(Object.assign(index.toJSON(), { etag })), cache)
61+
return { index, data }
6462
}
65-
66-
return { index, data }
6763
}
6864

69-
loadIndex().catch(console.error)
70-
7165
onmessage = function(e) {
72-
if (search) {
66+
if (e.data.q && search) {
7367
postMessage({ e: 'query-ready', d: search(e.data.q) })
68+
} else if (e.data.init) {
69+
loadIndex(e.data.init).catch(console.error)
7470
}
7571
}
7672

templates/modern/src/search.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
import { loc, meta } from './helper'
4+
import { loc, meta, options } from './helper'
55
import { html, render, TemplateResult } from 'lit-html'
66
import { classMap } from 'lit-html/directives/class-map.js'
77

@@ -16,7 +16,7 @@ let query
1616
/**
1717
* Support full-text-search
1818
*/
19-
export function enableSearch() {
19+
export async function enableSearch() {
2020
const searchQuery = document.getElementById('search-query') as HTMLInputElement
2121
if (!searchQuery || !window.Worker) {
2222
return
@@ -47,6 +47,9 @@ export function enableSearch() {
4747
}
4848
}
4949

50+
const { lunrLanguages } = await options()
51+
worker.postMessage({ init: { lunrLanguages } })
52+
5053
function onSearchQueryInput() {
5154
query = searchQuery.value
5255

@@ -100,7 +103,7 @@ export function enableSearch() {
100103
const curHits = hits.slice(start, start + numPerPage)
101104

102105
const items = html`
103-
<div class="search-list">${loc('searchResultsCount', { count: hits.length, query })}</div>
106+
<div class="search-list">${loc('searchResultsCount', { count: hits.length.toString(), query })}</div>
104107
<div class="sr-items">${curHits.map(hit => {
105108
const currentUrl = window.location.href
106109
const itemRawHref = relativeUrlToAbsoluteUrl(currentUrl, relHref + hit.href)

0 commit comments

Comments
 (0)