Skip to content

Commit

Permalink
feat: support partial include directive
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Aug 19, 2021
1 parent 8af8b40 commit 7b3a9e5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/node/markdownToVue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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) {
Expand All @@ -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)

Expand Down Expand Up @@ -110,7 +123,8 @@ export function createMarkdownToVueRenderFn(
const result = {
vueSrc,
pageData,
deadLinks
deadLinks,
includes
}
cache.set(src, result)
return result
Expand Down
11 changes: 10 additions & 1 deletion src/node/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
},
Expand Down

0 comments on commit 7b3a9e5

Please sign in to comment.