Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add binding for external vars #9

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 20 additions & 11 deletions src/runtime/components/MDCRenderer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,14 @@ export default defineComponent({
components: {
type: Object as PropType<Record<string, string | DefineComponent<any, any, any>>>,
default: () => ({})
}
},
/**
* External vars binding to use for rendering.
*/
vars: {
type: Object,
default: () => ({})
},
},
async setup (props) {
const { mdc } = useRuntimeConfig().public
Expand All @@ -76,7 +83,7 @@ export default defineComponent({
return { tags }
},
render (ctx: any) {
const { tags, tag, body, data } = ctx
const { tags, tag, body, data, vars } = ctx

if (!body) {
return null
Expand All @@ -95,15 +102,15 @@ export default defineComponent({
...meta.component?.props,
...this.$attrs
},
renderSlots(body, h, meta, meta)
renderSlots(body, h, meta, meta, vars)
)
}
})

/**
* Render a markdown node
*/
function renderNode (node: MDCNode, h: CreateElement, documentMeta: MDCData, parentScope: any = {}): VNode {
function renderNode (node: MDCNode, h: CreateElement, documentMeta: MDCData, parentScope: any = {}, vars: any = {}): VNode {
/**
* Render Text node
*/
Expand All @@ -120,7 +127,7 @@ function renderNode (node: MDCNode, h: CreateElement, documentMeta: MDCData, par
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 @@ -133,7 +140,7 @@ function renderNode (node: MDCNode, h: CreateElement, documentMeta: MDCData, par
return h(
component as any,
props,
renderSlots(node, h, documentMeta, { ...parentScope, ...props })
renderSlots(node, h, documentMeta, { ...parentScope, ...props }, vars)
)
}

Expand All @@ -149,12 +156,13 @@ function renderToText (node: MDCNode): string {
return `<${node.tag}>${node.children?.map(renderToText).join('') || ''}</${node.tag}>`
}

function renderBinding (node: MDCElement, h: CreateElement, documentMeta: MDCData, parentScope: any = {}): VNode {
function renderBinding (node: MDCElement, h: CreateElement, documentMeta: MDCData, 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 @@ -168,14 +176,15 @@ function renderBinding (node: MDCElement, h: CreateElement, documentMeta: MDCDat
}
return {}
}, data)
const defaultValue = node.props?.defaultValue

return h(Text, value)
return h(Text, value ?? defaultValue ?? '')
}

/**
* Create slots from `node` template children.
*/
function renderSlots (node: MDCNode, h: CreateElement, documentMeta: MDCData, parentProps: any): Record<string, () => VNode[]> {
function renderSlots (node: MDCNode, h: CreateElement, documentMeta: MDCData, parentProps: any, vars: any): Record<string, () => VNode[]> {
const children: MDCNode[] = (node as MDCElement).children || []

const slotNodes: Record<string, MDCNode[]> = children.reduce((data, node) => {
Expand All @@ -200,7 +209,7 @@ function renderSlots (node: MDCNode, h: CreateElement, documentMeta: MDCData, pa
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