@markdoc/markdoc 0.1.2 → 0.1.5

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 (57) hide show
  1. package/README.md +5 -2
  2. package/dist/index.d.ts +48 -30
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +7040 -6593
  5. package/dist/index.js.map +3 -3
  6. package/dist/index.mjs +6904 -6457
  7. package/dist/index.mjs.map +3 -3
  8. package/dist/src/ast/base.d.ts +2 -1
  9. package/dist/src/ast/base.d.ts.map +1 -1
  10. package/dist/src/ast/index.d.ts +1 -0
  11. package/dist/src/ast/index.d.ts.map +1 -1
  12. package/dist/src/ast/node.d.ts +5 -4
  13. package/dist/src/ast/node.d.ts.map +1 -1
  14. package/dist/src/formatter.d.ts +9 -0
  15. package/dist/src/formatter.d.ts.map +1 -0
  16. package/dist/src/parser.d.ts.map +1 -1
  17. package/dist/src/schema.d.ts +1 -0
  18. package/dist/src/schema.d.ts.map +1 -1
  19. package/dist/src/{ast/tag.d.ts → tag.d.ts} +1 -1
  20. package/dist/src/tag.d.ts.map +1 -0
  21. package/dist/src/tags/conditional.d.ts.map +1 -1
  22. package/dist/src/tags/partial.d.ts.map +1 -1
  23. package/dist/src/tokenizer/index.d.ts.map +1 -1
  24. package/dist/src/tokenizer/plugins/annotations.d.ts.map +1 -1
  25. package/dist/src/transformer.d.ts +1 -7
  26. package/dist/src/transformer.d.ts.map +1 -1
  27. package/dist/src/transforms/table.d.ts.map +1 -1
  28. package/dist/src/types.d.ts +12 -4
  29. package/dist/src/types.d.ts.map +1 -1
  30. package/dist/src/utils.d.ts +2 -1
  31. package/dist/src/utils.d.ts.map +1 -1
  32. package/dist/src/validator.d.ts.map +1 -1
  33. package/global.d.ts +7 -0
  34. package/index.ts +47 -29
  35. package/package.json +7 -4
  36. package/src/ast/base.ts +5 -0
  37. package/src/ast/node.test.ts +73 -21
  38. package/src/ast/node.ts +9 -4
  39. package/src/formatter.test.ts +403 -0
  40. package/src/formatter.ts +377 -0
  41. package/src/parser.test.ts +42 -0
  42. package/src/parser.ts +33 -3
  43. package/src/schema.ts +3 -1
  44. package/src/{ast/tag.ts → tag.ts} +1 -1
  45. package/src/tags/conditional.ts +10 -3
  46. package/src/tags/partial.ts +20 -3
  47. package/src/tokenizer/index.ts +5 -1
  48. package/src/tokenizer/plugins/annotations.test.ts +60 -0
  49. package/src/tokenizer/plugins/annotations.ts +4 -0
  50. package/src/transformer.ts +15 -17
  51. package/src/transforms/table.ts +3 -3
  52. package/src/types.ts +31 -19
  53. package/src/utils.test.ts +15 -2
  54. package/src/utils.ts +14 -1
  55. package/src/validator.test.ts +70 -10
  56. package/src/validator.ts +31 -12
  57. package/dist/src/ast/tag.d.ts.map +0 -1
@@ -0,0 +1,377 @@
1
+ import Ast from './ast';
2
+ import { OPEN, CLOSE } from './utils';
3
+ import type { AttributeValue, Function, Node, Value, Variable } from './types';
4
+
5
+ type Options = {
6
+ allowIndentation?: boolean;
7
+ parent?: Node;
8
+ indent?: number;
9
+ };
10
+
11
+ const SPACE = ' ';
12
+ const NL = '\n'; // Newline
13
+ const OL = '1. '; // Ordered list
14
+ const UL = '- '; // Unordered list
15
+
16
+ const MAX_TAG_HEAD_LENGTH = 80;
17
+
18
+ const max = (a: number, b: number) => Math.max(a, b);
19
+ const increment = (o: Options, n = 2) => ({
20
+ ...o,
21
+ indent: (o.indent || 0) + n,
22
+ });
23
+
24
+ function* renderChildren(a: Node, options: Options) {
25
+ for (const child of a.children) {
26
+ yield* render(child, options);
27
+ }
28
+ }
29
+
30
+ function* renderTableRow(items: Array<string>) {
31
+ yield `| ${items.join(' | ')} |`;
32
+ }
33
+
34
+ function renderValue(v: Value): string {
35
+ if (v === null) {
36
+ return '';
37
+ }
38
+ if (Ast.isAst(v)) {
39
+ return format(v);
40
+ }
41
+ if (Array.isArray(v)) {
42
+ return '[' + v.map(renderValue).join(', ') + ']';
43
+ }
44
+ if (typeof v === 'object') {
45
+ return (
46
+ '{' +
47
+ Object.entries(v)
48
+ .map(([key, value]) => `${key}: ${renderValue(value)}`)
49
+ .join(', ') +
50
+ '}'
51
+ );
52
+ }
53
+ return JSON.stringify(v);
54
+ }
55
+
56
+ function renderAnnotationValue(a: AttributeValue): string {
57
+ if (a.name === 'primary') return a.value;
58
+ if (a.name === 'id' && typeof a.value === 'string') return '#' + a.value;
59
+ if (a.type === 'class') return '.' + a.name;
60
+ return `${a.name}=${renderValue(a.value)}`;
61
+ }
62
+
63
+ function* renderAttributes(n: Node) {
64
+ for (const [key, value] of Object.entries(n.attributes)) {
65
+ if (key === 'class' && !Ast.isAst(value))
66
+ for (const name of Object.keys(value)) {
67
+ yield renderAnnotationValue({ type: 'class', name, value });
68
+ }
69
+ else yield renderAnnotationValue({ type: 'attribute', name: key, value });
70
+ }
71
+ }
72
+
73
+ function* renderAnnotations(n: Node) {
74
+ if (n.annotations.length) {
75
+ yield OPEN + SPACE;
76
+ yield n.annotations.map(renderAnnotationValue).join(SPACE);
77
+ yield SPACE + CLOSE;
78
+ }
79
+ }
80
+
81
+ function* renderVariable(v: Variable) {
82
+ yield '$';
83
+ yield v.path.join('.');
84
+ }
85
+
86
+ function* renderFunction(f: Function) {
87
+ yield f.name;
88
+ yield '(';
89
+ yield Object.values(f.parameters).map(renderValue).join(', ');
90
+ yield ')';
91
+ }
92
+
93
+ function* trimStart(g: Generator) {
94
+ let n;
95
+ do {
96
+ n = g.next().value.trimStart();
97
+ } while (!n.length);
98
+ yield n;
99
+ yield* g;
100
+ }
101
+
102
+ function* renderNode(n: Node, o: Options = {}) {
103
+ const no = { ...o, parent: n };
104
+ const indent = SPACE.repeat(no.indent || 0);
105
+
106
+ switch (n.type) {
107
+ case 'document': {
108
+ if (n.attributes.frontmatter && n.attributes.frontmatter.length) {
109
+ yield '---' + NL + n.attributes.frontmatter + NL + '---' + NL + NL;
110
+ }
111
+ yield* trimStart(renderChildren(n, no));
112
+ break;
113
+ }
114
+ case 'heading': {
115
+ yield NL;
116
+ yield indent;
117
+ yield '#'.repeat(n.attributes.level || 1);
118
+ yield SPACE;
119
+ yield* renderChildren(n, no);
120
+ yield* renderAnnotations(n);
121
+ yield NL;
122
+ break;
123
+ }
124
+ case 'paragraph': {
125
+ yield NL;
126
+ yield indent;
127
+ yield* renderChildren(n, no);
128
+ yield* renderAnnotations(n);
129
+ yield NL;
130
+ break;
131
+ }
132
+ case 'inline': {
133
+ yield* renderChildren(n, no);
134
+ break;
135
+ }
136
+ case 'link': {
137
+ yield '[';
138
+ yield* renderChildren(n, no);
139
+ yield ']';
140
+ yield '(';
141
+ yield* render(n.attributes.href, no);
142
+ yield ')';
143
+ break;
144
+ }
145
+ case 'text': {
146
+ if (Ast.isAst(n.attributes.content)) yield OPEN + SPACE;
147
+ yield* render(n.attributes.content, no);
148
+ if (Ast.isAst(n.attributes.content)) yield SPACE + CLOSE;
149
+ break;
150
+ }
151
+ case 'blockquote': {
152
+ yield NL;
153
+ yield indent;
154
+ yield '> ';
155
+ yield* trimStart(renderChildren(n, no));
156
+ break;
157
+ }
158
+ case 'hr': {
159
+ yield NL;
160
+ yield indent;
161
+ yield '---';
162
+ yield NL;
163
+ break;
164
+ }
165
+ case 'image': {
166
+ yield '!';
167
+ yield '[';
168
+ yield* render(n.attributes.alt, no);
169
+ yield ']';
170
+ yield '(';
171
+ yield* render(n.attributes.src, no);
172
+ yield ')';
173
+ break;
174
+ }
175
+ case 'fence': {
176
+ yield NL;
177
+ yield indent;
178
+ yield '```';
179
+ yield (n.attributes.language || '').toLowerCase();
180
+ if (n.annotations.length) yield SPACE;
181
+ yield* renderAnnotations(n);
182
+ yield NL;
183
+ yield indent;
184
+ yield* renderChildren(n, no);
185
+ yield indent;
186
+ yield '```';
187
+ yield NL;
188
+ break;
189
+ }
190
+ case 'tag': {
191
+ if (!n.inline) {
192
+ yield NL;
193
+ yield indent;
194
+ }
195
+ const open = OPEN + SPACE;
196
+ const tag = [open + n.tag, ...renderAttributes(n)];
197
+ const inlineTag = tag.join(SPACE);
198
+ if (inlineTag.length + open.length * 2 > MAX_TAG_HEAD_LENGTH) {
199
+ yield tag.join('\n' + SPACE.repeat(open.length) + indent);
200
+ } else {
201
+ yield inlineTag;
202
+ }
203
+ yield SPACE + (n.children.length ? '' : '/') + CLOSE;
204
+ if (n.children.length) {
205
+ yield* renderChildren(n, no.allowIndentation ? increment(no) : no);
206
+ if (!n.inline) {
207
+ yield indent;
208
+ }
209
+ yield OPEN + SPACE + '/';
210
+ yield n.tag;
211
+ yield SPACE + CLOSE;
212
+ }
213
+ if (!n.inline) {
214
+ yield NL;
215
+ }
216
+ break;
217
+ }
218
+ case 'list': {
219
+ yield NL;
220
+ for (let i = 0; i < n.children.length; i++) {
221
+ yield indent;
222
+ const prefix = n.attributes.ordered ? OL : UL;
223
+ yield prefix;
224
+ yield* render(n.children[i], increment(no, prefix.length));
225
+ // TODO do we need this newline?
226
+ if (!no.indent) yield NL;
227
+ }
228
+ break;
229
+ }
230
+ case 'item': {
231
+ yield* trimStart(renderChildren(n, no));
232
+ yield* renderAnnotations(n);
233
+ break;
234
+ }
235
+ case 'strong': {
236
+ yield '**';
237
+ yield* renderChildren(n, no);
238
+ yield '**';
239
+ break;
240
+ }
241
+ case 'em': {
242
+ yield '_';
243
+ yield* renderChildren(n, no);
244
+ yield '_';
245
+ break;
246
+ }
247
+ case 'code': {
248
+ yield '`';
249
+ yield* render(n.attributes.content, no);
250
+ yield '`';
251
+ break;
252
+ }
253
+ case 's': {
254
+ yield '~~';
255
+ yield* renderChildren(n, no);
256
+ yield '~~';
257
+ break;
258
+ }
259
+ case 'hardbreak': {
260
+ yield '\\\n';
261
+ yield indent;
262
+ break;
263
+ }
264
+ case 'softbreak': {
265
+ yield NL;
266
+ yield indent;
267
+ break;
268
+ }
269
+ case 'table': {
270
+ const table = [...renderChildren(n, increment(no))] as any as string[][];
271
+ if (o.parent && o.parent.type === 'tag' && o.parent.tag === 'table') {
272
+ for (const row of table) {
273
+ yield NL;
274
+ for (const d of row) {
275
+ // TODO see if we should move trim() to `td`
276
+ yield indent + UL + d.trim();
277
+ yield NL;
278
+ }
279
+ if (row !== table[table.length - 1]) {
280
+ yield indent + '---';
281
+ }
282
+ }
283
+ } else {
284
+ yield NL;
285
+ const [head, ...rows] = table;
286
+
287
+ const ml = table
288
+ .map((arr) => arr.map((s) => s.length).reduce(max))
289
+ .reduce(max);
290
+
291
+ yield* renderTableRow(head.map((h) => h + SPACE.repeat(ml - h.length)));
292
+ yield NL;
293
+ yield* renderTableRow(head.map(() => '-'.repeat(ml)));
294
+ yield NL;
295
+ for (const row of rows) {
296
+ yield* renderTableRow(
297
+ row.map((r) => r + SPACE.repeat(ml - r.length))
298
+ );
299
+ yield NL;
300
+ }
301
+ }
302
+ break;
303
+ }
304
+ case 'thead': {
305
+ const [head] = [...renderChildren(n, no)];
306
+ yield head || [];
307
+ break;
308
+ }
309
+ case 'tr': {
310
+ yield [...renderChildren(n, no)];
311
+ break;
312
+ }
313
+ case 'td':
314
+ case 'th': {
315
+ yield [...renderChildren(n, no), ...renderAnnotations(n)].join('');
316
+ break;
317
+ }
318
+ case 'tbody': {
319
+ yield* renderChildren(n, no);
320
+ break;
321
+ }
322
+ case 'error': {
323
+ console.error(n);
324
+ break;
325
+ }
326
+ case 'node':
327
+ break;
328
+ }
329
+ }
330
+
331
+ function* render(
332
+ v: Value,
333
+ o: Options = {}
334
+ ): Generator<string, boolean, unknown> {
335
+ switch (typeof v) {
336
+ case 'undefined':
337
+ break;
338
+ case 'boolean':
339
+ case 'number':
340
+ case 'string': {
341
+ yield v.toString();
342
+ break;
343
+ }
344
+ case 'object': {
345
+ if (v === null) break;
346
+ if (Array.isArray(v)) {
347
+ for (const n of v) {
348
+ yield* render(n, o);
349
+ }
350
+ break;
351
+ }
352
+ switch (v.$$mdtype) {
353
+ case 'Function': {
354
+ yield* renderFunction(v as Function);
355
+ break;
356
+ }
357
+ case 'Node':
358
+ yield* renderNode(v as Node, o);
359
+ break;
360
+ case 'Variable': {
361
+ yield* renderVariable(v as Variable);
362
+ break;
363
+ }
364
+ default:
365
+ throw new Error(`Unimplemented: "${v.$$mdtype}"`);
366
+ }
367
+ break;
368
+ }
369
+ }
370
+ return true;
371
+ }
372
+
373
+ export default function format(v: Value, options?: Options): string {
374
+ let doc = '';
375
+ for (const s of trimStart(render(v, options))) doc += s;
376
+ return doc;
377
+ }
@@ -86,6 +86,15 @@ describe('Markdown parser', function () {
86
86
  expect(link.attributes).toDeepEqual({ href: '/bar' });
87
87
  });
88
88
 
89
+ it('for link with one word and a title', function () {
90
+ const document = convert(`
91
+ [foo](/bar "title")
92
+ `);
93
+
94
+ const link = document.children[0].children[0].children[0];
95
+ expect(link.attributes).toDeepEqual({ href: '/bar', title: 'title' });
96
+ });
97
+
89
98
  it('for link', function () {
90
99
  const document = convert(`
91
100
  [this is a test](/bar)
@@ -137,6 +146,39 @@ describe('Markdown parser', function () {
137
146
  content: 'This is a test\n',
138
147
  });
139
148
  });
149
+
150
+ it('for image with no title', function () {
151
+ const document = convert(`![foo](/url)`);
152
+ const image = document.children[0].children[0].children[0];
153
+ expect(image.attributes.title).toBe(undefined);
154
+ expect(image.attributes.src).toBe('/url');
155
+ expect(image.attributes.alt).toBe('foo');
156
+ });
157
+
158
+ it('for image with a title', function () {
159
+ const document = convert(`![foo](/url "title")`);
160
+ const image = document.children[0].children[0].children[0];
161
+ expect(image.attributes.title).toBe('title');
162
+ expect(image.attributes.src).toBe('/url');
163
+ expect(image.attributes.alt).toBe('foo');
164
+ });
165
+
166
+ it('for table with alignments', function () {
167
+ const document = convert(`
168
+ | Left | Center | Right |
169
+ | :--- | :----: | ----: |
170
+ | Left | Center | Right |
171
+ `);
172
+ const [thead, tbody] = document.children[0].children;
173
+
174
+ expect(thead.children[0].children[0].attributes.align).toBe('left');
175
+ expect(thead.children[0].children[1].attributes.align).toBe('center');
176
+ expect(thead.children[0].children[2].attributes.align).toBe('right');
177
+
178
+ expect(tbody.children[0].children[0].attributes.align).toBe('left');
179
+ expect(tbody.children[0].children[1].attributes.align).toBe('center');
180
+ expect(tbody.children[0].children[2].attributes.align).toBe('right');
181
+ });
140
182
  });
141
183
 
142
184
  it('handling a header', function () {
package/src/parser.ts CHANGED
@@ -14,11 +14,15 @@ const mappings: Record<string, string> = {
14
14
  };
15
15
 
16
16
  function annotate(node: Node, attributes: AttributeValue[]) {
17
- for (const { name, value, type } of attributes)
17
+ for (const attribute of attributes) {
18
+ node.annotations.push(attribute);
19
+
20
+ const { name, value, type } = attribute;
18
21
  if (type === 'attribute') node.attributes[name] = value;
19
22
  else if (type === 'class')
20
23
  if (node.attributes.class) node.attributes.class[name] = value;
21
24
  else node.attributes.class = { [name]: value };
25
+ }
22
26
  }
23
27
 
24
28
  function handleAttrs(token: Token, type: string) {
@@ -29,11 +33,15 @@ function handleAttrs(token: Token, type: string) {
29
33
  return { ordered: token.type.startsWith('ordered') };
30
34
  case 'link': {
31
35
  const attrs = Object.fromEntries(token.attrs);
32
- return { href: attrs.href };
36
+ return attrs.title
37
+ ? { href: attrs.href, title: attrs.title }
38
+ : { href: attrs.href };
33
39
  }
34
40
  case 'image': {
35
41
  const attrs = Object.fromEntries(token.attrs);
36
- return { alt: token.content, src: attrs.src };
42
+ return attrs.title
43
+ ? { alt: token.content, src: attrs.src, title: attrs.title }
44
+ : { alt: token.content, src: attrs.src };
37
45
  }
38
46
  case 'text':
39
47
  case 'code':
@@ -44,6 +52,28 @@ function handleAttrs(token: Token, type: string) {
44
52
  ? { content: token.content }
45
53
  : { content: token.content, language };
46
54
  }
55
+ case 'td':
56
+ case 'th': {
57
+ if (token.attrs) {
58
+ const attrs = Object.fromEntries(token.attrs);
59
+
60
+ let align;
61
+ if (attrs.style) {
62
+ if (attrs.style.includes('left')) {
63
+ align = 'left';
64
+ } else if (attrs.style.includes('center')) {
65
+ align = 'center';
66
+ } else if (attrs.style.includes('right')) {
67
+ align = 'right';
68
+ }
69
+ }
70
+
71
+ if (align) {
72
+ return { align };
73
+ }
74
+ }
75
+ return {};
76
+ }
47
77
  default:
48
78
  return {};
49
79
  }
package/src/schema.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { Schema } from './types';
2
- import Tag from './ast/tag';
2
+ import Tag from './tag';
3
3
 
4
4
  export const document: Schema = {
5
5
  render: 'article',
@@ -43,6 +43,7 @@ export const image: Schema = {
43
43
  attributes: {
44
44
  src: { type: String, required: true },
45
45
  alt: { type: String },
46
+ title: { type: String },
46
47
  // width/height attributes will need to be to be implemented as an extension to markdown-it
47
48
  },
48
49
  };
@@ -231,3 +232,4 @@ export const softbreak: Schema = {
231
232
  };
232
233
 
233
234
  export const error = {};
235
+ export const node = {};
@@ -1,4 +1,4 @@
1
- import type { RenderableTreeNode } from '../types';
1
+ import type { RenderableTreeNode } from './types';
2
2
 
3
3
  export default class Tag {
4
4
  readonly $$mdtype = 'Tag' as const;
@@ -1,4 +1,6 @@
1
- import type { Node, Schema, Value } from '../types';
1
+ import { isPromise } from '../utils';
2
+
3
+ import type { Node, RenderableTreeNode, Schema, Value } from '../types';
2
4
 
3
5
  type Condition = { condition: Value; children: Node[] };
4
6
 
@@ -31,8 +33,13 @@ export const tagIf: Schema = {
31
33
  transform(node, config) {
32
34
  const conditions = renderConditions(node);
33
35
  for (const { condition, children } of conditions)
34
- if (truthy(condition))
35
- return children.flatMap((child) => child.transform(config));
36
+ if (truthy(condition)) {
37
+ const nodes = children.flatMap((child) => child.transform(config));
38
+ if (nodes.some(isPromise)) {
39
+ return Promise.all(nodes).then((nodes) => nodes.flat());
40
+ }
41
+ return nodes as RenderableTreeNode[];
42
+ }
36
43
  return [];
37
44
  },
38
45
  };
@@ -1,12 +1,29 @@
1
- import type { Node, Config, Schema } from '../types';
1
+ import type { Node, Config, Schema, ValidationError } from '../types';
2
+
3
+ class PartialFile {
4
+ validate(file: any, config: Config): ValidationError[] {
5
+ const { partials = {} } = config;
6
+ const partial = partials[file];
7
+
8
+ if (!partial)
9
+ return [
10
+ {
11
+ id: 'attribute-value-invalid',
12
+ level: 'error',
13
+ message: `Partial \`${file}\` not found. The 'file' attribute must be set in \`config.partials\``,
14
+ },
15
+ ];
16
+
17
+ return [];
18
+ }
19
+ }
2
20
 
3
21
  export const partial: Schema = {
4
22
  selfClosing: true,
5
23
  attributes: {
6
- file: { type: String, render: false, required: true },
24
+ file: { type: PartialFile, render: false, required: true },
7
25
  variables: { type: Object, render: false },
8
26
  },
9
-
10
27
  transform(node: Node, config: Config) {
11
28
  const { partials = {} } = config;
12
29
  const { file, variables } = node.attributes;
@@ -12,7 +12,11 @@ export default class Tokenizer {
12
12
  this.parser = new MarkdownIt(config);
13
13
  this.parser.use(annotations, 'annotations', {});
14
14
  this.parser.use(frontmatter, 'frontmatter', {});
15
- this.parser.disable('lheading');
15
+ this.parser.disable([
16
+ 'lheading',
17
+ // Disable indented `code_block` support https://spec.commonmark.org/0.30/#indented-code-block
18
+ 'code',
19
+ ]);
16
20
  }
17
21
 
18
22
  tokenize(content: string): Token[] {
@@ -280,6 +280,66 @@ describe('MarkdownIt Annotations plugin', function () {
280
280
  ]);
281
281
  });
282
282
  });
283
+
284
+ describe('fence', function () {
285
+ it('simple with no tags', function () {
286
+ const example = parse('```\nhello\n```');
287
+ expect(example).toDeepEqualSubset([
288
+ {
289
+ type: 'fence',
290
+ children: [
291
+ {
292
+ type: 'text',
293
+ content: 'hello\n',
294
+ },
295
+ ],
296
+ },
297
+ ]);
298
+ });
299
+
300
+ it('simple with one tag', function () {
301
+ const example = parse('```\nhello {% foo %}bar{% /foo %}\n```');
302
+ expect(example).toDeepEqualSubset([
303
+ {
304
+ type: 'fence',
305
+ children: [
306
+ {
307
+ type: 'text',
308
+ content: 'hello ',
309
+ },
310
+ {
311
+ type: 'tag_open',
312
+ info: '{% foo %}',
313
+ },
314
+ {
315
+ type: 'text',
316
+ content: 'bar',
317
+ },
318
+ {
319
+ type: 'tag_close',
320
+ info: '{% /foo %}',
321
+ },
322
+ ],
323
+ },
324
+ ]);
325
+ });
326
+
327
+ it('unclosed tag', function () {
328
+ const example = parse('```\nhello {%\n```');
329
+ // unclosed tags should not result in crashes
330
+ expect(example).toDeepEqualSubset([
331
+ {
332
+ type: 'fence',
333
+ children: [
334
+ {
335
+ type: 'text',
336
+ content: 'hello {%\n',
337
+ },
338
+ ],
339
+ },
340
+ ]);
341
+ });
342
+ });
283
343
  });
284
344
 
285
345
  describe('parsing inline annotations', function () {
@@ -23,6 +23,10 @@ function createToken(
23
23
  token.info = content;
24
24
  token.meta = meta;
25
25
 
26
+ if (!state.delimiters) {
27
+ state.delimiters = [];
28
+ }
29
+
26
30
  return token;
27
31
  } catch (error) {
28
32
  if (!(error instanceof SyntaxError)) throw error;