@markdoc/markdoc 0.2.2 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +21 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +154 -58
- package/dist/index.js.map +3 -3
- package/dist/index.mjs +154 -58
- package/dist/index.mjs.map +3 -3
- package/dist/src/ast/index.d.ts +1 -0
- package/dist/src/ast/index.d.ts.map +1 -1
- package/dist/src/ast/node.d.ts +1 -0
- package/dist/src/ast/node.d.ts.map +1 -1
- package/dist/src/formatter.d.ts.map +1 -1
- package/dist/src/parser.d.ts +2 -1
- package/dist/src/parser.d.ts.map +1 -1
- package/dist/src/schema-types/class.d.ts +2 -2
- package/dist/src/schema-types/class.d.ts.map +1 -1
- package/dist/src/schema.d.ts.map +1 -1
- package/dist/src/tags/index.d.ts +17 -2
- package/dist/src/tags/index.d.ts.map +1 -1
- package/dist/src/tags/slot.d.ts +3 -0
- package/dist/src/tags/slot.d.ts.map +1 -0
- package/dist/src/tokenizer/plugins/annotations.d.ts.map +1 -1
- package/dist/src/transformer.d.ts.map +1 -1
- package/dist/src/types.d.ts +14 -1
- package/dist/src/types.d.ts.map +1 -1
- package/dist/src/validator.d.ts +23 -1
- package/dist/src/validator.d.ts.map +1 -1
- package/index.ts +9 -24
- package/package.json +1 -1
- package/src/ast/node.test.ts +47 -0
- package/src/ast/node.ts +3 -1
- package/src/formatter.test.ts +99 -8
- package/src/formatter.ts +39 -16
- package/src/parser.test.ts +276 -2
- package/src/parser.ts +37 -11
- package/src/renderers/html.test.ts +7 -0
- package/src/renderers/html.ts +1 -1
- package/src/schema-types/class.ts +7 -3
- package/src/schema.ts +12 -2
- package/src/tags/index.ts +3 -1
- package/src/tags/slot.ts +7 -0
- package/src/tokenizer/plugins/annotations.test.ts +44 -22
- package/src/tokenizer/plugins/annotations.ts +3 -1
- package/src/transformer.ts +7 -0
- package/src/types.ts +16 -1
- package/src/validator.test.ts +202 -0
- package/src/validator.ts +73 -7
package/src/formatter.ts
CHANGED
|
@@ -12,8 +12,8 @@ type Options = {
|
|
|
12
12
|
const SPACE = ' ';
|
|
13
13
|
const SEP = ', '; // Value separator
|
|
14
14
|
const NL = '\n'; // Newline
|
|
15
|
-
const OL = '
|
|
16
|
-
const UL = '-
|
|
15
|
+
const OL = '.'; // Ordered list
|
|
16
|
+
const UL = '-'; // Unordered list
|
|
17
17
|
|
|
18
18
|
const MAX_TAG_OPENING_WIDTH = 80;
|
|
19
19
|
|
|
@@ -39,7 +39,10 @@ function* formatTableRow(items: Array<string>) {
|
|
|
39
39
|
yield `| ${items.join(' | ')} |`;
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
function formatScalar(v: Value): string {
|
|
42
|
+
function formatScalar(v: Value): string | undefined {
|
|
43
|
+
if (v === undefined) {
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
43
46
|
if (Ast.isAst(v)) {
|
|
44
47
|
return format(v);
|
|
45
48
|
}
|
|
@@ -64,12 +67,16 @@ function formatScalar(v: Value): string {
|
|
|
64
67
|
return JSON.stringify(v);
|
|
65
68
|
}
|
|
66
69
|
|
|
67
|
-
function formatAnnotationValue(a: AttributeValue): string {
|
|
68
|
-
|
|
70
|
+
function formatAnnotationValue(a: AttributeValue): string | undefined {
|
|
71
|
+
const formattedValue = formatScalar(a.value);
|
|
72
|
+
|
|
73
|
+
if (formattedValue === undefined) return undefined;
|
|
74
|
+
if (a.name === 'primary') return formattedValue;
|
|
69
75
|
if (a.name === 'id' && typeof a.value === 'string' && isIdentifier(a.value))
|
|
70
76
|
return '#' + a.value;
|
|
71
77
|
if (a.type === 'class' && isIdentifier(a.name)) return '.' + a.name;
|
|
72
|
-
|
|
78
|
+
|
|
79
|
+
return `${a.name}=${formattedValue}`;
|
|
73
80
|
}
|
|
74
81
|
|
|
75
82
|
function* formatAttributes(n: Node) {
|
|
@@ -258,7 +265,10 @@ function* formatNode(n: Node, o: Options = {}) {
|
|
|
258
265
|
yield indent;
|
|
259
266
|
}
|
|
260
267
|
const open = OPEN + SPACE;
|
|
261
|
-
const
|
|
268
|
+
const attributes = [...formatAttributes(n)].filter(
|
|
269
|
+
(v) => v !== undefined
|
|
270
|
+
);
|
|
271
|
+
const tag = [open + n.tag, ...attributes];
|
|
262
272
|
const inlineTag = tag.join(SPACE);
|
|
263
273
|
|
|
264
274
|
const isLongTagOpening =
|
|
@@ -287,10 +297,23 @@ function* formatNode(n: Node, o: Options = {}) {
|
|
|
287
297
|
break;
|
|
288
298
|
}
|
|
289
299
|
case 'list': {
|
|
290
|
-
const
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
300
|
+
const isLoose = n.children.some((n) =>
|
|
301
|
+
n.children.some((c) => c.type === 'paragraph')
|
|
302
|
+
);
|
|
303
|
+
|
|
304
|
+
for (let i = 0; i < n.children.length; i++) {
|
|
305
|
+
const prefix = n.attributes.ordered
|
|
306
|
+
? `${i === 0 ? n.attributes.start ?? '1' : '1'}${
|
|
307
|
+
n.attributes.marker ?? OL
|
|
308
|
+
}`
|
|
309
|
+
: n.attributes.marker ?? UL;
|
|
310
|
+
let d = format(n.children[i], increment(no, prefix.length + 1));
|
|
311
|
+
|
|
312
|
+
if (!isLoose || i === n.children.length - 1) {
|
|
313
|
+
d = d.trim();
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
yield NL + indent + prefix + ' ' + d;
|
|
294
317
|
}
|
|
295
318
|
yield NL;
|
|
296
319
|
break;
|
|
@@ -303,15 +326,15 @@ function* formatNode(n: Node, o: Options = {}) {
|
|
|
303
326
|
break;
|
|
304
327
|
}
|
|
305
328
|
case 'strong': {
|
|
306
|
-
yield '**';
|
|
329
|
+
yield n.attributes.marker ?? '**';
|
|
307
330
|
yield* formatInline(formatChildren(n, no));
|
|
308
|
-
yield '**';
|
|
331
|
+
yield n.attributes.marker ?? '**';
|
|
309
332
|
break;
|
|
310
333
|
}
|
|
311
334
|
case 'em': {
|
|
312
|
-
yield '
|
|
335
|
+
yield n.attributes.marker ?? '*';
|
|
313
336
|
yield* formatInline(formatChildren(n, no));
|
|
314
|
-
yield '
|
|
337
|
+
yield n.attributes.marker ?? '*';
|
|
315
338
|
break;
|
|
316
339
|
}
|
|
317
340
|
case 'code': {
|
|
@@ -353,7 +376,7 @@ function* formatNode(n: Node, o: Options = {}) {
|
|
|
353
376
|
yield indent + '---';
|
|
354
377
|
}
|
|
355
378
|
for (const d of row) {
|
|
356
|
-
yield NL + indent + UL + d;
|
|
379
|
+
yield NL + indent + UL + ' ' + d;
|
|
357
380
|
}
|
|
358
381
|
}
|
|
359
382
|
}
|
package/src/parser.test.ts
CHANGED
|
@@ -9,12 +9,50 @@ describe('Markdown parser', function () {
|
|
|
9
9
|
const fence = '```';
|
|
10
10
|
const tokenizer = new Tokenizer({ allowComments: true });
|
|
11
11
|
|
|
12
|
-
function convert(example) {
|
|
12
|
+
function convert(example, options?) {
|
|
13
13
|
const content = example.replace(/\n\s+/gm, '\n').trim();
|
|
14
14
|
const tokens = tokenizer.tokenize(content);
|
|
15
|
-
return parser(tokens);
|
|
15
|
+
return parser(tokens, options);
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
+
describe('handling options', function () {
|
|
19
|
+
it('no args', function () {
|
|
20
|
+
const example = convert(`# This is a test`);
|
|
21
|
+
expect(example.children[0]).toDeepEqual({
|
|
22
|
+
...any(),
|
|
23
|
+
type: 'heading',
|
|
24
|
+
location: {
|
|
25
|
+
...any(),
|
|
26
|
+
file: undefined,
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('filename as string', function () {
|
|
32
|
+
const example = convert(`# This is a test`, 'foo.md');
|
|
33
|
+
expect(example.children[0]).toDeepEqual({
|
|
34
|
+
...any(),
|
|
35
|
+
type: 'heading',
|
|
36
|
+
location: {
|
|
37
|
+
...any(),
|
|
38
|
+
file: 'foo.md',
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('filename as property', function () {
|
|
44
|
+
const example = convert(`# This is a test`, { file: 'foo.md' });
|
|
45
|
+
expect(example.children[0]).toDeepEqual({
|
|
46
|
+
...any(),
|
|
47
|
+
type: 'heading',
|
|
48
|
+
location: {
|
|
49
|
+
...any(),
|
|
50
|
+
file: 'foo.md',
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
18
56
|
describe('handling frontmatter', function () {
|
|
19
57
|
it('simple frontmatter', function () {
|
|
20
58
|
const example = convert(`
|
|
@@ -55,6 +93,29 @@ describe('Markdown parser', function () {
|
|
|
55
93
|
});
|
|
56
94
|
|
|
57
95
|
describe('handling attributes', function () {
|
|
96
|
+
it('for emphasis', function () {
|
|
97
|
+
const items = (doc, n = 0) =>
|
|
98
|
+
doc.children[0].children[0].children[n].attributes.marker;
|
|
99
|
+
|
|
100
|
+
const example1 = convert(`a*b*c`);
|
|
101
|
+
expect(items(example1, 1)).toEqual('*');
|
|
102
|
+
|
|
103
|
+
const example1a = convert(`a**b**c`);
|
|
104
|
+
expect(items(example1a, 1)).toEqual('**');
|
|
105
|
+
|
|
106
|
+
const example2 = convert(`_foo_ bar`);
|
|
107
|
+
expect(items(example2)).toEqual('_');
|
|
108
|
+
|
|
109
|
+
const example2a = convert(`__foo__ bar`);
|
|
110
|
+
expect(items(example2a)).toEqual('__');
|
|
111
|
+
|
|
112
|
+
const example3 = convert(`foo *bar* baz`);
|
|
113
|
+
expect(items(example3, 1)).toEqual('*');
|
|
114
|
+
|
|
115
|
+
const example3a = convert(`foo **bar** baz`);
|
|
116
|
+
expect(items(example3a, 1)).toEqual('**');
|
|
117
|
+
});
|
|
118
|
+
|
|
58
119
|
it('for heading', function () {
|
|
59
120
|
const document = convert(`# Sample Heading`);
|
|
60
121
|
expect(document.children[0].attributes).toDeepEqual({ level: 1 });
|
|
@@ -77,6 +138,32 @@ describe('Markdown parser', function () {
|
|
|
77
138
|
expect(ordered.children[0].attributes.ordered).toEqual(true);
|
|
78
139
|
});
|
|
79
140
|
|
|
141
|
+
it('for ordered list start', function () {
|
|
142
|
+
const unordered = convert(`
|
|
143
|
+
* Example 1
|
|
144
|
+
* Example 2
|
|
145
|
+
* Example 3
|
|
146
|
+
`);
|
|
147
|
+
|
|
148
|
+
const numbered = convert(`
|
|
149
|
+
3. Example 1
|
|
150
|
+
4. Example 2
|
|
151
|
+
5. Example 3
|
|
152
|
+
`);
|
|
153
|
+
|
|
154
|
+
const numberedStartAtOne = convert(`
|
|
155
|
+
1. Example 1
|
|
156
|
+
4. Example 2
|
|
157
|
+
5. Example 3
|
|
158
|
+
`);
|
|
159
|
+
|
|
160
|
+
const start = (list) => list.children[0].attributes.start;
|
|
161
|
+
|
|
162
|
+
expect(start(unordered)).toEqual(undefined);
|
|
163
|
+
expect(start(numbered)).toEqual(3);
|
|
164
|
+
expect(start(numberedStartAtOne)).toEqual(undefined);
|
|
165
|
+
});
|
|
166
|
+
|
|
80
167
|
it('for link with one word', function () {
|
|
81
168
|
const document = convert(`
|
|
82
169
|
[foo](/bar)
|
|
@@ -652,4 +739,191 @@ describe('Markdown parser', function () {
|
|
|
652
739
|
],
|
|
653
740
|
});
|
|
654
741
|
});
|
|
742
|
+
|
|
743
|
+
describe('handles attribute errors correctly', function () {
|
|
744
|
+
it('with error for duplicate attributes', function () {
|
|
745
|
+
const example = convert(`{% foo bar=1 bar=2 bar=3 bar=4 /%}`);
|
|
746
|
+
expect(example.children[0].errors.length).toBe(3);
|
|
747
|
+
expect(example).toDeepEqualSubset({
|
|
748
|
+
type: 'document',
|
|
749
|
+
children: [
|
|
750
|
+
{
|
|
751
|
+
tag: 'foo',
|
|
752
|
+
errors: [
|
|
753
|
+
{ id: 'duplicate-attribute' },
|
|
754
|
+
{ id: 'duplicate-attribute' },
|
|
755
|
+
{ id: 'duplicate-attribute' },
|
|
756
|
+
],
|
|
757
|
+
},
|
|
758
|
+
],
|
|
759
|
+
});
|
|
760
|
+
});
|
|
761
|
+
|
|
762
|
+
it('with error for duplicate ids', function () {
|
|
763
|
+
const example = convert(`{% foo #bar #baz #qux /%}`);
|
|
764
|
+
expect(example.children[0].errors.length).toBe(2);
|
|
765
|
+
expect(example).toDeepEqualSubset({
|
|
766
|
+
type: 'document',
|
|
767
|
+
children: [
|
|
768
|
+
{
|
|
769
|
+
tag: 'foo',
|
|
770
|
+
errors: [
|
|
771
|
+
{ id: 'duplicate-attribute' },
|
|
772
|
+
{ id: 'duplicate-attribute' },
|
|
773
|
+
],
|
|
774
|
+
},
|
|
775
|
+
],
|
|
776
|
+
});
|
|
777
|
+
});
|
|
778
|
+
|
|
779
|
+
it('with annotation values', function () {
|
|
780
|
+
const example = convert(`testing {% foo=1 foo=2 %}`);
|
|
781
|
+
expect(example.children[0].errors.length).toBe(1);
|
|
782
|
+
expect(example).toDeepEqualSubset({
|
|
783
|
+
type: 'document',
|
|
784
|
+
children: [
|
|
785
|
+
{
|
|
786
|
+
type: 'paragraph',
|
|
787
|
+
errors: [{ id: 'duplicate-attribute' }],
|
|
788
|
+
},
|
|
789
|
+
],
|
|
790
|
+
});
|
|
791
|
+
});
|
|
792
|
+
|
|
793
|
+
it('across annotations on the same node', function () {
|
|
794
|
+
const example = convert(`testing {% foo=1 %} another test {% foo=1 %}`);
|
|
795
|
+
expect(example.children[0].errors.length).toBe(1);
|
|
796
|
+
expect(example).toDeepEqualSubset({
|
|
797
|
+
type: 'document',
|
|
798
|
+
children: [
|
|
799
|
+
{
|
|
800
|
+
type: 'paragraph',
|
|
801
|
+
errors: [{ id: 'duplicate-attribute' }],
|
|
802
|
+
},
|
|
803
|
+
],
|
|
804
|
+
});
|
|
805
|
+
});
|
|
806
|
+
|
|
807
|
+
it('with no error for multiple classes', function () {
|
|
808
|
+
const example = convert(`{% foo .bar .baz .qux /%}`);
|
|
809
|
+
expect(example.children[0].errors.length).toBe(0);
|
|
810
|
+
});
|
|
811
|
+
});
|
|
812
|
+
|
|
813
|
+
it('displays error for annotations in a fence', function () {
|
|
814
|
+
const example = convert(`
|
|
815
|
+
~~~
|
|
816
|
+
test
|
|
817
|
+
{% #foo %}
|
|
818
|
+
test
|
|
819
|
+
~~~
|
|
820
|
+
`);
|
|
821
|
+
|
|
822
|
+
expect(Object.values(example.annotations).length).toEqual(0);
|
|
823
|
+
expect(example.children[0].errors[0]?.id).toEqual('no-inline-annotations');
|
|
824
|
+
});
|
|
825
|
+
|
|
826
|
+
it('correctly identifies inlines', function () {
|
|
827
|
+
const example = convert(`
|
|
828
|
+
# This is a test
|
|
829
|
+
|
|
830
|
+
{% foo %}
|
|
831
|
+
Another {% bar %}test{% /bar %} test
|
|
832
|
+
{% /foo %}
|
|
833
|
+
|
|
834
|
+
* bar
|
|
835
|
+
`);
|
|
836
|
+
|
|
837
|
+
expect(example).toDeepEqualSubset({
|
|
838
|
+
type: 'document',
|
|
839
|
+
inline: false,
|
|
840
|
+
children: [
|
|
841
|
+
{
|
|
842
|
+
type: 'heading',
|
|
843
|
+
inline: false,
|
|
844
|
+
children: [
|
|
845
|
+
{
|
|
846
|
+
type: 'inline',
|
|
847
|
+
inline: false,
|
|
848
|
+
children: [{ type: 'text', inline: true }],
|
|
849
|
+
},
|
|
850
|
+
],
|
|
851
|
+
},
|
|
852
|
+
{
|
|
853
|
+
type: 'tag',
|
|
854
|
+
tag: 'foo',
|
|
855
|
+
inline: false,
|
|
856
|
+
children: [
|
|
857
|
+
{
|
|
858
|
+
type: 'paragraph',
|
|
859
|
+
inline: false,
|
|
860
|
+
children: [
|
|
861
|
+
{
|
|
862
|
+
type: 'inline',
|
|
863
|
+
inline: false,
|
|
864
|
+
children: [
|
|
865
|
+
{ type: 'text', inline: true },
|
|
866
|
+
{
|
|
867
|
+
type: 'tag',
|
|
868
|
+
tag: 'bar',
|
|
869
|
+
inline: true,
|
|
870
|
+
children: [{ type: 'text', inline: true }],
|
|
871
|
+
},
|
|
872
|
+
{ type: 'text', inline: true },
|
|
873
|
+
],
|
|
874
|
+
},
|
|
875
|
+
],
|
|
876
|
+
},
|
|
877
|
+
],
|
|
878
|
+
},
|
|
879
|
+
{
|
|
880
|
+
type: 'list',
|
|
881
|
+
inline: false,
|
|
882
|
+
children: [
|
|
883
|
+
{
|
|
884
|
+
type: 'item',
|
|
885
|
+
inline: false,
|
|
886
|
+
children: [
|
|
887
|
+
{
|
|
888
|
+
type: 'inline',
|
|
889
|
+
inline: false,
|
|
890
|
+
children: [{ type: 'text', inline: true }],
|
|
891
|
+
},
|
|
892
|
+
],
|
|
893
|
+
},
|
|
894
|
+
],
|
|
895
|
+
},
|
|
896
|
+
],
|
|
897
|
+
});
|
|
898
|
+
});
|
|
899
|
+
|
|
900
|
+
describe('handles structural errors correctly', function () {
|
|
901
|
+
it('with unmatched closing tag', function () {
|
|
902
|
+
const example = convert(`
|
|
903
|
+
{% foo %}
|
|
904
|
+
Test
|
|
905
|
+
{% /bar %}
|
|
906
|
+
`);
|
|
907
|
+
|
|
908
|
+
expect(example.children[0].errors[0].id).toEqual('missing-closing');
|
|
909
|
+
});
|
|
910
|
+
|
|
911
|
+
it('missing opening', function () {
|
|
912
|
+
const example = convert(`
|
|
913
|
+
This a test
|
|
914
|
+
{% /foo %}
|
|
915
|
+
`);
|
|
916
|
+
|
|
917
|
+
expect(example.children[1].errors[0].id).toEqual('missing-opening');
|
|
918
|
+
});
|
|
919
|
+
|
|
920
|
+
it('with missing closing tag', function () {
|
|
921
|
+
const example = convert(`
|
|
922
|
+
{% foo %}
|
|
923
|
+
Test
|
|
924
|
+
`);
|
|
925
|
+
|
|
926
|
+
expect(example.children[0].errors[0].id).toEqual('missing-closing');
|
|
927
|
+
});
|
|
928
|
+
});
|
|
655
929
|
});
|
package/src/parser.ts
CHANGED
|
@@ -2,7 +2,7 @@ import Node from './ast/node';
|
|
|
2
2
|
import transforms from './transforms/index';
|
|
3
3
|
import { OPEN } from './utils';
|
|
4
4
|
|
|
5
|
-
import type { AttributeValue } from './types';
|
|
5
|
+
import type { AttributeValue, ParserArgs } from './types';
|
|
6
6
|
|
|
7
7
|
import type Token from 'markdown-it/lib/token';
|
|
8
8
|
|
|
@@ -19,8 +19,15 @@ function annotate(node: Node, attributes: AttributeValue[]) {
|
|
|
19
19
|
node.annotations.push(attribute);
|
|
20
20
|
|
|
21
21
|
const { name, value, type } = attribute;
|
|
22
|
-
if (type === 'attribute')
|
|
23
|
-
|
|
22
|
+
if (type === 'attribute') {
|
|
23
|
+
if (node.attributes[name] !== undefined)
|
|
24
|
+
node.errors.push({
|
|
25
|
+
id: 'duplicate-attribute',
|
|
26
|
+
level: 'warning',
|
|
27
|
+
message: `Attribute '${name}' already set`,
|
|
28
|
+
});
|
|
29
|
+
node.attributes[name] = value;
|
|
30
|
+
} else if (type === 'class')
|
|
24
31
|
if (node.attributes.class) node.attributes.class[name] = value;
|
|
25
32
|
else node.attributes.class = { [name]: value };
|
|
26
33
|
}
|
|
@@ -30,8 +37,13 @@ function handleAttrs(token: Token, type: string) {
|
|
|
30
37
|
switch (type) {
|
|
31
38
|
case 'heading':
|
|
32
39
|
return { level: Number(token.tag.replace('h', '')) };
|
|
33
|
-
case 'list':
|
|
34
|
-
|
|
40
|
+
case 'list': {
|
|
41
|
+
const attrs = token.attrs ? Object.fromEntries(token.attrs) : undefined;
|
|
42
|
+
const ordered = token.type.startsWith('ordered');
|
|
43
|
+
return ordered && attrs?.start
|
|
44
|
+
? { ordered: true, start: attrs.start, marker: token.markup }
|
|
45
|
+
: { ordered, marker: token.markup };
|
|
46
|
+
}
|
|
35
47
|
case 'link': {
|
|
36
48
|
const attrs = Object.fromEntries(token.attrs);
|
|
37
49
|
return attrs.title
|
|
@@ -44,6 +56,9 @@ function handleAttrs(token: Token, type: string) {
|
|
|
44
56
|
? { alt: token.content, src: attrs.src, title: attrs.title }
|
|
45
57
|
: { alt: token.content, src: attrs.src };
|
|
46
58
|
}
|
|
59
|
+
case 'em':
|
|
60
|
+
case 'strong':
|
|
61
|
+
return { marker: token.markup };
|
|
47
62
|
case 'text':
|
|
48
63
|
case 'code':
|
|
49
64
|
case 'comment':
|
|
@@ -85,6 +100,7 @@ function handleToken(
|
|
|
85
100
|
token: Token,
|
|
86
101
|
nodes: Node[],
|
|
87
102
|
file?: string,
|
|
103
|
+
handleSlots?: boolean,
|
|
88
104
|
inlineParent?: Node
|
|
89
105
|
) {
|
|
90
106
|
if (token.type === 'frontmatter') {
|
|
@@ -117,7 +133,7 @@ function handleToken(
|
|
|
117
133
|
}
|
|
118
134
|
|
|
119
135
|
if (token.nesting < 0) {
|
|
120
|
-
if (parent.type === typeName) {
|
|
136
|
+
if (parent.type === typeName && parent.tag === tag) {
|
|
121
137
|
if (parent.lines && token.map) parent.lines.push(...token.map);
|
|
122
138
|
return nodes.pop();
|
|
123
139
|
}
|
|
@@ -152,29 +168,39 @@ function handleToken(
|
|
|
152
168
|
if (attributes && ['tag', 'fence', 'image'].includes(typeName))
|
|
153
169
|
annotate(node, attributes);
|
|
154
170
|
|
|
155
|
-
|
|
171
|
+
if (
|
|
172
|
+
handleSlots &&
|
|
173
|
+
tag === 'slot' &&
|
|
174
|
+
typeof node.attributes.primary === 'string'
|
|
175
|
+
)
|
|
176
|
+
parent.slots[node.attributes.primary] = node;
|
|
177
|
+
else parent.push(node);
|
|
178
|
+
|
|
156
179
|
if (token.nesting > 0) nodes.push(node);
|
|
157
180
|
|
|
158
181
|
if (!Array.isArray(token.children)) return;
|
|
159
182
|
|
|
160
|
-
inlineParent = parent;
|
|
183
|
+
if (node.type === 'inline') inlineParent = parent;
|
|
161
184
|
|
|
162
185
|
nodes.push(node);
|
|
163
186
|
|
|
164
187
|
const isLeafNode = typeName === 'image';
|
|
165
188
|
if (!isLeafNode) {
|
|
166
189
|
for (const child of token.children)
|
|
167
|
-
handleToken(child, nodes, file, inlineParent);
|
|
190
|
+
handleToken(child, nodes, file, handleSlots, inlineParent);
|
|
168
191
|
}
|
|
169
192
|
|
|
170
193
|
nodes.pop();
|
|
171
194
|
}
|
|
172
195
|
|
|
173
|
-
export default function parser(tokens: Token[],
|
|
196
|
+
export default function parser(tokens: Token[], args?: string | ParserArgs) {
|
|
174
197
|
const doc = new Node('document');
|
|
175
198
|
const nodes = [doc];
|
|
176
199
|
|
|
177
|
-
|
|
200
|
+
if (typeof args === 'string') args = { file: args };
|
|
201
|
+
|
|
202
|
+
for (const token of tokens)
|
|
203
|
+
handleToken(token, nodes, args?.file, args?.slots);
|
|
178
204
|
|
|
179
205
|
if (nodes.length > 1)
|
|
180
206
|
for (const node of nodes.slice(1))
|
|
@@ -54,6 +54,13 @@ describe('HTML renderer', function () {
|
|
|
54
54
|
expect(html).toEqual('<p>1</p>');
|
|
55
55
|
});
|
|
56
56
|
|
|
57
|
+
it('lowercase attributes', function () {
|
|
58
|
+
const content = tag('td', { colSpan: 2, rowSpan: 3 }, ['Data']);
|
|
59
|
+
|
|
60
|
+
const html = render(content);
|
|
61
|
+
expect(html).toEqual('<td colspan="2" rowspan="3">Data</td>');
|
|
62
|
+
});
|
|
63
|
+
|
|
57
64
|
describe('attributes', function () {
|
|
58
65
|
it('with basic value', function () {
|
|
59
66
|
const example = tag('foo', { bar: 'baz' });
|
package/src/renderers/html.ts
CHANGED
|
@@ -36,7 +36,7 @@ export default function render(node: RenderableTreeNodes): string {
|
|
|
36
36
|
|
|
37
37
|
let output = `<${name}`;
|
|
38
38
|
for (const [k, v] of Object.entries(attributes ?? {}))
|
|
39
|
-
output += ` ${k}="${escapeHtml(String(v))}"`;
|
|
39
|
+
output += ` ${k.toLowerCase()}="${escapeHtml(String(v))}"`;
|
|
40
40
|
output += '>';
|
|
41
41
|
|
|
42
42
|
if (voidElements.has(name)) return output;
|
|
@@ -1,14 +1,18 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
Config,
|
|
3
|
+
CustomAttributeTypeInterface,
|
|
4
|
+
ValidationError,
|
|
5
|
+
} from '../types';
|
|
2
6
|
|
|
3
7
|
export class Class implements CustomAttributeTypeInterface {
|
|
4
|
-
validate(value: any): ValidationError[] {
|
|
8
|
+
validate(value: any, _config: Config, key: string): ValidationError[] {
|
|
5
9
|
if (typeof value === 'string' || typeof value === 'object') return [];
|
|
6
10
|
|
|
7
11
|
return [
|
|
8
12
|
{
|
|
9
13
|
id: 'attribute-type-invalid',
|
|
10
14
|
level: 'error',
|
|
11
|
-
message: `Attribute '
|
|
15
|
+
message: `Attribute '${key}' must be type 'string | object'`,
|
|
12
16
|
},
|
|
13
17
|
];
|
|
14
18
|
}
|
package/src/schema.ts
CHANGED
|
@@ -101,6 +101,8 @@ export const list: Schema = {
|
|
|
101
101
|
children: ['item'],
|
|
102
102
|
attributes: {
|
|
103
103
|
ordered: { type: Boolean, render: false, required: true },
|
|
104
|
+
start: { type: Number },
|
|
105
|
+
marker: { type: String, render: false },
|
|
104
106
|
},
|
|
105
107
|
transform(node, config) {
|
|
106
108
|
return new Tag(
|
|
@@ -134,9 +136,9 @@ export const td: Schema = {
|
|
|
134
136
|
'hr',
|
|
135
137
|
],
|
|
136
138
|
attributes: {
|
|
137
|
-
colspan: { type: Number },
|
|
138
|
-
rowspan: { type: Number },
|
|
139
139
|
align: { type: String },
|
|
140
|
+
colspan: { type: Number, render: 'colSpan' },
|
|
141
|
+
rowspan: { type: Number, render: 'rowSpan' },
|
|
140
142
|
},
|
|
141
143
|
};
|
|
142
144
|
|
|
@@ -145,6 +147,8 @@ export const th: Schema = {
|
|
|
145
147
|
attributes: {
|
|
146
148
|
width: { type: Number },
|
|
147
149
|
align: { type: String },
|
|
150
|
+
colspan: { type: Number, render: 'colSpan' },
|
|
151
|
+
rowspan: { type: Number, render: 'rowSpan' },
|
|
148
152
|
},
|
|
149
153
|
};
|
|
150
154
|
|
|
@@ -166,11 +170,17 @@ export const thead: Schema = {
|
|
|
166
170
|
export const strong: Schema = {
|
|
167
171
|
render: 'strong',
|
|
168
172
|
children: ['em', 's', 'link', 'code', 'text', 'tag'],
|
|
173
|
+
attributes: {
|
|
174
|
+
marker: { type: String, render: false },
|
|
175
|
+
},
|
|
169
176
|
};
|
|
170
177
|
|
|
171
178
|
export const em: Schema = {
|
|
172
179
|
render: 'em',
|
|
173
180
|
children: ['strong', 's', 'link', 'code', 'text', 'tag'],
|
|
181
|
+
attributes: {
|
|
182
|
+
marker: { type: String, render: false },
|
|
183
|
+
},
|
|
174
184
|
};
|
|
175
185
|
|
|
176
186
|
export const s: Schema = {
|
package/src/tags/index.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { tagIf, tagElse } from './conditional';
|
|
2
2
|
import { partial } from './partial';
|
|
3
3
|
import { table } from './table';
|
|
4
|
+
import { slot } from './slot';
|
|
4
5
|
|
|
5
6
|
export default {
|
|
6
|
-
if: tagIf,
|
|
7
7
|
else: tagElse,
|
|
8
|
+
if: tagIf,
|
|
8
9
|
partial,
|
|
10
|
+
slot,
|
|
9
11
|
table,
|
|
10
12
|
};
|