@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.
Files changed (86) hide show
  1. package/README.md +35 -1
  2. package/dist/index.d.ts +4 -4
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +472 -448
  5. package/dist/index.js.map +4 -4
  6. package/dist/index.mjs +472 -448
  7. package/dist/index.mjs.map +4 -4
  8. package/dist/react.js +4 -4
  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 +2 -2
  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.ts +21 -6
  43. package/src/ast/function.ts +1 -1
  44. package/src/ast/index.ts +3 -4
  45. package/src/ast/node.test.ts +126 -36
  46. package/src/ast/node.ts +4 -4
  47. package/src/ast/variable.test.ts +5 -4
  48. package/src/ast/variable.ts +1 -1
  49. package/src/formatter.test.ts +4 -3
  50. package/src/formatter.ts +11 -11
  51. package/src/functions/index.test.ts +8 -7
  52. package/src/functions/index.ts +1 -1
  53. package/src/grammar/tag.d.ts +1 -1
  54. package/src/grammar/tag.js +208 -285
  55. package/src/grammar/tag.pegjs +8 -8
  56. package/src/grammar/tag.test.ts +80 -48
  57. package/src/parser.test.ts +124 -66
  58. package/src/parser.ts +29 -7
  59. package/src/renderers/html.test.ts +56 -16
  60. package/src/renderers/html.ts +10 -2
  61. package/src/renderers/react/react.test.ts +39 -27
  62. package/src/renderers/react/react.ts +9 -10
  63. package/src/renderers/react/static.ts +5 -6
  64. package/src/schema-types/class.ts +1 -1
  65. package/src/schema-types/conditional.ts +1 -1
  66. package/src/schema.ts +9 -4
  67. package/src/tag.test.ts +2 -1
  68. package/src/tag.ts +1 -1
  69. package/src/tags/conditional.ts +4 -4
  70. package/src/tags/index.ts +2 -2
  71. package/src/tags/partial.ts +1 -1
  72. package/src/tokenizer/index.ts +6 -5
  73. package/src/tokenizer/plugins/annotations.test.ts +29 -28
  74. package/src/tokenizer/plugins/annotations.ts +12 -12
  75. package/src/tokenizer/plugins/comments.test.ts +11 -10
  76. package/src/tokenizer/plugins/comments.ts +2 -2
  77. package/src/tokenizer/plugins/frontmatter.test.ts +7 -6
  78. package/src/tokenizer/plugins/frontmatter.ts +2 -2
  79. package/src/tokenizer/plugins/link.test.ts +14 -13
  80. package/src/transformer.ts +11 -7
  81. package/src/transforms/table.test.ts +18 -17
  82. package/src/transforms/table.ts +1 -1
  83. package/src/utils.test.ts +23 -22
  84. package/src/utils.ts +3 -3
  85. package/src/validator.test.ts +205 -31
  86. package/src/validator.ts +21 -13
@@ -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,19 @@ const React = {
9
10
  },
10
11
  };
11
12
 
12
- describe('React dynamic renderer', function () {
13
- it('rendering a null node', function () {
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', function () {
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', function () {
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', function () {
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', function () {
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', function () {
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', function () {
65
- function tagName(name, components) {
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', function () {
82
- it('with an id attribute', function () {
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', function () {
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', function () {
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', function () {
117
- it('rendering a fenced code block', function () {
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', function () {
131
- it('rendering a null node', function () {
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', function () {
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', function () {
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', function () {
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', function () {
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', function () {
188
- function tagName(name, components) {
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', function () {
203
- it('with an id attribute', function () {
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', function () {
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, Scalar } 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;
@@ -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
- const output: Record<string, Scalar> = {};
45
- for (const [k, v] of Object.entries(value)) output[k] = deepRender(v);
46
- return output;
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 == 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,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 == 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
66
  // the resolveTagName function *must* be called tagName
68
67
  // throw an error if it does not
@@ -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
  };
@@ -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', 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
  }