Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
chore: drop configureLunr
  • Loading branch information
yufeih committed Oct 25, 2023
commit 24f1f937e0e86e01967cbf4ed170a35f92693bad
2 changes: 1 addition & 1 deletion templates/modern/src/docfx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ async function init() {
window.docfx = window.docfx || {}

initTheme()
enableSearch()

await Promise.all([
enableSearch(),
renderInThisArticle(),
renderMarkdown(),
renderNav(),
Expand Down
4 changes: 0 additions & 4 deletions templates/modern/src/options.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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,
}
72 changes: 34 additions & 38 deletions templates/modern/src/search-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
9 changes: 6 additions & 3 deletions templates/modern/src/search.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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
Expand Down Expand Up @@ -47,6 +47,9 @@ export function enableSearch() {
}
}

const { lunrLanguages } = await options()
worker.postMessage({ init: { lunrLanguages } })

function onSearchQueryInput() {
query = searchQuery.value

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

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