feat: process md includes before building local search index (#2906)

pull/2937/head
g-dx 1 year ago committed by GitHub
parent 070fc0a56d
commit c6ff5c7686
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -17,11 +17,10 @@ import {
type PageData
} from './shared'
import { getGitTimestamp } from './utils/getGitTimestamp'
import { processIncludes } from './utils/processIncludes'
const debug = _debug('vitepress:md')
const cache = new LRUCache<string, MarkdownCompileResult>({ max: 1024 })
const includesRE = /<!--\s*@include:\s*(.*?)\s*-->/g
const rangeRE = /\{(\d*),(\d*)\}$/
export interface MarkdownCompileResult {
vueSrc: string
@ -89,39 +88,7 @@ export async function createMarkdownToVueRenderFn(
// resolve includes
let includes: string[] = []
function processIncludes(src: string, file: string): string {
return src.replace(includesRE, (m: string, m1: string) => {
if (!m1.length) return m
const range = m1.match(rangeRE)
range && (m1 = m1.slice(0, -range[0].length))
const atPresent = m1[0] === '@'
try {
const includePath = atPresent
? path.join(srcDir, m1.slice(m1[1] === '/' ? 2 : 1))
: path.join(path.dirname(file), m1)
let content = fs.readFileSync(includePath, 'utf-8')
if (range) {
const [, startLine, endLine] = range
const lines = content.split(/\r?\n/)
content = lines
.slice(
startLine ? parseInt(startLine, 10) - 1 : undefined,
endLine ? parseInt(endLine, 10) : undefined
)
.join('\n')
}
includes.push(slash(includePath))
// recursively process includes in the content
return processIncludes(content, includePath)
} catch (error) {
return m // silently ignore error if file is not present
}
})
}
src = processIncludes(src, fileOrig)
src = processIncludes(srcDir, src, fileOrig, includes)
// reset env before render
const env: MarkdownEnv = {

@ -11,6 +11,7 @@ import {
type DefaultTheme,
type MarkdownEnv
} from '../shared'
import { processIncludes } from '../utils/processIncludes'
const debug = _debug('vitepress:local-search')
@ -56,7 +57,8 @@ export async function localSearchPlugin(
const { srcDir, cleanUrls = false } = siteConfig
const relativePath = slash(path.relative(srcDir, file))
const env: MarkdownEnv = { path: file, relativePath, cleanUrls }
const src = fs.readFileSync(file, 'utf-8')
let src = fs.readFileSync(file, 'utf-8')
src = processIncludes(srcDir, src, file, [])
if (options._render) return options._render(src, env, md)
const html = md.render(src, env)
return env.frontmatter?.search === false ? '' : html

@ -0,0 +1,41 @@
import path from 'path'
import fs from 'fs-extra'
import { slash } from '../shared'
export function processIncludes(
srcDir: string,
src: string,
file: string,
includes: string[]
): string {
const includesRE = /<!--\s*@include:\s*(.*?)\s*-->/g
const rangeRE = /\{(\d*),(\d*)\}$/
return src.replace(includesRE, (m: string, m1: string) => {
if (!m1.length) return m
const range = m1.match(rangeRE)
range && (m1 = m1.slice(0, -range[0].length))
const atPresent = m1[0] === '@'
try {
const includePath = atPresent
? path.join(srcDir, m1.slice(m1[1] === '/' ? 2 : 1))
: path.join(path.dirname(file), m1)
let content = fs.readFileSync(includePath, 'utf-8')
if (range) {
const [, startLine, endLine] = range
const lines = content.split(/\r?\n/)
content = lines
.slice(
startLine ? parseInt(startLine, 10) - 1 : undefined,
endLine ? parseInt(endLine, 10) : undefined
)
.join('\n')
}
includes.push(slash(includePath))
// recursively process includes in the content
return processIncludes(srcDir, content, includePath, includes)
} catch (error) {
return m // silently ignore error if file is not present
}
})
}
Loading…
Cancel
Save