update code

pull/3032/head
buqiyuan 2 years ago
parent 8d5f3f66ba
commit 3479d5a0ce

@ -25,7 +25,7 @@ interface IndexObject {
title: string title: string
titles: string[] titles: string[]
} }
let mdEnv: MarkdownEnv let mdEnv: MarkdownEnv | null
export async function localSearchPlugin( export async function localSearchPlugin(
siteConfig: SiteConfig<DefaultTheme.Config> siteConfig: SiteConfig<DefaultTheme.Config>
): Promise<Plugin> { ): Promise<Plugin> {
@ -276,6 +276,7 @@ function splitPageIntoSections(html: string) {
parentTitles[level] = title parentTitles[level] = title
} }
} }
mdEnv = null
return sections return sections
} }
@ -289,13 +290,13 @@ function clearHtmlTags(str: string) {
} }
function replaceInterpolation(str: string) { function replaceInterpolation(str: string) {
if (!mdEnv.frontmatter) { if (!mdEnv?.frontmatter) {
return str return str
} }
return str.replace(/{{\s*([^}]+)\s*}}/g, (_, expression: string) => { return str.replace(/{{\s*([^}]+)\s*}}/g, (_, expression: string) => {
const properties = expression.trim().split('.') const properties = expression.trim().split('.')
let value: Record<string, any> = { $frontmatter: { ...mdEnv.frontmatter } } let value: Record<string, any> = { $frontmatter: { ...mdEnv?.frontmatter } }
for (let prop of properties) { for (let prop of properties) {
value = value?.[prop] value = value?.[prop]

Loading…
Cancel
Save