pass minimum valid env to md.parse

fix/4375
Divyansh Singh 7 months ago
parent ef2cd66f25
commit 355ecdab4b

@ -142,7 +142,7 @@ export async function createMarkdownToVueRenderFn(
// resolve includes
let includes: string[] = []
src = processIncludes(md, srcDir, src, fileOrig, includes)
src = processIncludes(md, srcDir, src, fileOrig, includes, cleanUrls)
const localeIndex = getLocaleForPath(siteConfig?.site, relativePath)

@ -56,7 +56,7 @@ export async function localSearchPlugin(
const relativePath = slash(path.relative(srcDir, file))
const env: MarkdownEnv = { path: file, relativePath, cleanUrls }
const md_raw = await fs.promises.readFile(file, 'utf-8')
const md_src = processIncludes(md, srcDir, md_raw, file, [])
const md_src = processIncludes(md, srcDir, md_raw, file, [], cleanUrls)
if (options._render) {
return await options._render(md_src, env, md)
} else {

@ -4,14 +4,15 @@ import type { MarkdownItAsync } from 'markdown-it-async'
import path from 'node:path'
import c from 'picocolors'
import { findRegion } from '../markdown/plugins/snippet'
import { slash } from '../shared'
import { slash, type MarkdownEnv } from '../shared'
export function processIncludes(
md: MarkdownItAsync,
srcDir: string,
src: string,
file: string,
includes: string[]
includes: string[],
cleanUrls: boolean
): string {
const includesRE = /<!--\s*@include:\s*(.*?)\s*-->/g
const regionRE = /(#[^\s\{]+)/
@ -46,7 +47,11 @@ export function processIncludes(
if (start === undefined) {
// region not found, it might be a header
const tokens = md
.parse(content, {})
.parse(content, {
path: includePath,
relativePath: slash(path.relative(srcDir, includePath)),
cleanUrls
} satisfies MarkdownEnv)
.filter((t) => t.type === 'heading_open' && t.map)
const idx = tokens.findIndex(
(t) => t.attrGet('id') === regionName.slice(1)
@ -84,7 +89,14 @@ export function processIncludes(
includes.push(slash(includePath))
// recursively process includes in the content
return processIncludes(md, srcDir, content, includePath, includes)
return processIncludes(
md,
srcDir,
content,
includePath,
includes,
cleanUrls
)
//
} catch (error) {

Loading…
Cancel
Save