fix(hmr): allow disabling md cache during dev (#2581)

pull/2586/head
Divyansh Singh 1 year ago committed by GitHub
parent d5ccc52048
commit f60b32f02f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -36,4 +36,5 @@ export interface MarkdownEnv {
relativePath: string relativePath: string
cleanUrls: boolean cleanUrls: boolean
links?: string[] links?: string[]
includes?: string[]
} }

@ -50,6 +50,7 @@ export interface MarkdownOptions extends MarkdownIt.Options {
languages?: ILanguageRegistration[] languages?: ILanguageRegistration[]
toc?: TocPluginOptions toc?: TocPluginOptions
externalLinks?: Record<string, string> externalLinks?: Record<string, string>
cache?: boolean
} }
export type MarkdownRenderer = MarkdownIt export type MarkdownRenderer = MarkdownIt

@ -140,15 +140,15 @@ export const snippetPlugin = (md: MarkdownIt, srcDir: string) => {
const fence = md.renderer.rules.fence! const fence = md.renderer.rules.fence!
md.renderer.rules.fence = (...args) => { md.renderer.rules.fence = (...args) => {
const [tokens, idx, , { loader }] = args const [tokens, idx, , { includes }] = args
const token = tokens[idx] const token = tokens[idx]
// @ts-ignore // @ts-ignore
const [src, regionName] = token.src ?? [] const [src, regionName] = token.src ?? []
if (!src) return fence(...args) if (!src) return fence(...args)
if (loader) { if (includes) {
loader.addDependency(src) includes.push(src)
} }
const isAFile = fs.lstatSync(src).isFile() const isAFile = fs.lstatSync(src).isFile()

@ -67,11 +67,13 @@ export async function createMarkdownToVueRenderFn(
const relativePath = slash(path.relative(srcDir, file)) const relativePath = slash(path.relative(srcDir, file))
const cacheKey = JSON.stringify({ src, file }) const cacheKey = JSON.stringify({ src, file })
if (isBuild || options.cache !== false) {
const cached = cache.get(cacheKey) const cached = cache.get(cacheKey)
if (cached) { if (cached) {
debug(`[cache hit] ${relativePath}`) debug(`[cache hit] ${relativePath}`)
return cached return cached
} }
}
const start = Date.now() const start = Date.now()
@ -125,7 +127,8 @@ export async function createMarkdownToVueRenderFn(
const env: MarkdownEnv = { const env: MarkdownEnv = {
path: file, path: file,
relativePath, relativePath,
cleanUrls cleanUrls,
includes
} }
const html = md.render(src, env) const html = md.render(src, env)
const { const {
@ -243,7 +246,9 @@ export async function createMarkdownToVueRenderFn(
deadLinks, deadLinks,
includes includes
} }
if (isBuild || options.cache !== false) {
cache.set(cacheKey, result) cache.set(cacheKey, result)
}
return result return result
} }
} }

Loading…
Cancel
Save