Skip to content

Commit 94d9d97

Browse files
ManUtopiKfarnabaz
andauthored
feat: option for max attributes length (#79)
Co-authored-by: Farnabaz <[email protected]>
1 parent 5d44379 commit 94d9d97

File tree

5 files changed

+265
-1
lines changed

5 files changed

+265
-1
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"dev:prepare": "nuxi prepare playground",
2121
"generate": "nuxi generate playground",
2222
"lint": "eslint --ext .js,.ts,.vue .",
23+
"test:ui": "pnpm lint && vitest --ui --open=false",
2324
"test": "vitest run",
2425
"prepack": "pnpm build",
2526
"release": "release-it"

src/to-markdown.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export default (opts: RemarkMDCOptions = {}) => {
100100
const attributesText = attributes(node, context)
101101
const fmAttributes: Record<string, string> = node.fmAttributes || {}
102102

103-
if ((value + attributesText).length > 80 || Object.keys(fmAttributes).length > 0 || node.children?.some((child: RootContent) => child.type === 'componentContainerSection')) {
103+
if ((value + attributesText).length > (opts?.maxAttributesLength || 80) || Object.keys(fmAttributes).length > 0 || node.children?.some((child: RootContent) => child.type === 'componentContainerSection')) {
104104
Object.assign(fmAttributes, (node as any).attributes)
105105
} else {
106106
value += attributesText

src/types.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ interface ComponentHandler {
66

77
export interface RemarkMDCOptions {
88
components?: ComponentHandler[]
9+
maxAttributesLength?: number
910
experimental?: {
1011
autoUnwrap?: boolean
1112
componentCodeBlockYamlProps?: boolean

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

+235
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,52 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3+
exports[`block-component > component-attributes 1`] = `
4+
{
5+
"children": [
6+
{
7+
"attributes": {
8+
"key": "value",
9+
},
10+
"children": [],
11+
"data": {
12+
"hName": "component",
13+
"hProperties": {
14+
"key": "value",
15+
},
16+
},
17+
"fmAttributes": {},
18+
"name": "component",
19+
"position": {
20+
"end": {
21+
"column": 3,
22+
"line": 2,
23+
"offset": 27,
24+
},
25+
"start": {
26+
"column": 1,
27+
"line": 1,
28+
"offset": 0,
29+
},
30+
},
31+
"type": "containerComponent",
32+
},
33+
],
34+
"position": {
35+
"end": {
36+
"column": 3,
37+
"line": 2,
38+
"offset": 27,
39+
},
40+
"start": {
41+
"column": 1,
42+
"line": 1,
43+
"offset": 0,
44+
},
45+
},
46+
"type": "root",
47+
}
48+
`;
49+
350
exports[`block-component > component-attributes-array-convert-double-quote 1`] = `
451
{
552
"children": [
@@ -235,6 +282,100 @@ exports[`block-component > component-attributes-bind-frontmatter 1`] = `
235282
}
236283
`;
237284

285+
exports[`block-component > component-attributes-length-80 1`] = `
286+
{
287+
"children": [
288+
{
289+
"attributes": {
290+
"a": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
291+
},
292+
"children": [],
293+
"data": {
294+
"hName": "component",
295+
"hProperties": {
296+
"a": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
297+
},
298+
},
299+
"fmAttributes": {},
300+
"name": "component",
301+
"position": {
302+
"end": {
303+
"column": 3,
304+
"line": 2,
305+
"offset": 83,
306+
},
307+
"start": {
308+
"column": 1,
309+
"line": 1,
310+
"offset": 0,
311+
},
312+
},
313+
"type": "containerComponent",
314+
},
315+
],
316+
"position": {
317+
"end": {
318+
"column": 3,
319+
"line": 2,
320+
"offset": 83,
321+
},
322+
"start": {
323+
"column": 1,
324+
"line": 1,
325+
"offset": 0,
326+
},
327+
},
328+
"type": "root",
329+
}
330+
`;
331+
332+
exports[`block-component > component-attributes-length-81 1`] = `
333+
{
334+
"children": [
335+
{
336+
"attributes": {
337+
"a": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
338+
},
339+
"children": [],
340+
"data": {
341+
"hName": "component",
342+
"hProperties": {
343+
"a": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
344+
},
345+
},
346+
"fmAttributes": {},
347+
"name": "component",
348+
"position": {
349+
"end": {
350+
"column": 3,
351+
"line": 2,
352+
"offset": 84,
353+
},
354+
"start": {
355+
"column": 1,
356+
"line": 1,
357+
"offset": 0,
358+
},
359+
},
360+
"type": "containerComponent",
361+
},
362+
],
363+
"position": {
364+
"end": {
365+
"column": 3,
366+
"line": 2,
367+
"offset": 84,
368+
},
369+
"start": {
370+
"column": 1,
371+
"line": 1,
372+
"offset": 0,
373+
},
374+
},
375+
"type": "root",
376+
}
377+
`;
378+
238379
exports[`block-component > component-attributes-object 1`] = `
239380
{
240381
"children": [
@@ -282,6 +423,53 @@ exports[`block-component > component-attributes-object 1`] = `
282423
}
283424
`;
284425

426+
exports[`block-component > component-attributes-remove-duplicate 1`] = `
427+
{
428+
"children": [
429+
{
430+
"attributes": {
431+
"key": "value",
432+
},
433+
"children": [],
434+
"data": {
435+
"hName": "component",
436+
"hProperties": {
437+
"key": "value",
438+
},
439+
},
440+
"fmAttributes": {},
441+
"name": "component",
442+
"position": {
443+
"end": {
444+
"column": 3,
445+
"line": 2,
446+
"offset": 39,
447+
},
448+
"start": {
449+
"column": 1,
450+
"line": 1,
451+
"offset": 0,
452+
},
453+
},
454+
"type": "containerComponent",
455+
},
456+
],
457+
"position": {
458+
"end": {
459+
"column": 3,
460+
"line": 2,
461+
"offset": 39,
462+
},
463+
"start": {
464+
"column": 1,
465+
"line": 1,
466+
"offset": 0,
467+
},
468+
},
469+
"type": "root",
470+
}
471+
`;
472+
285473
exports[`block-component > component-hProperties-be-the-same 1`] = `
286474
{
287475
"children": [
@@ -362,6 +550,53 @@ items:
362550
}
363551
`;
364552

553+
exports[`block-component > component-max-attributes-length-option 1`] = `
554+
{
555+
"children": [
556+
{
557+
"attributes": {
558+
"a": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
559+
},
560+
"children": [],
561+
"data": {
562+
"hName": "component",
563+
"hProperties": {
564+
"a": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
565+
},
566+
},
567+
"fmAttributes": {},
568+
"name": "component",
569+
"position": {
570+
"end": {
571+
"column": 3,
572+
"line": 2,
573+
"offset": 103,
574+
},
575+
"start": {
576+
"column": 1,
577+
"line": 1,
578+
"offset": 0,
579+
},
580+
},
581+
"type": "containerComponent",
582+
},
583+
],
584+
"position": {
585+
"end": {
586+
"column": 3,
587+
"line": 2,
588+
"offset": 103,
589+
},
590+
"start": {
591+
"column": 1,
592+
"line": 1,
593+
"offset": 0,
594+
},
595+
},
596+
"type": "root",
597+
}
598+
`;
599+
365600
exports[`block-component > dangling-list 1`] = `
366601
{
367602
"children": [

test/block-component.test.ts

+27
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,33 @@ describe('block-component', () => {
2525
markdown: '::component\n#text\n::',
2626
expected: '::component\n#text\n::'
2727
},
28+
'component-attributes': {
29+
markdown: '::component{key="value"}\n::'
30+
},
31+
'component-attributes-remove-duplicate': {
32+
markdown: '::component{key="value" key="value"}\n::',
33+
expected: '::component{key="value"}\n::'
34+
},
35+
'component-attributes-length-80': {
36+
// `::component{a=""}` = 17 characters + 1 because array start at 0
37+
markdown: `::component{a="${new Array(80 - 17 + 1).join('a')}"}\n::`
38+
},
39+
'component-attributes-length-81': {
40+
markdown: `::component{a="${new Array(81 - 17 + 1).join('a')}"}\n::`,
41+
expected: [
42+
'::component',
43+
'---',
44+
`a: ${new Array(81 - 17 + 1).join('a')}`,
45+
'---',
46+
'::'
47+
].join('\n')
48+
},
49+
'component-max-attributes-length-option': {
50+
mdcOptions: {
51+
maxAttributesLength: 100
52+
},
53+
markdown: `::component{a="${new Array(100 - 17 + 1).join('a')}"}\n::`
54+
},
2855
frontmatter: {
2956
markdown: '::with-frontmatter\n---\nkey: value\narray:\n - item\n - itemKey: value\n---\n::',
3057
expected: '::with-frontmatter\n---\narray:\n - item\n - itemKey: value\nkey: value\n---\n::'

0 commit comments

Comments
 (0)