feat: support partial include directive

pull/370/head
Evan You 3 years ago
parent 8af8b40a80
commit 7b3a9e59b4

@ -10,11 +10,13 @@ import chalk from 'chalk'
const debug = require('debug')('vitepress:md')
const cache = new LRUCache<string, MarkdownCompileResult>({ max: 1024 })
const includesRE = /<!--\s*@include:\s*(.*?)\s*-->/g
export interface MarkdownCompileResult {
vueSrc: string
pageData: PageData
deadLinks: string[]
includes: string[]
}
export function createMarkdownToVueRenderFn(
@ -33,6 +35,7 @@ export function createMarkdownToVueRenderFn(
publicDir: string
): MarkdownCompileResult => {
const relativePath = slash(path.relative(srcDir, file))
const dir = path.dirname(file)
const cached = cache.get(src)
if (cached) {
@ -42,6 +45,16 @@ export function createMarkdownToVueRenderFn(
const start = Date.now()
// resolve includes
let includes: string[] = []
src = src.replace(includesRE, (_, m1) => {
const includePath = path.join(dir, m1)
console.log(includePath)
const content = fs.readFileSync(includePath, 'utf-8')
includes.push(slash(includePath))
return content
})
const { content, data: frontmatter } = matter(src)
let { html, data } = md.render(content)
@ -110,7 +123,8 @@ export function createMarkdownToVueRenderFn(
const result = {
vueSrc,
pageData,
deadLinks
deadLinks,
includes
}
cache.set(src, result)
return result

@ -106,10 +106,19 @@ export function createVitePressPlugin(
transform(code, id) {
if (id.endsWith('.md')) {
// transform .md files into vueSrc so plugin-vue can handle it
const { vueSrc, deadLinks } = markdownToVue(code, id, config.publicDir)
const { vueSrc, deadLinks, includes } = markdownToVue(
code,
id,
config.publicDir
)
if (deadLinks.length) {
hasDeadLinks = true
}
if (includes.length) {
includes.forEach((i) => {
this.addWatchFile(i)
})
}
return vueSrc
}
},

Loading…
Cancel
Save