Skip to content

Commit

Permalink
fix: resolve experimental features conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed Nov 15, 2024
1 parent d686ce3 commit b0e2daa
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
25 changes: 22 additions & 3 deletions src/from-markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,37 @@ import { NON_UNWRAPPABLE_TYPES } from './utils'
export default (opts: RemarkMDCOptions = {}) => {
const canContainEols = ['textComponent']

const experimentalCodeBlockYamlProps = (node: Container) => {
if (
node.children?.length &&
node.children[0].type === 'code' &&
node.children[0].lang === 'yaml' &&
node.children[0].meta === '[props]'
) {
node.rawData = node.children[0].value as string
node.mdc = node.mdc || {}
node.mdc.codeBlockProps = true
node.children!.splice(0, 1)
}
}
const experimentalAutoUnwrap = (node: Container) => {
if (opts.experimental?.autoUnwrap && NON_UNWRAPPABLE_TYPES.includes(node.type)) {
const nonSlotChildren = (node.children).filter((child: any) => child.type !== 'componentContainerSection')
if (nonSlotChildren.length === 1 && !NON_UNWRAPPABLE_TYPES.includes(nonSlotChildren[0].type)) {
const nonSlotChildIndex = node.children.indexOf(nonSlotChildren[0])

node.children.splice(nonSlotChildIndex, 1, ...(nonSlotChildren[0] as Container).children)
node.children.splice(nonSlotChildIndex, 1, ...((nonSlotChildren[0] as Container)?.children || []))
node.mdc = node.mdc || {}
node.mdc.unwrapped = nonSlotChildren[0].type
}
}
}
const processNode = (node: Container) => {
if (opts.experimental?.componentCodeBlockYamlProps) {
experimentalCodeBlockYamlProps(node)
}
experimentalAutoUnwrap(node)
}
const enter = {
componentContainer: enterContainer,
componentContainerSection: enterContainerSection,
Expand Down Expand Up @@ -109,7 +128,7 @@ export default (opts: RemarkMDCOptions = {}) => {
container.rawData = dataSection?.rawData
}

experimentalAutoUnwrap(container)
processNode(container)

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

experimentalAutoUnwrap(section)
processNode(section)

this.exit(token)
}
Expand Down
13 changes: 0 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,6 @@ function getNodeData (node: ComponentNode) {
return data
}

if (
node.children?.length &&
node.children[0].type === 'code' &&
node.children[0].lang === 'yaml' &&
node.children[0].meta === '[props]'
) {
const yaml = node.children[0].value as string
const { data } = parseFrontMatter(toFrontMatter(yaml))
node.rawData = yaml + '\n---'
node.children!.splice(0, 1)
return data
}

return {}
}

Expand Down
1 change: 1 addition & 0 deletions src/micromark-extension/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type Container = Parent & {
rawData?: string
mdc?: {
unwrapped?: string
codeBlockProps?: boolean
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/to-markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,14 @@ export default (opts: RemarkMDCOptions = {}) => {
}
}
}
const processNode = (node: Container) => {
experimentalAutoUnwrap(node)
}

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

experimentalAutoUnwrap(node as any)
processNode(node as any)

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

experimentalAutoUnwrap(node as any)
processNode(node as any)

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

0 comments on commit b0e2daa

Please sign in to comment.