Skip to content

Commit

Permalink
feat: add binding for external vars
Browse files Browse the repository at this point in the history
  • Loading branch information
maximepvrt committed Aug 10, 2023
1 parent a313f48 commit 57d5845
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/runtime/components/ContentRendererMarkdown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ export default defineComponent({
components: {
type: Object,
default: () => ({})
},
/**
* External vars binding to use for rendering.
*/
vars: {
type: Object,
default: () => ({})
}
},
async setup (props) {
Expand All @@ -74,7 +81,7 @@ export default defineComponent({
return { debug, tags }
},
render (ctx: any) {
const { tags, tag, value, excerpt, components, debug } = ctx
const { tags, tag, value, excerpt, components, debug, vars } = ctx
if (!value) {
return null
Expand Down Expand Up @@ -110,15 +117,15 @@ export default defineComponent({
...this.$attrs,
'data-content-id': debug ? value._id : undefined
},
renderSlots(body, h, meta, meta)
renderSlots(body, h, meta, meta, vars)
)
}
})
/**
* Render a markdown node
*/
function renderNode (node: MarkdownNode, h: CreateElement, documentMeta: ParsedContentMeta, parentScope: any = {}): VNode {
function renderNode (node: MarkdownNode, h: CreateElement, documentMeta: ParsedContentMeta, parentScope: any = {}, vars: any = {}): VNode {
/**
* Render Text node
*/
Expand All @@ -135,7 +142,7 @@ function renderNode (node: MarkdownNode, h: CreateElement, documentMeta: ParsedC
const renderTag: string = (typeof node.props?.__ignoreMap === 'undefined' && documentMeta.tags[originalTag]) || originalTag
if (node.tag === 'binding') {
return renderBinding(node, h, documentMeta, parentScope)
return renderBinding(node, h, documentMeta, parentScope, vars)
}
const component = resolveVueComponent(renderTag)
Expand All @@ -148,7 +155,7 @@ function renderNode (node: MarkdownNode, h: CreateElement, documentMeta: ParsedC
return h(
component as any,
props,
renderSlots(node, h, documentMeta, { ...parentScope, ...props })
renderSlots(node, h, documentMeta, { ...parentScope, ...props }, vars)
)
}
Expand All @@ -164,12 +171,13 @@ function renderToText (node: MarkdownNode): string {
return `<${node.tag}>${node.children?.map(renderToText).join('') || ''}</${node.tag}>`
}
function renderBinding (node: MarkdownNode, h: CreateElement, documentMeta: ParsedContentMeta, parentScope: any = {}): VNode {
function renderBinding (node: MarkdownNode, h: CreateElement, documentMeta: ParsedContentMeta, parentScope: any = {}, vars: any = {}): VNode {
const data = {
...parentScope,
$route: () => useRoute(),
$document: documentMeta,
$doc: documentMeta
$doc: documentMeta,
$vars: vars
}
const splitter = /\.|\[(\d+)\]/
const keys: string[] = node.props?.value.trim().split(splitter).filter(Boolean)
Expand All @@ -190,7 +198,7 @@ function renderBinding (node: MarkdownNode, h: CreateElement, documentMeta: Pars
/**
* Create slots from `node` template children.
*/
function renderSlots (node: MarkdownNode, h: CreateElement, documentMeta: ParsedContentMeta, parentProps: any): Record<string, () => VNode[]> {
function renderSlots (node: MarkdownNode, h: CreateElement, documentMeta: ParsedContentMeta, parentProps: any, vars: any): Record<string, () => VNode[]> {
const children: MarkdownNode[] = node.children || []
const slotNodes: Record<string, MarkdownNode[]> = children.reduce((data, node) => {
Expand All @@ -213,7 +221,7 @@ function renderSlots (node: MarkdownNode, h: CreateElement, documentMeta: Parsed
if (!children.length) { return slots }
slots[name] = () => {
const vNodes = children.map(child => renderNode(child, h, documentMeta, parentProps))
const vNodes = children.map(child => renderNode(child, h, documentMeta, parentProps, vars))
return mergeTextNodes(vNodes)
}
Expand Down

0 comments on commit 57d5845

Please sign in to comment.