@markdoc/markdoc 0.1.6 → 0.1.7

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,6 +1,7 @@
1
1
  import type { Node, Value } from './types';
2
2
  declare type Options = {
3
3
  allowIndentation?: boolean;
4
+ maxTagOpeningWidth?: number;
4
5
  parent?: Node;
5
6
  indent?: number;
6
7
  };
@@ -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;AAwWF,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,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,MAAM,CAAC,EAAE,IAAI,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAyXF,MAAM,CAAC,OAAO,UAAU,MAAM,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,MAAM,CAIlE"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@markdoc/markdoc",
3
3
  "author": "Ryan Paul",
4
- "version": "0.1.6",
4
+ "version": "0.1.7",
5
5
  "description": "A text markup language for documentation",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.mjs",
@@ -79,8 +79,8 @@ function check(source, expected, options = {}) {
79
79
  throw d;
80
80
  }
81
81
 
82
- function stable(source) {
83
- return check(source, source);
82
+ function stable(source, options?) {
83
+ return check(source, source, options);
84
84
  }
85
85
 
86
86
  describe('Formatter', () => {
@@ -105,10 +105,17 @@ describe('Formatter', () => {
105
105
 
106
106
  it('variables', () => {
107
107
  const source = `
108
+ {% if $primary %}
109
+ X
110
+ {% /if %}
108
111
  {% $user.name %}
109
112
  {% key x=$user.name new=$flag /%}
110
113
  `;
111
114
  const expected = `
115
+ {% if $primary %}
116
+ X
117
+ {% /if %}
118
+
112
119
  {% $user.name %}
113
120
 
114
121
  {% key x=$user.name new=$flag /%}
@@ -128,11 +135,21 @@ describe('Formatter', () => {
128
135
  `;
129
136
 
130
137
  check(source, expected);
138
+ stable(expected, expected);
131
139
  });
132
140
 
133
141
  it('tags', () => {
134
142
  const source = `
135
143
  {% key /%}
144
+
145
+ {% a %}{% /a %}
146
+
147
+ {% a %}
148
+ {% /a %}
149
+
150
+ {% a %}
151
+
152
+ {% /a %}
136
153
 
137
154
  {% checkout %}
138
155
  {% if true %}
@@ -143,6 +160,12 @@ describe('Formatter', () => {
143
160
  const expected = `
144
161
  {% key /%}
145
162
 
163
+ {% a /%}
164
+
165
+ {% a /%}
166
+
167
+ {% a /%}
168
+
146
169
  {% checkout %}
147
170
  {% if true %}
148
171
  Yes!
@@ -168,6 +191,13 @@ Yes!
168
191
  stable(expected);
169
192
  });
170
193
 
194
+ it('long tags with maxTagOpeningWidth=Infinity', () => {
195
+ const source = `
196
+ {% tag a=true b="My very long text well over 80 characters in total" c=123456789 d=false /%}
197
+ `;
198
+ check(source, source, { maxTagOpeningWidth: Infinity });
199
+ });
200
+
171
201
  it('nested tags — allowIndentation: true', () => {
172
202
  const source = `
173
203
  {% checkout %}
@@ -186,6 +216,7 @@ Yes!
186
216
  `;
187
217
 
188
218
  check(source, expected, { allowIndentation: true });
219
+ stable(expected, { allowIndentation: true });
189
220
  });
190
221
 
191
222
  it('tables', () => {
@@ -251,6 +282,34 @@ Yes!
251
282
  stable(expected);
252
283
  });
253
284
 
285
+ it('tables with tags', () => {
286
+ const source = `
287
+ {% table %}
288
+ * H1
289
+ * H2
290
+ {% if $var %}
291
+ ---
292
+ * H3
293
+ * H4
294
+ {% /if %}
295
+ {% /table %}
296
+ `;
297
+ const expected = `
298
+ {% table %}
299
+ - H1
300
+ - H2
301
+ {% if $var %}
302
+ ---
303
+ - H3
304
+ - H4
305
+ {% /if %}
306
+ {% /table %}
307
+ `;
308
+
309
+ check(source, expected);
310
+ stable(expected);
311
+ });
312
+
254
313
  it('lists', () => {
255
314
  const source = `
256
315
  - [Install Markdoc](/docs/getting-started)
@@ -363,7 +422,13 @@ Yes!
363
422
  * Proxy status
364
423
  * \`Off\`
365
424
  * Set the proxy status to \`off\` to avoid issues during setup.
366
- {% /table %}`;
425
+ {% /table %}
426
+ 1. foo\\
427
+ baz
428
+
429
+ Soft
430
+ break
431
+ Markdoc uses…`;
367
432
 
368
433
  const expected = `
369
434
  - Create your CNAME record
@@ -396,6 +461,12 @@ Yes!
396
461
  - \`Off\`
397
462
  - Set the proxy status to \`off\` to avoid issues during setup.
398
463
  {% /table %}
464
+ 1. foo\\
465
+ baz
466
+
467
+ Soft
468
+ break
469
+ Markdoc uses…
399
470
  `;
400
471
 
401
472
  check(source, expected);
package/src/formatter.ts CHANGED
@@ -4,6 +4,7 @@ import type { AttributeValue, Function, Node, Value, Variable } from './types';
4
4
 
5
5
  type Options = {
6
6
  allowIndentation?: boolean;
7
+ maxTagOpeningWidth?: number;
7
8
  parent?: Node;
8
9
  indent?: number;
9
10
  };
@@ -14,7 +15,7 @@ const NL = '\n'; // Newline
14
15
  const OL = '1. '; // Ordered list
15
16
  const UL = '- '; // Unordered list
16
17
 
17
- const MAX_TAG_HEAD_LENGTH = 80;
18
+ const MAX_TAG_OPENING_WIDTH = 80;
18
19
 
19
20
  const max = (a: number, b: number) => Math.max(a, b);
20
21
  const increment = (o: Options, n = 2) => ({
@@ -55,7 +56,7 @@ function formatScalar(v: Value): string {
55
56
  }
56
57
 
57
58
  function formatAnnotationValue(a: AttributeValue): string {
58
- if (a.name === 'primary') return a.value;
59
+ if (a.name === 'primary') return formatScalar(a.value);
59
60
  if (a.name === 'id' && typeof a.value === 'string') return '#' + a.value;
60
61
  if (a.type === 'class') return '.' + a.name;
61
62
  return `${a.name}=${formatScalar(a.value)}`;
@@ -196,20 +197,26 @@ function* formatNode(n: Node, o: Options = {}) {
196
197
  const open = OPEN + SPACE;
197
198
  const tag = [open + n.tag, ...formatAttributes(n)];
198
199
  const inlineTag = tag.join(SPACE);
199
- if (inlineTag.length + open.length * 2 > MAX_TAG_HEAD_LENGTH) {
200
- yield tag.join(NL + SPACE.repeat(open.length) + indent);
201
- } else {
202
- yield inlineTag;
203
- }
204
- yield SPACE + (n.children.length ? '' : '/') + CLOSE;
200
+
201
+ const isLongTagOpening =
202
+ inlineTag.length + open.length * 2 >
203
+ (o.maxTagOpeningWidth || MAX_TAG_OPENING_WIDTH);
204
+
205
+ // {% tag attributes={...} %}
206
+ yield (isLongTagOpening
207
+ ? tag.join(NL + SPACE.repeat(open.length) + indent)
208
+ : inlineTag) +
209
+ SPACE +
210
+ (n.children.length ? '' : '/') +
211
+ CLOSE;
212
+
205
213
  if (n.children.length) {
206
214
  yield* formatChildren(n, no.allowIndentation ? increment(no) : no);
207
215
  if (!n.inline) {
208
216
  yield indent;
209
217
  }
210
- yield OPEN + SPACE + '/';
211
- yield n.tag;
212
- yield SPACE + CLOSE;
218
+ // {% /tag %}
219
+ yield OPEN + SPACE + '/' + n.tag + SPACE + CLOSE;
213
220
  }
214
221
  if (!n.inline) {
215
222
  yield NL;
@@ -266,25 +273,36 @@ function* formatNode(n: Node, o: Options = {}) {
266
273
  break;
267
274
  }
268
275
  case 'table': {
269
- const table = [...formatChildren(n, increment(no))] as any as string[][];
276
+ const table = [...formatChildren(n, increment(no))] as any as any[];
270
277
  if (o.parent && o.parent.type === 'tag' && o.parent.tag === 'table') {
271
- for (const row of table) {
272
- yield NL;
273
- for (const d of row) {
274
- // TODO see if we should move trim() to `td`
275
- yield indent + UL + d.trim();
276
- yield NL;
277
- }
278
- if (row !== table[table.length - 1]) {
279
- yield indent + '---';
278
+ for (let i = 0; i < table.length; i++) {
279
+ const row = table[i];
280
+ // format tags like "if" in the middle of a table list
281
+ if (typeof row === 'string') {
282
+ if (row.trim().length) {
283
+ yield NL;
284
+ yield row;
285
+ }
286
+ } else {
287
+ if (i !== 0) {
288
+ yield NL;
289
+ yield indent + '---';
290
+ }
291
+ for (let j = 0; j < row.length; j++) {
292
+ const d = row[j];
293
+ yield NL;
294
+ // TODO see if we should move trim() to `td`
295
+ yield indent + UL + d.trim();
296
+ }
280
297
  }
281
298
  }
299
+ yield NL;
282
300
  } else {
283
301
  yield NL;
284
- const [head, ...rows] = table;
302
+ const [head, ...rows] = table as string[][];
285
303
 
286
304
  const ml = table
287
- .map((arr) => arr.map((s) => s.length).reduce(max))
305
+ .map((arr) => arr.map((s: string) => s.length).reduce(max))
288
306
  .reduce(max);
289
307
 
290
308
  yield* formatTableRow(head.map((h) => h + SPACE.repeat(ml - h.length)));
@@ -369,5 +387,5 @@ function* formatValue(
369
387
  export default function format(v: Value, options?: Options): string {
370
388
  let doc = '';
371
389
  for (const s of formatValue(v, options)) doc += s;
372
- return doc;
390
+ return doc.trimStart();
373
391
  }
package/src/schema.ts CHANGED
@@ -226,7 +226,7 @@ export const hardbreak: Schema = {
226
226
  };
227
227
 
228
228
  export const softbreak: Schema = {
229
- transform(_node, _config) {
229
+ transform() {
230
230
  return ' ';
231
231
  },
232
232
  };
@@ -232,6 +232,52 @@ describe('validate', function () {
232
232
  });
233
233
  });
234
234
 
235
+ describe('attribute validation', () => {
236
+ it('should return error on failure to match array', () => {
237
+ const example = '{% foo jawn="cat" /%}';
238
+ const schema = {
239
+ tags: {
240
+ foo: {
241
+ attributes: {
242
+ jawn: {
243
+ type: String,
244
+ matches: ['bar', 'baz', 'bat'],
245
+ },
246
+ },
247
+ },
248
+ },
249
+ };
250
+
251
+ expect(validate(example, schema)).toDeepEqualSubset([
252
+ {
253
+ type: 'tag',
254
+ error: {
255
+ id: 'attribute-value-invalid',
256
+ message: `Attribute 'jawn' must match one of ["bar","baz","bat"]. Got 'cat' instead.`,
257
+ },
258
+ },
259
+ ]);
260
+ });
261
+
262
+ // https://github.com/markdoc/markdoc/issues/122
263
+ it('should validate partial file attributes', () => {
264
+ const example = `{% partial file="non-existent.md" /%}`;
265
+ const output = validate(example, {});
266
+
267
+ expect(output).toDeepEqualSubset([
268
+ {
269
+ type: 'tag',
270
+ error: {
271
+ id: 'attribute-value-invalid',
272
+ level: 'error',
273
+ message:
274
+ "Partial `non-existent.md` not found. The 'file' attribute must be set in `config.partials`",
275
+ },
276
+ },
277
+ ]);
278
+ });
279
+ });
280
+
235
281
  describe('custom type registration example', () => {
236
282
  class Link {
237
283
  validate(value) {
@@ -337,22 +383,4 @@ describe('validate', function () {
337
383
 
338
384
  expect(output).toEqual([]);
339
385
  });
340
-
341
- // https://github.com/markdoc/markdoc/issues/122
342
- it('should validate partial file attributes', () => {
343
- const example = `{% partial file="non-existent.md" /%}`;
344
- const output = validate(example, {});
345
-
346
- expect(output).toDeepEqualSubset([
347
- {
348
- type: 'tag',
349
- error: {
350
- id: 'attribute-value-invalid',
351
- level: 'error',
352
- message:
353
- "Partial `non-existent.md` not found. The 'file' attribute must be set in `config.partials`",
354
- },
355
- },
356
- ]);
357
- });
358
386
  });
package/src/validator.ts CHANGED
@@ -220,14 +220,14 @@ export default function validate(node: Node, config: Config) {
220
220
  level: errorLevel || 'error',
221
221
  message: `Attribute '${key}' must match one of ${JSON.stringify(
222
222
  matches
223
- )}`,
223
+ )}. Got '${value}' instead.`,
224
224
  });
225
225
 
226
226
  if (matches instanceof RegExp && !matches.test(value))
227
227
  errors.push({
228
228
  id: 'attribute-value-invalid',
229
229
  level: errorLevel || 'error',
230
- message: `Attribute '${key}' must match ${matches}`,
230
+ message: `Attribute '${key}' must match ${matches}. Got '${value}' instead.`,
231
231
  });
232
232
  }
233
233