Skip to content

Commit

Permalink
fix(localSearchPlugin): create markdown env for localSearchPlugin, close
Browse files Browse the repository at this point in the history
  • Loading branch information
theniceangel committed Apr 30, 2023
1 parent e36101c commit 792f057
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/node/plugins/localSearchPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import fs from 'fs-extra'
import _debug from 'debug'
import type { SiteConfig } from '../config'
import { createMarkdownRenderer } from '../markdown/markdown'
import type { MarkdownEnv } from '../markdown'
import { resolveSiteDataByRoute, slash } from '../shared'

const debug = _debug('vitepress:local-search')
Expand Down Expand Up @@ -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<string, MiniSearch<IndexObject>>()

function getIndexByLocale(locale: string) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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}`
Expand Down

0 comments on commit 792f057

Please sign in to comment.