Skip to content

Commit 9da1c52

Browse files
committed
feat: slot attributes syntax
1 parent e96baaa commit 9da1c52

File tree

4 files changed

+135
-1
lines changed

4 files changed

+135
-1
lines changed

src/micromark-extension/tokenize-container.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,26 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat
101101
}
102102

103103
function sectionTitle (code: Code): State | undefined {
104+
// parse attributes
105+
if (code === Codes.openingCurlyBracket) {
106+
return effects.check(
107+
attributes,
108+
(code) => {
109+
effects.exit('componentContainerSectionTitle')
110+
return effects.attempt(attributes, factorySpace(effects, lineStart, 'linePrefix', 4), nok)(code)
111+
},
112+
(code) => {
113+
effects.consume(code)
114+
return sectionTitle
115+
}
116+
)(code)
117+
}
118+
104119
if (markdownLineEnding(code)) {
105120
effects.exit('componentContainerSectionTitle')
106121
return factorySpace(effects, lineStart, 'linePrefix', 4)(code)
107122
}
123+
108124
effects.consume(code)
109125
return sectionTitle
110126
}

src/to-markdown.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export default (opts: RemarkMDCOptions = {}) => {
9696

9797
processNode(node as any)
9898

99-
return `#${(node as any).name}\n${content(node, context)}`.trim()
99+
return `#${(node as any).name}${attributes(node, context)}\n${content(node, context)}`.trim()
100100
}
101101

102102
type NodeTextComponent = Parents & { name: string; rawData: string; attributes: any }

test/__snapshots__/block-component.test.ts.snap

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2033,6 +2033,111 @@ exports[`block-component > section-order-2 1`] = `
20332033
}
20342034
`;
20352035

2036+
exports[`block-component > slot-attributes 1`] = `
2037+
{
2038+
"children": [
2039+
{
2040+
"attributes": {},
2041+
"children": [
2042+
{
2043+
"attributes": {
2044+
"key": "value",
2045+
},
2046+
"children": [
2047+
{
2048+
"children": [
2049+
{
2050+
"position": {
2051+
"end": {
2052+
"column": 13,
2053+
"line": 3,
2054+
"offset": 53,
2055+
},
2056+
"start": {
2057+
"column": 1,
2058+
"line": 3,
2059+
"offset": 41,
2060+
},
2061+
},
2062+
"type": "text",
2063+
"value": "slot content",
2064+
},
2065+
],
2066+
"position": {
2067+
"end": {
2068+
"column": 13,
2069+
"line": 3,
2070+
"offset": 53,
2071+
},
2072+
"start": {
2073+
"column": 1,
2074+
"line": 3,
2075+
"offset": 41,
2076+
},
2077+
},
2078+
"type": "paragraph",
2079+
},
2080+
],
2081+
"data": {
2082+
"hName": "component-slot",
2083+
"hProperties": {
2084+
"key": "value",
2085+
"v-slot:slot": "",
2086+
},
2087+
},
2088+
"name": "slot",
2089+
"position": {
2090+
"end": {
2091+
"column": 3,
2092+
"line": 4,
2093+
"offset": 56,
2094+
},
2095+
"start": {
2096+
"column": 2,
2097+
"line": 2,
2098+
"offset": 23,
2099+
},
2100+
},
2101+
"type": "componentContainerSection",
2102+
},
2103+
],
2104+
"data": {
2105+
"hName": "container-component",
2106+
"hProperties": {},
2107+
},
2108+
"fmAttributes": {},
2109+
"name": "container-component",
2110+
"position": {
2111+
"end": {
2112+
"column": 3,
2113+
"line": 4,
2114+
"offset": 56,
2115+
},
2116+
"start": {
2117+
"column": 1,
2118+
"line": 1,
2119+
"offset": 0,
2120+
},
2121+
},
2122+
"type": "containerComponent",
2123+
},
2124+
],
2125+
"position": {
2126+
"end": {
2127+
"column": 3,
2128+
"line": 4,
2129+
"offset": 56,
2130+
},
2131+
"start": {
2132+
"column": 1,
2133+
"line": 1,
2134+
"offset": 0,
2135+
},
2136+
},
2137+
"type": "root",
2138+
}
2139+
`;
2140+
20362141
exports[`block-component > sugar-syntax 1`] = `
20372142
{
20382143
"children": [

test/block-component.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,19 @@ describe('block-component', () => {
315315
expect(ast.children[1].attributes).toEqual({})
316316
expect(ast.children[0].data.hProperties).toEqual(ast.children[1].data.hProperties)
317317
}
318+
},
319+
'slot-attributes': {
320+
markdown: [
321+
'::container-component',
322+
'#slot{key="value"}',
323+
'slot content',
324+
'::'
325+
].join('\n'),
326+
extra (_, ast) {
327+
const slot = ast.children[0].children[0]
328+
expect(slot.type).toBe('componentContainerSection')
329+
expect(slot.attributes).toEqual({ key: 'value' })
330+
}
318331
}
319332
})
320333
})

0 commit comments

Comments
 (0)