feat: expose __path and __relativePath on md instance for md plugins

pull/486/head
Evan You 3 years ago
parent 7387649ff7
commit 4cec660401

@ -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
}

@ -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

Loading…
Cancel
Save