diff --git a/src/node/markdownToVue.ts b/src/node/markdownToVue.ts index 36ca9ff8c154..fc4b36c58dd5 100644 --- a/src/node/markdownToVue.ts +++ b/src/node/markdownToVue.ts @@ -10,11 +10,13 @@ import chalk from 'chalk' const debug = require('debug')('vitepress:md') const cache = new LRUCache({ max: 1024 }) +const includesRE = //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 diff --git a/src/node/plugin.ts b/src/node/plugin.ts index 93b5af6abff3..6bfdcbb9cc3c 100644 --- a/src/node/plugin.ts +++ b/src/node/plugin.ts @@ -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 } },