Skip to content

Commit

Permalink
feat: process md includes before building local search index (#2906)
Browse files Browse the repository at this point in the history
  • Loading branch information
g-dx authored Sep 10, 2023
1 parent 070fc0a commit c6ff5c7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 36 deletions.
37 changes: 2 additions & 35 deletions src/node/markdownToVue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = {
Expand Down
4 changes: 3 additions & 1 deletion src/node/plugins/localSearchPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
type DefaultTheme,
type MarkdownEnv
} from '../shared'
import { processIncludes } from '../utils/processIncludes'

const debug = _debug('vitepress:local-search')

Expand Down Expand Up @@ -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
Expand Down
41 changes: 41 additions & 0 deletions src/node/utils/processIncludes.ts
Original file line number Diff line number Diff line change
@@ -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
}
})
}

0 comments on commit c6ff5c7

Please sign in to comment.