@markdoc/markdoc 0.5.13 → 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 +11 -0
  2. package/dist/index.d.ts +4 -4
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +467 -462
  5. package/dist/index.js.map +4 -4
  6. package/dist/index.mjs +467 -462
  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 +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 +7 -13
  41. package/spec/support/matchers.d.ts +6 -4
  42. package/src/ast/base.ts +2 -2
  43. package/src/ast/function.ts +1 -1
  44. package/src/ast/index.ts +3 -4
  45. package/src/ast/node.test.ts +60 -46
  46. package/src/ast/node.ts +3 -3
  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 +4 -4
  56. package/src/grammar/tag.test.ts +56 -52
  57. package/src/parser.test.ts +93 -70
  58. package/src/parser.ts +19 -6
  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 +29 -28
  62. package/src/renderers/react/react.ts +7 -8
  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 +7 -5
  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 +60 -59
  86. package/src/validator.ts +13 -11
@@ -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,8 @@ 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('rendering a null node', () => {
143
144
  const example = { name: 'h1', children: ['test', null] };
144
145
  const code = renderStatic(example);
145
146
  const output = eval(code)();
@@ -147,14 +148,14 @@ describe('React static renderer', function () {
147
148
  expect(output).toDeepEqual({ ...example, attributes: null });
148
149
  });
149
150
 
150
- it('rendering a tag', function () {
151
+ it('rendering a tag', () => {
151
152
  const example = { name: 'h1', children: ['test'] };
152
153
  const code = renderStatic(example);
153
154
  const output = eval(code)();
154
155
  expect(output).toDeepEqualSubset(example);
155
156
  });
156
157
 
157
- it('rendering nested tags', function () {
158
+ it('rendering nested tags', () => {
158
159
  const example = {
159
160
  name: 'div',
160
161
  children: [{ name: 'p', children: ['test'] }],
@@ -164,7 +165,7 @@ describe('React static renderer', function () {
164
165
  expect(output).toDeepEqualSubset(example);
165
166
  });
166
167
 
167
- it('rendering a fragment', function () {
168
+ it('rendering a fragment', () => {
168
169
  const example = [
169
170
  { name: 'p', children: ['foo'] },
170
171
  { name: 'p', children: ['bar'] },
@@ -183,7 +184,7 @@ describe('React static renderer', function () {
183
184
  });
184
185
  });
185
186
 
186
- it('rendering an external component', function () {
187
+ it('rendering an external component', () => {
187
188
  const components = { Foo: 'bar' };
188
189
  const example = new Tag('Foo', undefined, ['test']);
189
190
  const code = renderStatic(example);
@@ -195,8 +196,8 @@ describe('React static renderer', function () {
195
196
  });
196
197
  });
197
198
 
198
- it('rendering with a custom tag resolution function', function () {
199
- function tagName(name, components) {
199
+ it('rendering with a custom tag resolution function', () => {
200
+ function tagName(_name, components) {
200
201
  return components.Foo;
201
202
  }
202
203
  const components = { Foo: 'bar' };
@@ -210,8 +211,8 @@ describe('React static renderer', function () {
210
211
  });
211
212
  });
212
213
 
213
- describe('attributes', function () {
214
- it('with an id attribute', function () {
214
+ describe('attributes', () => {
215
+ it('with an id attribute', () => {
215
216
  const example = {
216
217
  name: 'h1',
217
218
  attributes: { id: 'foo' },
@@ -224,7 +225,7 @@ describe('React static renderer', function () {
224
225
  expect(output).toDeepEqualSubset(example);
225
226
  });
226
227
 
227
- it('with a number attribute value', function () {
228
+ it('with a number attribute value', () => {
228
229
  const example = {
229
230
  name: 'h1',
230
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 } 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;
@@ -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,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
  }