Skip to content

Commit 15bae0f

Browse files
committed
fix(stringify): yaml props if more than 3 props or have multi-line value
1 parent 0c5d4fa commit 15bae0f

File tree

4 files changed

+310
-0
lines changed

4 files changed

+310
-0
lines changed

src/to-markdown.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ export default (opts: RemarkMDCOptions = {}) => {
151151
const attributesText = attributes(node, context)
152152
if (
153153
(value + attributesText).length > (opts?.attributes?.maxLength || 80)
154+
|| Object.keys((node as any).attributes || {}).length > 3
155+
|| attributesText.includes('\n')
154156
|| Object.keys(node.fmAttributes).length > 0 // remove: allow using both yaml and inline attributes simentensoly
155157
|| node.children?.some((child: RootContent) => child.type === 'componentContainerSection') // remove: allow using both yaml and inline attributes simentensoly
156158
) {
@@ -262,6 +264,9 @@ export default (opts: RemarkMDCOptions = {}) => {
262264
else if (key.startsWith(':') && isValidJSON(value)) {
263265
values.push(`${key}='${value.replace(/([^/])'/g, '$1\\\'')}'`)
264266
}
267+
else if (typeof attr[1] === 'object') {
268+
values.push(quoted(key, JSON.stringify(value)))
269+
}
265270
else {
266271
values.push(quoted(key, value))
267272
}
Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`ul > nested1 1`] = `
4+
{
5+
"children": [
6+
{
7+
"attributes": {},
8+
"children": [
9+
{
10+
"children": [
11+
{
12+
"position": {
13+
"end": {
14+
"column": 19,
15+
"line": 6,
16+
"offset": 77,
17+
},
18+
"start": {
19+
"column": 3,
20+
"line": 6,
21+
"offset": 61,
22+
},
23+
},
24+
"type": "text",
25+
"value": "This is a header",
26+
},
27+
],
28+
"depth": 1,
29+
"position": {
30+
"end": {
31+
"column": 19,
32+
"line": 6,
33+
"offset": 77,
34+
},
35+
"start": {
36+
"column": 1,
37+
"line": 6,
38+
"offset": 59,
39+
},
40+
},
41+
"type": "heading",
42+
},
43+
{
44+
"attributes": {
45+
"color": "#000",
46+
"name": "mdi:github",
47+
"size": "36px",
48+
},
49+
"children": [],
50+
"data": {
51+
"hName": "icon",
52+
"hProperties": {
53+
"color": "#000",
54+
"name": "mdi:github",
55+
"size": "36px",
56+
},
57+
},
58+
"fmAttributes": {},
59+
"name": "icon",
60+
"position": {
61+
"end": {
62+
"column": 50,
63+
"line": 8,
64+
"offset": 128,
65+
},
66+
"start": {
67+
"column": 1,
68+
"line": 8,
69+
"offset": 79,
70+
},
71+
},
72+
"type": "textComponent",
73+
},
74+
{
75+
"attributes": {},
76+
"children": [
77+
{
78+
"children": [
79+
{
80+
"position": {
81+
"end": {
82+
"column": 7,
83+
"line": 11,
84+
"offset": 150,
85+
},
86+
"start": {
87+
"column": 3,
88+
"line": 11,
89+
"offset": 146,
90+
},
91+
},
92+
"type": "text",
93+
"value": "Well",
94+
},
95+
],
96+
"position": {
97+
"end": {
98+
"column": 7,
99+
"line": 11,
100+
"offset": 150,
101+
},
102+
"start": {
103+
"column": 3,
104+
"line": 11,
105+
"offset": 146,
106+
},
107+
},
108+
"type": "paragraph",
109+
},
110+
],
111+
"data": {
112+
"hName": "content2",
113+
"hProperties": {},
114+
},
115+
"fmAttributes": {},
116+
"name": "content2",
117+
"position": {
118+
"end": {
119+
"column": 6,
120+
"line": 12,
121+
"offset": 156,
122+
},
123+
"start": {
124+
"column": 3,
125+
"line": 10,
126+
"offset": 132,
127+
},
128+
},
129+
"type": "containerComponent",
130+
},
131+
],
132+
"data": {
133+
"hName": "container",
134+
"hProperties": {
135+
"background-color": "#eee",
136+
"padding": "20px",
137+
},
138+
},
139+
"fmAttributes": {
140+
"background-color": "#eee",
141+
"padding": "20px",
142+
},
143+
"name": "container",
144+
"position": {
145+
"end": {
146+
"column": 3,
147+
"line": 13,
148+
"offset": 159,
149+
},
150+
"start": {
151+
"column": 1,
152+
"line": 1,
153+
"offset": 0,
154+
},
155+
},
156+
"rawData": "
157+
background-color: "#eee"
158+
padding: 20px
159+
---",
160+
"type": "containerComponent",
161+
},
162+
],
163+
"position": {
164+
"end": {
165+
"column": 3,
166+
"line": 13,
167+
"offset": 159,
168+
},
169+
"start": {
170+
"column": 1,
171+
"line": 1,
172+
"offset": 0,
173+
},
174+
},
175+
"type": "root",
176+
}
177+
`;
178+
179+
exports[`ul > nested2 1`] = `
180+
{
181+
"children": [
182+
{
183+
"attributes": {},
184+
"children": [],
185+
"data": {
186+
"hName": "container",
187+
"hProperties": {
188+
":bi": "{"url":"https://example.com","bg":"contain"}",
189+
"styles": "p {
190+
color: red;
191+
}
192+
",
193+
},
194+
},
195+
"fmAttributes": {
196+
"bi": {
197+
"bg": "contain",
198+
"url": "https://example.com",
199+
},
200+
"styles": "p {
201+
color: red;
202+
}
203+
",
204+
},
205+
"name": "container",
206+
"position": {
207+
"end": {
208+
"column": 3,
209+
"line": 11,
210+
"offset": 103,
211+
},
212+
"start": {
213+
"column": 1,
214+
"line": 1,
215+
"offset": 0,
216+
},
217+
},
218+
"rawData": "
219+
bi:
220+
url: https://example.com
221+
bg: contain
222+
styles: |
223+
p {
224+
color: red;
225+
}
226+
---",
227+
"type": "containerComponent",
228+
},
229+
],
230+
"position": {
231+
"end": {
232+
"column": 3,
233+
"line": 11,
234+
"offset": 103,
235+
},
236+
"start": {
237+
"column": 1,
238+
"line": 1,
239+
"offset": 0,
240+
},
241+
},
242+
"type": "root",
243+
}
244+
`;

test/format.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { describe } from 'vitest'
2+
import { runMarkdownTests } from './utils'
3+
4+
describe('ul', () => {
5+
runMarkdownTests({
6+
nested1: {
7+
markdown: [
8+
'::container',
9+
'---',
10+
'background-color: "#eee"',
11+
'padding: 20px',
12+
'---',
13+
'# This is a header',
14+
'',
15+
':icon{color="#000" name="mdi:github" size="36px"}',
16+
'',
17+
' :::content2',
18+
' Well',
19+
' :::',
20+
'::',
21+
].join('\n'),
22+
extra(markdown, ast, expected) {
23+
console.log(JSON.stringify(ast, null, 2))
24+
}
25+
},
26+
nested2: {
27+
markdown: [
28+
'::container',
29+
'---',
30+
'bi:',
31+
' url: https://example.com',
32+
' bg: contain',
33+
'styles: |',
34+
' p {',
35+
' color: red;',
36+
' }',
37+
'---',
38+
'::',
39+
].join('\n'),
40+
},
41+
})
42+
})

test/utils/index.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,22 @@ function _removePosition(ast: any) {
4949
}
5050
}
5151
}
52+
53+
// Internal function to remove frontmatter attributes from ast
54+
function _removeFmAttributes(ast: any) {
55+
if (Array.isArray(ast)) {
56+
ast.forEach(child => _removeFmAttributes(child))
57+
}
58+
else if (ast && typeof ast === 'object') {
59+
ast.attributes = {
60+
...(ast.attributes || {}),
61+
...(ast.fmAttributes || {}),
62+
}
63+
delete ast.fmAttributes
64+
delete ast.data?.hProperties
65+
delete ast.rawData
66+
if (ast.children) {
67+
_removeFmAttributes(ast.children)
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)