fix: index rewritten local search pages by locale (#5241)

pull/5207/merge
T 3 days ago committed by GitHub
parent a357e5ef67
commit 80cf2650aa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,102 @@
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import path from 'node:path'
import { resolveConfig } from 'node/config'
import { localSearchPlugin } from 'node/plugins/localSearchPlugin'
import MiniSearch from 'minisearch'
describe('node/plugins/localSearchPlugin', () => {
let root: string | undefined
let nodeEnv: string | undefined
beforeEach(() => {
nodeEnv = process.env.NODE_ENV
process.env.NODE_ENV = 'production'
})
afterEach(async () => {
if (nodeEnv === undefined) {
delete process.env.NODE_ENV
} else {
process.env.NODE_ENV = nodeEnv
}
if (root) {
await rm(root, { recursive: true, force: true })
root = undefined
}
})
test('indexes rewritten pages by rewritten locale path', async () => {
root = await mkdtemp(path.join(tmpdir(), 'vitepress-local-search-'))
const configDir = path.join(root, '.vitepress')
await mkdir(configDir)
await writeFile(
path.join(root, 'index.md'),
'# English home\n\nrootonlytoken\n'
)
await writeFile(
path.join(root, 'zh.md'),
'# Chinese home\n\nlocaleonlytoken\n'
)
await writeFile(
path.join(configDir, 'config.ts'),
[
'export default {',
' rewrites: {',
" 'index.md': 'guide.md',",
" 'zh.md': 'zh/guide.md'",
' },',
' locales: {',
" root: { label: 'English', lang: 'en' },",
" zh: { label: 'Chinese', lang: 'zh' }",
' },',
' themeConfig: {',
" search: { provider: 'local' }",
' }',
'}'
].join('\n')
)
const siteConfig = await resolveConfig(root, 'build', 'production')
const plugin = await localSearchPlugin(siteConfig)
const indexModule = (await plugin.load?.call(
{} as never,
'/@localSearchIndex'
)) as string
expect(indexModule).toContain(
'"root": () => import(\'@localSearchIndexroot\')'
)
expect(indexModule).toContain('"zh": () => import(\'@localSearchIndexzh\')')
const rootIndex = loadIndex(
(await plugin.load?.call({} as never, '/@localSearchIndexroot')) as string
)
const zhIndex = loadIndex(
(await plugin.load?.call({} as never, '/@localSearchIndexzh')) as string
)
expect(rootIndex.search('rootonlytoken')).toMatchObject([
{ id: '/guide.html#english-home' }
])
expect(rootIndex.search('localeonlytoken')).toEqual([])
expect(zhIndex.search('localeonlytoken')).toMatchObject([
{ id: '/zh/guide.html#chinese-home' }
])
expect(zhIndex.search('rootonlytoken')).toEqual([])
})
})
function loadIndex(serializedModule: string) {
const serializedIndex = JSON.parse(
serializedModule.slice('export default '.length)
)
return MiniSearch.loadJSON(serializedIndex, {
fields: ['title', 'titles', 'text'],
storeFields: ['title', 'titles']
})
}

@ -116,7 +116,10 @@ export async function localSearchPlugin(
const file = path.join(siteConfig.srcDir, page)
// get file metadata
const fileId = getDocId(file)
const locale = getLocaleForPath(siteConfig.site, page)
const locale = getLocaleForPath(
siteConfig.site,
siteConfig.rewrites.map[page] || page
)
const index = getIndexByLocale(locale)
// retrieve file and split into "sections"
const html = await render(file)

Loading…
Cancel
Save