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 // resolve includes
let includes: string[] = [] let includes: string[] = []
src = processIncludes(md, srcDir, src, fileOrig, includes) src = processIncludes(md, srcDir, src, fileOrig, includes, cleanUrls)
const localeIndex = getLocaleForPath(siteConfig?.site, relativePath) const localeIndex = getLocaleForPath(siteConfig?.site, relativePath)

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

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

Loading…
Cancel
Save