Skip to content

Commit 597e125

Browse files
committed
fix: convert json attributes to yaml syntax
1 parent 8b0c25f commit 597e125

File tree

3 files changed

+69
-4
lines changed

3 files changed

+69
-4
lines changed

src/to-markdown.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ export default (opts: RemarkMDCOptions = {}) => {
158158
|| attributesEntries.length > 3
159159
// It is recommended to use yaml for complex attributes
160160
|| attributesEntries.some(([_, value]) => typeof value === 'object')
161-
|| attributesText.match(/(=['"][\{\[]|\n)/)
161+
|| attributesText.match(/(=['"][{[]|\n)/) // ='[]' ='{}' ="{}" ="[]"
162162
|| node.children?.some((child: RootContent) => child.type === 'componentContainerSection') // remove: allow using both yaml and inline attributes simentensoly
163163
) {
164164
// add attributes to frontmatter

test/block-component.test.ts

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ describe('block-component', () => {
6565
},
6666
'jsonScapeAttr': {
6767
markdown: '::foo{:test=\'{"foo":"I\\\'d love to"}\'}\n::',
68+
expected: [
69+
'::foo',
70+
'---',
71+
'test:',
72+
' foo: I\'d love to',
73+
'---',
74+
'::',
75+
].join('\n'),
6876
extra: (_md, ast) => {
6977
expect(ast.children[0].type).toBe('containerComponent')
7078
},
@@ -271,36 +279,78 @@ describe('block-component', () => {
271279
},
272280
'component-attributes-array-of-string': {
273281
markdown: '::container-component{:items=\'["Nuxt", "Vue"]\'}\n::',
274-
// expected: '::container-component{:items="["Nuxt", "Vue"]"}\n::',
282+
expected: [
283+
'::container-component',
284+
'---',
285+
'items:',
286+
' - Nuxt',
287+
' - Vue',
288+
'---',
289+
'::',
290+
].join('\n'),
275291
extra(_, ast) {
276292
expect(ast.children[0].attributes).toEqual({ ':items': '["Nuxt", "Vue"]' })
277293
expect(ast.children[0].data.hProperties).toEqual({ ':items': '["Nuxt", "Vue"]' })
278294
},
279295
},
280296
'component-attributes-bad-array': {
281297
markdown: '::container-component{:items="[Nuxt,Vue]"}\n::',
298+
expected: [
299+
'::container-component',
300+
'---',
301+
':items: "[Nuxt,Vue]"',
302+
'---',
303+
'::',
304+
].join('\n'),
282305
extra(_, ast) {
283306
expect(ast.children[0].attributes).toEqual({ ':items': '[Nuxt,Vue]' })
284307
expect(ast.children[0].data.hProperties).toEqual({ ':items': '[Nuxt,Vue]' })
285308
},
286309
},
287310
'component-attributes-array-of-number': {
288311
markdown: '::container-component{:items=\'[1,2,3.5]\'}\n::',
312+
expected: [
313+
'::container-component',
314+
'---',
315+
'items:',
316+
' - 1',
317+
' - 2',
318+
' - 3.5',
319+
'---',
320+
'::',
321+
].join('\n'),
289322
extra(_, ast) {
290323
expect(ast.children[0].attributes).toEqual({ ':items': '[1,2,3.5]' })
291324
expect(ast.children[0].data.hProperties).toEqual({ ':items': '[1,2,3.5]' })
292325
},
293326
},
294327
'component-attributes-array-convert-double-quote': {
295328
markdown: '::container-component{:items="[1,2,3.5]"}\n::',
296-
expected: '::container-component{:items=\'[1,2,3.5]\'}\n::',
329+
expected: [
330+
'::container-component',
331+
'---',
332+
'items:',
333+
' - 1',
334+
' - 2',
335+
' - 3.5',
336+
'---',
337+
'::',
338+
].join('\n'),
297339
extra(_, ast) {
298340
expect(ast.children[0].attributes).toEqual({ ':items': '[1,2,3.5]' })
299341
expect(ast.children[0].data.hProperties).toEqual({ ':items': '[1,2,3.5]' })
300342
},
301343
},
302344
'component-attributes-object': {
303345
markdown: '::container-component{:items=\'{"key": "value"}\'}\n::',
346+
expected: [
347+
'::container-component',
348+
'---',
349+
'items:',
350+
' key: value',
351+
'---',
352+
'::',
353+
].join('\n'),
304354
extra(_, ast) {
305355
expect(ast.children[0].attributes).toEqual({ ':items': '{"key": "value"}' })
306356
expect(ast.children[0].data.hProperties).toEqual({ ':items': '{"key": "value"}' })
@@ -317,6 +367,21 @@ describe('block-component', () => {
317367
'---',
318368
'::',
319369
].join('\n'),
370+
expected: [
371+
'::container-component',
372+
'---',
373+
'items:',
374+
' key: value',
375+
'---',
376+
'::',
377+
'',
378+
'::container-component',
379+
'---',
380+
'items:',
381+
' key: value',
382+
'---',
383+
'::',
384+
].join('\n'),
320385
extra(_, ast) {
321386
expect(ast.children[0].attributes).toEqual({ ':items': '{"key":"value"}' })
322387
expect(ast.children[1].attributes).toEqual({})

test/format.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,6 @@ attributes:
230230
---
231231
::`,
232232
removeFmAttributes: true,
233-
}
233+
},
234234
})
235235
})

0 commit comments

Comments
 (0)