Skip to content

Commit

Permalink
Merge branch 'main' into fix/parseComponentPropsObject
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed Nov 18, 2024
2 parents 6219f5a + 58ca74a commit a7a29da
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 136 deletions.
12 changes: 4 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,11 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '18'
- uses: pnpm/[email protected]
name: Install pnpm
id: pnpm-install
with:
version: 8
node-version: '20'
- run: corepack enable
- run: pnpm install
- run: pnpm dev:prepare
- run: pnpm lint
Expand Down
82 changes: 0 additions & 82 deletions .github/workflows/studio.yml

This file was deleted.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"dev:prepare": "nuxi prepare playground",
"generate": "nuxi generate playground",
"lint": "eslint --ext .js,.ts,.vue .",
"test": "pnpm lint && vitest run",
"test": "vitest run",
"prepack": "pnpm build",
"release": "release-it"
},
Expand Down Expand Up @@ -74,5 +74,6 @@
"hooks": {
"after:bump": "npx changelogen@latest --no-commit --no-tag --output --r $(node -p \"require('./package.json').version\")"
}
}
},
"packageManager": "[email protected]"
}
30 changes: 15 additions & 15 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 24 additions & 3 deletions src/from-markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,39 @@ import { NON_UNWRAPPABLE_TYPES } from './utils'
export default (opts: RemarkMDCOptions = {}) => {
const canContainEols = ['textComponent']

const experimentalCodeBlockYamlProps = (node: Container) => {
const firstSection = node.children[0] as Container
if (
firstSection &&
firstSection.children?.length &&
firstSection.children[0].type === 'code' &&
firstSection.children[0].lang === 'yaml' &&
firstSection.children[0].meta === '[props]'
) {
node.rawData = firstSection.children[0].value as string
node.mdc = node.mdc || {}
node.mdc.codeBlockProps = true
firstSection.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 +130,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 +174,7 @@ export default (opts: RemarkMDCOptions = {}) => {
*/
attemptClosingOpenListSection.call(this, section)

experimentalAutoUnwrap(section)
processNode(section)

this.exit(token)
}
Expand Down
15 changes: 1 addition & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ interface ComponentNode extends Node {
children?: ChildrenNode[]
}

export default <Plugin<Array<RemarkMDCOptions>>> function (opts: RemarkMDCOptions = {}) {
export default <Plugin<Array<RemarkMDCOptions>>> function remarkMDC (opts: RemarkMDCOptions = {}) {
const data: Record<string, any> = this.data()

add('micromarkExtensions', syntax())
Expand Down 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
3 changes: 2 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ export const NON_UNWRAPPABLE_TYPES = [
'componentContainerSection',
'componentContainerDataSection',
'containerComponent',
'leafComponent'
'leafComponent',
'table'
]
19 changes: 19 additions & 0 deletions test/__snapshots__/basic.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,25 @@ exports[`basic > link 1`] = `
}
`;

exports[`basic > remarkPluginName 1`] = `
{
"children": [],
"position": {
"end": {
"column": 1,
"line": 1,
"offset": 0,
},
"start": {
"column": 1,
"line": 1,
"offset": 0,
},
},
"type": "root",
}
`;

exports[`basic > simple 1`] = `
{
"children": [
Expand Down
Loading

0 comments on commit a7a29da

Please sign in to comment.