@@ -18,6 +18,15 @@ import { computeHighlightRanges, refineCodeLanguage } from './utils'
18
18
* @see https://github.com/syntax-tree/hast-util-to-mdast/blob/main/lib/state.js#L258-L283
19
19
*/
20
20
const mdcRemarkElementType = 'mdc-element'
21
+ /**
22
+ * We use `textDirective` in the middle of the pipeline to allow `hast-util-to-mdast` to handle
23
+ * text components as pharsing nodes.
24
+ *
25
+ * `hast-util-to-mdast` does not handle `textComponent` as pharsing nodes, so we need to use `textDirective` instead.
26
+ * This is temporary mapping to avoid invalid output. The mapping will be reverted in `toMdast` function result.
27
+ */
28
+ const mdastTextComponentType = 'textDirective'
29
+ const mdcTextComponentType = 'textComponent'
21
30
const own = { } . hasOwnProperty
22
31
type Parents = HastParents & { properties : Record < string , unknown > , tagName : string }
23
32
@@ -48,6 +57,13 @@ export function mdcRemark(options?: Options | undefined | null) {
48
57
} as Options [ 'nodeHandlers' ]
49
58
} ) as MDastRoot
50
59
60
+ /**
61
+ * Revert textDirective to textComponent
62
+ */
63
+ visit ( mdast , node => node . type === mdastTextComponentType , ( node ) => {
64
+ node . type = mdcTextComponentType as typeof node . type
65
+ } )
66
+
51
67
return mdast
52
68
}
53
69
}
@@ -112,7 +128,7 @@ const mdcRemarkNodeHandlers = {
112
128
const isInlineElement = ( parent ?. children || [ ] ) . some ( child => child . type === 'text' ) || [ 'p' , 'li' ] . includes ( parent ?. tagName )
113
129
if ( isInlineElement ) {
114
130
return {
115
- type : 'textComponent' ,
131
+ type : mdastTextComponentType ,
116
132
name : node . tagName ,
117
133
attributes : node . properties ,
118
134
children : state . all ( node )
@@ -196,19 +212,7 @@ const mdcRemarkHandlers: Record<string, (state: State, node: Parents) => unknown
196
212
meta
197
213
}
198
214
} ,
199
-
200
- span : ( state : State , node : Parents ) => {
201
- const result = {
202
- type : 'textComponent' ,
203
- name : 'span' ,
204
- attributes : node . properties ,
205
- children : state . all ( node )
206
- }
207
-
208
- state . patch ( node , result as Nodes )
209
-
210
- return result
211
- } ,
215
+ span : createTextComponent ( 'span' ) ,
212
216
binding : createTextComponent ( 'binding' ) ,
213
217
iframe : createTextComponent ( 'iframe' ) ,
214
218
video : createTextComponent ( 'video' ) ,
@@ -283,11 +287,15 @@ const mdcRemarkHandlers: Record<string, (state: State, node: Parents) => unknown
283
287
284
288
function createTextComponent ( name : string ) {
285
289
return ( state : State , node : Parents ) => {
286
- return {
287
- type : 'textComponent' ,
290
+ const result = {
291
+ type : mdastTextComponentType ,
288
292
name,
289
293
attributes : node . properties ,
290
- children : node . children ? state . toFlow ( state . all ( node ) ) : undefined
294
+ children : state . all ( node )
291
295
}
296
+
297
+ state . patch ( node , result as Nodes )
298
+
299
+ return result
292
300
}
293
301
}
0 commit comments