@markdoc/markdoc 0.1.3 → 0.1.6

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 (53) hide show
  1. package/README.md +1 -1
  2. package/dist/index.d.ts +48 -30
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +6744 -6329
  5. package/dist/index.js.map +3 -3
  6. package/dist/index.mjs +6827 -6412
  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/tag.d.ts +9 -0
  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/transformer.d.ts +1 -7
  25. package/dist/src/transformer.d.ts.map +1 -1
  26. package/dist/src/transforms/table.d.ts.map +1 -1
  27. package/dist/src/types.d.ts +12 -4
  28. package/dist/src/types.d.ts.map +1 -1
  29. package/dist/src/utils.d.ts +1 -0
  30. package/dist/src/utils.d.ts.map +1 -1
  31. package/dist/src/validator.d.ts.map +1 -1
  32. package/index.ts +47 -29
  33. package/package.json +3 -2
  34. package/src/ast/base.ts +5 -0
  35. package/src/ast/node.test.ts +73 -21
  36. package/src/ast/node.ts +9 -4
  37. package/src/formatter.test.ts +404 -0
  38. package/src/formatter.ts +373 -0
  39. package/src/parser.ts +5 -1
  40. package/src/schema.ts +2 -1
  41. package/src/tag.ts +22 -0
  42. package/src/tags/conditional.ts +10 -3
  43. package/src/tags/partial.ts +20 -3
  44. package/src/tokenizer/index.ts +5 -1
  45. package/src/transformer.ts +15 -17
  46. package/src/transforms/table.ts +2 -2
  47. package/src/types.ts +31 -19
  48. package/src/utils.ts +4 -0
  49. package/src/validator.test.ts +60 -0
  50. package/src/validator.ts +20 -1
  51. package/dist/src/ast/tag.d.ts +0 -9
  52. package/dist/src/ast/tag.d.ts.map +0 -1
  53. package/src/ast/tag.ts +0 -19
package/src/ast/node.ts CHANGED
@@ -2,8 +2,11 @@ import { resolve } from './base';
2
2
  import transformer from '../transformer';
3
3
  import type {
4
4
  AstType,
5
- Location,
5
+ AttributeValue,
6
6
  Config,
7
+ Location,
8
+ MaybePromise,
9
+ NodeType,
7
10
  RenderableTreeNode,
8
11
  RenderableTreeNodes,
9
12
  Schema,
@@ -17,14 +20,15 @@ export default class Node implements AstType {
17
20
  children: Node[];
18
21
  errors: ValidationError[] = [];
19
22
  lines: number[] = [];
20
- type: string;
23
+ type: NodeType;
21
24
  tag?: string;
25
+ annotations: AttributeValue[];
22
26
 
23
27
  inline = false;
24
28
  location?: Location;
25
29
 
26
30
  constructor(
27
- type = 'node',
31
+ type: NodeType = 'node',
28
32
  attributes: Record<string, any> = {},
29
33
  children: Node[] = [],
30
34
  tag?: string
@@ -33,6 +37,7 @@ export default class Node implements AstType {
33
37
  this.children = children;
34
38
  this.type = type;
35
39
  this.tag = tag;
40
+ this.annotations = [];
36
41
  }
37
42
 
38
43
  *walk(): Generator<Node, void, unknown> {
@@ -65,7 +70,7 @@ export default class Node implements AstType {
65
70
  return transformer.children(this, config);
66
71
  }
67
72
 
68
- transform(config: Config): RenderableTreeNodes {
73
+ transform(config: Config): MaybePromise<RenderableTreeNodes> {
69
74
  return transformer.node(this, config);
70
75
  }
71
76
  }
@@ -0,0 +1,404 @@
1
+ import { diff } from 'jest-diff';
2
+
3
+ import Markdoc from '../index';
4
+ import format from './formatter';
5
+
6
+ const source = `---
7
+
8
+
9
+ title: What is Markdoc?
10
+
11
+ ---
12
+
13
+ # {% $markdoc.frontmatter.title %} {% #overview %}
14
+
15
+ Markdoc is a **Markdown**-based \`syntax\` and _toolchain_ for creating ~~custom~~ documentation sites. Stripe created Markdoc to power [our public docs](http://stripe.com/docs).
16
+
17
+ > Blockquote {% .special %}
18
+
19
+ ---
20
+
21
+ ![Alt](/image)
22
+
23
+ {% callout #id .class .class2 a="check" b={"e":{f: 5}} c=8 d=[1, "2",true] %}
24
+ Markdoc is open-source—check out it's [source](http://github.com/markdoc/markdoc) to see how it works.
25
+ {% /callout %}
26
+
27
+ \`\`\`js {% .class #id x="test" render=false %}
28
+ Code!
29
+ \`\`\`
30
+
31
+ ## How is {% markdoc("test", 1) %} different? {% .classname %}
32
+
33
+ foo\\
34
+ baz
35
+
36
+ Soft
37
+ break
38
+ Markdoc uses…
39
+ `;
40
+
41
+ const expected = `---
42
+ title: What is Markdoc?
43
+ ---
44
+
45
+ # {% $markdoc.frontmatter.title %} {% #overview %}
46
+
47
+ Markdoc is a **Markdown**-based \`syntax\` and _toolchain_ for creating ~~custom~~ documentation sites. Stripe created Markdoc to power [our public docs](http://stripe.com/docs).
48
+
49
+ > Blockquote {% .special %}
50
+
51
+ ---
52
+
53
+ ![Alt](/image)
54
+
55
+ {% callout #id .class .class2 a="check" b={e: {f: 5}} c=8 d=[1, "2", true] %}
56
+ Markdoc is open-source—check out it's [source](http://github.com/markdoc/markdoc) to see how it works.
57
+ {% /callout %}
58
+
59
+ \`\`\`js {% .class #id x="test" render=false %}
60
+ Code!
61
+ \`\`\`
62
+
63
+ ## How is {% markdoc("test", 1) %} different? {% .classname %}
64
+
65
+ foo\\
66
+ baz
67
+
68
+ Soft
69
+ break
70
+ Markdoc uses…
71
+ `;
72
+
73
+ function check(source, expected, options = {}) {
74
+ const a = expected?.trimStart();
75
+ const b = format(Markdoc.parse(source), options);
76
+ // console.log(a, b);
77
+ const d = diff(a, b);
78
+ if (d && d.includes('Compared values have no visual difference.')) return;
79
+ throw d;
80
+ }
81
+
82
+ function stable(source) {
83
+ return check(source, source);
84
+ }
85
+
86
+ describe('Formatter', () => {
87
+ it('null', () => {
88
+ expect(format(null)).toBe('');
89
+ });
90
+
91
+ it('basics', () => {
92
+ check(source, expected);
93
+ stable(expected);
94
+ });
95
+
96
+ it('attribute edge cases', () => {
97
+ const source = `{% key id=$user.name class=default($y, "test") %}Child{% /key %}`;
98
+ const expected = `
99
+ {% key id=$user.name class=default($y, "test") %}Child{% /key %}
100
+ `;
101
+
102
+ check(source, expected);
103
+ stable(expected);
104
+ });
105
+
106
+ it('variables', () => {
107
+ const source = `
108
+ {% $user.name %}
109
+ {% key x=$user.name new=$flag /%}
110
+ `;
111
+ const expected = `
112
+ {% $user.name %}
113
+
114
+ {% key x=$user.name new=$flag /%}
115
+ `;
116
+
117
+ check(source, expected);
118
+ stable(expected);
119
+ });
120
+
121
+ it('functions', () => {
122
+ const source = `
123
+ {% markdoc("test", 1) %}
124
+ {% key x=default($x, 1) /%}
125
+ `;
126
+ const expected = `{% markdoc("test", 1) %}
127
+ {% key x=default($x, 1) /%}
128
+ `;
129
+
130
+ check(source, expected);
131
+ });
132
+
133
+ it('tags', () => {
134
+ const source = `
135
+ {% key /%}
136
+
137
+ {% checkout %}
138
+ {% if true %}
139
+ Yes!
140
+ {% /if %}
141
+ {% /checkout %}
142
+ `;
143
+ const expected = `
144
+ {% key /%}
145
+
146
+ {% checkout %}
147
+ {% if true %}
148
+ Yes!
149
+ {% /if %}
150
+ {% /checkout %}
151
+ `;
152
+ check(source, expected);
153
+ stable(expected);
154
+ });
155
+
156
+ it('long tags', () => {
157
+ const source = `
158
+ {% tag a=true b="My very long text well over 80 characters in total" c=123456789 d=false /%}
159
+ `;
160
+ const expected = `
161
+ {% tag
162
+ a=true
163
+ b="My very long text well over 80 characters in total"
164
+ c=123456789
165
+ d=false /%}
166
+ `;
167
+ check(source, expected);
168
+ stable(expected);
169
+ });
170
+
171
+ it('nested tags — allowIndentation: true', () => {
172
+ const source = `
173
+ {% checkout %}
174
+ {% if true %}
175
+ Yes!
176
+ {% /if %}
177
+ {% /checkout %}
178
+ `;
179
+
180
+ const expected = `
181
+ {% checkout %}
182
+ {% if true %}
183
+ Yes!
184
+ {% /if %}
185
+ {% /checkout %}
186
+ `;
187
+
188
+ check(source, expected, { allowIndentation: true });
189
+ });
190
+
191
+ it('tables', () => {
192
+ const source = `
193
+ | Syntax | Description |
194
+ | ------ | ---- |
195
+ | Header | Title |
196
+ | Paragraph | Text |
197
+
198
+ {% table %}
199
+
200
+ - One {% align="middle" %}
201
+ - Two
202
+
203
+
204
+ ---
205
+ - Three
206
+ - Four {% align="end" %}
207
+
208
+ ---
209
+
210
+ * **Five**
211
+ *
212
+ A bunch of words
213
+
214
+ And more words
215
+
216
+ {% /table %}
217
+
218
+ {% table %}
219
+ ---
220
+ - H1
221
+ - H2
222
+ {% /table %}
223
+ `;
224
+ const expected = `
225
+ | Syntax | Description |
226
+ | ----------- | ----------- |
227
+ | Header | Title |
228
+ | Paragraph | Text |
229
+
230
+ {% table %}
231
+ - One {% align="middle" %}
232
+ - Two
233
+ ---
234
+ - Three
235
+ - Four {% align="end" %}
236
+ ---
237
+ - **Five**
238
+ - A bunch of words
239
+
240
+ And more words
241
+ {% /table %}
242
+
243
+ {% table %}
244
+ ---
245
+ - H1
246
+ - H2
247
+ {% /table %}
248
+ `;
249
+
250
+ check(source, expected);
251
+ stable(expected);
252
+ });
253
+
254
+ it('lists', () => {
255
+ const source = `
256
+ - [Install Markdoc](/docs/getting-started)
257
+ - [Try it out online](/sandbox)
258
+
259
+ 1. One {% align="left" %}
260
+ 2. Two
261
+ 3. Three
262
+
263
+ - A
264
+ - B
265
+ - B2
266
+ - C`;
267
+ const expected = `
268
+ - [Install Markdoc](/docs/getting-started)
269
+ - [Try it out online](/sandbox)
270
+
271
+ 1. One {% align="left" %}
272
+ 1. Two
273
+ 1. Three
274
+
275
+ - A
276
+ - B
277
+ - B2
278
+ - C
279
+ `;
280
+ check(source, expected);
281
+ stable(expected);
282
+ });
283
+ it('"loose" lists', () => {
284
+ const source = `
285
+ - One
286
+
287
+ My first paragraph
288
+ Test
289
+
290
+ {% tag %}
291
+ Indented tag
292
+ {% /tag %}
293
+
294
+ \`\`\`
295
+ {% $code %}
296
+ \`\`\`
297
+
298
+ - Two
299
+
300
+ My second paragraph
301
+
302
+ ---
303
+
304
+ ## Indented header
305
+
306
+ > Indented blockquote`;
307
+ const expected = `
308
+ - One
309
+
310
+ My first paragraph
311
+ Test
312
+
313
+ {% tag %}
314
+ Indented tag
315
+ {% /tag %}
316
+
317
+ \`\`\`
318
+ {% $code %}
319
+ \`\`\`
320
+ - Two
321
+
322
+ My second paragraph
323
+
324
+ ---
325
+
326
+ ## Indented header
327
+
328
+ > Indented blockquote
329
+ `;
330
+
331
+ check(source, expected);
332
+ stable(expected);
333
+ });
334
+
335
+ it('complicated nested lists', () => {
336
+ const source = `
337
+ * Create your CNAME record
338
+
339
+ 1. Click **Add record**.
340
+ 1. Enter these values in the form that opens:
341
+
342
+ {% table %}
343
+ * Field
344
+ * Value to enter
345
+ * Description
346
+ ---
347
+ * Type
348
+ * Select \`CNAME\` from the dropdown
349
+ * What kind of DNS record this is.
350
+ ---
351
+ * Target
352
+ * If your custom subdomain is checkout.powdur.me, enter \`checkout\`
353
+ * For CNAME records, this field is the first part of your subdomain (the part leading up to the first period).
354
+ ---
355
+ * Value
356
+ * {% code %}hosted-checkout.stripecdn.com{% /code %}
357
+ * This is what the new subdomain record points to-in this case, Stripe Checkout.
358
+ ---
359
+ * TTL
360
+ * \`5 min\`
361
+ * An expiration of 5 minutes (300 seconds) is OK.
362
+ ---
363
+ * Proxy status
364
+ * \`Off\`
365
+ * Set the proxy status to \`off\` to avoid issues during setup.
366
+ {% /table %}`;
367
+
368
+ const expected = `
369
+ - Create your CNAME record
370
+
371
+ 1. Click **Add record**.
372
+ 1. Enter these values in the form that opens:
373
+
374
+ {% table %}
375
+ - Field
376
+ - Value to enter
377
+ - Description
378
+ ---
379
+ - Type
380
+ - Select \`CNAME\` from the dropdown
381
+ - What kind of DNS record this is.
382
+ ---
383
+ - Target
384
+ - If your custom subdomain is checkout.powdur.me, enter \`checkout\`
385
+ - For CNAME records, this field is the first part of your subdomain (the part leading up to the first period).
386
+ ---
387
+ - Value
388
+ - {% code %}hosted-checkout.stripecdn.com{% /code %}
389
+ - This is what the new subdomain record points to-in this case, Stripe Checkout.
390
+ ---
391
+ - TTL
392
+ - \`5 min\`
393
+ - An expiration of 5 minutes (300 seconds) is OK.
394
+ ---
395
+ - Proxy status
396
+ - \`Off\`
397
+ - Set the proxy status to \`off\` to avoid issues during setup.
398
+ {% /table %}
399
+ `;
400
+
401
+ check(source, expected);
402
+ stable(expected);
403
+ });
404
+ });