@markdoc/markdoc 0.1.0
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/LICENSE +21 -0
- package/README.md +75 -0
- package/dist/index.d.ts +120 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7961 -0
- package/dist/index.js.map +7 -0
- package/dist/index.mjs +7959 -0
- package/dist/index.mjs.map +7 -0
- package/dist/react.js +40 -0
- package/dist/react.js.map +7 -0
- package/dist/src/ast/base.d.ts +6 -0
- package/dist/src/ast/base.d.ts.map +1 -0
- package/dist/src/ast/function.d.ts +9 -0
- package/dist/src/ast/function.d.ts.map +1 -0
- package/dist/src/ast/index.d.ts +26 -0
- package/dist/src/ast/index.d.ts.map +1 -0
- package/dist/src/ast/node.d.ts +21 -0
- package/dist/src/ast/node.d.ts.map +1 -0
- package/dist/src/ast/tag.d.ts +9 -0
- package/dist/src/ast/tag.d.ts.map +1 -0
- package/dist/src/ast/variable.d.ts +8 -0
- package/dist/src/ast/variable.d.ts.map +1 -0
- package/dist/src/functions/index.d.ts +11 -0
- package/dist/src/functions/index.d.ts.map +1 -0
- package/dist/src/parser.d.ts +4 -0
- package/dist/src/parser.d.ts.map +1 -0
- package/dist/src/renderers/html.d.ts +3 -0
- package/dist/src/renderers/html.d.ts.map +1 -0
- package/dist/src/renderers/index.d.ts +10 -0
- package/dist/src/renderers/index.d.ts.map +1 -0
- package/dist/src/renderers/react/react.d.ts +11 -0
- package/dist/src/renderers/react/react.d.ts.map +1 -0
- package/dist/src/renderers/react/shared.d.ts +5 -0
- package/dist/src/renderers/react/shared.d.ts.map +1 -0
- package/dist/src/renderers/react/static.d.ts +3 -0
- package/dist/src/renderers/react/static.d.ts.map +1 -0
- package/dist/src/schema-types/class.d.ts +6 -0
- package/dist/src/schema-types/class.d.ts.map +1 -0
- package/dist/src/schema.d.ts +27 -0
- package/dist/src/schema.d.ts.map +1 -0
- package/dist/src/tags/conditional.d.ts +5 -0
- package/dist/src/tags/conditional.d.ts.map +1 -0
- package/dist/src/tags/index.d.ts +44 -0
- package/dist/src/tags/index.d.ts.map +1 -0
- package/dist/src/tags/partial.d.ts +3 -0
- package/dist/src/tags/partial.d.ts.map +1 -0
- package/dist/src/tags/table.d.ts +3 -0
- package/dist/src/tags/table.d.ts.map +1 -0
- package/dist/src/tokenizer/index.d.ts +10 -0
- package/dist/src/tokenizer/index.d.ts.map +1 -0
- package/dist/src/tokenizer/plugins/annotations.d.ts +3 -0
- package/dist/src/tokenizer/plugins/annotations.d.ts.map +1 -0
- package/dist/src/tokenizer/plugins/frontmatter.d.ts +3 -0
- package/dist/src/tokenizer/plugins/frontmatter.d.ts.map +1 -0
- package/dist/src/transformer.d.ts +12 -0
- package/dist/src/transformer.d.ts.map +1 -0
- package/dist/src/transforms/index.d.ts +4 -0
- package/dist/src/transforms/index.d.ts.map +1 -0
- package/dist/src/transforms/table.d.ts +3 -0
- package/dist/src/transforms/table.d.ts.map +1 -0
- package/dist/src/types.d.ts +88 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/utils.d.ts +6 -0
- package/dist/src/utils.d.ts.map +1 -0
- package/dist/src/validator.d.ts +6 -0
- package/dist/src/validator.d.ts.map +1 -0
- package/global.d.ts +11 -0
- package/index.ts +138 -0
- package/package.json +63 -0
- package/spec/support/matchers.d.ts +6 -0
- package/src/ast/base.ts +41 -0
- package/src/ast/function.ts +22 -0
- package/src/ast/index.ts +28 -0
- package/src/ast/node.test.ts +307 -0
- package/src/ast/node.ts +71 -0
- package/src/ast/tag.ts +19 -0
- package/src/ast/variable.test.ts +35 -0
- package/src/ast/variable.ts +17 -0
- package/src/functions/index.test.ts +34 -0
- package/src/functions/index.ts +45 -0
- package/src/grammar/tag.d.ts +23 -0
- package/src/grammar/tag.js +1836 -0
- package/src/grammar/tag.pegjs +170 -0
- package/src/grammar/tag.test.ts +364 -0
- package/src/parser.test.ts +597 -0
- package/src/parser.ts +158 -0
- package/src/renderers/html.test.ts +62 -0
- package/src/renderers/html.ts +46 -0
- package/src/renderers/index.ts +5 -0
- package/src/renderers/react/react.test.ts +204 -0
- package/src/renderers/react/react.ts +51 -0
- package/src/renderers/react/shared.ts +16 -0
- package/src/renderers/react/static.ts +52 -0
- package/src/schema-types/class.ts +23 -0
- package/src/schema.ts +233 -0
- package/src/tags/conditional.ts +45 -0
- package/src/tags/index.ts +10 -0
- package/src/tags/partial.ts +33 -0
- package/src/tags/table.ts +5 -0
- package/src/tokenizer/index.ts +21 -0
- package/src/tokenizer/plugins/annotations.test.ts +337 -0
- package/src/tokenizer/plugins/annotations.ts +133 -0
- package/src/tokenizer/plugins/frontmatter.test.ts +86 -0
- package/src/tokenizer/plugins/frontmatter.ts +39 -0
- package/src/transformer.ts +72 -0
- package/src/transforms/index.ts +2 -0
- package/src/transforms/table.ts +51 -0
- package/src/types.ts +142 -0
- package/src/utils.test.ts +173 -0
- package/src/utils.ts +116 -0
- package/src/validator.test.ts +298 -0
- package/src/validator.ts +233 -0
package/src/parser.ts
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import Node from './ast/node';
|
|
2
|
+
import transforms from './transforms/index';
|
|
3
|
+
|
|
4
|
+
import type { AttributeValue } from './types';
|
|
5
|
+
|
|
6
|
+
import type Token from 'markdown-it/lib/token';
|
|
7
|
+
|
|
8
|
+
const mappings: Record<string, string> = {
|
|
9
|
+
ordered_list: 'list',
|
|
10
|
+
bullet_list: 'list',
|
|
11
|
+
code_inline: 'code',
|
|
12
|
+
list_item: 'item',
|
|
13
|
+
variable: 'text',
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
function annotate(node: Node, attributes: AttributeValue[]) {
|
|
17
|
+
for (const { name, value, type } of attributes)
|
|
18
|
+
if (type === 'attribute') node.attributes[name] = value;
|
|
19
|
+
else if (type === 'class')
|
|
20
|
+
if (node.attributes.class) node.attributes.class[name] = value;
|
|
21
|
+
else node.attributes.class = { [name]: value };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function handleAttrs(token: Token, type: string) {
|
|
25
|
+
switch (type) {
|
|
26
|
+
case 'heading':
|
|
27
|
+
return { level: Number(token.tag.replace('h', '')) };
|
|
28
|
+
case 'list':
|
|
29
|
+
return { ordered: token.type.startsWith('ordered') };
|
|
30
|
+
case 'link': {
|
|
31
|
+
const attrs = Object.fromEntries(token.attrs);
|
|
32
|
+
return { href: attrs.href };
|
|
33
|
+
}
|
|
34
|
+
case 'image': {
|
|
35
|
+
const attrs = Object.fromEntries(token.attrs);
|
|
36
|
+
return { alt: token.content, src: attrs.src };
|
|
37
|
+
}
|
|
38
|
+
case 'text':
|
|
39
|
+
case 'code':
|
|
40
|
+
return { content: (token.meta || {}).variable || token.content };
|
|
41
|
+
case 'fence': {
|
|
42
|
+
const [language] = token.info.split(' ', 1);
|
|
43
|
+
return language === ''
|
|
44
|
+
? { content: token.content }
|
|
45
|
+
: { content: token.content, language };
|
|
46
|
+
}
|
|
47
|
+
default:
|
|
48
|
+
return {};
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function handleToken(
|
|
53
|
+
token: Token,
|
|
54
|
+
nodes: Node[],
|
|
55
|
+
file?: string,
|
|
56
|
+
inlineParent?: Node
|
|
57
|
+
) {
|
|
58
|
+
if (token.type === 'frontmatter') {
|
|
59
|
+
nodes[0].attributes.frontmatter = token.content;
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (token.hidden || (token.type === 'text' && token.content === '')) return;
|
|
64
|
+
|
|
65
|
+
const errors = token.errors || [];
|
|
66
|
+
const parent = nodes[nodes.length - 1];
|
|
67
|
+
const { tag, attributes, error } = token.meta || {};
|
|
68
|
+
|
|
69
|
+
if (token.type === 'annotation') {
|
|
70
|
+
if (inlineParent) return annotate(inlineParent, attributes);
|
|
71
|
+
|
|
72
|
+
return parent.errors.push({
|
|
73
|
+
id: 'no-inline-annotations',
|
|
74
|
+
level: 'error',
|
|
75
|
+
message: `Can't apply inline annotations to '${parent.type}'`,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
let typeName = token.type.replace(/_(open|close)$/, '');
|
|
80
|
+
if (mappings[typeName]) typeName = mappings[typeName];
|
|
81
|
+
|
|
82
|
+
if (typeName === 'error') {
|
|
83
|
+
const { message, location } = error;
|
|
84
|
+
errors.push({ id: 'parse-error', level: 'critical', message, location });
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (token.nesting < 0) {
|
|
88
|
+
if (parent.type === typeName) {
|
|
89
|
+
if (parent.lines && token.map) parent.lines.push(...token.map);
|
|
90
|
+
return nodes.pop();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
errors.push({
|
|
94
|
+
id: 'missing-opening',
|
|
95
|
+
level: 'critical',
|
|
96
|
+
message: `Node '${typeName}' is missing opening`,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const attrs = handleAttrs(token, typeName);
|
|
101
|
+
const node = new Node(typeName, attrs, undefined, tag || undefined);
|
|
102
|
+
const { position = {} } = token;
|
|
103
|
+
|
|
104
|
+
node.errors = errors;
|
|
105
|
+
node.lines = token.map || parent.lines || [];
|
|
106
|
+
node.location = {
|
|
107
|
+
file,
|
|
108
|
+
start: {
|
|
109
|
+
line: node.lines[0],
|
|
110
|
+
character: position.start,
|
|
111
|
+
},
|
|
112
|
+
end: {
|
|
113
|
+
line: node.lines[1],
|
|
114
|
+
character: position.end,
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
if (inlineParent) node.inline = true;
|
|
119
|
+
|
|
120
|
+
if (attributes && ['tag', 'fence', 'image'].includes(typeName))
|
|
121
|
+
annotate(node, attributes);
|
|
122
|
+
|
|
123
|
+
parent.push(node);
|
|
124
|
+
if (token.nesting > 0) nodes.push(node);
|
|
125
|
+
|
|
126
|
+
if (!Array.isArray(token.children)) return;
|
|
127
|
+
|
|
128
|
+
inlineParent = parent;
|
|
129
|
+
|
|
130
|
+
nodes.push(node);
|
|
131
|
+
|
|
132
|
+
const isLeafNode = typeName === 'image';
|
|
133
|
+
if (!isLeafNode) {
|
|
134
|
+
for (const child of token.children)
|
|
135
|
+
handleToken(child, nodes, file, inlineParent);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
nodes.pop();
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export default function parser(tokens: Token[], file?: string) {
|
|
142
|
+
const doc = new Node('document');
|
|
143
|
+
const nodes = [doc];
|
|
144
|
+
|
|
145
|
+
for (const token of tokens) handleToken(token, nodes, file);
|
|
146
|
+
|
|
147
|
+
if (nodes.length > 1)
|
|
148
|
+
for (const node of nodes.slice(1))
|
|
149
|
+
node.errors.push({
|
|
150
|
+
id: 'missing-closing',
|
|
151
|
+
level: 'critical',
|
|
152
|
+
message: `Node '${node.tag || node.type}' is missing closing`,
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
for (const transform of transforms) transform(doc);
|
|
156
|
+
|
|
157
|
+
return doc;
|
|
158
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import render from './html';
|
|
2
|
+
import { RenderableTreeNode } from '../types';
|
|
3
|
+
|
|
4
|
+
function tag(
|
|
5
|
+
name: string,
|
|
6
|
+
attributes: Record<string, any> = {},
|
|
7
|
+
children: RenderableTreeNode[] = []
|
|
8
|
+
) {
|
|
9
|
+
return { name, attributes, children };
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
describe('HTML renderer', function () {
|
|
13
|
+
it('rendering a tag', function () {
|
|
14
|
+
const example = render(tag('h1', null, ['test'])).trim();
|
|
15
|
+
expect(example).toEqual('<h1>test</h1>');
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('rendering string child nodes', function () {
|
|
19
|
+
const example = tag('h1', null, ['test ', '1']);
|
|
20
|
+
expect(render(example)).toEqual('<h1>test 1</h1>');
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('rendering nested tags', function () {
|
|
24
|
+
const example = tag('div', null, [tag('p', null, ['test'])]);
|
|
25
|
+
|
|
26
|
+
expect(render(example)).toEqual('<div><p>test</p></div>');
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('rendering parallel tags', function () {
|
|
30
|
+
const example = [tag('p', null, ['foo']), tag('p', null, ['bar'])];
|
|
31
|
+
|
|
32
|
+
expect(render(example)).toEqual('<p>foo</p><p>bar</p>');
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('rendering a tag with an invalid child', function () {
|
|
36
|
+
const example = tag('div', null, ['test', { foo: 'bar' }]);
|
|
37
|
+
|
|
38
|
+
expect(render(example)).toEqual('<div>test</div>');
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('rendering a void element', function () {
|
|
42
|
+
const example = tag('hr');
|
|
43
|
+
expect(render(example)).toEqual('<hr>');
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
describe('attributes', function () {
|
|
47
|
+
it('with basic value', function () {
|
|
48
|
+
const example = tag('foo', { bar: 'baz' });
|
|
49
|
+
expect(render(example)).toEqual('<foo bar="baz"></foo>');
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('with an id attribute', function () {
|
|
53
|
+
const example = tag('h1', { id: 'foo', test: 'bar' }, ['test']);
|
|
54
|
+
expect(render(example)).toEqual('<h1 id="foo" test="bar">test</h1>');
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('with a number attribute value', function () {
|
|
58
|
+
const example = tag('h1', { 'data-foo': 42 }, ['test']);
|
|
59
|
+
expect(render(example)).toEqual('<h1 data-foo="42">test</h1>');
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import MarkdownIt from 'markdown-it';
|
|
2
|
+
import type { RenderableTreeNodes } from '../types';
|
|
3
|
+
const { escapeHtml } = MarkdownIt().utils;
|
|
4
|
+
|
|
5
|
+
// HTML elements that do not have a matching close tag
|
|
6
|
+
// Defined in the HTML standard: https://html.spec.whatwg.org/#void-elements
|
|
7
|
+
const voidElements = new Set([
|
|
8
|
+
'area',
|
|
9
|
+
'base',
|
|
10
|
+
'br',
|
|
11
|
+
'col',
|
|
12
|
+
'embed',
|
|
13
|
+
'hr',
|
|
14
|
+
'img',
|
|
15
|
+
'input',
|
|
16
|
+
'link',
|
|
17
|
+
'meta',
|
|
18
|
+
'param',
|
|
19
|
+
'source',
|
|
20
|
+
'track',
|
|
21
|
+
'wbr',
|
|
22
|
+
]);
|
|
23
|
+
|
|
24
|
+
export default function render(node: RenderableTreeNodes): string {
|
|
25
|
+
if (typeof node === 'string') return escapeHtml(node);
|
|
26
|
+
|
|
27
|
+
if (Array.isArray(node)) return node.map(render).join('');
|
|
28
|
+
|
|
29
|
+
if (node === null || typeof node !== 'object') return '';
|
|
30
|
+
|
|
31
|
+
const { name, attributes, children = [] } = node;
|
|
32
|
+
|
|
33
|
+
if (!name) return render(children);
|
|
34
|
+
|
|
35
|
+
let output = `<${name}`;
|
|
36
|
+
for (const [k, v] of Object.entries(attributes ?? {}))
|
|
37
|
+
output += ` ${k}="${escapeHtml(String(v))}"`;
|
|
38
|
+
output += '>';
|
|
39
|
+
|
|
40
|
+
if (voidElements.has(name)) return output;
|
|
41
|
+
|
|
42
|
+
if (children.length) output += render(children);
|
|
43
|
+
output += `</${name}>`;
|
|
44
|
+
|
|
45
|
+
return output;
|
|
46
|
+
}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import dynamic from './react';
|
|
2
|
+
import renderStatic from './static';
|
|
3
|
+
|
|
4
|
+
const React = {
|
|
5
|
+
Fragment: 'fragment',
|
|
6
|
+
createElement(name, attributes, ...children) {
|
|
7
|
+
return { name, attributes, children };
|
|
8
|
+
},
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
describe('React dynamic renderer', function () {
|
|
12
|
+
it('rendering a null node', function () {
|
|
13
|
+
const example = { name: 'h1', children: ['test', null] };
|
|
14
|
+
const output = dynamic(example, React);
|
|
15
|
+
expect(output).toDeepEqual({
|
|
16
|
+
name: 'h1',
|
|
17
|
+
attributes: null,
|
|
18
|
+
children: ['test', null],
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('rendering a tag', function () {
|
|
23
|
+
const example = { name: 'h1', children: ['test'] };
|
|
24
|
+
const output = dynamic(example, React);
|
|
25
|
+
expect(output).toDeepEqualSubset(example);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('rendering string and number child nodes', function () {
|
|
29
|
+
const example = { name: 'h1', children: ['test ', 1] };
|
|
30
|
+
const output = dynamic(example, React);
|
|
31
|
+
expect(output).toDeepEqualSubset(example);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('rendering nested tags', function () {
|
|
35
|
+
const example = {
|
|
36
|
+
name: 'div',
|
|
37
|
+
children: [{ name: 'p', children: ['test'] }],
|
|
38
|
+
};
|
|
39
|
+
const output = dynamic(example, React);
|
|
40
|
+
expect(output).toDeepEqualSubset(example);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('rendering parallel tags', function () {
|
|
44
|
+
const children = [
|
|
45
|
+
{ name: 'p', children: ['foo'] },
|
|
46
|
+
{ name: 'p', children: ['bar'] },
|
|
47
|
+
];
|
|
48
|
+
|
|
49
|
+
const output = dynamic(children, React);
|
|
50
|
+
expect(output).toDeepEqualSubset({ name: 'fragment', children });
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('rendering an external component', function () {
|
|
54
|
+
const components = { Foo: 'bar' };
|
|
55
|
+
const example = { name: 'Foo', children: ['test'] };
|
|
56
|
+
const output = dynamic(example, React, { components });
|
|
57
|
+
expect(output).toDeepEqualSubset({
|
|
58
|
+
name: 'bar',
|
|
59
|
+
children: ['test'],
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
describe('attributes', function () {
|
|
64
|
+
it('with an id attribute', function () {
|
|
65
|
+
const example = {
|
|
66
|
+
name: 'h1',
|
|
67
|
+
attributes: { id: 'foo' },
|
|
68
|
+
children: ['test'],
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const output = dynamic(example, React);
|
|
72
|
+
expect(output).toDeepEqualSubset(example);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('with a class attribute', function () {
|
|
76
|
+
const example = {
|
|
77
|
+
name: 'h1',
|
|
78
|
+
attributes: { class: 'foo bar' },
|
|
79
|
+
children: ['test'],
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const output = dynamic(example, React);
|
|
83
|
+
expect(output).toDeepEqualSubset({
|
|
84
|
+
name: 'h1',
|
|
85
|
+
attributes: { className: 'foo bar' },
|
|
86
|
+
children: ['test'],
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('with a number attribute value', function () {
|
|
91
|
+
const example = {
|
|
92
|
+
name: 'h1',
|
|
93
|
+
attributes: { 'data-foo': 42 },
|
|
94
|
+
children: ['test'],
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const output = dynamic(example, React);
|
|
98
|
+
expect(output).toDeepEqualSubset(example);
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
describe('rendering built-in nodes', function () {
|
|
103
|
+
it('rendering a fenced code block', function () {
|
|
104
|
+
const example = {
|
|
105
|
+
name: 'pre',
|
|
106
|
+
attributes: { class: 'code code-ruby' },
|
|
107
|
+
children: ['test'],
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const output = dynamic(example, React);
|
|
111
|
+
expect(output).toDeepEqual({
|
|
112
|
+
name: 'pre',
|
|
113
|
+
attributes: { className: 'code code-ruby' },
|
|
114
|
+
children: ['test'],
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
describe('React static renderer', function () {
|
|
121
|
+
it('rendering a null node', function () {
|
|
122
|
+
const example = { name: 'h1', children: ['test', null] };
|
|
123
|
+
const code = renderStatic(example);
|
|
124
|
+
const output = eval(code)();
|
|
125
|
+
|
|
126
|
+
expect(output).toDeepEqual({ ...example, attributes: null });
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('rendering a tag', function () {
|
|
130
|
+
const example = { name: 'h1', children: ['test'] };
|
|
131
|
+
const code = renderStatic(example);
|
|
132
|
+
const output = eval(code)();
|
|
133
|
+
expect(output).toDeepEqualSubset(example);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('rendering nested tags', function () {
|
|
137
|
+
const example = {
|
|
138
|
+
name: 'div',
|
|
139
|
+
children: [{ name: 'p', children: ['test'] }],
|
|
140
|
+
};
|
|
141
|
+
const code = renderStatic(example);
|
|
142
|
+
const output = eval(code)();
|
|
143
|
+
expect(output).toDeepEqualSubset(example);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('rendering a fragment', function () {
|
|
147
|
+
const example = [
|
|
148
|
+
{ name: 'p', children: ['foo'] },
|
|
149
|
+
{ name: 'p', children: ['bar'] },
|
|
150
|
+
];
|
|
151
|
+
|
|
152
|
+
const code = renderStatic(example);
|
|
153
|
+
const output = eval(code)();
|
|
154
|
+
|
|
155
|
+
expect(output).toDeepEqual({
|
|
156
|
+
name: 'fragment',
|
|
157
|
+
attributes: null,
|
|
158
|
+
children: [
|
|
159
|
+
{ name: 'p', attributes: null, children: ['foo'] },
|
|
160
|
+
{ name: 'p', attributes: null, children: ['bar'] },
|
|
161
|
+
],
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it('rendering an external component', function () {
|
|
166
|
+
const components = { Foo: 'bar' };
|
|
167
|
+
const example = { name: 'Foo', children: ['test'] };
|
|
168
|
+
const code = renderStatic(example);
|
|
169
|
+
const output = eval(code)({ components });
|
|
170
|
+
|
|
171
|
+
expect(output).toDeepEqualSubset({
|
|
172
|
+
name: 'bar',
|
|
173
|
+
children: ['test'],
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
describe('attributes', function () {
|
|
178
|
+
it('with an id attribute', function () {
|
|
179
|
+
const example = {
|
|
180
|
+
name: 'h1',
|
|
181
|
+
attributes: { id: 'foo' },
|
|
182
|
+
children: ['test'],
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
const code = renderStatic(example);
|
|
186
|
+
const output = eval(code)();
|
|
187
|
+
|
|
188
|
+
expect(output).toDeepEqualSubset(example);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it('with a number attribute value', function () {
|
|
192
|
+
const example = {
|
|
193
|
+
name: 'h1',
|
|
194
|
+
attributes: { 'data-foo': 42 },
|
|
195
|
+
children: ['test'],
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
const code = renderStatic(example);
|
|
199
|
+
const output = eval(code)();
|
|
200
|
+
|
|
201
|
+
expect(output).toDeepEqualSubset(example);
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
});
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { tagName } from './shared';
|
|
2
|
+
import type { createElement, Fragment, ReactNode } from 'react';
|
|
3
|
+
import type { RenderableTreeNodes, Scalar } from '../../types';
|
|
4
|
+
|
|
5
|
+
type ReactShape = Readonly<{
|
|
6
|
+
createElement: typeof createElement;
|
|
7
|
+
Fragment: typeof Fragment;
|
|
8
|
+
}>;
|
|
9
|
+
|
|
10
|
+
export default function dynamic(
|
|
11
|
+
node: RenderableTreeNodes,
|
|
12
|
+
React: ReactShape,
|
|
13
|
+
{ components = {} } = {}
|
|
14
|
+
) {
|
|
15
|
+
function deepRender(value: any): any {
|
|
16
|
+
if (value == null || typeof value !== 'object') return value;
|
|
17
|
+
|
|
18
|
+
if (Array.isArray(value)) return value.map((item) => deepRender(item));
|
|
19
|
+
|
|
20
|
+
if (value.$$mdtype === 'Tag') return render(value);
|
|
21
|
+
|
|
22
|
+
if (typeof value !== 'object') return value;
|
|
23
|
+
|
|
24
|
+
const output: Record<string, Scalar> = {};
|
|
25
|
+
for (const [k, v] of Object.entries(value)) output[k] = deepRender(v);
|
|
26
|
+
return output;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function render(node: RenderableTreeNodes): ReactNode {
|
|
30
|
+
if (Array.isArray(node))
|
|
31
|
+
return React.createElement(React.Fragment, null, ...node.map(render));
|
|
32
|
+
|
|
33
|
+
if (node === null || typeof node !== 'object') return node;
|
|
34
|
+
|
|
35
|
+
const {
|
|
36
|
+
name,
|
|
37
|
+
attributes: { class: className, ...attrs } = {},
|
|
38
|
+
children = [],
|
|
39
|
+
} = node;
|
|
40
|
+
|
|
41
|
+
if (className) attrs.className = className;
|
|
42
|
+
|
|
43
|
+
return React.createElement(
|
|
44
|
+
tagName(name, components),
|
|
45
|
+
Object.keys(attrs).length == 0 ? null : deepRender(attrs),
|
|
46
|
+
...children.map(render)
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return render(node);
|
|
51
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ComponentType } from 'react';
|
|
2
|
+
|
|
3
|
+
type Component = ComponentType<unknown>;
|
|
4
|
+
|
|
5
|
+
export function tagName(
|
|
6
|
+
name: string,
|
|
7
|
+
components: Record<string, Component> | ((string: string) => Component)
|
|
8
|
+
): string | Component {
|
|
9
|
+
return typeof name !== 'string'
|
|
10
|
+
? 'Fragment'
|
|
11
|
+
: name[0] !== name[0].toUpperCase()
|
|
12
|
+
? name
|
|
13
|
+
: components instanceof Function
|
|
14
|
+
? components(name)
|
|
15
|
+
: components[name];
|
|
16
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { tagName } from './shared';
|
|
2
|
+
import type { RenderableTreeNode, RenderableTreeNodes } from '../../types';
|
|
3
|
+
|
|
4
|
+
function renderArray(children: RenderableTreeNode[]): string {
|
|
5
|
+
return children.map(render).join(', ');
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function deepRender(value: any): any {
|
|
9
|
+
if (value == null || typeof value !== 'object') return JSON.stringify(value);
|
|
10
|
+
|
|
11
|
+
if (Array.isArray(value))
|
|
12
|
+
return `[${value.map((item) => deepRender(item)).join(', ')}]`;
|
|
13
|
+
|
|
14
|
+
if (value.$$mdtype === 'Tag') return render(value);
|
|
15
|
+
|
|
16
|
+
if (typeof value !== 'object') return JSON.stringify(value);
|
|
17
|
+
|
|
18
|
+
const object = Object.entries(value)
|
|
19
|
+
.map(([k, v]) => [JSON.stringify(k), deepRender(v)].join(': '))
|
|
20
|
+
.join(', ');
|
|
21
|
+
|
|
22
|
+
return `{${object}}`;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function render(node: RenderableTreeNodes): string {
|
|
26
|
+
if (Array.isArray(node))
|
|
27
|
+
return `React.createElement(React.Fragment, null, ${renderArray(node)})`;
|
|
28
|
+
|
|
29
|
+
if (node === null || typeof node !== 'object') return JSON.stringify(node);
|
|
30
|
+
|
|
31
|
+
const {
|
|
32
|
+
name,
|
|
33
|
+
attributes: { class: className, ...attrs } = {},
|
|
34
|
+
children = [],
|
|
35
|
+
} = node;
|
|
36
|
+
|
|
37
|
+
if (className) attrs.className = className;
|
|
38
|
+
|
|
39
|
+
return `React.createElement(
|
|
40
|
+
tagName(${JSON.stringify(name)}, components),
|
|
41
|
+
${Object.keys(attrs).length == 0 ? 'null' : deepRender(attrs)},
|
|
42
|
+
${renderArray(children)})`;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export default function reactStatic(node: RenderableTreeNodes): string {
|
|
46
|
+
return `
|
|
47
|
+
(({components = {}} = {}) => {
|
|
48
|
+
${tagName}
|
|
49
|
+
return ${render(node)};
|
|
50
|
+
})
|
|
51
|
+
`;
|
|
52
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { CustomAttributeTypeInterface, ValidationError } from '../types';
|
|
2
|
+
|
|
3
|
+
export class Class implements CustomAttributeTypeInterface {
|
|
4
|
+
validate(value: any): ValidationError[] {
|
|
5
|
+
if (typeof value === 'string' || typeof value === 'object') return [];
|
|
6
|
+
|
|
7
|
+
return [
|
|
8
|
+
{
|
|
9
|
+
id: 'attribute-type-invalid',
|
|
10
|
+
level: 'error',
|
|
11
|
+
message: `Attribute 'class' must be type 'string | object'`,
|
|
12
|
+
},
|
|
13
|
+
];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
transform(value: any) {
|
|
17
|
+
if (!value || typeof value === 'string') return value;
|
|
18
|
+
|
|
19
|
+
const classes = [];
|
|
20
|
+
for (const [k, v] of Object.entries(value ?? {})) if (v) classes.push(k);
|
|
21
|
+
return classes.join(' ');
|
|
22
|
+
}
|
|
23
|
+
}
|