Skip to content

Commit

Permalink
feat: expose __path and __relativePath on md instance for md plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 6, 2022
1 parent 7387649 commit 4cec660
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
21 changes: 15 additions & 6 deletions src/node/markdown/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion src/node/markdownToVue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4cec660

Please sign in to comment.