From c9a98ac6bb854a7a24c08cfc23e84ebc243ba347 Mon Sep 17 00:00:00 2001 From: JiZhi <471695625@qq.com> Date: Sat, 10 Jun 2023 16:59:00 +0800 Subject: [PATCH] fix(build): create markdown env for localSearchPlugin (#2322) --- src/node/plugins/localSearchPlugin.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/node/plugins/localSearchPlugin.ts b/src/node/plugins/localSearchPlugin.ts index 30a7bf9f..5979da43 100644 --- a/src/node/plugins/localSearchPlugin.ts +++ b/src/node/plugins/localSearchPlugin.ts @@ -4,6 +4,7 @@ import MiniSearch from 'minisearch' import fs from 'fs-extra' import _debug from 'debug' import type { SiteConfig } from '../config' +import type { MarkdownEnv } from '../markdown' import { createMarkdownRenderer } from '../markdown' import { resolveSiteDataByRoute, slash } from '../shared' @@ -45,6 +46,16 @@ export async function localSearchPlugin( siteConfig.logger ) + function createMarkdownEnv(file: string): MarkdownEnv { + const { srcDir, cleanUrls = false } = siteConfig + const relativePath = slash(path.relative(srcDir, file)) + return { + path: file, + relativePath, + cleanUrls + } + } + const indexByLocales = new Map>() function getIndexByLocale(locale: string) { @@ -111,7 +122,7 @@ export async function localSearchPlugin( .map(async (file) => { const fileId = getDocId(file) const sections = splitPageIntoSections( - md.render(await fs.readFile(file, 'utf-8')) + md.render(await fs.readFile(file, 'utf-8'), createMarkdownEnv(file)) ) const locale = getLocaleForPath(file) let documents = documentsByLocale.get(locale) @@ -191,7 +202,10 @@ export async function localSearchPlugin( } const index = getIndexForPath(ctx.file) const sections = splitPageIntoSections( - md.render(await fs.readFile(ctx.file, 'utf-8')) + md.render( + await fs.readFile(ctx.file, 'utf-8'), + createMarkdownEnv(ctx.file) + ) ) for (const section of sections) { const id = `${fileId}#${section.anchor}`