@@ -10,7 +10,6 @@ import { useRuntimeConfig, useRoute } from '#app'
10
10
import type { MarkdownNode , ParsedContentMeta } from ' ../types'
11
11
12
12
type CreateElement = typeof h
13
- type ContentVNode = VNode | string
14
13
15
14
/**
16
15
* Default slot name
@@ -96,27 +95,19 @@ export default defineComponent({
96
95
// Resolve component if it's a Vue component
97
96
component = resolveVueComponent (component as string )
98
97
99
- // Process children
100
- const children = (body .children || []).map (child => renderNode (child , h , meta ))
101
-
102
98
// Return Vue component
103
99
return h (
104
- component as any ,
105
- {
106
- ... meta .component ?.props ,
107
- ... this .$attrs
108
- },
109
- {
110
- default: createSlotFunction (children )
111
- }
100
+ component as any ,
101
+ { ... meta .component ?.props , ... this .$attrs },
102
+ renderSlots (body , h , meta , meta )
112
103
)
113
104
}
114
105
})
115
106
116
107
/**
117
108
* Render a markdown node
118
109
*/
119
- function renderNode (node : MarkdownNode , h : CreateElement , documentMeta : ParsedContentMeta , parentScope : any = {}): ContentVNode {
110
+ function renderNode (node : MarkdownNode , h : CreateElement , documentMeta : ParsedContentMeta , parentScope : any = {}): VNode {
120
111
/**
121
112
* Render Text node
122
113
*/
@@ -138,14 +129,15 @@ function renderNode (node: MarkdownNode, h: CreateElement, documentMeta: ParsedC
138
129
}
139
130
140
131
const props = propsToData (node , documentMeta )
132
+
141
133
return h (
142
134
component as any ,
143
135
props ,
144
136
renderSlots (node , h , documentMeta , { ... parentScope , ... props })
145
137
)
146
138
}
147
139
148
- function renderBinding (node : MarkdownNode , h : CreateElement , documentMeta : ParsedContentMeta , parentScope : any = {}): ContentVNode {
140
+ function renderBinding (node : MarkdownNode , h : CreateElement , documentMeta : ParsedContentMeta , parentScope : any = {}): VNode {
149
141
const data = {
150
142
... parentScope ,
151
143
$route : () => useRoute (),
@@ -171,31 +163,37 @@ function renderBinding (node: MarkdownNode, h: CreateElement, documentMeta: Pars
171
163
/**
172
164
* Create slots from `node` template children.
173
165
*/
174
- function renderSlots (node : MarkdownNode , h : CreateElement , documentMeta : ParsedContentMeta , parentProps : any ): ContentVNode [] {
166
+ function renderSlots (node : MarkdownNode , h : CreateElement , documentMeta : ParsedContentMeta , parentProps : any ): Record < string , () => VNode []> {
175
167
const children: MarkdownNode [] = node .children || []
176
168
177
- const slots : Record <string , Array < VNode | string > > = children .reduce ((data , node ) => {
169
+ const slotNodes : Record <string , MarkdownNode [] > = children .reduce ((data , node ) => {
178
170
if (! isTemplate (node )) {
179
- data [DEFAULT_SLOT ].push (renderNode (node , h , documentMeta , parentProps ))
180
- return data
181
- }
182
-
183
- if (isDefaultTemplate (node )) {
184
- data [DEFAULT_SLOT ].push (... (node .children || []).map (child => renderNode (child , h , documentMeta , parentProps )))
171
+ data [DEFAULT_SLOT ].push (node )
185
172
return data
186
173
}
187
174
188
175
const slotName = getSlotName (node )
189
- data [slotName ] = (node .children || []).map (child => renderNode (child , h , documentMeta , parentProps ))
176
+ data [slotName ] = data [slotName ] || []
177
+ // Append children to slot
178
+ data [slotName ].push (... (node .children || []))
190
179
191
180
return data
192
181
}, {
193
182
[DEFAULT_SLOT ]: [] as any []
194
183
})
195
184
196
- const slotEntries = Object .entries (slots ).map (([name , vDom ]) => ([name , createSlotFunction (vDom )]))
185
+ const slots = Object .entries (slotNodes ).reduce ((slots , [name , children ]) => {
186
+ if (! children .length ) { return slots }
187
+
188
+ slots [name ] = () => {
189
+ const vNodes = children .map (child => renderNode (child , h , documentMeta , parentProps ))
190
+ return mergeTextNodes (vNodes )
191
+ }
192
+
193
+ return slots
194
+ }, {} as Record <string , () => VNode []>)
197
195
198
- return Object . fromEntries ( slotEntries )
196
+ return slots
199
197
}
200
198
201
199
/**
@@ -344,20 +342,6 @@ function getSlotName (node: MarkdownNode) {
344
342
return name || DEFAULT_SLOT
345
343
}
346
344
347
- /**
348
- * Create a factory function if there is a node in the list
349
- */
350
- function createSlotFunction (nodes : Array <VNode | string >) {
351
- return (nodes .length ? () => mergeTextNodes (nodes as VNode []) : undefined )
352
- }
353
-
354
- /**
355
- * Check if node is root
356
- */
357
- function isDefaultTemplate (node : MarkdownNode ) {
358
- return isTemplate (node ) && getSlotName (node ) === DEFAULT_SLOT
359
- }
360
-
361
345
/**
362
346
* Check if node is Vue template tag
363
347
*/
0 commit comments