@markdoc/markdoc 0.1.12 → 0.2.0

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 +9 -7
  2. package/dist/index.d.ts +3 -3
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +83 -53
  5. package/dist/index.js.map +3 -3
  6. package/dist/index.mjs +83 -53
  7. package/dist/index.mjs.map +3 -3
  8. package/dist/react.js +16 -5
  9. package/dist/react.js.map +3 -3
  10. package/dist/src/formatter.d.ts.map +1 -1
  11. package/dist/src/parser.d.ts.map +1 -1
  12. package/dist/src/renderers/html.d.ts.map +1 -1
  13. package/dist/src/renderers/react/react.d.ts +1 -1
  14. package/dist/src/renderers/react/react.d.ts.map +1 -1
  15. package/dist/src/renderers/react/static.d.ts +1 -1
  16. package/dist/src/renderers/react/static.d.ts.map +1 -1
  17. package/dist/src/schema-types/id.d.ts +5 -0
  18. package/dist/src/schema-types/id.d.ts.map +1 -0
  19. package/dist/src/tag.d.ts +1 -0
  20. package/dist/src/tag.d.ts.map +1 -1
  21. package/dist/src/tags/conditional.d.ts +1 -1
  22. package/dist/src/tags/conditional.d.ts.map +1 -1
  23. package/dist/src/tags/partial.d.ts.map +1 -1
  24. package/dist/src/tags/table.d.ts.map +1 -1
  25. package/dist/src/transformer.d.ts.map +1 -1
  26. package/dist/src/types.d.ts +2 -1
  27. package/dist/src/types.d.ts.map +1 -1
  28. package/dist/src/utils.d.ts +1 -1
  29. package/dist/src/utils.d.ts.map +1 -1
  30. package/dist/src/validator.d.ts.map +1 -1
  31. package/index.ts +3 -3
  32. package/package.json +10 -3
  33. package/src/formatter.test.ts +109 -6
  34. package/src/formatter.ts +51 -27
  35. package/src/parser.ts +2 -1
  36. package/src/renderers/html.test.ts +17 -6
  37. package/src/renderers/html.ts +4 -2
  38. package/src/renderers/react/react.test.ts +5 -12
  39. package/src/renderers/react/react.ts +21 -4
  40. package/src/renderers/react/static.ts +21 -3
  41. package/src/schema-types/id.ts +15 -0
  42. package/src/tag.ts +4 -0
  43. package/src/tags/conditional.ts +11 -2
  44. package/src/tags/partial.ts +1 -0
  45. package/src/tags/table.ts +1 -0
  46. package/src/transformer.ts +14 -3
  47. package/src/types.ts +2 -1
  48. package/src/utils.ts +2 -2
  49. package/src/validator.test.ts +67 -0
  50. package/src/validator.ts +9 -6
  51. package/dist/src/renderers/react/shared.d.ts +0 -5
  52. package/dist/src/renderers/react/shared.d.ts.map +0 -1
  53. package/src/renderers/react/shared.ts +0 -16
package/src/parser.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import Node from './ast/node';
2
2
  import transforms from './transforms/index';
3
+ import { OPEN } from './utils';
3
4
 
4
5
  import type { AttributeValue } from './types';
5
6
 
@@ -49,7 +50,7 @@ function handleAttrs(token: Token, type: string) {
49
50
  return { content: (token.meta || {}).variable || token.content };
50
51
  case 'fence': {
51
52
  const [language] = token.info.split(' ', 1);
52
- return language === ''
53
+ return language === '' || language === OPEN
53
54
  ? { content: token.content }
54
55
  : { content: token.content, language };
55
56
  }
@@ -1,39 +1,43 @@
1
1
  import render from './html';
2
2
  import { RenderableTreeNode } from '../types';
3
+ import Tag from '../tag';
3
4
 
4
5
  function tag(
5
6
  name: string,
6
7
  attributes: Record<string, any> = {},
7
8
  children: RenderableTreeNode[] = []
8
9
  ) {
9
- return { name, attributes, children };
10
+ return new Tag(name, attributes, children);
10
11
  }
11
12
 
12
13
  describe('HTML renderer', function () {
13
14
  it('rendering a tag', function () {
14
- const example = render(tag('h1', null, ['test'])).trim();
15
+ const example = render(tag('h1', undefined, ['test'])).trim();
15
16
  expect(example).toEqual('<h1>test</h1>');
16
17
  });
17
18
 
18
19
  it('rendering string child nodes', function () {
19
- const example = tag('h1', null, ['test ', '1']);
20
+ const example = tag('h1', undefined, ['test ', '1']);
20
21
  expect(render(example)).toEqual('<h1>test 1</h1>');
21
22
  });
22
23
 
23
24
  it('rendering nested tags', function () {
24
- const example = tag('div', null, [tag('p', null, ['test'])]);
25
+ const example = tag('div', undefined, [tag('p', undefined, ['test'])]);
25
26
 
26
27
  expect(render(example)).toEqual('<div><p>test</p></div>');
27
28
  });
28
29
 
29
30
  it('rendering parallel tags', function () {
30
- const example = [tag('p', null, ['foo']), tag('p', null, ['bar'])];
31
+ const example = [
32
+ tag('p', undefined, ['foo']),
33
+ tag('p', undefined, ['bar']),
34
+ ];
31
35
 
32
36
  expect(render(example)).toEqual('<p>foo</p><p>bar</p>');
33
37
  });
34
38
 
35
39
  it('rendering a tag with an invalid child', function () {
36
- const example = tag('div', null, ['test', { foo: 'bar' }]);
40
+ const example = tag('div', undefined, ['test', { foo: 'bar' }]);
37
41
 
38
42
  expect(render(example)).toEqual('<div>test</div>');
39
43
  });
@@ -43,6 +47,13 @@ describe('HTML renderer', function () {
43
47
  expect(render(example)).toEqual('<hr>');
44
48
  });
45
49
 
50
+ it('rendering a tag with numeric children', function () {
51
+ const content = tag('p', {}, [1]);
52
+
53
+ const html = render(content);
54
+ expect(html).toEqual('<p>1</p>');
55
+ });
56
+
46
57
  describe('attributes', function () {
47
58
  it('with basic value', function () {
48
59
  const example = tag('foo', { bar: 'baz' });
@@ -1,4 +1,5 @@
1
1
  import MarkdownIt from 'markdown-it';
2
+ import Tag from '../tag';
2
3
  import type { RenderableTreeNodes } from '../types';
3
4
  const { escapeHtml } = MarkdownIt().utils;
4
5
 
@@ -22,11 +23,12 @@ const voidElements = new Set([
22
23
  ]);
23
24
 
24
25
  export default function render(node: RenderableTreeNodes): string {
25
- if (typeof node === 'string') return escapeHtml(node);
26
+ if (typeof node === 'string' || typeof node === 'number')
27
+ return escapeHtml(String(node));
26
28
 
27
29
  if (Array.isArray(node)) return node.map(render).join('');
28
30
 
29
- if (node === null || typeof node !== 'object') return '';
31
+ if (node === null || typeof node !== 'object' || !Tag.isTag(node)) return '';
30
32
 
31
33
  const { name, attributes, children = [] } = node;
32
34
 
@@ -1,5 +1,6 @@
1
1
  import dynamic from './react';
2
2
  import renderStatic from './static';
3
+ import Tag from '../../tag';
3
4
 
4
5
  const React = {
5
6
  Fragment: 'fragment',
@@ -52,7 +53,7 @@ describe('React dynamic renderer', function () {
52
53
 
53
54
  it('rendering an external component', function () {
54
55
  const components = { Foo: 'bar' };
55
- const example = { name: 'Foo', children: ['test'] };
56
+ const example = new Tag('Foo', undefined, ['test']);
56
57
  const output = dynamic(example, React, { components });
57
58
  expect(output).toDeepEqualSubset({
58
59
  name: 'bar',
@@ -73,11 +74,7 @@ describe('React dynamic renderer', function () {
73
74
  });
74
75
 
75
76
  it('with a class attribute', function () {
76
- const example = {
77
- name: 'h1',
78
- attributes: { class: 'foo bar' },
79
- children: ['test'],
80
- };
77
+ const example = new Tag('h1', { class: 'foo bar' }, ['test']);
81
78
 
82
79
  const output = dynamic(example, React);
83
80
  expect(output).toDeepEqualSubset({
@@ -101,11 +98,7 @@ describe('React dynamic renderer', function () {
101
98
 
102
99
  describe('rendering built-in nodes', function () {
103
100
  it('rendering a fenced code block', function () {
104
- const example = {
105
- name: 'pre',
106
- attributes: { class: 'code code-ruby' },
107
- children: ['test'],
108
- };
101
+ const example = new Tag('pre', { class: 'code code-ruby' }, ['test']);
109
102
 
110
103
  const output = dynamic(example, React);
111
104
  expect(output).toDeepEqual({
@@ -164,7 +157,7 @@ describe('React static renderer', function () {
164
157
 
165
158
  it('rendering an external component', function () {
166
159
  const components = { Foo: 'bar' };
167
- const example = { name: 'Foo', children: ['test'] };
160
+ const example = new Tag('Foo', undefined, ['test']);
168
161
  const code = renderStatic(example);
169
162
  const output = eval(code)({ components });
170
163
 
@@ -1,12 +1,28 @@
1
- import { tagName } from './shared';
2
- import type { createElement, Fragment, ReactNode } from 'react';
3
- import type { RenderableTreeNodes, Scalar } from '../../types';
1
+ import Tag from '../../tag';
2
+ import { RenderableTreeNodes, Scalar } from '../../types';
3
+
4
+ import type { createElement, ComponentType, Fragment, ReactNode } from 'react';
4
5
 
5
6
  type ReactShape = Readonly<{
6
7
  createElement: typeof createElement;
7
8
  Fragment: typeof Fragment;
8
9
  }>;
9
10
 
11
+ type Component = ComponentType<unknown>;
12
+
13
+ function tagName(
14
+ name: string,
15
+ components: Record<string, Component> | ((string: string) => Component)
16
+ ): string | Component {
17
+ return typeof name !== 'string'
18
+ ? name // This can be an object, e.g. when React.forwardRef is used
19
+ : name[0] !== name[0].toUpperCase()
20
+ ? name
21
+ : components instanceof Function
22
+ ? components(name)
23
+ : components[name];
24
+ }
25
+
10
26
  export default function dynamic(
11
27
  node: RenderableTreeNodes,
12
28
  React: ReactShape,
@@ -30,7 +46,8 @@ export default function dynamic(
30
46
  if (Array.isArray(node))
31
47
  return React.createElement(React.Fragment, null, ...node.map(render));
32
48
 
33
- if (node === null || typeof node !== 'object') return node;
49
+ if (node === null || typeof node !== 'object' || !Tag.isTag(node))
50
+ return node;
34
51
 
35
52
  const {
36
53
  name,
@@ -1,5 +1,22 @@
1
- import { tagName } from './shared';
2
- import type { RenderableTreeNode, RenderableTreeNodes } from '../../types';
1
+ import Tag from '../../tag';
2
+ import { RenderableTreeNode, RenderableTreeNodes } from '../../types';
3
+
4
+ import type { ComponentType } from 'react';
5
+
6
+ type Component = ComponentType<unknown>;
7
+
8
+ function tagName(
9
+ name: string,
10
+ components: Record<string, Component> | ((string: string) => Component)
11
+ ): string | Component {
12
+ return typeof name !== 'string'
13
+ ? 'Fragment'
14
+ : name[0] !== name[0].toUpperCase()
15
+ ? name
16
+ : components instanceof Function
17
+ ? components(name)
18
+ : components[name];
19
+ }
3
20
 
4
21
  function renderArray(children: RenderableTreeNode[]): string {
5
22
  return children.map(render).join(', ');
@@ -26,7 +43,8 @@ function render(node: RenderableTreeNodes): string {
26
43
  if (Array.isArray(node))
27
44
  return `React.createElement(React.Fragment, null, ${renderArray(node)})`;
28
45
 
29
- if (node === null || typeof node !== 'object') return JSON.stringify(node);
46
+ if (node === null || typeof node !== 'object' || !Tag.isTag(node))
47
+ return JSON.stringify(node);
30
48
 
31
49
  const {
32
50
  name,
@@ -0,0 +1,15 @@
1
+ import type { CustomAttributeTypeInterface, ValidationError } from '../types';
2
+
3
+ export class Id implements CustomAttributeTypeInterface {
4
+ validate(value: any): ValidationError[] {
5
+ if (typeof value === 'string' && value.match(/^[a-zA-Z]/)) return [];
6
+
7
+ return [
8
+ {
9
+ id: 'attribute-value-invalid',
10
+ level: 'error',
11
+ message: "The 'id' attribute must start with a letter",
12
+ },
13
+ ];
14
+ }
15
+ }
package/src/tag.ts CHANGED
@@ -6,6 +6,10 @@ export default class Tag<
6
6
  > {
7
7
  readonly $$mdtype = 'Tag' as const;
8
8
 
9
+ static isTag = (tag: any): tag is Tag => {
10
+ return '$$mdtype' in tag && tag.$$mdtype === 'Tag';
11
+ };
12
+
9
13
  name: N;
10
14
  attributes: A;
11
15
  children: RenderableTreeNode[];
@@ -1,6 +1,13 @@
1
1
  import { isPromise } from '../utils';
2
2
 
3
- import type { Node, RenderableTreeNode, Schema, Value } from '../types';
3
+ import {
4
+ MaybePromise,
5
+ Node,
6
+ RenderableTreeNode,
7
+ RenderableTreeNodes,
8
+ Schema,
9
+ Value,
10
+ } from '../types';
4
11
 
5
12
  type Condition = { condition: Value; children: Node[] };
6
13
 
@@ -34,7 +41,9 @@ export const tagIf: Schema = {
34
41
  const conditions = renderConditions(node);
35
42
  for (const { condition, children } of conditions)
36
43
  if (truthy(condition)) {
37
- const nodes = children.flatMap((child) => child.transform(config));
44
+ const nodes = children.flatMap<MaybePromise<RenderableTreeNodes>>(
45
+ (child) => child.transform(config)
46
+ );
38
47
  if (nodes.some(isPromise)) {
39
48
  return Promise.all(nodes).then((nodes) => nodes.flat());
40
49
  }
@@ -19,6 +19,7 @@ class PartialFile {
19
19
  }
20
20
 
21
21
  export const partial: Schema = {
22
+ inline: false,
22
23
  selfClosing: true,
23
24
  attributes: {
24
25
  file: { type: PartialFile, render: false, required: true },
package/src/tags/table.ts CHANGED
@@ -2,4 +2,5 @@ import type { Schema } from '../types';
2
2
 
3
3
  export const table: Schema = {
4
4
  children: ['table'],
5
+ inline: false,
5
6
  };
@@ -1,13 +1,22 @@
1
1
  import Tag from './tag';
2
2
  import { Class } from './schema-types/class';
3
+ import { Id } from './schema-types/id';
3
4
  import { isPromise } from './utils';
4
- import type { Config, Node, NodeType, Schema, Transformer } from './types';
5
+ import type {
6
+ Config,
7
+ MaybePromise,
8
+ Node,
9
+ NodeType,
10
+ RenderableTreeNodes,
11
+ Schema,
12
+ Transformer,
13
+ } from './types';
5
14
 
6
15
  type AttributesSchema = Schema['attributes'];
7
16
 
8
17
  export const globalAttributes: AttributesSchema = {
9
18
  class: { type: Class, render: true },
10
- id: { type: String, render: true },
19
+ id: { type: Id, render: true },
11
20
  };
12
21
 
13
22
  export default {
@@ -42,7 +51,9 @@ export default {
42
51
  },
43
52
 
44
53
  children(node: Node, config: Config = {}) {
45
- const children = node.children.flatMap((child) => this.node(child, config));
54
+ const children = node.children.flatMap<MaybePromise<RenderableTreeNodes>>(
55
+ (child) => this.node(child, config)
56
+ );
46
57
  if (children.some(isPromise)) {
47
58
  return Promise.all(children);
48
59
  }
package/src/types.ts CHANGED
@@ -93,7 +93,7 @@ export type NodeType =
93
93
 
94
94
  export type Primitive = null | boolean | number | string;
95
95
 
96
- export type RenderableTreeNode = Tag | string | null;
96
+ export type RenderableTreeNode = Tag | Scalar;
97
97
  export type RenderableTreeNodes = RenderableTreeNode | RenderableTreeNode[];
98
98
 
99
99
  export type Scalar = Primitive | Scalar[] | { [key: string]: Scalar };
@@ -103,6 +103,7 @@ export type Schema<C extends Config = Config, R = string> = {
103
103
  children?: string[];
104
104
  attributes?: Record<string, SchemaAttribute>;
105
105
  selfClosing?: boolean;
106
+ inline?: boolean;
106
107
  transform?(node: Node, config: C): MaybePromise<RenderableTreeNodes>;
107
108
  validate?(node: Node, config: C): MaybePromise<ValidationError[]>;
108
109
  };
package/src/utils.ts CHANGED
@@ -15,8 +15,8 @@ export const CLOSE = '%}';
15
15
 
16
16
  export const IDENTIFIER_REGEX = /^[a-zA-Z0-9_-]+$/;
17
17
 
18
- export function isIdentifier(s: string): boolean {
19
- return IDENTIFIER_REGEX.test(s);
18
+ export function isIdentifier(s: any): s is string {
19
+ return typeof s === 'string' && IDENTIFIER_REGEX.test(s);
20
20
  }
21
21
 
22
22
  export function isPromise(a: any): a is Promise<any> {
@@ -232,6 +232,62 @@ describe('validate', function () {
232
232
  });
233
233
  });
234
234
 
235
+ describe('inline rule', () => {
236
+ const config = {
237
+ tags: {
238
+ foo: { inline: true },
239
+ bar: { inline: false },
240
+ baz: {},
241
+ },
242
+ };
243
+
244
+ it('allows inline or block when undefined', () => {
245
+ const inline = validate(`this is inline {% baz %}bar{% /baz %}`, config);
246
+ expect(inline).toEqual([]);
247
+
248
+ const block = validate(
249
+ `
250
+ {% baz %}
251
+ bar
252
+ {% /baz %}
253
+ `,
254
+ config
255
+ );
256
+ expect(block).toEqual([]);
257
+ });
258
+
259
+ it('validates inline tag', () => {
260
+ const correct = validate(`this is inline {% foo %}bar{% /foo %}`, config);
261
+ expect(correct).toEqual([]);
262
+
263
+ const wrong = validate(
264
+ `
265
+ {% foo %}
266
+ bar
267
+ {% /foo %}
268
+ `,
269
+ config
270
+ );
271
+ expect(wrong[0]?.error.id).toEqual('tag-placement-invalid');
272
+ expect(wrong[0]?.error.message).toContain('should be inline');
273
+ });
274
+
275
+ it('validates block tag', () => {
276
+ const correct = validate(
277
+ `
278
+ {% bar %}
279
+ bar
280
+ {% /bar %}
281
+ `,
282
+ config
283
+ );
284
+ expect(correct).toEqual([]);
285
+ const wrong = validate(`this is inline {% bar %}bar{% /bar %}`, config);
286
+ expect(wrong[0]?.error.id).toEqual('tag-placement-invalid');
287
+ expect(wrong[0]?.error.message).toContain('should be block');
288
+ });
289
+ });
290
+
235
291
  describe('attribute validation', () => {
236
292
  it('should return error on failure to match array', () => {
237
293
  const example = '{% foo jawn="cat" /%}';
@@ -276,6 +332,17 @@ describe('validate', function () {
276
332
  },
277
333
  ]);
278
334
  });
335
+
336
+ it('properly validates ids', () => {
337
+ const correct = validate(`# foo {% #bar %}`, {});
338
+ expect(correct).toEqual([]);
339
+
340
+ const number = validate(`# foo {% #1bar %}`, {});
341
+ expect(number[0]?.error.id).toEqual('attribute-value-invalid');
342
+
343
+ const hash = validate(`# foo {% id="#bar" %}`, {});
344
+ expect(hash[0]?.error.id).toEqual('attribute-value-invalid');
345
+ });
279
346
  });
280
347
 
281
348
  describe('custom type registration example', () => {
package/src/validator.ts CHANGED
@@ -137,6 +137,15 @@ export default function validator(node: Node, config: Config) {
137
137
  return errors;
138
138
  }
139
139
 
140
+ if (schema.inline != undefined && node.inline !== schema.inline)
141
+ errors.push({
142
+ id: 'tag-placement-invalid',
143
+ level: 'critical',
144
+ message: `'${node.tag}' tag should be ${
145
+ schema.inline ? 'inline' : 'block'
146
+ }`,
147
+ });
148
+
140
149
  if (schema.selfClosing && node.children.length > 0)
141
150
  errors.push({
142
151
  id: 'tag-selfclosing-has-children',
@@ -190,12 +199,6 @@ export default function validator(node: Node, config: Config) {
190
199
  }
191
200
 
192
201
  value = value as string;
193
- if (key === 'id' && value.match(/^[0-9]/))
194
- errors.push({
195
- id: 'attribute-value-invalid',
196
- level: 'error',
197
- message: 'The id attribute must not start with a number',
198
- });
199
202
 
200
203
  if (type) {
201
204
  const valid = validateType(type, value, config);
@@ -1,5 +0,0 @@
1
- import type { ComponentType } from 'react';
2
- declare type Component = ComponentType<unknown>;
3
- export declare function tagName(name: string, components: Record<string, Component> | ((string: string) => Component)): string | Component;
4
- export {};
5
- //# sourceMappingURL=shared.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../../../../src/renderers/react/shared.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AAE3C,aAAK,SAAS,GAAG,aAAa,CAAC,OAAO,CAAC,CAAC;AAExC,wBAAgB,OAAO,CACrB,IAAI,EAAE,MAAM,EACZ,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,MAAM,KAAK,SAAS,CAAC,GACtE,MAAM,GAAG,SAAS,CAQpB"}
@@ -1,16 +0,0 @@
1
- import type { ComponentType } from 'react';
2
-
3
- type Component = ComponentType<unknown>;
4
-
5
- export function tagName(
6
- name: string,
7
- components: Record<string, Component> | ((string: string) => Component)
8
- ): string | Component {
9
- return typeof name !== 'string'
10
- ? 'Fragment'
11
- : name[0] !== name[0].toUpperCase()
12
- ? name
13
- : components instanceof Function
14
- ? components(name)
15
- : components[name];
16
- }