@markdoc/markdoc 0.5.12 → 0.5.14
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/README.md +35 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +472 -448
- package/dist/index.js.map +4 -4
- package/dist/index.mjs +472 -448
- package/dist/index.mjs.map +4 -4
- package/dist/react.js +4 -4
- package/dist/react.js.map +2 -2
- package/dist/src/ast/base.d.ts +1 -1
- package/dist/src/ast/base.d.ts.map +1 -1
- package/dist/src/ast/function.d.ts +1 -1
- package/dist/src/ast/function.d.ts.map +1 -1
- package/dist/src/ast/index.d.ts +2 -2
- package/dist/src/ast/index.d.ts.map +1 -1
- package/dist/src/ast/node.d.ts.map +1 -1
- package/dist/src/ast/variable.d.ts +1 -1
- package/dist/src/ast/variable.d.ts.map +1 -1
- package/dist/src/formatter.d.ts.map +1 -1
- package/dist/src/functions/index.d.ts.map +1 -1
- package/dist/src/parser.d.ts.map +1 -1
- package/dist/src/renderers/html.d.ts.map +1 -1
- package/dist/src/renderers/react/react.d.ts +2 -2
- package/dist/src/renderers/react/react.d.ts.map +1 -1
- package/dist/src/renderers/react/static.d.ts +1 -1
- package/dist/src/renderers/react/static.d.ts.map +1 -1
- package/dist/src/schema-types/class.d.ts +1 -1
- package/dist/src/schema-types/class.d.ts.map +1 -1
- package/dist/src/schema-types/conditional.d.ts +1 -1
- package/dist/src/schema-types/conditional.d.ts.map +1 -1
- package/dist/src/schema.d.ts.map +1 -1
- package/dist/src/tags/conditional.d.ts +1 -1
- package/dist/src/tags/conditional.d.ts.map +1 -1
- package/dist/src/tokenizer/index.d.ts.map +1 -1
- package/dist/src/tokenizer/plugins/annotations.d.ts.map +1 -1
- package/dist/src/transformer.d.ts.map +1 -1
- package/dist/src/validator.d.ts +1 -1
- package/dist/src/validator.d.ts.map +1 -1
- package/index.ts +21 -22
- package/package.json +9 -14
- package/spec/support/matchers.d.ts +6 -4
- package/src/ast/base.ts +21 -6
- package/src/ast/function.ts +1 -1
- package/src/ast/index.ts +3 -4
- package/src/ast/node.test.ts +126 -36
- package/src/ast/node.ts +4 -4
- package/src/ast/variable.test.ts +5 -4
- package/src/ast/variable.ts +1 -1
- package/src/formatter.test.ts +4 -3
- package/src/formatter.ts +11 -11
- package/src/functions/index.test.ts +8 -7
- package/src/functions/index.ts +1 -1
- package/src/grammar/tag.d.ts +1 -1
- package/src/grammar/tag.js +208 -285
- package/src/grammar/tag.pegjs +8 -8
- package/src/grammar/tag.test.ts +80 -48
- package/src/parser.test.ts +124 -66
- package/src/parser.ts +29 -7
- package/src/renderers/html.test.ts +56 -16
- package/src/renderers/html.ts +10 -2
- package/src/renderers/react/react.test.ts +39 -27
- package/src/renderers/react/react.ts +9 -10
- package/src/renderers/react/static.ts +5 -6
- package/src/schema-types/class.ts +1 -1
- package/src/schema-types/conditional.ts +1 -1
- package/src/schema.ts +9 -4
- package/src/tag.test.ts +2 -1
- package/src/tag.ts +1 -1
- package/src/tags/conditional.ts +4 -4
- package/src/tags/index.ts +2 -2
- package/src/tags/partial.ts +1 -1
- package/src/tokenizer/index.ts +6 -5
- package/src/tokenizer/plugins/annotations.test.ts +29 -28
- package/src/tokenizer/plugins/annotations.ts +12 -12
- package/src/tokenizer/plugins/comments.test.ts +11 -10
- package/src/tokenizer/plugins/comments.ts +2 -2
- package/src/tokenizer/plugins/frontmatter.test.ts +7 -6
- package/src/tokenizer/plugins/frontmatter.ts +2 -2
- package/src/tokenizer/plugins/link.test.ts +14 -13
- package/src/transformer.ts +11 -7
- package/src/transforms/table.test.ts +18 -17
- package/src/transforms/table.ts +1 -1
- package/src/utils.test.ts +23 -22
- package/src/utils.ts +3 -3
- package/src/validator.test.ts +205 -31
- package/src/validator.ts +21 -13
|
@@ -1,33 +1,35 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import Markdoc from '../../index';
|
|
3
3
|
import Tag from '../tag';
|
|
4
|
+
import type { RenderableTreeNode } from '../types';
|
|
5
|
+
import render from './html';
|
|
4
6
|
|
|
5
7
|
function tag(
|
|
6
8
|
name: string,
|
|
7
9
|
attributes: Record<string, any> = {},
|
|
8
|
-
children: RenderableTreeNode[] = []
|
|
10
|
+
children: RenderableTreeNode[] = [],
|
|
9
11
|
) {
|
|
10
12
|
return new Tag(name, attributes, children);
|
|
11
13
|
}
|
|
12
14
|
|
|
13
|
-
describe('HTML renderer',
|
|
14
|
-
it('rendering a tag',
|
|
15
|
+
describe('HTML renderer', () => {
|
|
16
|
+
it('rendering a tag', () => {
|
|
15
17
|
const example = render(tag('h1', undefined, ['test'])).trim();
|
|
16
18
|
expect(example).toEqual('<h1>test</h1>');
|
|
17
19
|
});
|
|
18
20
|
|
|
19
|
-
it('rendering string child nodes',
|
|
21
|
+
it('rendering string child nodes', () => {
|
|
20
22
|
const example = tag('h1', undefined, ['test ', '1']);
|
|
21
23
|
expect(render(example)).toEqual('<h1>test 1</h1>');
|
|
22
24
|
});
|
|
23
25
|
|
|
24
|
-
it('rendering nested tags',
|
|
26
|
+
it('rendering nested tags', () => {
|
|
25
27
|
const example = tag('div', undefined, [tag('p', undefined, ['test'])]);
|
|
26
28
|
|
|
27
29
|
expect(render(example)).toEqual('<div><p>test</p></div>');
|
|
28
30
|
});
|
|
29
31
|
|
|
30
|
-
it('rendering parallel tags',
|
|
32
|
+
it('rendering parallel tags', () => {
|
|
31
33
|
const example = [
|
|
32
34
|
tag('p', undefined, ['foo']),
|
|
33
35
|
tag('p', undefined, ['bar']),
|
|
@@ -36,45 +38,83 @@ describe('HTML renderer', function () {
|
|
|
36
38
|
expect(render(example)).toEqual('<p>foo</p><p>bar</p>');
|
|
37
39
|
});
|
|
38
40
|
|
|
39
|
-
it('rendering a tag with an invalid child',
|
|
41
|
+
it('rendering a tag with an invalid child', () => {
|
|
40
42
|
const example = tag('div', undefined, ['test', { foo: 'bar' }]);
|
|
41
43
|
|
|
42
44
|
expect(render(example)).toEqual('<div>test</div>');
|
|
43
45
|
});
|
|
44
46
|
|
|
45
|
-
it('rendering a void element',
|
|
47
|
+
it('rendering a void element', () => {
|
|
46
48
|
const example = tag('hr');
|
|
47
49
|
expect(render(example)).toEqual('<hr>');
|
|
48
50
|
});
|
|
49
51
|
|
|
50
|
-
it('rendering a tag with numeric children',
|
|
52
|
+
it('rendering a tag with numeric children', () => {
|
|
51
53
|
const content = tag('p', {}, [1]);
|
|
52
54
|
|
|
53
55
|
const html = render(content);
|
|
54
56
|
expect(html).toEqual('<p>1</p>');
|
|
55
57
|
});
|
|
56
58
|
|
|
57
|
-
it('lowercase attributes',
|
|
59
|
+
it('lowercase attributes', () => {
|
|
58
60
|
const content = tag('td', { colSpan: 2, rowSpan: 3 }, ['Data']);
|
|
59
61
|
|
|
60
62
|
const html = render(content);
|
|
61
63
|
expect(html).toEqual('<td colspan="2" rowspan="3">Data</td>');
|
|
62
64
|
});
|
|
63
65
|
|
|
64
|
-
describe('attributes',
|
|
65
|
-
it('with basic value',
|
|
66
|
+
describe('attributes', () => {
|
|
67
|
+
it('with basic value', () => {
|
|
66
68
|
const example = tag('foo', { bar: 'baz' });
|
|
67
69
|
expect(render(example)).toEqual('<foo bar="baz"></foo>');
|
|
68
70
|
});
|
|
69
71
|
|
|
70
|
-
it('with an id attribute',
|
|
72
|
+
it('with an id attribute', () => {
|
|
71
73
|
const example = tag('h1', { id: 'foo', test: 'bar' }, ['test']);
|
|
72
74
|
expect(render(example)).toEqual('<h1 id="foo" test="bar">test</h1>');
|
|
73
75
|
});
|
|
74
76
|
|
|
75
|
-
it('with a number attribute value',
|
|
77
|
+
it('with a number attribute value', () => {
|
|
76
78
|
const example = tag('h1', { 'data-foo': 42 }, ['test']);
|
|
77
79
|
expect(render(example)).toEqual('<h1 data-foo="42">test</h1>');
|
|
78
80
|
});
|
|
81
|
+
|
|
82
|
+
it('with null and undefined attribute values', () => {
|
|
83
|
+
const example = tag('p', { a: null, b: undefined, c: false }, ['test']);
|
|
84
|
+
expect(render(example)).toEqual('<p c="false">test</p>');
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('with an invalid attribute name', () => {
|
|
88
|
+
const example = tag('p', { 'a>b': 1, 'x y': 2, 'a=b': 3, ok: 4 }, [
|
|
89
|
+
'test',
|
|
90
|
+
]);
|
|
91
|
+
expect(render(example)).toEqual('<p ok="4">test</p>');
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
describe('tag names', () => {
|
|
96
|
+
it('with a custom element name', () => {
|
|
97
|
+
const example = tag('my-element', undefined, ['test']);
|
|
98
|
+
expect(render(example)).toEqual('<my-element>test</my-element>');
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('with an invalid tag name', () => {
|
|
102
|
+
const names = ['h1>x', 'a b', '1a', 'a/b'];
|
|
103
|
+
for (const name of names) {
|
|
104
|
+
const example = tag(name, { id: 'foo' }, ['test']);
|
|
105
|
+
expect(render(example)).toEqual('test');
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('with an invalid nested tag name', () => {
|
|
110
|
+
const example = tag('div', undefined, [tag('p>x', undefined, ['test'])]);
|
|
111
|
+
expect(render(example)).toEqual('<div>test</div>');
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('rendering a heading with an invalid level annotation', () => {
|
|
116
|
+
const source = '# Heading {% level="1>x" %}';
|
|
117
|
+
const html = render(Markdoc.transform(Markdoc.parse(source)));
|
|
118
|
+
expect(html).toEqual('<article><h1>Heading </h1></article>');
|
|
79
119
|
});
|
|
80
120
|
});
|
package/src/renderers/html.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import MarkdownIt from 'markdown-it';
|
|
2
2
|
import Tag from '../tag';
|
|
3
3
|
import type { RenderableTreeNodes } from '../types';
|
|
4
|
+
|
|
4
5
|
const { escapeHtml } = MarkdownIt().utils;
|
|
5
6
|
|
|
6
7
|
// HTML elements that do not have a matching close tag
|
|
@@ -22,6 +23,12 @@ const voidElements = new Set([
|
|
|
22
23
|
'wbr',
|
|
23
24
|
]);
|
|
24
25
|
|
|
26
|
+
// Names are written into the markup verbatim, so reject anything that would
|
|
27
|
+
// end the tag or attribute early. Based on the HTML tokenizer's tag name and
|
|
28
|
+
// attribute name states: https://html.spec.whatwg.org/#tokenization
|
|
29
|
+
const tagName = /^[a-zA-Z][^\s/>\0]*$/;
|
|
30
|
+
const attributeName = /^[^\s"'>/=\0]+$/;
|
|
31
|
+
|
|
25
32
|
export default function render(node: RenderableTreeNodes): string {
|
|
26
33
|
if (typeof node === 'string' || typeof node === 'number')
|
|
27
34
|
return escapeHtml(String(node));
|
|
@@ -32,11 +39,12 @@ export default function render(node: RenderableTreeNodes): string {
|
|
|
32
39
|
|
|
33
40
|
const { name, attributes, children = [] } = node;
|
|
34
41
|
|
|
35
|
-
if (!name) return render(children);
|
|
42
|
+
if (!name || !tagName.test(name)) return render(children);
|
|
36
43
|
|
|
37
44
|
let output = `<${name}`;
|
|
38
45
|
for (const [k, v] of Object.entries(attributes ?? {}))
|
|
39
|
-
|
|
46
|
+
if (v != null && attributeName.test(k))
|
|
47
|
+
output += ` ${k.toLowerCase()}="${escapeHtml(String(v))}"`;
|
|
40
48
|
output += '>';
|
|
41
49
|
|
|
42
50
|
if (voidElements.has(name)) return output;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import Tag from '../../tag';
|
|
1
3
|
import dynamic from './react';
|
|
2
4
|
import renderStatic from './static';
|
|
3
|
-
import Tag from '../../tag';
|
|
4
5
|
|
|
5
6
|
const React = {
|
|
6
7
|
Fragment: 'fragment',
|
|
@@ -9,8 +10,19 @@ const React = {
|
|
|
9
10
|
},
|
|
10
11
|
};
|
|
11
12
|
|
|
12
|
-
describe('React dynamic renderer',
|
|
13
|
-
it('
|
|
13
|
+
describe('React dynamic renderer', () => {
|
|
14
|
+
it('preserves __proto__ as data without changing the props prototype', () => {
|
|
15
|
+
const attributes = Object.fromEntries([
|
|
16
|
+
['__proto__', { src: 'javascript:alert(1)' }],
|
|
17
|
+
['title', 'example'],
|
|
18
|
+
]);
|
|
19
|
+
const output: any = dynamic(new Tag('iframe', attributes), React);
|
|
20
|
+
expect(Object.getPrototypeOf(output.attributes)).toBe(Object.prototype);
|
|
21
|
+
expect(Object.keys(output.attributes)).toEqual(['__proto__', 'title']);
|
|
22
|
+
expect(output.attributes.src).toBeUndefined();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('rendering a null node', () => {
|
|
14
26
|
const example = { name: 'h1', children: ['test', null] };
|
|
15
27
|
const output = dynamic(example, React);
|
|
16
28
|
expect(output).toDeepEqual({
|
|
@@ -20,19 +32,19 @@ describe('React dynamic renderer', function () {
|
|
|
20
32
|
});
|
|
21
33
|
});
|
|
22
34
|
|
|
23
|
-
it('rendering a tag',
|
|
35
|
+
it('rendering a tag', () => {
|
|
24
36
|
const example = { name: 'h1', children: ['test'] };
|
|
25
37
|
const output = dynamic(example, React);
|
|
26
38
|
expect(output).toDeepEqualSubset(example);
|
|
27
39
|
});
|
|
28
40
|
|
|
29
|
-
it('rendering string and number child nodes',
|
|
41
|
+
it('rendering string and number child nodes', () => {
|
|
30
42
|
const example = { name: 'h1', children: ['test ', 1] };
|
|
31
43
|
const output = dynamic(example, React);
|
|
32
44
|
expect(output).toDeepEqualSubset(example);
|
|
33
45
|
});
|
|
34
46
|
|
|
35
|
-
it('rendering nested tags',
|
|
47
|
+
it('rendering nested tags', () => {
|
|
36
48
|
const example = {
|
|
37
49
|
name: 'div',
|
|
38
50
|
children: [{ name: 'p', children: ['test'] }],
|
|
@@ -41,7 +53,7 @@ describe('React dynamic renderer', function () {
|
|
|
41
53
|
expect(output).toDeepEqualSubset(example);
|
|
42
54
|
});
|
|
43
55
|
|
|
44
|
-
it('rendering parallel tags',
|
|
56
|
+
it('rendering parallel tags', () => {
|
|
45
57
|
const children = [
|
|
46
58
|
{ name: 'p', children: ['foo'] },
|
|
47
59
|
{ name: 'p', children: ['bar'] },
|
|
@@ -51,7 +63,7 @@ describe('React dynamic renderer', function () {
|
|
|
51
63
|
expect(output).toDeepEqualSubset({ name: 'fragment', children });
|
|
52
64
|
});
|
|
53
65
|
|
|
54
|
-
it('rendering an external component',
|
|
66
|
+
it('rendering an external component', () => {
|
|
55
67
|
const components = { Foo: 'bar' };
|
|
56
68
|
const example = new Tag('Foo', undefined, ['test']);
|
|
57
69
|
const output = dynamic(example, React, { components });
|
|
@@ -61,8 +73,8 @@ describe('React dynamic renderer', function () {
|
|
|
61
73
|
});
|
|
62
74
|
});
|
|
63
75
|
|
|
64
|
-
it('rendering with a custom tag resolution function',
|
|
65
|
-
function tagName(
|
|
76
|
+
it('rendering with a custom tag resolution function', () => {
|
|
77
|
+
function tagName(_name, components) {
|
|
66
78
|
return components.Foo;
|
|
67
79
|
}
|
|
68
80
|
const components = { Foo: 'bar' };
|
|
@@ -78,8 +90,8 @@ describe('React dynamic renderer', function () {
|
|
|
78
90
|
});
|
|
79
91
|
});
|
|
80
92
|
|
|
81
|
-
describe('attributes',
|
|
82
|
-
it('with an id attribute',
|
|
93
|
+
describe('attributes', () => {
|
|
94
|
+
it('with an id attribute', () => {
|
|
83
95
|
const example = {
|
|
84
96
|
name: 'h1',
|
|
85
97
|
attributes: { id: 'foo' },
|
|
@@ -90,7 +102,7 @@ describe('React dynamic renderer', function () {
|
|
|
90
102
|
expect(output).toDeepEqualSubset(example);
|
|
91
103
|
});
|
|
92
104
|
|
|
93
|
-
it('with a class attribute',
|
|
105
|
+
it('with a class attribute', () => {
|
|
94
106
|
const example = new Tag('h1', { class: 'foo bar' }, ['test']);
|
|
95
107
|
|
|
96
108
|
const output = dynamic(example, React);
|
|
@@ -101,7 +113,7 @@ describe('React dynamic renderer', function () {
|
|
|
101
113
|
});
|
|
102
114
|
});
|
|
103
115
|
|
|
104
|
-
it('with a number attribute value',
|
|
116
|
+
it('with a number attribute value', () => {
|
|
105
117
|
const example = {
|
|
106
118
|
name: 'h1',
|
|
107
119
|
attributes: { 'data-foo': 42 },
|
|
@@ -113,8 +125,8 @@ describe('React dynamic renderer', function () {
|
|
|
113
125
|
});
|
|
114
126
|
});
|
|
115
127
|
|
|
116
|
-
describe('rendering built-in nodes',
|
|
117
|
-
it('rendering a fenced code block',
|
|
128
|
+
describe('rendering built-in nodes', () => {
|
|
129
|
+
it('rendering a fenced code block', () => {
|
|
118
130
|
const example = new Tag('pre', { class: 'code code-ruby' }, ['test']);
|
|
119
131
|
|
|
120
132
|
const output = dynamic(example, React);
|
|
@@ -127,8 +139,8 @@ describe('React dynamic renderer', function () {
|
|
|
127
139
|
});
|
|
128
140
|
});
|
|
129
141
|
|
|
130
|
-
describe('React static renderer',
|
|
131
|
-
it('rendering a null node',
|
|
142
|
+
describe('React static renderer', () => {
|
|
143
|
+
it('rendering a null node', () => {
|
|
132
144
|
const example = { name: 'h1', children: ['test', null] };
|
|
133
145
|
const code = renderStatic(example);
|
|
134
146
|
const output = eval(code)();
|
|
@@ -136,14 +148,14 @@ describe('React static renderer', function () {
|
|
|
136
148
|
expect(output).toDeepEqual({ ...example, attributes: null });
|
|
137
149
|
});
|
|
138
150
|
|
|
139
|
-
it('rendering a tag',
|
|
151
|
+
it('rendering a tag', () => {
|
|
140
152
|
const example = { name: 'h1', children: ['test'] };
|
|
141
153
|
const code = renderStatic(example);
|
|
142
154
|
const output = eval(code)();
|
|
143
155
|
expect(output).toDeepEqualSubset(example);
|
|
144
156
|
});
|
|
145
157
|
|
|
146
|
-
it('rendering nested tags',
|
|
158
|
+
it('rendering nested tags', () => {
|
|
147
159
|
const example = {
|
|
148
160
|
name: 'div',
|
|
149
161
|
children: [{ name: 'p', children: ['test'] }],
|
|
@@ -153,7 +165,7 @@ describe('React static renderer', function () {
|
|
|
153
165
|
expect(output).toDeepEqualSubset(example);
|
|
154
166
|
});
|
|
155
167
|
|
|
156
|
-
it('rendering a fragment',
|
|
168
|
+
it('rendering a fragment', () => {
|
|
157
169
|
const example = [
|
|
158
170
|
{ name: 'p', children: ['foo'] },
|
|
159
171
|
{ name: 'p', children: ['bar'] },
|
|
@@ -172,7 +184,7 @@ describe('React static renderer', function () {
|
|
|
172
184
|
});
|
|
173
185
|
});
|
|
174
186
|
|
|
175
|
-
it('rendering an external component',
|
|
187
|
+
it('rendering an external component', () => {
|
|
176
188
|
const components = { Foo: 'bar' };
|
|
177
189
|
const example = new Tag('Foo', undefined, ['test']);
|
|
178
190
|
const code = renderStatic(example);
|
|
@@ -184,8 +196,8 @@ describe('React static renderer', function () {
|
|
|
184
196
|
});
|
|
185
197
|
});
|
|
186
198
|
|
|
187
|
-
it('rendering with a custom tag resolution function',
|
|
188
|
-
function tagName(
|
|
199
|
+
it('rendering with a custom tag resolution function', () => {
|
|
200
|
+
function tagName(_name, components) {
|
|
189
201
|
return components.Foo;
|
|
190
202
|
}
|
|
191
203
|
const components = { Foo: 'bar' };
|
|
@@ -199,8 +211,8 @@ describe('React static renderer', function () {
|
|
|
199
211
|
});
|
|
200
212
|
});
|
|
201
213
|
|
|
202
|
-
describe('attributes',
|
|
203
|
-
it('with an id attribute',
|
|
214
|
+
describe('attributes', () => {
|
|
215
|
+
it('with an id attribute', () => {
|
|
204
216
|
const example = {
|
|
205
217
|
name: 'h1',
|
|
206
218
|
attributes: { id: 'foo' },
|
|
@@ -213,7 +225,7 @@ describe('React static renderer', function () {
|
|
|
213
225
|
expect(output).toDeepEqualSubset(example);
|
|
214
226
|
});
|
|
215
227
|
|
|
216
|
-
it('with a number attribute value',
|
|
228
|
+
it('with a number attribute value', () => {
|
|
217
229
|
const example = {
|
|
218
230
|
name: 'h1',
|
|
219
231
|
attributes: { 'data-foo': 42 },
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
+
import type { ComponentType, createElement, Fragment, ReactNode } from 'react';
|
|
1
2
|
import Tag from '../../tag';
|
|
2
|
-
import { RenderableTreeNodes
|
|
3
|
-
|
|
4
|
-
import type { createElement, ComponentType, Fragment, ReactNode } from 'react';
|
|
3
|
+
import type { RenderableTreeNodes } from '../../types';
|
|
5
4
|
|
|
6
5
|
type ReactShape = Readonly<{
|
|
7
6
|
createElement: typeof createElement;
|
|
@@ -12,7 +11,7 @@ type Component = ComponentType<any>;
|
|
|
12
11
|
|
|
13
12
|
function tagName(
|
|
14
13
|
name: string,
|
|
15
|
-
components: Record<string, Component> | ((string: string) => Component)
|
|
14
|
+
components: Record<string, Component> | ((string: string) => Component),
|
|
16
15
|
): string | Component {
|
|
17
16
|
return typeof name !== 'string'
|
|
18
17
|
? name // This can be an object, e.g. when React.forwardRef is used
|
|
@@ -30,7 +29,7 @@ export type RenderOpts = {
|
|
|
30
29
|
export default function dynamic(
|
|
31
30
|
node: RenderableTreeNodes,
|
|
32
31
|
React: ReactShape,
|
|
33
|
-
{ components = {}, resolveTagName = tagName }: RenderOpts = {}
|
|
32
|
+
{ components = {}, resolveTagName = tagName }: RenderOpts = {},
|
|
34
33
|
) {
|
|
35
34
|
function deepRender(value: any): any {
|
|
36
35
|
if (value == null || typeof value !== 'object') return value;
|
|
@@ -41,9 +40,9 @@ export default function dynamic(
|
|
|
41
40
|
|
|
42
41
|
if (typeof value !== 'object') return value;
|
|
43
42
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
43
|
+
return Object.fromEntries(
|
|
44
|
+
Object.entries(value).map(([k, v]) => [k, deepRender(v)]),
|
|
45
|
+
);
|
|
47
46
|
}
|
|
48
47
|
|
|
49
48
|
function render(node: RenderableTreeNodes): ReactNode {
|
|
@@ -65,8 +64,8 @@ export default function dynamic(
|
|
|
65
64
|
|
|
66
65
|
return React.createElement(
|
|
67
66
|
resolveTagName(name, components),
|
|
68
|
-
Object.keys(attrs).length
|
|
69
|
-
...children.map(render)
|
|
67
|
+
Object.keys(attrs).length === 0 ? null : deepRender(attrs),
|
|
68
|
+
...children.map(render),
|
|
70
69
|
);
|
|
71
70
|
}
|
|
72
71
|
|
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
import Tag from '../../tag';
|
|
2
|
-
import { RenderableTreeNode, RenderableTreeNodes } from '../../types';
|
|
3
|
-
|
|
4
1
|
import type { ComponentType } from 'react';
|
|
2
|
+
import Tag from '../../tag';
|
|
3
|
+
import type { RenderableTreeNode, RenderableTreeNodes } from '../../types';
|
|
5
4
|
|
|
6
5
|
type Component = ComponentType<unknown>;
|
|
7
6
|
|
|
8
7
|
function tagName(
|
|
9
8
|
name: string,
|
|
10
|
-
components: Record<string, Component> | ((string: string) => Component)
|
|
9
|
+
components: Record<string, Component> | ((string: string) => Component),
|
|
11
10
|
): string | Component {
|
|
12
11
|
return typeof name !== 'string'
|
|
13
12
|
? 'Fragment'
|
|
@@ -56,13 +55,13 @@ function render(node: RenderableTreeNodes): string {
|
|
|
56
55
|
|
|
57
56
|
return `React.createElement(
|
|
58
57
|
tagName(${JSON.stringify(name)}, components),
|
|
59
|
-
${Object.keys(attrs).length
|
|
58
|
+
${Object.keys(attrs).length === 0 ? 'null' : deepRender(attrs)},
|
|
60
59
|
${renderArray(children)})`;
|
|
61
60
|
}
|
|
62
61
|
|
|
63
62
|
export default function reactStatic(
|
|
64
63
|
node: RenderableTreeNodes,
|
|
65
|
-
{ resolveTagName = tagName }: { resolveTagName?: typeof tagName } = {}
|
|
64
|
+
{ resolveTagName = tagName }: { resolveTagName?: typeof tagName } = {},
|
|
66
65
|
): string {
|
|
67
66
|
// the resolveTagName function *must* be called tagName
|
|
68
67
|
// throw an error if it does not
|
package/src/schema.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { Schema } from './types';
|
|
2
1
|
import Tag from './tag';
|
|
2
|
+
import type { Schema } from './types';
|
|
3
3
|
|
|
4
4
|
export const document: Schema = {
|
|
5
5
|
render: 'article',
|
|
@@ -26,10 +26,15 @@ export const heading: Schema = {
|
|
|
26
26
|
level: { type: Number, render: false, required: true },
|
|
27
27
|
},
|
|
28
28
|
transform(node, config) {
|
|
29
|
+
const level = node.attributes['level'];
|
|
30
|
+
// The level becomes part of the tag name, so only accept h1-h6. Otherwise
|
|
31
|
+
// render the children without a wrapping element.
|
|
32
|
+
if (!(Number.isInteger(level) && level >= 1 && level <= 6))
|
|
33
|
+
return node.transformChildren(config);
|
|
29
34
|
return new Tag(
|
|
30
|
-
`h${
|
|
35
|
+
`h${level}`,
|
|
31
36
|
node.transformAttributes(config),
|
|
32
|
-
node.transformChildren(config)
|
|
37
|
+
node.transformChildren(config),
|
|
33
38
|
);
|
|
34
39
|
},
|
|
35
40
|
};
|
|
@@ -108,7 +113,7 @@ export const list: Schema = {
|
|
|
108
113
|
return new Tag(
|
|
109
114
|
node.attributes.ordered ? 'ol' : 'ul',
|
|
110
115
|
node.transformAttributes(config),
|
|
111
|
-
node.transformChildren(config)
|
|
116
|
+
node.transformChildren(config),
|
|
112
117
|
);
|
|
113
118
|
},
|
|
114
119
|
};
|
package/src/tag.test.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
1
2
|
import Tag from './tag';
|
|
2
3
|
|
|
3
|
-
describe('Tag.isTag',
|
|
4
|
+
describe('Tag.isTag', () => {
|
|
4
5
|
it('should detect when value is not a tag', () => {
|
|
5
6
|
expect(Tag.isTag(undefined)).toBe(false);
|
|
6
7
|
expect(Tag.isTag(null)).toBe(false);
|
package/src/tag.ts
CHANGED
package/src/tags/conditional.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ConditionalAttributeType } from '../schema-types/conditional';
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import type {
|
|
4
4
|
MaybePromise,
|
|
5
5
|
Node,
|
|
6
6
|
RenderableTreeNode,
|
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
Schema,
|
|
9
9
|
Value,
|
|
10
10
|
} from '../types';
|
|
11
|
-
import {
|
|
11
|
+
import { isPromise } from '../utils';
|
|
12
12
|
|
|
13
13
|
type Condition = { condition: Value; children: Node[] };
|
|
14
14
|
|
|
@@ -43,7 +43,7 @@ export const tagIf: Schema = {
|
|
|
43
43
|
for (const { condition, children } of conditions)
|
|
44
44
|
if (truthy(condition)) {
|
|
45
45
|
const nodes = children.flatMap<MaybePromise<RenderableTreeNodes>>(
|
|
46
|
-
(child) => child.transform(config)
|
|
46
|
+
(child) => child.transform(config),
|
|
47
47
|
);
|
|
48
48
|
if (nodes.some(isPromise)) {
|
|
49
49
|
return Promise.all(nodes).then((nodes) => nodes.flat());
|
package/src/tags/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { tagElse, tagIf } from './conditional';
|
|
2
2
|
import { partial } from './partial';
|
|
3
|
-
import { table } from './table';
|
|
4
3
|
import { slot } from './slot';
|
|
4
|
+
import { table } from './table';
|
|
5
5
|
|
|
6
6
|
export default {
|
|
7
7
|
else: tagElse,
|
package/src/tags/partial.ts
CHANGED
package/src/tokenizer/index.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import MarkdownIt from 'markdown-it';
|
|
2
1
|
import type { MarkdownIt as MarkdownItParser } from 'markdown-it';
|
|
2
|
+
import MarkdownIt from 'markdown-it';
|
|
3
|
+
import type { Token } from '../types';
|
|
3
4
|
import annotations from './plugins/annotations';
|
|
4
|
-
import frontmatter from './plugins/frontmatter';
|
|
5
5
|
import comments from './plugins/comments';
|
|
6
|
+
import frontmatter from './plugins/frontmatter';
|
|
6
7
|
import link from './plugins/link';
|
|
7
|
-
import type { Token } from '../types';
|
|
8
8
|
|
|
9
9
|
export type LinkPluginOptions = { validatedProtocols: string[] };
|
|
10
10
|
|
|
@@ -19,7 +19,8 @@ export type TokenizerOptions = {
|
|
|
19
19
|
typographer?: boolean;
|
|
20
20
|
quotes?: string | string[];
|
|
21
21
|
highlight?:
|
|
22
|
-
((content: string, language: string, attributes: string) => string)
|
|
22
|
+
| ((content: string, language: string, attributes: string) => string)
|
|
23
|
+
| null;
|
|
23
24
|
maxNesting?: number;
|
|
24
25
|
allowIndentation?: boolean;
|
|
25
26
|
allowComments?: boolean;
|
|
@@ -48,7 +49,7 @@ export default class Tokenizer {
|
|
|
48
49
|
link,
|
|
49
50
|
config.linkValidationOptions ?? {
|
|
50
51
|
validatedProtocols: ['http', 'https'],
|
|
51
|
-
}
|
|
52
|
+
},
|
|
52
53
|
);
|
|
53
54
|
}
|
|
54
55
|
}
|