@markdoc/markdoc 0.5.14 → 0.6.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.
package/dist/react.js.map CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/tag.ts", "../src/renderers/react/react.ts"],
4
- "sourcesContent": ["import type { RenderableTreeNode } from './types';\n\nexport default class Tag<\n N extends string = string,\n A extends Record<string, any> = Record<string, any>,\n> {\n readonly $$mdtype = 'Tag' as const;\n\n static isTag = (tag: any): tag is Tag => {\n return !!(tag?.$$mdtype === 'Tag');\n };\n\n name: N;\n attributes: A;\n children: RenderableTreeNode[];\n\n constructor(\n name = 'div' as N,\n attributes = {} as A,\n children: RenderableTreeNode[] = [],\n ) {\n this.name = name;\n this.attributes = attributes;\n this.children = children;\n }\n}\n", "import type { ComponentType, createElement, Fragment, ReactNode } from 'react';\nimport Tag from '../../tag';\nimport type { RenderableTreeNodes } from '../../types';\n\ntype ReactShape = Readonly<{\n createElement: typeof createElement;\n Fragment: typeof Fragment;\n}>;\n\ntype Component = ComponentType<any>;\n\nfunction tagName(\n name: string,\n components: Record<string, Component> | ((string: string) => Component),\n): string | Component {\n return typeof name !== 'string'\n ? name // This can be an object, e.g. when React.forwardRef is used\n : name[0] !== name[0].toUpperCase()\n ? name\n : components instanceof Function\n ? components(name)\n : components[name];\n}\n\nexport type RenderOpts = {\n components?: Record<string, Component> | ((string: string) => Component);\n resolveTagName?: typeof tagName;\n};\nexport default function dynamic(\n node: RenderableTreeNodes,\n React: ReactShape,\n { components = {}, resolveTagName = tagName }: RenderOpts = {},\n) {\n function deepRender(value: any): any {\n if (value == null || typeof value !== 'object') return value;\n\n if (Array.isArray(value)) return value.map((item) => deepRender(item));\n\n if (value.$$mdtype === 'Tag') return render(value);\n\n if (typeof value !== 'object') return value;\n\n return Object.fromEntries(\n Object.entries(value).map(([k, v]) => [k, deepRender(v)]),\n );\n }\n\n function render(node: RenderableTreeNodes): ReactNode {\n if (Array.isArray(node))\n return React.createElement(React.Fragment, null, ...node.map(render));\n\n if (node === null || typeof node !== 'object' || !Tag.isTag(node)) {\n // Preserve passthrough values from custom transforms, including React elements.\n return node as ReactNode;\n }\n\n const {\n name,\n attributes: { class: className, ...attrs } = {},\n children = [],\n } = node;\n\n if (className) attrs.className = className;\n\n return React.createElement(\n resolveTagName(name, components),\n Object.keys(attrs).length === 0 ? null : deepRender(attrs),\n ...children.map(render),\n );\n }\n\n return render(node);\n}\n"],
4
+ "sourcesContent": ["import type { RenderableTreeNode } from './types';\n\nexport default class Tag<\n N extends string = string,\n A extends Record<string, any> = Record<string, any>,\n> {\n readonly $$mdtype = 'Tag' as const;\n\n static isTag = (tag: any): tag is Tag => {\n return !!(tag?.$$mdtype === 'Tag');\n };\n\n name: N;\n attributes: A;\n children: RenderableTreeNode[];\n\n constructor(\n name = 'div' as N,\n attributes = {} as A,\n children: RenderableTreeNode[] = [],\n ) {\n this.name = name;\n this.attributes = attributes;\n this.children = children;\n }\n}\n", "import type { ComponentType, createElement, Fragment, ReactNode } from 'react';\nimport Tag from '../../tag';\nimport type { RenderableTreeNodes } from '../../types';\n\ntype ReactShape = Readonly<{\n createElement: typeof createElement;\n Fragment: typeof Fragment;\n}>;\n\ntype Component = string | ComponentType<any>;\n\nfunction tagName(\n name: string,\n components: Record<string, Component> | ((string: string) => Component),\n): string | Component {\n return typeof name !== 'string'\n ? name // This can be an object, e.g. when React.forwardRef is used\n : name[0] !== name[0].toUpperCase()\n ? name\n : components instanceof Function\n ? components(name)\n : components[name];\n}\n\nexport type RenderOpts = {\n components?: Record<string, Component> | ((string: string) => Component);\n resolveTagName?: typeof tagName;\n};\nexport default function dynamic(\n node: RenderableTreeNodes,\n React: ReactShape,\n { components = {}, resolveTagName = tagName }: RenderOpts = {},\n) {\n function deepRender(value: any): any {\n if (value == null || typeof value !== 'object') return value;\n\n if (Array.isArray(value)) return value.map((item) => deepRender(item));\n\n if (value.$$mdtype === 'Tag') return render(value);\n\n if (typeof value !== 'object') return value;\n\n return Object.fromEntries(\n Object.entries(value).map(([k, v]) => [k, deepRender(v)]),\n );\n }\n\n function render(node: RenderableTreeNodes): ReactNode {\n if (Array.isArray(node))\n return React.createElement(React.Fragment, null, ...node.map(render));\n\n if (node === null || typeof node !== 'object' || !Tag.isTag(node)) {\n // Preserve passthrough values from custom transforms, including React elements.\n return node as ReactNode;\n }\n\n const {\n name,\n attributes: { class: className, ...attrs } = {},\n children = [],\n } = node;\n\n if (className) attrs.className = className;\n\n return React.createElement(\n resolveTagName(name, components),\n Object.keys(attrs).length === 0 ? null : deepRender(attrs),\n ...children.map(render),\n );\n }\n\n return render(node);\n}\n"],
5
5
  "mappings": ";AAEA,IAAqB,MAArB,MAGE;AAAA,EAWA,YACE,OAAO,OACP,aAAa,CAAC,GACd,WAAiC,CAAC,GAClC;AAdF,SAAS,WAAW;AAelB,SAAK,OAAO;AACZ,SAAK,aAAa;AAClB,SAAK,WAAW;AAAA,EAClB;AAAA,EAhBA;AAAA,SAAO,QAAQ,CAAC,QAAyB;AACvC,aAAO,CAAC,EAAE,KAAK,aAAa;AAAA,IAC9B;AAAA;AAeF;;;ACdA,SAAS,QACP,MACA,YACoB;AACpB,SAAO,OAAO,SAAS,WACnB,OACA,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,YAAY,IAC9B,OACA,sBAAsB,WACpB,WAAW,IAAI,IACf,WAAW,IAAI;AACzB;AAMe,SAAR,QACL,MACA,OACA,EAAE,aAAa,CAAC,GAAG,iBAAiB,QAAQ,IAAgB,CAAC,GAC7D;AACA,WAAS,WAAW,OAAiB;AACnC,QAAI,SAAS,QAAQ,OAAO,UAAU,SAAU,QAAO;AAEvD,QAAI,MAAM,QAAQ,KAAK,EAAG,QAAO,MAAM,IAAI,CAAC,SAAS,WAAW,IAAI,CAAC;AAErE,QAAI,MAAM,aAAa,MAAO,QAAO,OAAO,KAAK;AAEjD,QAAI,OAAO,UAAU,SAAU,Q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AAiB,YAAY,CAAC,KAAK,EAAE,GAAG,GAAG,SAAS,CAAC,OAAO,EAAE,IAAI,EAAE,OAAO,CAAC,CAW3E;AAED,wBAAgB,OAAO,CAAC,KAAK,EAAE,GAAG,EAAE,MAAM,GAAE,MAAW,GAAG,GAAG,CAyB5D"}
1
+ {"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../../src/ast/base.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,QAAQ,IAAI,WAAW,EACvB,OAAO,EACP,QAAQ,IAAI,WAAW,EACvB,MAAM,EAEP,MAAM,UAAU,CAAC;AAIlB,wBAAgB,KAAK,CAAC,KAAK,CAAC,EAAE,GAAG,GAAG,KAAK,IAAI,OAAO,CAEnD;AAED,wBAAgB,UAAU,CAAC,KAAK,CAAC,EAAE,GAAG,GAAG,KAAK,IAAI,WAAW,CAE5D;AAED,wBAAgB,UAAU,CAAC,KAAK,CAAC,EAAE,GAAG,GAAG,KAAK,IAAI,WAAW,CAE5D;AAED,wBAAiB,YAAY,CAAC,KAAK,EAAE,GAAG,GAAG,SAAS,CAAC,OAAO,EAAE,IAAI,EAAE,OAAO,CAAC,CAW3E;AAED,wBAAgB,OAAO,CAAC,KAAK,EAAE,GAAG,EAAE,MAAM,GAAE,MAAW,GAAG,GAAG,CA8B5D"}
@@ -1 +1 @@
1
- {"version":3,"file":"node.d.ts","sourceRoot":"","sources":["../../../src/ast/node.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,OAAO,EACP,cAAc,EACd,MAAM,EACN,QAAQ,EACR,YAAY,EACZ,QAAQ,EACR,kBAAkB,EAClB,mBAAmB,EACnB,MAAM,EACN,eAAe,EAChB,MAAM,UAAU,CAAC;AAGlB,MAAM,CAAC,OAAO,OAAO,IAAK,YAAW,OAAO;IAC1C,QAAQ,CAAC,QAAQ,UAAU;IAE3B,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAChC,KAAK,EAAE,MAAM,CAAC,M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AAC,MAAM,EAAE,IAAI,CAAC,CAAC;IAC5B,QAAQ,EAAE,IAAI,EAAE,CAAC;IACjB,MAAM,EAAE,eAAe,EAAE,CAAM;IAC/B,KAAK,EAAE,MAAM,EAAE,CAAM;IACrB,IAAI,EAAE,QAAQ,CAAC;IACf,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,cAAc,EAAE,CAAC;IAE9B,MAAM,UAAS;IACf,QAAQ,CAAC,EAAE,QAAQ,CAAC;gBAGlB,IAAI,GAAE,QAAiB,EACvB,UAAU,GAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAM,EACpC,QAAQ,GAAE,IAAI,EAAO,EACrB,GAAG,CAAC,EAAE,MAAM;IAUb,IAAI,IAAI,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC;IAmBvC,IAAI,CAAC,IAAI,EAAE,IAAI;IAIf,OAAO,CAAC,MAAM,GAAE,MAAW,GAAG,IAAI;IAiBlC,UAAU,CAAC,MAAM,GAAE,MAAW,GAAG,MAAM,GAAG,SAAS;IAInD,mBAAmB,CAAC,MAAM,GAAE,MAAW;IAIvC,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,kBAAkB,EAAE;IAIvD,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,YAAY,CAAC,mBAAmB,CAAC;CAG7D"}
@@ -4,7 +4,7 @@ type ReactShape = Readonly<{
4
4
  createElement: typeof createElement;
5
5
  Fragment: typeof Fragment;
6
6
  }>;
7
- type Component = ComponentType<any>;
7
+ type Component = string | ComponentType<any>;
8
8
  declare function tagName(name: string, components: Record<string, Component> | ((string: string) => Component)): string | Component;
9
9
  export type RenderOpts = {
10
10
  components?: Record<string, Component> | ((string: string) => Component);
@@ -1 +1 @@
1
- {"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../../../../src/renderers/react/react.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,aAAa,EAAE,QAAQ,EAAE,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AE7C,iBAAS,OAAO,CACd,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;AAED,MAAM,MAAM,UAAU,GAAG;IACvB,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,MAAM,KAAK,SAAS,CAAC,CAAC;IACzE,cAAc,CAAC,EAAE,OAAO,OAAO,CAAC;CACjC,CAAC;AACF,MAAM,CAAC,OAAO,UAAU,OAAO,CAC7B,IAAI,EAAE,mBAAmB,EACzB,KAAK,EAAE,UAAU,EACjB,EAAE,UAAe,EAAE,cAAwB,EAAE,GAAE,UAAe,aAyC/D"}
@@ -1 +1 @@
1
- {"version":3,"file":"static.d.ts","sourceRoot":"","sources":["../../../../src/renderers/react/static.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AAE3C,OAAO,KAAK,EAAsB,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAE3E,KAAK,SAAS,GAAG,aAAa,CAAC,OAAO,CAAC,CAAC;AAExC,iBAAS,OAAO,CACd,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;AA4CD,MAAM,CAAC,OAAO,UAAU,WAAW,CACjC,IAAI,EAAE,mBAAmB,EACzB,EAAE,cAAwB,EAAE,GAAE;IAAE,cAAc,CAAC,EAAE,OAAO,OAAO,CAAA;CAAO,GACrE,MAAM,CAYR"}
1
+ {"version":3,"file":"static.d.ts","sourceRoot":"","sources":["../../../../src/renderers/react/static.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAA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E,CAAC,CAAC,CAI3B;AA6BD,wBAAgB,YAAY,CAAC,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,2EAoBzD"}
1
+ {"version":3,"file":"validator.d.ts","sourceRoot":"","sources":["../../src/validator.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,MAAM,EAGN,IAAI,EACJ,eAAe,EACf,aAAa,EACb,eAAe,EACf,KAAK,EACN,MAAM,SAAS,CAAC;AAWjB,KAAK,SAAS,GAAG,WAAW,CAAC,eAAe,CAAC,MAAM,CAAC,CAAC,CAAC;AAEtD,wBAAgB,YAAY,CAC1B,IAAI,EAAE,SAAS,EACf,KAAK,EAAE,KAAK,EACZ,MAAM,EAAE,MAAM,EACd,GAAG,EAAE,MAAM,GACV,OAAO,GAAG,eAAe,EAAE,CA2B7B;AA2ED,MAAM,CAAC,OAAO,UAAU,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,kDA6K3D;AAED,wBAAiB,eAAe,CAC9B,IAAI,EAAE,IAAI,EACV,OAAO,GAAE,IAAI,EAAO,GACnB,SAAS,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC,CA+B3B;AA6BD,wBAAgB,YAAY,CAAC,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,2EAoBzD"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@markdoc/markdoc",
3
3
  "author": "Ryan Paul",
4
- "version": "0.5.14",
4
+ "version": "0.6.0",
5
5
  "description": "A text markup language for documentation",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.mjs",
@@ -33,12 +33,13 @@
33
33
  "diff": "^9.0.0",
34
34
  "esbuild": "0.28.2",
35
35
  "jest-diff": "^30.5.2",
36
- "markdown-it": "15.0.1",
36
+ "markdown-it": "15.0.3",
37
37
  "pegjs": "0.11.0-master.b7b87ea",
38
38
  "tsx": "4.23.15",
39
39
  "typedoc": "0.28.20",
40
40
  "typescript": "^6.0.3",
41
- "vitest": "5.0.4",
41
+ "vite": "8.3.4",
42
+ "vitest": "5.0.5",
42
43
  "yaml-js": "^0.3.1"
43
44
  },
44
45
  "repository": {
@@ -0,0 +1,120 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { resolve } from './base';
3
+ import FunctionNode from './function';
4
+ import Variable from './variable';
5
+
6
+ describe('resolving attribute objects', () => {
7
+ it.each([Object.prototype, null])(
8
+ 'preserves the prototype and own data properties of %s objects',
9
+ (prototype) => {
10
+ const input = Object.assign(
11
+ Object.create(prototype),
12
+ Object.fromEntries([
13
+ ['title', new Variable(['title'])],
14
+ ['constructor', 'custom'],
15
+ ['toString', 'text'],
16
+ ]),
17
+ );
18
+ Object.defineProperty(input, '__proto__', {
19
+ value: { title: new Variable(['title']) },
20
+ enumerable: true,
21
+ });
22
+
23
+ const output = resolve(input, { variables: { title: 'Resolved' } });
24
+
25
+ expect(output).not.toBe(input);
26
+ expect(Object.getPrototypeOf(output)).toBe(prototype);
27
+ expect(Object.keys(output)).toEqual(Object.keys(input));
28
+ for (const [key, value] of [
29
+ ['title', 'Resolved'],
30
+ ['constructor', 'custom'],
31
+ ['toString', 'text'],
32
+ ['__proto__', { title: 'Resolved' }],
33
+ ] as const) {
34
+ expect(Object.getOwnPropertyDescriptor(output, key)).toEqual({
35
+ value,
36
+ enumerable: true,
37
+ configurable: true,
38
+ writable: true,
39
+ });
40
+ }
41
+ },
42
+ );
43
+
44
+ it.each([
45
+ {
46
+ set() {
47
+ throw new Error('Inherited setter must not run');
48
+ },
49
+ },
50
+ { value: 'inherited', writable: false },
51
+ ])('shadows inherited properties with descriptor %j', (descriptor) => {
52
+ const key = '__markdocResolveTest__';
53
+ const previous = Object.getOwnPropertyDescriptor(Object.prototype, key);
54
+ Object.defineProperty(Object.prototype, key, {
55
+ ...descriptor,
56
+ configurable: true,
57
+ });
58
+ try {
59
+ const input = Object.fromEntries([[key, new Variable(['value'])]]);
60
+ const output = resolve(input, { variables: { value: 'resolved' } });
61
+
62
+ expect(Object.getOwnPropertyDescriptor(output, key)).toEqual({
63
+ value: 'resolved',
64
+ enumerable: true,
65
+ configurable: true,
66
+ writable: true,
67
+ });
68
+ expect(Object.getPrototypeOf(output)).toBe(Object.prototype);
69
+ } finally {
70
+ if (previous) Object.defineProperty(Object.prototype, key, previous);
71
+ else Reflect.deleteProperty(Object.prototype, key);
72
+ }
73
+ });
74
+
75
+ it('handles inherited setters installed while resolving a value', () => {
76
+ const key = '__markdocResolveTest__';
77
+ const previous = Object.getOwnPropertyDescriptor(Object.prototype, key);
78
+ const input = Object.fromEntries([[key, new FunctionNode('install', {})]]);
79
+ try {
80
+ const output = resolve(input, {
81
+ functions: {
82
+ install: {
83
+ transform() {
84
+ Object.defineProperty(Object.prototype, key, {
85
+ configurable: true,
86
+ set() {
87
+ throw new Error('Inherited setter must not run');
88
+ },
89
+ });
90
+ return 'resolved';
91
+ },
92
+ },
93
+ },
94
+ });
95
+ expect(Object.getOwnPropertyDescriptor(output, key)?.value).toBe(
96
+ 'resolved',
97
+ );
98
+ } finally {
99
+ if (previous) Object.defineProperty(Object.prototype, key, previous);
100
+ else Reflect.deleteProperty(Object.prototype, key);
101
+ }
102
+ });
103
+
104
+ it('snapshots attribute values before resolving functions', () => {
105
+ const input = { first: new FunctionNode('mutate', {}), second: 'before' };
106
+ const output = resolve(input, {
107
+ functions: {
108
+ mutate: {
109
+ transform() {
110
+ input.second = 'after';
111
+ return 'resolved';
112
+ },
113
+ },
114
+ },
115
+ });
116
+
117
+ expect(output).toEqual({ first: 'resolved', second: 'before' });
118
+ expect(input.second).toBe('after');
119
+ });
120
+ });
package/src/ast/base.ts CHANGED
@@ -50,12 +50,17 @@ export function resolve(value: any, config: Config = {}): any {
50
50
  if (prototype !== Object.prototype && prototype !== null) return value;
51
51
 
52
52
  const output: Record<string, Scalar> = Object.create(prototype);
53
- for (const [k, v] of Object.entries(value))
54
- Object.defineProperty(output, k, {
55
- value: resolve(v, config),
56
- enumerable: true,
57
- configurable: true,
58
- writable: true,
59
- });
53
+ for (const [k, v] of Object.entries(value)) {
54
+ const resolved = resolve(v, config);
55
+ // Define inherited keys explicitly to bypass setters and read-only properties.
56
+ if (prototype === null || !(k in output)) output[k] = resolved;
57
+ else
58
+ Object.defineProperty(output, k, {
59
+ value: resolved,
60
+ enumerable: true,
61
+ configurable: true,
62
+ writable: true,
63
+ });
64
+ }
60
65
  return output;
61
66
  }
@@ -7,6 +7,65 @@ import Node from './node';
7
7
  import Variable from './variable';
8
8
 
9
9
  describe('Node object', () => {
10
+ it('resolves into independent children, slots, and nested attributes', () => {
11
+ const node = new Node('tag', {
12
+ nested: { values: [new Variable(['value'])] },
13
+ });
14
+ node.children.push(new Node('text', { content: new Variable(['value']) }));
15
+ node.slots.title = new Node('text', {
16
+ content: new FunctionNode('identity', { 0: new Variable(['value']) }),
17
+ });
18
+ const config = {
19
+ variables: { value: 'resolved' },
20
+ functions: { identity: { transform: (parameters) => parameters[0] } },
21
+ };
22
+ const resolved = node.resolve(config);
23
+
24
+ expect(resolved).not.toBe(node);
25
+ expect(Object.getPrototypeOf(resolved)).toBe(Node.prototype);
26
+ expect(Object.keys(resolved)).toEqual(Object.keys(node));
27
+ expect(resolved.attributes.nested).toEqual({ values: ['resolved'] });
28
+ expect(resolved.children[0].attributes.content).toBe('resolved');
29
+ expect(resolved.slots.title.attributes.content).toBe('resolved');
30
+ expect(resolved.children).not.toBe(node.children);
31
+ expect(resolved.children[0]).not.toBe(node.children[0]);
32
+ expect(resolved.slots).not.toBe(node.slots);
33
+ expect(resolved.slots.title).not.toBe(node.slots.title);
34
+ expect(resolved.attributes).not.toBe(node.attributes);
35
+ expect(resolved.attributes.nested).not.toBe(node.attributes.nested);
36
+ expect(resolved.attributes.nested.values).not.toBe(
37
+ node.attributes.nested.values,
38
+ );
39
+
40
+ resolved.children[0].attributes.content = 'changed';
41
+ resolved.slots.title.attributes.content = 'changed';
42
+ resolved.attributes.nested.values.push('changed');
43
+ const again = node.resolve(config);
44
+ expect(again.children[0].attributes.content).toBe('resolved');
45
+ expect(again.slots.title.attributes.content).toBe('resolved');
46
+ expect(again.attributes.nested.values).toEqual(['resolved']);
47
+ });
48
+
49
+ it('restores missing defaults and preserves metadata while resolving', () => {
50
+ const node = new Node('text', { content: 'example' });
51
+ const initialKeys = Object.keys(node);
52
+ Reflect.deleteProperty(node, 'inline');
53
+ Reflect.deleteProperty(node, 'errors');
54
+ Reflect.deleteProperty(node, 'annotations');
55
+ node.lines = [1, 2];
56
+
57
+ const resolved = node.resolve();
58
+ const another = node.resolve();
59
+ expect(Object.keys(resolved)).toEqual(initialKeys);
60
+ expect(resolved.inline).toBe(false);
61
+ expect(resolved.errors).toEqual([]);
62
+ expect(resolved.annotations).toEqual([]);
63
+ expect(resolved.errors).not.toBe(another.errors);
64
+ expect(resolved.annotations).not.toBe(another.annotations);
65
+ expect(resolved.lines).toBe(node.lines);
66
+ expect(resolved.slots).not.toBe(node.slots);
67
+ });
68
+
10
69
  it('copies only own attributes into a prototype-free dictionary', () => {
11
70
  const attributes = Object.assign(Object.create({ inherited: 'value' }), {
12
71
  own: 'value',
@@ -195,6 +254,28 @@ describe('transform', () => {
195
254
  new Tag('pre', { 'data-language': 'ruby' }, ['test']),
196
255
  );
197
256
  });
257
+
258
+ it('fenced code block with a custom render', () => {
259
+ const example = new Node('fence', {
260
+ language: 'ruby',
261
+ content: 'test',
262
+ process: false,
263
+ });
264
+ const output = transform(example, {
265
+ nodes: { fence: { ...markdoc.nodes.fence, render: 'Code' } },
266
+ });
267
+ expect(output).toDeepEqual(
268
+ new Tag('Code', { 'data-language': 'ruby' }, ['test']),
269
+ );
270
+ });
271
+ });
272
+
273
+ it('an inline code node with a custom render', () => {
274
+ const example = new Node('code', { content: 'test' });
275
+ const output = transform(example, {
276
+ nodes: { code: { ...markdoc.nodes.code, render: 'InlineCode' } },
277
+ });
278
+ expect(output).toDeepEqual(new Tag('InlineCode', {}, ['test']));
198
279
  });
199
280
  });
200
281
 
package/src/ast/node.ts CHANGED
@@ -43,9 +43,21 @@ export default class Node implements AstType {
43
43
  }
44
44
 
45
45
  *walk(): Generator<Node, void, unknown> {
46
- for (const child of [...Object.values(this.slots), ...this.children]) {
46
+ const stack = [...Object.values(this.slots), ...this.children].reverse();
47
+ while (stack.length) {
48
+ const child = stack.pop()!;
47
49
  yield child;
48
- yield* child.walk();
50
+
51
+ const walk = child.walk;
52
+ if (walk !== defaultWalk) {
53
+ yield* Reflect.apply(walk, child, []);
54
+ continue;
55
+ }
56
+
57
+ // Snapshot descendants after yielding their parent, as recursive walk did.
58
+ const children = [...Object.values(child.slots), ...child.children];
59
+ for (let index = children.length - 1; index >= 0; index--)
60
+ stack.push(children[index]);
49
61
  }
50
62
  }
51
63
 
@@ -54,15 +66,19 @@ export default class Node implements AstType {
54
66
  }
55
67
 
56
68
  resolve(config: Config = {}): Node {
57
- return Object.assign(new Node(), this, {
58
- children: this.children.map((child) => child.resolve(config)),
59
- attributes: resolve(this.attributes, config),
60
- slots: Object.fromEntries(
61
- Object.entries(this.slots).map(([name, slot]) => [
62
- name,
63
- slot.resolve(config),
64
- ]),
65
- ),
69
+ const node = new Node();
70
+ const children = this.children.map((child) => child.resolve(config));
71
+ const attributes = resolve(this.attributes, config);
72
+ const slots = Object.entries(this.slots);
73
+
74
+ return Object.assign(node, this, {
75
+ children,
76
+ attributes,
77
+ slots: slots.length
78
+ ? Object.fromEntries(
79
+ slots.map(([name, slot]) => [name, slot.resolve(config)]),
80
+ )
81
+ : {},
66
82
  });
67
83
  }
68
84
 
@@ -82,3 +98,6 @@ export default class Node implements AstType {
82
98
  return transformer.node(this, config);
83
99
  }
84
100
  }
101
+
102
+ // Keep the original method so replacements on the prototype are also delegated.
103
+ const defaultWalk = Node.prototype.walk;