Skip to content

Commit cd1c4fd

Browse files
committed
fix(stringify): force hast-util-to-mdast to treat textComponent nodes as text
1 parent bcb41b1 commit cd1c4fd

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

src/runtime/stringify/mdc-remark.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ import { computeHighlightRanges, refineCodeLanguage } from './utils'
1818
* @see https://github.com/syntax-tree/hast-util-to-mdast/blob/main/lib/state.js#L258-L283
1919
*/
2020
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'
2130
const own = {}.hasOwnProperty
2231
type Parents = HastParents & { properties: Record<string, unknown>, tagName: string }
2332

@@ -48,6 +57,13 @@ export function mdcRemark(options?: Options | undefined | null) {
4857
} as Options['nodeHandlers']
4958
}) as MDastRoot
5059

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+
5167
return mdast
5268
}
5369
}
@@ -112,7 +128,7 @@ const mdcRemarkNodeHandlers = {
112128
const isInlineElement = (parent?.children || []).some(child => child.type === 'text') || ['p', 'li'].includes(parent?.tagName)
113129
if (isInlineElement) {
114130
return {
115-
type: 'textComponent',
131+
type: mdastTextComponentType,
116132
name: node.tagName,
117133
attributes: node.properties,
118134
children: state.all(node)
@@ -196,19 +212,7 @@ const mdcRemarkHandlers: Record<string, (state: State, node: Parents) => unknown
196212
meta
197213
}
198214
},
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'),
212216
binding: createTextComponent('binding'),
213217
iframe: createTextComponent('iframe'),
214218
video: createTextComponent('video'),
@@ -283,11 +287,15 @@ const mdcRemarkHandlers: Record<string, (state: State, node: Parents) => unknown
283287

284288
function createTextComponent(name: string) {
285289
return (state: State, node: Parents) => {
286-
return {
287-
type: 'textComponent',
290+
const result = {
291+
type: mdastTextComponentType,
288292
name,
289293
attributes: node.properties,
290-
children: node.children ? state.toFlow(state.all(node)) : undefined
294+
children: state.all(node)
291295
}
296+
297+
state.patch(node, result as Nodes)
298+
299+
return result
292300
}
293301
}

0 commit comments

Comments
 (0)