@markdoc/markdoc 0.1.5 → 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.
@@ -1 +1 @@
1
- {"version":3,"file":"formatter.d.ts","sourceRoot":"","sources":["../../src/formatter.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAA4B,IAAI,EAAE,KAAK,EAAY,MAAM,SAAS,CAAC;AAE/E,aAAK,OAAO,GAAG;IACb,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,MAAM,CAAC,EAAE,IAAI,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AA4WF,MAAM,CAAC,OAAO,UAAU,MAAM,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,MAAM,CAIlE"}
1
+ {"version":3,"file":"formatter.d.ts","sourceRoot":"","sources":["../../src/formatter.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAA4B,IAAI,EAAE,KAAK,EAAY,MAAM,SAAS,CAAC;AAE/E,aAAK,OAAO,GAAG;IACb,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,MAAM,CAAC,EAAE,IAAI,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAwWF,MAAM,CAAC,OAAO,UAAU,MAAM,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,MAAM,CAIlE"}
package/dist/src/tag.d.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import type { RenderableTreeNode } from './types';
2
- export default class Tag {
2
+ export default class Tag<N extends string = string, A extends Record<string, any> = Record<string, any>> {
3
3
  readonly $$mdtype: "Tag";
4
- name: string;
5
- attributes: Record<string, any>;
4
+ name: N;
5
+ attributes: A;
6
6
  children: RenderableTreeNode[];
7
- constructor(name?: string, attributes?: {}, children?: RenderableTreeNode[]);
7
+ constructor(name?: N, attributes?: A, children?: RenderableTreeNode[]);
8
8
  }
9
9
  //# sourceMappingURL=tag.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"tag.d.ts","sourceRoot":"","sources":["../../src/tag.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAElD,MAAM,CAAC,OAAO,OAAO,GAAG;IACtB,QAAQ,CAAC,QAAQ,QAAkB;IAEnC,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAChC,QAAQ,EAAE,kBAAkB,EAAE,CAAC;gBAG7B,IAAI,SAAQ,EACZ,UAAU,KAAK,EACf,QAAQ,GAAE,kBAAkB,EAAO;CAMtC"}
1
+ {"version":3,"file":"tag.d.ts","sourceRoot":"","sources":["../../src/tag.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAElD,MAAM,CAAC,OAAO,OAAO,GAAG,CACtB,CAAC,SAAS,MAAM,GAAG,MAAM,EACzB,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IAEnD,QAAQ,CAAC,QAAQ,QAAkB;IAEnC,IAAI,EAAE,CAAC,CAAC;IACR,UAAU,EAAE,CAAC,CAAC;IACd,QAAQ,EAAE,kBAAkB,EAAE,CAAC;gBAG7B,IAAI,IAAa,EACjB,UAAU,IAAU,EACpB,QAAQ,GAAE,kBAAkB,EAAO;CAMtC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@markdoc/markdoc",
3
3
  "author": "Ryan Paul",
4
- "version": "0.1.5",
4
+ "version": "0.1.6",
5
5
  "description": "A text markup language for documentation",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.mjs",
@@ -71,7 +71,7 @@ Markdoc uses…
71
71
  `;
72
72
 
73
73
  function check(source, expected, options = {}) {
74
- const a = expected.trimStart();
74
+ const a = expected?.trimStart();
75
75
  const b = format(Markdoc.parse(source), options);
76
76
  // console.log(a, b);
77
77
  const d = diff(a, b);
@@ -84,6 +84,10 @@ function stable(source) {
84
84
  }
85
85
 
86
86
  describe('Formatter', () => {
87
+ it('null', () => {
88
+ expect(format(null)).toBe('');
89
+ });
90
+
87
91
  it('basics', () => {
88
92
  check(source, expected);
89
93
  stable(expected);
@@ -313,7 +317,6 @@ Yes!
313
317
  \`\`\`
314
318
  {% $code %}
315
319
  \`\`\`
316
-
317
320
  - Two
318
321
 
319
322
  My second paragraph
@@ -323,7 +326,6 @@ Yes!
323
326
  ## Indented header
324
327
 
325
328
  > Indented blockquote
326
-
327
329
  `;
328
330
 
329
331
  check(source, expected);
@@ -394,7 +396,6 @@ Yes!
394
396
  - \`Off\`
395
397
  - Set the proxy status to \`off\` to avoid issues during setup.
396
398
  {% /table %}
397
-
398
399
  `;
399
400
 
400
401
  check(source, expected);
package/src/formatter.ts CHANGED
@@ -9,6 +9,7 @@ type Options = {
9
9
  };
10
10
 
11
11
  const SPACE = ' ';
12
+ const SEP = ', '; // Value separator
12
13
  const NL = '\n'; // Newline
13
14
  const OL = '1. '; // Ordered list
14
15
  const UL = '- '; // Unordered list
@@ -21,72 +22,72 @@ const increment = (o: Options, n = 2) => ({
21
22
  indent: (o.indent || 0) + n,
22
23
  });
23
24
 
24
- function* renderChildren(a: Node, options: Options) {
25
+ function* formatChildren(a: Node, options: Options) {
25
26
  for (const child of a.children) {
26
- yield* render(child, options);
27
+ yield* formatValue(child, options);
27
28
  }
28
29
  }
29
30
 
30
- function* renderTableRow(items: Array<string>) {
31
+ function* formatTableRow(items: Array<string>) {
31
32
  yield `| ${items.join(' | ')} |`;
32
33
  }
33
34
 
34
- function renderValue(v: Value): string {
35
- if (v === null) {
36
- return '';
37
- }
35
+ function formatScalar(v: Value): string {
38
36
  if (Ast.isAst(v)) {
39
37
  return format(v);
40
38
  }
39
+ if (v === null) {
40
+ return '';
41
+ }
41
42
  if (Array.isArray(v)) {
42
- return '[' + v.map(renderValue).join(', ') + ']';
43
+ return '[' + v.map(formatScalar).join(SEP) + ']';
43
44
  }
44
45
  if (typeof v === 'object') {
45
46
  return (
46
47
  '{' +
47
48
  Object.entries(v)
48
- .map(([key, value]) => `${key}: ${renderValue(value)}`)
49
- .join(', ') +
49
+ .map(([key, value]) => `${key}: ${formatScalar(value)}`)
50
+ .join(SEP) +
50
51
  '}'
51
52
  );
52
53
  }
53
54
  return JSON.stringify(v);
54
55
  }
55
56
 
56
- function renderAnnotationValue(a: AttributeValue): string {
57
+ function formatAnnotationValue(a: AttributeValue): string {
57
58
  if (a.name === 'primary') return a.value;
58
59
  if (a.name === 'id' && typeof a.value === 'string') return '#' + a.value;
59
60
  if (a.type === 'class') return '.' + a.name;
60
- return `${a.name}=${renderValue(a.value)}`;
61
+ return `${a.name}=${formatScalar(a.value)}`;
61
62
  }
62
63
 
63
- function* renderAttributes(n: Node) {
64
+ function* formatAttributes(n: Node) {
64
65
  for (const [key, value] of Object.entries(n.attributes)) {
65
66
  if (key === 'class' && !Ast.isAst(value))
66
67
  for (const name of Object.keys(value)) {
67
- yield renderAnnotationValue({ type: 'class', name, value });
68
+ yield formatAnnotationValue({ type: 'class', name, value });
68
69
  }
69
- else yield renderAnnotationValue({ type: 'attribute', name: key, value });
70
+ else yield formatAnnotationValue({ type: 'attribute', name: key, value });
70
71
  }
71
72
  }
72
73
 
73
- function* renderAnnotations(n: Node) {
74
+ function* formatAnnotations(n: Node) {
74
75
  if (n.annotations.length) {
75
76
  yield OPEN + SPACE;
76
- yield n.annotations.map(renderAnnotationValue).join(SPACE);
77
+ yield n.annotations.map(formatAnnotationValue).join(SPACE);
77
78
  yield SPACE + CLOSE;
78
79
  }
79
80
  }
80
81
 
81
- function* renderVariable(v: Variable) {
82
+ function* formatVariable(v: Variable) {
82
83
  yield '$';
83
84
  yield v.path.join('.');
84
85
  }
85
86
 
86
- function* renderFunction(f: Function) {
87
+ function* formatFunction(f: Function) {
87
88
  yield f.name;
88
89
  yield '(';
89
- yield Object.values(f.parameters).map(renderValue).join(', ');
90
+ yield Object.values(f.parameters).map(formatScalar).join(SEP);
90
91
  yield ')';
91
92
  }
92
93
 
@@ -99,7 +100,7 @@ function* trimStart(g: Generator) {
99
100
  yield* g;
100
101
  }
101
102
 
102
- function* renderNode(n: Node, o: Options = {}) {
103
+ function* formatNode(n: Node, o: Options = {}) {
103
104
  const no = { ...o, parent: n };
104
105
  const indent = SPACE.repeat(no.indent || 0);
105
106
 
@@ -108,7 +109,7 @@ function* renderNode(n: Node, o: Options = {}) {
108
109
  if (n.attributes.frontmatter && n.attributes.frontmatter.length) {
109
110
  yield '---' + NL + n.attributes.frontmatter + NL + '---' + NL + NL;
110
111
  }
111
- yield* trimStart(renderChildren(n, no));
112
+ yield* trimStart(formatChildren(n, no));
112
113
  break;
113
114
  }
114
115
  case 'heading': {
@@ -116,35 +117,35 @@ function* renderNode(n: Node, o: Options = {}) {
116
117
  yield indent;
117
118
  yield '#'.repeat(n.attributes.level || 1);
118
119
  yield SPACE;
119
- yield* renderChildren(n, no);
120
- yield* renderAnnotations(n);
120
+ yield* formatChildren(n, no);
121
+ yield* formatAnnotations(n);
121
122
  yield NL;
122
123
  break;
123
124
  }
124
125
  case 'paragraph': {
125
126
  yield NL;
126
127
  yield indent;
127
- yield* renderChildren(n, no);
128
- yield* renderAnnotations(n);
128
+ yield* formatChildren(n, no);
129
+ yield* formatAnnotations(n);
129
130
  yield NL;
130
131
  break;
131
132
  }
132
133
  case 'inline': {
133
- yield* renderChildren(n, no);
134
+ yield* formatChildren(n, no);
134
135
  break;
135
136
  }
136
137
  case 'link': {
137
138
  yield '[';
138
- yield* renderChildren(n, no);
139
+ yield* formatChildren(n, no);
139
140
  yield ']';
140
141
  yield '(';
141
- yield* render(n.attributes.href, no);
142
+ yield* formatValue(n.attributes.href, no);
142
143
  yield ')';
143
144
  break;
144
145
  }
145
146
  case 'text': {
146
147
  if (Ast.isAst(n.attributes.content)) yield OPEN + SPACE;
147
- yield* render(n.attributes.content, no);
148
+ yield* formatValue(n.attributes.content, no);
148
149
  if (Ast.isAst(n.attributes.content)) yield SPACE + CLOSE;
149
150
  break;
150
151
  }
@@ -152,7 +153,7 @@ function* renderNode(n: Node, o: Options = {}) {
152
153
  yield NL;
153
154
  yield indent;
154
155
  yield '> ';
155
- yield* trimStart(renderChildren(n, no));
156
+ yield* trimStart(formatChildren(n, no));
156
157
  break;
157
158
  }
158
159
  case 'hr': {
@@ -165,10 +166,10 @@ function* renderNode(n: Node, o: Options = {}) {
165
166
  case 'image': {
166
167
  yield '!';
167
168
  yield '[';
168
- yield* render(n.attributes.alt, no);
169
+ yield* formatValue(n.attributes.alt, no);
169
170
  yield ']';
170
171
  yield '(';
171
- yield* render(n.attributes.src, no);
172
+ yield* formatValue(n.attributes.src, no);
172
173
  yield ')';
173
174
  break;
174
175
  }
@@ -178,10 +179,10 @@ function* renderNode(n: Node, o: Options = {}) {
178
179
  yield '```';
179
180
  yield (n.attributes.language || '').toLowerCase();
180
181
  if (n.annotations.length) yield SPACE;
181
- yield* renderAnnotations(n);
182
+ yield* formatAnnotations(n);
182
183
  yield NL;
183
184
  yield indent;
184
- yield* renderChildren(n, no);
185
+ yield* formatChildren(n, no);
185
186
  yield indent;
186
187
  yield '```';
187
188
  yield NL;
@@ -193,16 +194,16 @@ function* renderNode(n: Node, o: Options = {}) {
193
194
  yield indent;
194
195
  }
195
196
  const open = OPEN + SPACE;
196
- const tag = [open + n.tag, ...renderAttributes(n)];
197
+ const tag = [open + n.tag, ...formatAttributes(n)];
197
198
  const inlineTag = tag.join(SPACE);
198
199
  if (inlineTag.length + open.length * 2 > MAX_TAG_HEAD_LENGTH) {
199
- yield tag.join('\n' + SPACE.repeat(open.length) + indent);
200
+ yield tag.join(NL + SPACE.repeat(open.length) + indent);
200
201
  } else {
201
202
  yield inlineTag;
202
203
  }
203
204
  yield SPACE + (n.children.length ? '' : '/') + CLOSE;
204
205
  if (n.children.length) {
205
- yield* renderChildren(n, no.allowIndentation ? increment(no) : no);
206
+ yield* formatChildren(n, no.allowIndentation ? increment(no) : no);
206
207
  if (!n.inline) {
207
208
  yield indent;
208
209
  }
@@ -216,48 +217,46 @@ function* renderNode(n: Node, o: Options = {}) {
216
217
  break;
217
218
  }
218
219
  case 'list': {
220
+ const prefix = n.attributes.ordered ? OL : UL;
219
221
  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;
222
+ for (const child of n.children) {
223
+ const d = format(child, increment(no, prefix.length)).trim();
224
+ yield indent + prefix + d;
225
+ yield NL;
227
226
  }
228
227
  break;
229
228
  }
230
229
  case 'item': {
231
- yield* trimStart(renderChildren(n, no));
232
- yield* renderAnnotations(n);
230
+ yield* formatChildren(n, no);
231
+ yield* formatAnnotations(n);
233
232
  break;
234
233
  }
235
234
  case 'strong': {
236
235
  yield '**';
237
- yield* renderChildren(n, no);
236
+ yield* formatChildren(n, no);
238
237
  yield '**';
239
238
  break;
240
239
  }
241
240
  case 'em': {
242
241
  yield '_';
243
- yield* renderChildren(n, no);
242
+ yield* formatChildren(n, no);
244
243
  yield '_';
245
244
  break;
246
245
  }
247
246
  case 'code': {
248
247
  yield '`';
249
- yield* render(n.attributes.content, no);
248
+ yield* formatValue(n.attributes.content, no);
250
249
  yield '`';
251
250
  break;
252
251
  }
253
252
  case 's': {
254
253
  yield '~~';
255
- yield* renderChildren(n, no);
254
+ yield* formatChildren(n, no);
256
255
  yield '~~';
257
256
  break;
258
257
  }
259
258
  case 'hardbreak': {
260
- yield '\\\n';
259
+ yield '\\' + NL;
261
260
  yield indent;
262
261
  break;
263
262
  }
@@ -267,7 +266,7 @@ function* renderNode(n: Node, o: Options = {}) {
267
266
  break;
268
267
  }
269
268
  case 'table': {
270
- const table = [...renderChildren(n, increment(no))] as any as string[][];
269
+ const table = [...formatChildren(n, increment(no))] as any as string[][];
271
270
  if (o.parent && o.parent.type === 'tag' && o.parent.tag === 'table') {
272
271
  for (const row of table) {
273
272
  yield NL;
@@ -288,12 +287,12 @@ function* renderNode(n: Node, o: Options = {}) {
288
287
  .map((arr) => arr.map((s) => s.length).reduce(max))
289
288
  .reduce(max);
290
289
 
291
- yield* renderTableRow(head.map((h) => h + SPACE.repeat(ml - h.length)));
290
+ yield* formatTableRow(head.map((h) => h + SPACE.repeat(ml - h.length)));
292
291
  yield NL;
293
- yield* renderTableRow(head.map(() => '-'.repeat(ml)));
292
+ yield* formatTableRow(head.map(() => '-'.repeat(ml)));
294
293
  yield NL;
295
294
  for (const row of rows) {
296
- yield* renderTableRow(
295
+ yield* formatTableRow(
297
296
  row.map((r) => r + SPACE.repeat(ml - r.length))
298
297
  );
299
298
  yield NL;
@@ -302,21 +301,21 @@ function* renderNode(n: Node, o: Options = {}) {
302
301
  break;
303
302
  }
304
303
  case 'thead': {
305
- const [head] = [...renderChildren(n, no)];
304
+ const [head] = [...formatChildren(n, no)];
306
305
  yield head || [];
307
306
  break;
308
307
  }
309
308
  case 'tr': {
310
- yield [...renderChildren(n, no)];
309
+ yield [...formatChildren(n, no)];
311
310
  break;
312
311
  }
313
312
  case 'td':
314
313
  case 'th': {
315
- yield [...renderChildren(n, no), ...renderAnnotations(n)].join('');
314
+ yield [...formatChildren(n, no), ...formatAnnotations(n)].join('');
316
315
  break;
317
316
  }
318
317
  case 'tbody': {
319
- yield* renderChildren(n, no);
318
+ yield* formatChildren(n, no);
320
319
  break;
321
320
  }
322
321
  case 'error': {
@@ -328,10 +327,10 @@ function* renderNode(n: Node, o: Options = {}) {
328
327
  }
329
328
  }
330
329
 
331
- function* render(
330
+ function* formatValue(
332
331
  v: Value,
333
332
  o: Options = {}
334
- ): Generator<string, boolean, unknown> {
333
+ ): Generator<string, void, unknown> {
335
334
  switch (typeof v) {
336
335
  case 'undefined':
337
336
  break;
@@ -344,21 +343,19 @@ function* render(
344
343
  case 'object': {
345
344
  if (v === null) break;
346
345
  if (Array.isArray(v)) {
347
- for (const n of v) {
348
- yield* render(n, o);
349
- }
346
+ for (const n of v) yield* formatValue(n, o);
350
347
  break;
351
348
  }
352
349
  switch (v.$$mdtype) {
353
350
  case 'Function': {
354
- yield* renderFunction(v as Function);
351
+ yield* formatFunction(v as Function);
355
352
  break;
356
353
  }
357
354
  case 'Node':
358
- yield* renderNode(v as Node, o);
355
+ yield* formatNode(v as Node, o);
359
356
  break;
360
357
  case 'Variable': {
361
- yield* renderVariable(v as Variable);
358
+ yield* formatVariable(v as Variable);
362
359
  break;
363
360
  }
364
361
  default:
@@ -367,11 +364,10 @@ function* render(
367
364
  break;
368
365
  }
369
366
  }
370
- return true;
371
367
  }
372
368
 
373
369
  export default function format(v: Value, options?: Options): string {
374
370
  let doc = '';
375
- for (const s of trimStart(render(v, options))) doc += s;
371
+ for (const s of formatValue(v, options)) doc += s;
376
372
  return doc;
377
373
  }
package/src/tag.ts CHANGED
@@ -1,15 +1,18 @@
1
1
  import type { RenderableTreeNode } from './types';
2
2
 
3
- export default class Tag {
3
+ export default class Tag<
4
+ N extends string = string,
5
+ A extends Record<string, any> = Record<string, any>
6
+ > {
4
7
  readonly $$mdtype = 'Tag' as const;
5
8
 
6
- name: string;
7
- attributes: Record<string, any>;
9
+ name: N;
10
+ attributes: A;
8
11
  children: RenderableTreeNode[];
9
12
 
10
13
  constructor(
11
- name = 'div',
12
- attributes = {},
14
+ name = 'div' as N,
15
+ attributes = {} as A,
13
16
  children: RenderableTreeNode[] = []
14
17
  ) {
15
18
  this.name = name;