@markdoc/markdoc 0.5.13 → 0.6.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.
Files changed (88) hide show
  1. package/README.md +29 -1
  2. package/dist/index.d.ts +4 -4
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +1228 -1143
  5. package/dist/index.js.map +4 -4
  6. package/dist/index.mjs +1228 -1143
  7. package/dist/index.mjs.map +4 -4
  8. package/dist/react.js +1 -1
  9. package/dist/react.js.map +2 -2
  10. package/dist/src/ast/base.d.ts +1 -1
  11. package/dist/src/ast/base.d.ts.map +1 -1
  12. package/dist/src/ast/function.d.ts +1 -1
  13. package/dist/src/ast/function.d.ts.map +1 -1
  14. package/dist/src/ast/index.d.ts +2 -2
  15. package/dist/src/ast/index.d.ts.map +1 -1
  16. package/dist/src/ast/node.d.ts.map +1 -1
  17. package/dist/src/ast/variable.d.ts +1 -1
  18. package/dist/src/ast/variable.d.ts.map +1 -1
  19. package/dist/src/formatter.d.ts.map +1 -1
  20. package/dist/src/functions/index.d.ts.map +1 -1
  21. package/dist/src/parser.d.ts.map +1 -1
  22. package/dist/src/renderers/html.d.ts.map +1 -1
  23. package/dist/src/renderers/react/react.d.ts +3 -3
  24. package/dist/src/renderers/react/react.d.ts.map +1 -1
  25. package/dist/src/renderers/react/static.d.ts +1 -1
  26. package/dist/src/renderers/react/static.d.ts.map +1 -1
  27. package/dist/src/schema-types/class.d.ts +1 -1
  28. package/dist/src/schema-types/class.d.ts.map +1 -1
  29. package/dist/src/schema-types/conditional.d.ts +1 -1
  30. package/dist/src/schema-types/conditional.d.ts.map +1 -1
  31. package/dist/src/schema.d.ts.map +1 -1
  32. package/dist/src/tags/conditional.d.ts +1 -1
  33. package/dist/src/tags/conditional.d.ts.map +1 -1
  34. package/dist/src/tokenizer/index.d.ts.map +1 -1
  35. package/dist/src/tokenizer/plugins/annotations.d.ts.map +1 -1
  36. package/dist/src/transformer.d.ts.map +1 -1
  37. package/dist/src/validator.d.ts +1 -1
  38. package/dist/src/validator.d.ts.map +1 -1
  39. package/index.ts +21 -22
  40. package/package.json +9 -14
  41. package/spec/support/matchers.d.ts +6 -4
  42. package/src/ast/base.test.ts +120 -0
  43. package/src/ast/base.ts +14 -9
  44. package/src/ast/function.ts +1 -1
  45. package/src/ast/index.ts +3 -4
  46. package/src/ast/node.test.ts +140 -45
  47. package/src/ast/node.ts +32 -13
  48. package/src/ast/variable.test.ts +5 -4
  49. package/src/ast/variable.ts +1 -1
  50. package/src/ast/walk.test.ts +283 -0
  51. package/src/formatter.test.ts +31 -3
  52. package/src/formatter.ts +12 -12
  53. package/src/functions/index.test.ts +8 -7
  54. package/src/functions/index.ts +1 -1
  55. package/src/grammar/tag.d.ts +1 -1
  56. package/src/grammar/tag.js +208 -285
  57. package/src/grammar/tag.pegjs +4 -4
  58. package/src/grammar/tag.test.ts +56 -52
  59. package/src/parser.test.ts +93 -70
  60. package/src/parser.ts +19 -6
  61. package/src/renderers/html.test.ts +56 -16
  62. package/src/renderers/html.ts +10 -2
  63. package/src/renderers/react/react.test.ts +49 -28
  64. package/src/renderers/react/react.ts +8 -9
  65. package/src/renderers/react/static.ts +7 -12
  66. package/src/schema-types/class.ts +1 -1
  67. package/src/schema-types/conditional.ts +1 -1
  68. package/src/schema.ts +13 -6
  69. package/src/tag.test.ts +2 -1
  70. package/src/tag.ts +1 -1
  71. package/src/tags/conditional.ts +4 -4
  72. package/src/tags/index.ts +2 -2
  73. package/src/tags/partial.ts +1 -1
  74. package/src/tokenizer/index.ts +6 -5
  75. package/src/tokenizer/plugins/annotations.test.ts +29 -28
  76. package/src/tokenizer/plugins/annotations.ts +12 -12
  77. package/src/tokenizer/plugins/comments.test.ts +11 -10
  78. package/src/tokenizer/plugins/comments.ts +2 -2
  79. package/src/tokenizer/plugins/frontmatter.test.ts +7 -6
  80. package/src/tokenizer/plugins/frontmatter.ts +2 -2
  81. package/src/tokenizer/plugins/link.test.ts +14 -13
  82. package/src/transformer.ts +7 -5
  83. package/src/transforms/table.test.ts +18 -17
  84. package/src/transforms/table.ts +1 -1
  85. package/src/utils.test.ts +23 -22
  86. package/src/utils.ts +3 -3
  87. package/src/validator.test.ts +60 -59
  88. package/src/validator.ts +43 -14
@@ -1,33 +1,35 @@
1
- import render from './html';
2
- import { RenderableTreeNode } from '../types';
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', function () {
14
- it('rendering a tag', function () {
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', function () {
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', function () {
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', function () {
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', function () {
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', function () {
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', function () {
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', function () {
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', function () {
65
- it('with basic value', function () {
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', function () {
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', function () {
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
  });
@@ -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
- output += ` ${k.toLowerCase()}="${escapeHtml(String(v))}"`;
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,8 @@ const React = {
9
10
  },
10
11
  };
11
12
 
12
- describe('React dynamic renderer', function () {
13
- it('preserves __proto__ as data without changing the props prototype', function () {
13
+ describe('React dynamic renderer', () => {
14
+ it('preserves __proto__ as data without changing the props prototype', () => {
14
15
  const attributes = Object.fromEntries([
15
16
  ['__proto__', { src: 'javascript:alert(1)' }],
16
17
  ['title', 'example'],
@@ -21,7 +22,7 @@ describe('React dynamic renderer', function () {
21
22
  expect(output.attributes.src).toBeUndefined();
22
23
  });
23
24
 
24
- it('rendering a null node', function () {
25
+ it('rendering a null node', () => {
25
26
  const example = { name: 'h1', children: ['test', null] };
26
27
  const output = dynamic(example, React);
27
28
  expect(output).toDeepEqual({
@@ -31,19 +32,19 @@ describe('React dynamic renderer', function () {
31
32
  });
32
33
  });
33
34
 
34
- it('rendering a tag', function () {
35
+ it('rendering a tag', () => {
35
36
  const example = { name: 'h1', children: ['test'] };
36
37
  const output = dynamic(example, React);
37
38
  expect(output).toDeepEqualSubset(example);
38
39
  });
39
40
 
40
- it('rendering string and number child nodes', function () {
41
+ it('rendering string and number child nodes', () => {
41
42
  const example = { name: 'h1', children: ['test ', 1] };
42
43
  const output = dynamic(example, React);
43
44
  expect(output).toDeepEqualSubset(example);
44
45
  });
45
46
 
46
- it('rendering nested tags', function () {
47
+ it('rendering nested tags', () => {
47
48
  const example = {
48
49
  name: 'div',
49
50
  children: [{ name: 'p', children: ['test'] }],
@@ -52,7 +53,7 @@ describe('React dynamic renderer', function () {
52
53
  expect(output).toDeepEqualSubset(example);
53
54
  });
54
55
 
55
- it('rendering parallel tags', function () {
56
+ it('rendering parallel tags', () => {
56
57
  const children = [
57
58
  { name: 'p', children: ['foo'] },
58
59
  { name: 'p', children: ['bar'] },
@@ -62,7 +63,7 @@ describe('React dynamic renderer', function () {
62
63
  expect(output).toDeepEqualSubset({ name: 'fragment', children });
63
64
  });
64
65
 
65
- it('rendering an external component', function () {
66
+ it('rendering an external component', () => {
66
67
  const components = { Foo: 'bar' };
67
68
  const example = new Tag('Foo', undefined, ['test']);
68
69
  const output = dynamic(example, React, { components });
@@ -72,8 +73,8 @@ describe('React dynamic renderer', function () {
72
73
  });
73
74
  });
74
75
 
75
- it('rendering with a custom tag resolution function', function () {
76
- function tagName(name, components) {
76
+ it('rendering with a custom tag resolution function', () => {
77
+ function tagName(_name, components) {
77
78
  return components.Foo;
78
79
  }
79
80
  const components = { Foo: 'bar' };
@@ -89,8 +90,8 @@ describe('React dynamic renderer', function () {
89
90
  });
90
91
  });
91
92
 
92
- describe('attributes', function () {
93
- it('with an id attribute', function () {
93
+ describe('attributes', () => {
94
+ it('with an id attribute', () => {
94
95
  const example = {
95
96
  name: 'h1',
96
97
  attributes: { id: 'foo' },
@@ -101,7 +102,7 @@ describe('React dynamic renderer', function () {
101
102
  expect(output).toDeepEqualSubset(example);
102
103
  });
103
104
 
104
- it('with a class attribute', function () {
105
+ it('with a class attribute', () => {
105
106
  const example = new Tag('h1', { class: 'foo bar' }, ['test']);
106
107
 
107
108
  const output = dynamic(example, React);
@@ -112,7 +113,7 @@ describe('React dynamic renderer', function () {
112
113
  });
113
114
  });
114
115
 
115
- it('with a number attribute value', function () {
116
+ it('with a number attribute value', () => {
116
117
  const example = {
117
118
  name: 'h1',
118
119
  attributes: { 'data-foo': 42 },
@@ -124,8 +125,8 @@ describe('React dynamic renderer', function () {
124
125
  });
125
126
  });
126
127
 
127
- describe('rendering built-in nodes', function () {
128
- it('rendering a fenced code block', function () {
128
+ describe('rendering built-in nodes', () => {
129
+ it('rendering a fenced code block', () => {
129
130
  const example = new Tag('pre', { class: 'code code-ruby' }, ['test']);
130
131
 
131
132
  const output = dynamic(example, React);
@@ -138,8 +139,28 @@ describe('React dynamic renderer', function () {
138
139
  });
139
140
  });
140
141
 
141
- describe('React static renderer', function () {
142
- it('rendering a null node', function () {
142
+ describe('React static renderer', () => {
143
+ it.each([
144
+ [
145
+ 'a renamed function',
146
+ function renamedResolver(_name, components) {
147
+ return components.Foo;
148
+ },
149
+ ],
150
+ ['an arrow function', (_name, components) => components.Foo],
151
+ ])('rendering with %s as the tag resolver', (_label, resolveTagName) => {
152
+ const example = new Tag('Custom', undefined, ['test']);
153
+ const code = renderStatic(example, { resolveTagName });
154
+ const output = eval(code)({ components: { Foo: 'bar' } });
155
+
156
+ expect(output).toDeepEqual({
157
+ name: 'bar',
158
+ attributes: null,
159
+ children: ['test'],
160
+ });
161
+ });
162
+
163
+ it('rendering a null node', () => {
143
164
  const example = { name: 'h1', children: ['test', null] };
144
165
  const code = renderStatic(example);
145
166
  const output = eval(code)();
@@ -147,14 +168,14 @@ describe('React static renderer', function () {
147
168
  expect(output).toDeepEqual({ ...example, attributes: null });
148
169
  });
149
170
 
150
- it('rendering a tag', function () {
171
+ it('rendering a tag', () => {
151
172
  const example = { name: 'h1', children: ['test'] };
152
173
  const code = renderStatic(example);
153
174
  const output = eval(code)();
154
175
  expect(output).toDeepEqualSubset(example);
155
176
  });
156
177
 
157
- it('rendering nested tags', function () {
178
+ it('rendering nested tags', () => {
158
179
  const example = {
159
180
  name: 'div',
160
181
  children: [{ name: 'p', children: ['test'] }],
@@ -164,7 +185,7 @@ describe('React static renderer', function () {
164
185
  expect(output).toDeepEqualSubset(example);
165
186
  });
166
187
 
167
- it('rendering a fragment', function () {
188
+ it('rendering a fragment', () => {
168
189
  const example = [
169
190
  { name: 'p', children: ['foo'] },
170
191
  { name: 'p', children: ['bar'] },
@@ -183,7 +204,7 @@ describe('React static renderer', function () {
183
204
  });
184
205
  });
185
206
 
186
- it('rendering an external component', function () {
207
+ it('rendering an external component', () => {
187
208
  const components = { Foo: 'bar' };
188
209
  const example = new Tag('Foo', undefined, ['test']);
189
210
  const code = renderStatic(example);
@@ -195,8 +216,8 @@ describe('React static renderer', function () {
195
216
  });
196
217
  });
197
218
 
198
- it('rendering with a custom tag resolution function', function () {
199
- function tagName(name, components) {
219
+ it('rendering with a custom tag resolution function', () => {
220
+ function tagName(_name, components) {
200
221
  return components.Foo;
201
222
  }
202
223
  const components = { Foo: 'bar' };
@@ -210,8 +231,8 @@ describe('React static renderer', function () {
210
231
  });
211
232
  });
212
233
 
213
- describe('attributes', function () {
214
- it('with an id attribute', function () {
234
+ describe('attributes', () => {
235
+ it('with an id attribute', () => {
215
236
  const example = {
216
237
  name: 'h1',
217
238
  attributes: { id: 'foo' },
@@ -224,7 +245,7 @@ describe('React static renderer', function () {
224
245
  expect(output).toDeepEqualSubset(example);
225
246
  });
226
247
 
227
- it('with a number attribute value', function () {
248
+ it('with a number attribute value', () => {
228
249
  const example = {
229
250
  name: 'h1',
230
251
  attributes: { 'data-foo': 42 },
@@ -1,18 +1,17 @@
1
+ import type { ComponentType, createElement, Fragment, ReactNode } from 'react';
1
2
  import Tag from '../../tag';
2
- import { RenderableTreeNodes } from '../../types';
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;
8
7
  Fragment: typeof Fragment;
9
8
  }>;
10
9
 
11
- type Component = ComponentType<any>;
10
+ type Component = string | 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;
@@ -42,7 +41,7 @@ export default function dynamic(
42
41
  if (typeof value !== 'object') return value;
43
42
 
44
43
  return Object.fromEntries(
45
- Object.entries(value).map(([k, v]) => [k, deepRender(v)])
44
+ Object.entries(value).map(([k, v]) => [k, deepRender(v)]),
46
45
  );
47
46
  }
48
47
 
@@ -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 == 0 ? null : deepRender(attrs),
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,22 +55,18 @@ function render(node: RenderableTreeNodes): string {
56
55
 
57
56
  return `React.createElement(
58
57
  tagName(${JSON.stringify(name)}, components),
59
- ${Object.keys(attrs).length == 0 ? 'null' : deepRender(attrs)},
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
- // the resolveTagName function *must* be called tagName
68
- // throw an error if it does not
69
- if (resolveTagName.name !== 'tagName') {
70
- throw new Error('resolveTagName must be named tagName');
71
- }
66
+ // Bind the serialized resolver explicitly because bundlers can rename it.
72
67
  return `
73
68
  (({components = {}} = {}) => {
74
- ${resolveTagName}
69
+ const tagName = (${resolveTagName});
75
70
  return ${render(node)};
76
71
  })
77
72
  `;
@@ -1,4 +1,4 @@
1
- import {
1
+ import type {
2
2
  Config,
3
3
  CustomAttributeTypeInterface,
4
4
  ValidationError,
@@ -1,4 +1,4 @@
1
- import {
1
+ import type {
2
2
  Config,
3
3
  CustomAttributeTypeInterface,
4
4
  ValidationError,
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${node.attributes['level']}`,
35
+ `h${level}`,
31
36
  node.transformAttributes(config),
32
- node.transformChildren(config)
37
+ node.transformChildren(config),
33
38
  );
34
39
  },
35
40
  };
@@ -62,7 +67,7 @@ export const fence: Schema = {
62
67
  ? node.transformChildren(config)
63
68
  : [node.attributes.content];
64
69
 
65
- return new Tag('pre', attributes, children);
70
+ return new Tag(this.render ?? 'pre', attributes, children);
66
71
  },
67
72
  };
68
73
 
@@ -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
  };
@@ -220,7 +225,9 @@ export const code: Schema = {
220
225
  },
221
226
  transform(node, config) {
222
227
  const attributes = node.transformAttributes(config);
223
- return new Tag('code', attributes, [node.attributes.content]);
228
+ return new Tag(this.render ?? 'code', attributes, [
229
+ node.attributes.content,
230
+ ]);
224
231
  },
225
232
  };
226
233
 
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', function () {
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
@@ -17,7 +17,7 @@ export default class Tag<
17
17
  constructor(
18
18
  name = 'div' as N,
19
19
  attributes = {} as A,
20
- children: RenderableTreeNode[] = []
20
+ children: RenderableTreeNode[] = [],
21
21
  ) {
22
22
  this.name = name;
23
23
  this.attributes = attributes;
@@ -1,6 +1,6 @@
1
- import { isPromise } from '../utils';
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 { ConditionalAttributeType } from '../schema-types/conditional';
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 { tagIf, tagElse } from './conditional';
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,
@@ -1,4 +1,4 @@
1
- import type { Node, Config, Schema, ValidationError } from '../types';
1
+ import type { Config, Node, Schema, ValidationError } from '../types';
2
2
 
3
3
  class PartialFile {
4
4
  validate(file: any, config: Config): ValidationError[] {
@@ -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) | null;
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
  }