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