Skip to content

Commit

Permalink
fix(build): create markdown env for localSearchPlugin (#2322)
Browse files Browse the repository at this point in the history
  • Loading branch information
theniceangel committed Jun 10, 2023
1 parent 29a9647 commit c9a98ac
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/node/plugins/localSearchPlugin.ts
Expand Up @@ -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'

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 c9a98ac

Please sign in to comment.