Skip to content

Commit b0e2daa

Browse files
committed
fix: resolve experimental features conflict
1 parent d686ce3 commit b0e2daa

File tree

4 files changed

+28
-18
lines changed

4 files changed

+28
-18
lines changed

src/from-markdown.ts

+22-3
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,37 @@ import { NON_UNWRAPPABLE_TYPES } from './utils'
1212
export default (opts: RemarkMDCOptions = {}) => {
1313
const canContainEols = ['textComponent']
1414

15+
const experimentalCodeBlockYamlProps = (node: Container) => {
16+
if (
17+
node.children?.length &&
18+
node.children[0].type === 'code' &&
19+
node.children[0].lang === 'yaml' &&
20+
node.children[0].meta === '[props]'
21+
) {
22+
node.rawData = node.children[0].value as string
23+
node.mdc = node.mdc || {}
24+
node.mdc.codeBlockProps = true
25+
node.children!.splice(0, 1)
26+
}
27+
}
1528
const experimentalAutoUnwrap = (node: Container) => {
1629
if (opts.experimental?.autoUnwrap && NON_UNWRAPPABLE_TYPES.includes(node.type)) {
1730
const nonSlotChildren = (node.children).filter((child: any) => child.type !== 'componentContainerSection')
1831
if (nonSlotChildren.length === 1 && !NON_UNWRAPPABLE_TYPES.includes(nonSlotChildren[0].type)) {
1932
const nonSlotChildIndex = node.children.indexOf(nonSlotChildren[0])
2033

21-
node.children.splice(nonSlotChildIndex, 1, ...(nonSlotChildren[0] as Container).children)
34+
node.children.splice(nonSlotChildIndex, 1, ...((nonSlotChildren[0] as Container)?.children || []))
2235
node.mdc = node.mdc || {}
2336
node.mdc.unwrapped = nonSlotChildren[0].type
2437
}
2538
}
2639
}
40+
const processNode = (node: Container) => {
41+
if (opts.experimental?.componentCodeBlockYamlProps) {
42+
experimentalCodeBlockYamlProps(node)
43+
}
44+
experimentalAutoUnwrap(node)
45+
}
2746
const enter = {
2847
componentContainer: enterContainer,
2948
componentContainerSection: enterContainerSection,
@@ -109,7 +128,7 @@ export default (opts: RemarkMDCOptions = {}) => {
109128
container.rawData = dataSection?.rawData
110129
}
111130

112-
experimentalAutoUnwrap(container)
131+
processNode(container)
113132

114133
container.children = container.children.flatMap((child: any) => {
115134
if (child.rawData) {
@@ -153,7 +172,7 @@ export default (opts: RemarkMDCOptions = {}) => {
153172
*/
154173
attemptClosingOpenListSection.call(this, section)
155174

156-
experimentalAutoUnwrap(section)
175+
processNode(section)
157176

158177
this.exit(token)
159178
}

src/index.ts

-13
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,6 @@ function getNodeData (node: ComponentNode) {
9292
return data
9393
}
9494

95-
if (
96-
node.children?.length &&
97-
node.children[0].type === 'code' &&
98-
node.children[0].lang === 'yaml' &&
99-
node.children[0].meta === '[props]'
100-
) {
101-
const yaml = node.children[0].value as string
102-
const { data } = parseFrontMatter(toFrontMatter(yaml))
103-
node.rawData = yaml + '\n---'
104-
node.children!.splice(0, 1)
105-
return data
106-
}
107-
10895
return {}
10996
}
11097

src/micromark-extension/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export type Container = Parent & {
1111
rawData?: string
1212
mdc?: {
1313
unwrapped?: string
14+
codeBlockProps?: boolean
1415
}
1516
}
1617

src/to-markdown.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,14 @@ export default (opts: RemarkMDCOptions = {}) => {
5252
}
5353
}
5454
}
55+
const processNode = (node: Container) => {
56+
experimentalAutoUnwrap(node)
57+
}
5558

5659
function componentContainerSection (node: NodeComponentContainerSection, _: any, context: any) {
5760
context.indexStack = context.stack
5861

59-
experimentalAutoUnwrap(node as any)
62+
processNode(node as any)
6063

6164
return `#${(node as any).name}\n${content(node, context)}`.trim()
6265
}
@@ -134,7 +137,7 @@ export default (opts: RemarkMDCOptions = {}) => {
134137
...slots
135138
]
136139

137-
experimentalAutoUnwrap(node as any)
140+
processNode(node as any)
138141

139142
if ((node.type as string) === 'containerComponent') {
140143
subvalue = content(node, context)

0 commit comments

Comments
 (0)