refactor(markdown): make processIncludes async

Resolve includes through replaceAsync and fs/promises so include
files are read without blocking the event loop while pages render
concurrently.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
pull/5342/head
Divyansh Singh 2 months ago
parent 5ebd6b39ce
commit b4e7554608

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

@ -59,7 +59,7 @@ export async function localSearchPlugin(
} }
throw e throw e
}) })
const src = processIncludes(md, srcDir, raw, file, [], cleanUrls) const src = await processIncludes(md, srcDir, raw, file, [], cleanUrls)
if (options._render) { if (options._render) {
return options._render(src, env, md) return options._render(src, env, md)
} else { } else {

@ -1,6 +1,6 @@
import matter from 'gray-matter' import matter from 'gray-matter'
import type { MarkdownItAsync } from 'markdown-it-async' import { replaceAsync, type MarkdownItAsync } from 'markdown-it-async'
import fs from 'node:fs' import { readFile } from 'node:fs/promises'
import path from 'node:path' import path from 'node:path'
import { findRegion } from '../markdown/plugins/snippet' import { findRegion } from '../markdown/plugins/snippet'
import { slash, type MarkdownEnv } from '../shared' import { slash, type MarkdownEnv } from '../shared'
@ -12,12 +12,12 @@ export function processIncludes(
file: string, file: string,
includes: string[], includes: string[],
cleanUrls: boolean cleanUrls: boolean
): string { ): Promise<string> {
const includesRE = /<!--\s*@include:\s*(.*?)\s*-->/g const includesRE = /<!--\s*@include:\s*(.*?)\s*-->/g
const regionRE = /(#[^\s\{]+)/ const regionRE = /(#[^\s\{]+)/
const rangeRE = /\{(\d*),(\d*)\}$/ const rangeRE = /\{(\d*),(\d*)\}$/
return src.replace(includesRE, (m: string, m1: string) => { return replaceAsync(src, includesRE, async (m: string, m1: string) => {
if (!m1.length) return m if (!m1.length) return m
const range = m1.match(rangeRE) const range = m1.match(rangeRE)
@ -36,7 +36,7 @@ export function processIncludes(
? path.join(srcDir, m1.slice(m1[1] === '/' ? 2 : 1)) ? path.join(srcDir, m1.slice(m1[1] === '/' ? 2 : 1))
: path.join(path.dirname(file), m1) : path.join(path.dirname(file), m1)
let content = fs.readFileSync(includePath, 'utf8') let content = await readFile(includePath, 'utf8')
if (region) { if (region) {
const [regionName] = region const [regionName] = region

Loading…
Cancel
Save