|
|
@ -8,12 +8,7 @@ import { PageData, HeadConfig } from '../../types/shared'
|
|
|
|
import slash from 'slash'
|
|
|
|
import slash from 'slash'
|
|
|
|
|
|
|
|
|
|
|
|
const debug = require('debug')('vitepress:md')
|
|
|
|
const debug = require('debug')('vitepress:md')
|
|
|
|
const cache = new LRUCache<string, MarkdownCompileCachedResult>({ max: 1024 })
|
|
|
|
const cache = new LRUCache<string, MarkdownCompileResult>({ max: 1024 })
|
|
|
|
|
|
|
|
|
|
|
|
interface MarkdownCompileCachedResult extends MarkdownCompileResult {
|
|
|
|
|
|
|
|
tagsWithPageData: string
|
|
|
|
|
|
|
|
tagsWithoutPageData: string
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
interface MarkdownCompileResult {
|
|
|
|
interface MarkdownCompileResult {
|
|
|
|
vueSrc: string
|
|
|
|
vueSrc: string
|
|
|
@ -26,17 +21,13 @@ export function createMarkdownToVueRenderFn(
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
const md = createMarkdownRenderer(options)
|
|
|
|
const md = createMarkdownRenderer(options)
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
return (src: string, file: string): MarkdownCompileResult => {
|
|
|
|
src: string,
|
|
|
|
|
|
|
|
file: string,
|
|
|
|
|
|
|
|
injectData = true
|
|
|
|
|
|
|
|
): MarkdownCompileResult => {
|
|
|
|
|
|
|
|
const relativePath = slash(path.relative(root, file))
|
|
|
|
const relativePath = slash(path.relative(root, file))
|
|
|
|
|
|
|
|
|
|
|
|
const cached = cache.get(src)
|
|
|
|
const cached = cache.get(src)
|
|
|
|
if (cached) {
|
|
|
|
if (cached) {
|
|
|
|
debug(`[cache hit] ${relativePath}`)
|
|
|
|
debug(`[cache hit] ${relativePath}`)
|
|
|
|
return pickResult(cached, injectData)
|
|
|
|
return cached
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const start = Date.now()
|
|
|
|
const start = Date.now()
|
|
|
@ -44,8 +35,6 @@ export function createMarkdownToVueRenderFn(
|
|
|
|
const { content, data: frontmatter } = matter(src)
|
|
|
|
const { content, data: frontmatter } = matter(src)
|
|
|
|
const { html, data } = md.render(content)
|
|
|
|
const { html, data } = md.render(content)
|
|
|
|
|
|
|
|
|
|
|
|
const vueSrc = `\n<template><div>${html}</div></template>`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO validate data.links?
|
|
|
|
// TODO validate data.links?
|
|
|
|
const pageData: PageData = {
|
|
|
|
const pageData: PageData = {
|
|
|
|
title: inferTitle(frontmatter, content),
|
|
|
|
title: inferTitle(frontmatter, content),
|
|
|
@ -54,38 +43,21 @@ export function createMarkdownToVueRenderFn(
|
|
|
|
headers: data.headers,
|
|
|
|
headers: data.headers,
|
|
|
|
relativePath,
|
|
|
|
relativePath,
|
|
|
|
// TODO use git timestamp?
|
|
|
|
// TODO use git timestamp?
|
|
|
|
lastUpdated: fs.statSync(file).mtimeMs
|
|
|
|
lastUpdated: Math.round(fs.statSync(file).mtimeMs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const tagsWithPageData = genPageDataCode(
|
|
|
|
const vueSrc =
|
|
|
|
data.hoistedTags || [],
|
|
|
|
genPageDataCode(data.hoistedTags || [], pageData).join('\n') +
|
|
|
|
pageData
|
|
|
|
`\n<template><div>${html}</div></template>`
|
|
|
|
).join('\n')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const tagsWithoutPageData = (data.hoistedTags || []).join('\n')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
debug(`[render] ${file} in ${Date.now() - start}ms.`)
|
|
|
|
debug(`[render] ${file} in ${Date.now() - start}ms.`)
|
|
|
|
|
|
|
|
|
|
|
|
const result = {
|
|
|
|
const result = {
|
|
|
|
vueSrc,
|
|
|
|
vueSrc,
|
|
|
|
pageData,
|
|
|
|
pageData
|
|
|
|
tagsWithPageData,
|
|
|
|
|
|
|
|
tagsWithoutPageData
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cache.set(src, result)
|
|
|
|
cache.set(src, result)
|
|
|
|
return pickResult(result, injectData)
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function pickResult(
|
|
|
|
|
|
|
|
res: MarkdownCompileCachedResult,
|
|
|
|
|
|
|
|
injectData: boolean
|
|
|
|
|
|
|
|
): MarkdownCompileResult {
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
vueSrc:
|
|
|
|
|
|
|
|
res.vueSrc +
|
|
|
|
|
|
|
|
(injectData ? res.tagsWithPageData : res.tagsWithoutPageData),
|
|
|
|
|
|
|
|
pageData: res.pageData
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|