From 4cec660401d8d01830e5a11b9c66bc0ac5a935db Mon Sep 17 00:00:00 2001 From: Evan You Date: Thu, 6 Jan 2022 12:22:44 +0800 Subject: [PATCH] feat: expose __path and __relativePath on md instance for md plugins --- src/node/markdown/markdown.ts | 21 +++++++++++++++------ src/node/markdownToVue.ts | 2 +- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/node/markdown/markdown.ts b/src/node/markdown/markdown.ts index ff8a637fadc4..1b204b141956 100644 --- a/src/node/markdown/markdown.ts +++ b/src/node/markdown/markdown.ts @@ -40,8 +40,14 @@ export interface MarkdownParsedData { } export interface MarkdownRenderer { + __path: string + __relativePath: string __data: MarkdownParsedData - render: (src: string, env?: any) => { html: string; data: any } + render: ( + src: string, + path: string, + relatiovePath: string + ) => { html: string; data: any } } export type { Header } @@ -94,17 +100,20 @@ export const createMarkdownRenderer = ( md.use(lineNumberPlugin) } + const wrappedMd = md as any as MarkdownRenderer + // wrap render so that we can return both the html and extracted data. const render = md.render - const wrappedRender: MarkdownRenderer['render'] = (src) => { - ;(md as any).__data = {} + wrappedMd.render = (src, path, relativePath) => { + wrappedMd.__data = {} + wrappedMd.__path = path + wrappedMd.__relativePath = relativePath const html = render.call(md, src) return { html, - data: (md as any).__data + data: wrappedMd.__data } } - ;(md as any).render = wrappedRender - return md as any + return wrappedMd } diff --git a/src/node/markdownToVue.ts b/src/node/markdownToVue.ts index fc3c755d4825..d0d50ef3ab1f 100644 --- a/src/node/markdownToVue.ts +++ b/src/node/markdownToVue.ts @@ -65,7 +65,7 @@ export function createMarkdownToVueRenderFn( }) const { content, data: frontmatter } = matter(src) - let { html, data } = md.render(content) + let { html, data } = md.render(content, file, relativePath) if (isBuild) { // avoid env variables being replaced by vite