@markdoc/markdoc 0.5.12 → 0.5.13

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 CHANGED
@@ -23,9 +23,9 @@ function dynamic(node, React, { components = {}, resolveTagName = tagName } = {}
23
23
  if (Array.isArray(value)) return value.map((item) => deepRender(item));
24
24
  if (value.$$mdtype === "Tag") return render(value);
25
25
  if (typeof value !== "object") return value;
26
- const output = {};
27
- for (const [k, v] of Object.entries(value)) output[k] = deepRender(v);
28
- return output;
26
+ return Object.fromEntries(
27
+ Object.entries(value).map(([k, v]) => [k, deepRender(v)])
28
+ );
29
29
  }
30
30
  function render(node2) {
31
31
  if (Array.isArray(node2))
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 Tag from '../../tag';\nimport { RenderableTreeNodes, Scalar } from '../../types';\n\nimport type { createElement, ComponentType, Fragment, ReactNode } from 'react';\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 const output: Record<string, Scalar> = {};\n for (const [k, v] of Object.entries(value)) output[k] = deepRender(v);\n return output;\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
- "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;;;ACbA,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,QAAO;AAEtC,UAAM,SAAiC,CAAC;AACxC,eAAW,CAAC,GAAG,CAAC,KAAK,OAAO,QAAQ,KAAK,EAAG,QAAO,CAAC,IAAI,WAAW,CAAC;AACpE,WAAO;AAAA,EACT;AAEA,WAAS,OAAOA,OAAsC;AACpD,QAAI,MAAM,QAAQA,KAAI;AACpB,aAAO,MAAM,cAAc,MAAM,UAAU,MAAM,GAAGA,MAAK,IAAI,MAAM,CAAC;AAEtE,QAAIA,UAAS,QAAQ,OAAOA,UAAS,YAAY,CAAC,IAAI,MAAMA,KAAI,GAAG;AAEjE,aAAOA;AAAA,IACT;AAEA,UAAM;AAAA,MACJ;AAAA,MACA,YAAY,EAAE,OAAO,WAAW,GAAG,MAAM,IAAI,CAAC;AAAA,MAC9C,WAAW,CAAC;AAAA,IACd,IAAIA;AAEJ,QAAI,UAAW,OAAM,YAAY;AAEjC,WAAO,MAAM;AAAA,MACX,eAAe,MAAM,UAAU;AAAA,MAC/B,OAAO,KAAK,KAAK,EAAE,UAAU,IAAI,OAAO,WAAW,KAAK;AAAA,MACxD,GAAG,SAAS,IAAI,MAAM;AAAA,IACxB;AAAA,EACF;AAEA,SAAO,OAAO,IAAI;AACpB;",
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 Tag from '../../tag';\nimport { RenderableTreeNodes } from '../../types';\n\nimport type { createElement, ComponentType, Fragment, ReactNode } from 'react';\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"],
5
+ "mappings": ";AAEA,IAAqB,MAArB,MAGE;AAAA,EAWA,YACE,OAAO,OACP,aAAa,CAAC,GACd,WAAiC,CAAC,GAClC;AAdF,SAAS,WAAW;AAelB,SAAK,OAA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,eAAe,MAAM,UAAU;AAAA,MAC/B,OAAO,KAAK,KAAK,EAAE,UAAU,IAAI,OAAO,WAAW,KAAK;AAAA,MACxD,GAAG,SAAS,IAAI,MAAM;AAAA,IACxB;AAAA,EACF;AAEA,SAAO,OAAO,IAAI;AACpB;",
6
6
  "names": ["node"]
7
7
  }
@@ -1 +1 @@
1
- {"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../../src/ast/base.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,OAAO,EACP,MAAM,EACN,QAAQ,IAAI,WAAW,EAEvB,QAAQ,IAAI,WAAW,EACxB,MAAM,UAAU,CAAC;AAElB,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,CAU3E;AAED,wBAAgB,OAAO,CAAC,KAAK,EAAE,GAAG,EAAE,MAAM,GAAE,MAAW,GAAG,GAAG,CAa5D"}
1
+ {"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../../src/ast/base.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,OAAO,EACP,MAAM,EACN,QAAQ,IAAI,WAAW,EAEvB,QAAQ,IAAI,WAAW,EACxB,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,CAyB5D"}
@@ -1 +1 @@
1
- {"version":3,"file":"parser.d.ts","sourceRoot":"","sources":["../../src/parser.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,YAAY,CAAC;AAI9B,OAAO,KAAK,EAA4B,UAAU,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAwM3E,MAAM,CAAC,OAAO,UAAU,MAAM,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,UAAU,QAoBzE"}
1
+ {"version":3,"file":"parser.d.ts","sourceRoot":"","sources":["../../src/parser.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,YAAY,CAAC;AAI9B,OAAO,KAAK,EAA4B,UAAU,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAiN3E,MAAM,CAAC,OAAO,UAAU,MAAM,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,UAAU,QAoBzE"}
@@ -1 +1 @@
1
- {"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../../../../src/renderers/react/react.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAU,MAAM,aAAa,CAAC;AAE1D,OAAO,KAAK,EAAE,aAAa,EAAE,aAAa,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAE/E,KAAK,UAAU,GAAG,QAAQ,CAAC;IACzB,aAAa,EAAE,OAAO,aAAa,CAAC;IACpC,QAAQ,EAAE,OAAO,QAAQ,CAAC;CAC3B,CAAC,CAAC;AAEH,KAAK,SAAS,GAAG,aAAa,CAAC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,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":"transformer.d.ts","sourceRoot":"","sources":["../../src/transformer.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAMV,MAAM,EACN,WAAW,EACZ,MAAM,SAAS,CAAC;AAEjB,KAAK,gBAAgB,GAAG,MAAM,CAAC,YAAY,CAAC,CAAC;AAE7C,eAAO,MAAM,gBAAgB,EAAE,gBAG9B,CAAC;wBAqEG,WAAW;AAnEhB,wBAmEiB"}
1
+ {"version":3,"file":"transformer.d.ts","sourceRoot":"","sources":["../../src/transformer.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAMV,MAAM,EACN,WAAW,EACZ,MAAM,SAAS,CAAC;AAEjB,KAAK,gBAAgB,GAAG,MAAM,CAAC,YAAY,CAAC,CAAC;AAE7C,eAAO,MAAM,gBAAgB,EAAE,gBAG9B,CAAC;wBAuEG,WAAW;AArEhB,wBAqEiB"}
@@ -1 +1 @@
1
- {"version":3,"file":"validator.d.ts","sourceRoot":"","sources":["../../src/validator.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAEV,IAAI,EAEJ,MAAM,EACN,eAAe,EACf,aAAa,EACb,eAAe,EACf,KAAK,EACN,MAAM,SAAS,CAAC;AAUjB,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,kDAoK3D;AAED,wBAAiB,eAAe,CAC9B,IAAI,EAAE,IAAI,EACV,OAAO,GAAE,IAAI,EAAO,GACnB,SAAS,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,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":"AAIA,OAAO,KAAK,EAEV,IAAI,EAEJ,MAAM,EACN,eAAe,EACf,aAAa,EACb,eAAe,EACf,KAAK,EACN,MAAM,SAAS,CAAC;AAUjB,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,kDA0K3D;AAED,wBAAiB,eAAe,CAC9B,IAAI,EAAE,IAAI,EACV,OAAO,GAAE,IAAI,EAAO,GACnB,SAAS,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC,CAI3B;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.12",
4
+ "version": "0.5.13",
5
5
  "description": "A text markup language for documentation",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.mjs",
@@ -49,7 +49,8 @@
49
49
  },
50
50
  "repository": {
51
51
  "type": "git",
52
- "url": "git+https://github.com/markdoc/markdoc.git"
52
+ "url": "git+https://github.com/markdoc/markdoc.git",
53
+ "directory": "packages/markdoc"
53
54
  },
54
55
  "bugs": {
55
56
  "url": "https://github.com/markdoc/markdoc/issues"
package/src/ast/base.ts CHANGED
@@ -5,6 +5,8 @@ import type {
5
5
  Scalar,
6
6
  Variable as AstVariable,
7
7
  } from '../types';
8
+ import FunctionNode from './function';
9
+ import VariableNode from './variable';
8
10
 
9
11
  export function isAst(value?: any): value is AstType {
10
12
  return !!value?.$$mdtype;
@@ -25,7 +27,8 @@ export function* getAstValues(value: any): Generator<AstType, void, unknown> {
25
27
 
26
28
  if (isAst(value)) yield value;
27
29
 
28
- if (Object.getPrototypeOf(value) !== Object.prototype) return;
30
+ const prototype = Object.getPrototypeOf(value);
31
+ if (prototype !== Object.prototype && prototype !== null) return;
29
32
 
30
33
  for (const v of Object.values(value)) yield* getAstValues(v);
31
34
  }
@@ -38,9 +41,21 @@ export function resolve(value: any, config: Config = {}): any {
38
41
  if (isAst(value) && value?.resolve instanceof Function)
39
42
  return value.resolve(config);
40
43
 
41
- if (Object.getPrototypeOf(value) !== Object.prototype) return value;
44
+ if (isFunction(value))
45
+ return new FunctionNode(value.name, value.parameters).resolve(config);
42
46
 
43
- const output: Record<string, Scalar> = {};
44
- for (const [k, v] of Object.entries(value)) output[k] = resolve(v, config);
47
+ if (isVariable(value)) return new VariableNode(value.path).resolve(config);
48
+
49
+ const prototype = Object.getPrototypeOf(value);
50
+ if (prototype !== Object.prototype && prototype !== null) return value;
51
+
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
+ });
45
60
  return output;
46
61
  }
@@ -1,11 +1,87 @@
1
1
  /* eslint-disable @typescript-eslint/ban-ts-comment */
2
2
  import markdoc from '../../index';
3
+ import FunctionNode from './function';
3
4
  import Node from './node';
4
5
  import Variable from './variable';
6
+ import { getAstValues } from './base';
5
7
 
6
8
  import Tag from '../tag';
7
9
 
8
10
  describe('Node object', function () {
11
+ it('copies only own attributes into a prototype-free dictionary', function () {
12
+ const attributes = Object.assign(Object.create({ inherited: 'value' }), {
13
+ own: 'value',
14
+ });
15
+ const node = new Node('tag', attributes);
16
+ expect(Object.getPrototypeOf(node.attributes)).toBeNull();
17
+ expect(Object.keys(node.attributes)).toEqual(['own']);
18
+ expect(node.attributes.own).toBe('value');
19
+ });
20
+
21
+ it('preserves prototype-free attributes and resolves nested values', function () {
22
+ const attributes = Object.fromEntries([
23
+ ['__proto__', { src: new Variable(['url']) }],
24
+ ['src', new Variable(['url'])],
25
+ ]);
26
+ const node = new Node('tag', attributes);
27
+ const resolved = node.resolve({
28
+ variables: { url: 'https://example.com/' },
29
+ });
30
+ expect(Object.getPrototypeOf(resolved.attributes)).toBeNull();
31
+ expect(Object.keys(resolved.attributes)).toEqual(['__proto__', 'src']);
32
+ expect(resolved.attributes.__proto__.src).toBe('https://example.com/');
33
+ expect(resolved.attributes.src).toBe('https://example.com/');
34
+ });
35
+
36
+ it('traverses AST values in prototype-free attribute dictionaries', function () {
37
+ const variable = new Variable(['url']);
38
+ const node = new Node('tag', { src: variable });
39
+ expect([...getAstValues(node.attributes)]).toEqual([variable]);
40
+ });
41
+
42
+ it('resolves object values without invoking prototype setters', function () {
43
+ const value = Object.fromEntries([
44
+ ['safe', new Variable(['url'])],
45
+ ['__proto__', { src: new Variable(['url']) }],
46
+ ]);
47
+ const node = new Node('tag', { value });
48
+ const resolved = node.resolve({
49
+ variables: { url: 'https://example.com/' },
50
+ });
51
+ expect(Object.getPrototypeOf(resolved.attributes.value)).toBe(
52
+ Object.prototype
53
+ );
54
+ expect(Object.keys(resolved.attributes.value)).toEqual([
55
+ 'safe',
56
+ '__proto__',
57
+ ]);
58
+ expect(resolved.attributes.value.src).toBeUndefined();
59
+ expect(resolved.attributes.value.__proto__.src).toBe(
60
+ 'https://example.com/'
61
+ );
62
+ });
63
+
64
+ it('resolves serialized function and variable attributes', function () {
65
+ const serialized = JSON.parse(
66
+ JSON.stringify({
67
+ direct: new Variable(['answer']),
68
+ computed: new FunctionNode('identity', {
69
+ 0: new Variable(['answer']),
70
+ }),
71
+ })
72
+ );
73
+ const node = new Node('tag', serialized);
74
+ const resolved = node.resolve({
75
+ variables: { answer: 'resolved' },
76
+ functions: {
77
+ identity: { transform: (parameters) => parameters[0] },
78
+ },
79
+ });
80
+
81
+ expect(resolved.attributes.direct).toBe('resolved');
82
+ expect(resolved.attributes.computed).toBe('resolved');
83
+ });
84
+
9
85
  describe('traversal', function () {
10
86
  it('with a simple document', function () {
11
87
  const example = new Node('document', {}, [
package/src/ast/node.ts CHANGED
@@ -34,7 +34,7 @@ export default class Node implements AstType {
34
34
  children: Node[] = [],
35
35
  tag?: string
36
36
  ) {
37
- this.attributes = attributes;
37
+ this.attributes = Object.assign(Object.create(null), attributes);
38
38
  this.children = children;
39
39
  this.type = type;
40
40
  this.tag = tag;
@@ -255,9 +255,9 @@ function peg$parse(input, options) {
255
255
  return head ? [head, ...tail] : [];
256
256
  };
257
257
  var peg$f14 = function (name, params) {
258
- let parameters = {};
259
- for (let [index, { name, value }] of params.entries())
260
- parameters[name || index] = value;
258
+ const parameters = Object.fromEntries(
259
+ params.map(({ name, value }, index) => [name || index, value])
260
+ );
261
261
  return new Function(name, parameters);
262
262
  };
263
263
  var peg$f15 = function (name) {
@@ -289,7 +289,7 @@ function peg$parse(input, options) {
289
289
  return value || [];
290
290
  };
291
291
  var peg$f24 = function (head, tail) {
292
- return Object.assign(head, ...tail);
292
+ return Object.fromEntries([head, ...tail].flatMap(Object.entries));
293
293
  };
294
294
  var peg$f25 = function (value) {
295
295
  return value || {};
@@ -78,9 +78,9 @@ Function =
78
78
  tail:FunctionParameterTail* { return head ? [head, ...tail] : []; }
79
79
  )?
80
80
  ')' {
81
- let parameters = {};
82
- for (let [index, {name, value}] of params.entries())
83
- parameters[name || index] = value;
81
+ const parameters = Object.fromEntries(
82
+ params.map(({name, value}, index) => [name || index, value])
83
+ );
84
84
  return new Function(name, parameters);
85
85
  }
86
86
 
@@ -140,7 +140,7 @@ ValueHash =
140
140
  value:(
141
141
  head:ValueHashItem
142
142
  tail:ValueHashTail*
143
- TrailingComma { return Object.assign(head, ...tail); }
143
+ TrailingComma { return Object.fromEntries([head, ...tail].flatMap(Object.entries)); }
144
144
  )?
145
145
  _* '}' { return value || {}; }
146
146
 
@@ -1,7 +1,35 @@
1
1
  import { parse, SyntaxError } from '../../src/grammar/tag';
2
2
  import Variable from '../../src/ast/variable';
3
+ import Function from '../../src/ast/function';
3
4
 
4
5
  describe('Markdoc tag parser', function () {
6
+ it('preserves __proto__ hash keys as own data properties in any position', function () {
7
+ for (const value of [
8
+ '{__proto__:{src:"unsafe"}, safe:true}',
9
+ '{safe:true, __proto__:{src:"unsafe"}}',
10
+ ]) {
11
+ const result = parse(`foo value=${value} /`);
12
+ const hash = result.meta.attributes[0].value;
13
+ expect(Object.getPrototypeOf(hash)).toBe(Object.prototype);
14
+ expect(Object.prototype.hasOwnProperty.call(hash, '__proto__')).toBe(
15
+ true
16
+ );
17
+ expect(hash.src).toBeUndefined();
18
+ expect(hash.__proto__.src).toBe('unsafe');
19
+ }
20
+ });
21
+
22
+ it('preserves __proto__ function parameters as own data properties', function () {
23
+ const result = parse('foo value=example(__proto__={src:"unsafe"}) /', {
24
+ Function,
25
+ });
26
+ const parameters = result.meta.attributes[0].value.parameters;
27
+ expect(Object.getPrototypeOf(parameters)).toBe(Object.prototype);
28
+ expect(Object.keys(parameters)).toEqual(['__proto__']);
29
+ expect(parameters.src).toBeUndefined();
30
+ expect(parameters.__proto__.src).toBe('unsafe');
31
+ });
32
+
5
33
  describe('tag parsing', function () {
6
34
  it('with a simple opening tag', function () {
7
35
  const example = parse('foo');
@@ -761,6 +761,41 @@ describe('Markdown parser', function () {
761
761
  });
762
762
 
763
763
  describe('handles attribute errors correctly', function () {
764
+ for (const name of ['__proto__', 'prototype', 'constructor']) {
765
+ for (const source of [
766
+ `{% foo ${name}={src:"javascript:alert(1)"} /%}`,
767
+ `text {% ${name}={src:"javascript:alert(1)"} %}`,
768
+ `![text](/image.png) {% ${name}={src:"javascript:alert(1)"} %}`,
769
+ `${fence} {% ${name}={src:"javascript:alert(1)"} %}\ntext\n${fence}`,
770
+ ]) {
771
+ it(`rejects reserved attribute ${name} in ${source}`, function () {
772
+ const document = convert(source);
773
+ const node = [...document.walk()].find((node) =>
774
+ node.errors.some((error) => error.id === 'attribute-name-invalid')
775
+ );
776
+
777
+ expect(node).toBeDefined();
778
+ expect(node?.errors).toDeepEqualSubset([
779
+ { id: 'attribute-name-invalid', level: 'critical' },
780
+ ]);
781
+ expect(Object.getPrototypeOf(node?.attributes)).toBeNull();
782
+ expect(
783
+ Object.prototype.hasOwnProperty.call(node?.attributes, name)
784
+ ).toBe(false);
785
+ expect(node?.attributes.src).toBeUndefined();
786
+ });
787
+ }
788
+ }
789
+
790
+ it('does not treat inherited property names as duplicates', function () {
791
+ const document = convert('{% foo toString="first" toString="second" /%}');
792
+ expect(document.children[0].errors).toDeepEqualSubset([
793
+ { id: 'duplicate-attribute', level: 'warning' },
794
+ ]);
795
+ expect(document.children[0].errors.length).toBe(1);
796
+ expect(document.children[0].attributes.toString).toBe('second');
797
+ });
798
+
764
799
  it('with error for duplicate attributes', function () {
765
800
  const example = convert(`{% foo bar=1 bar=2 bar=3 bar=4 /%}`);
766
801
  expect(example.children[0].errors.length).toBe(3);
package/src/parser.ts CHANGED
@@ -18,7 +18,16 @@ function annotate(node: Node, attributes: AttributeValue[]) {
18
18
 
19
19
  const { name, value, type } = attribute;
20
20
  if (type === 'attribute') {
21
- if (node.attributes[name] !== undefined)
21
+ if (['__proto__', 'prototype', 'constructor'].includes(name)) {
22
+ node.errors.push({
23
+ id: 'attribute-name-invalid',
24
+ level: 'critical',
25
+ message: `Invalid attribute name: '${name}'`,
26
+ });
27
+ continue;
28
+ }
29
+
30
+ if (Object.prototype.hasOwnProperty.call(node.attributes, name))
22
31
  node.errors.push({
23
32
  id: 'duplicate-attribute',
24
33
  level: 'warning',
@@ -10,6 +10,17 @@ const React = {
10
10
  };
11
11
 
12
12
  describe('React dynamic renderer', function () {
13
+ it('preserves __proto__ as data without changing the props prototype', function () {
14
+ const attributes = Object.fromEntries([
15
+ ['__proto__', { src: 'javascript:alert(1)' }],
16
+ ['title', 'example'],
17
+ ]);
18
+ const output: any = dynamic(new Tag('iframe', attributes), React);
19
+ expect(Object.getPrototypeOf(output.attributes)).toBe(Object.prototype);
20
+ expect(Object.keys(output.attributes)).toEqual(['__proto__', 'title']);
21
+ expect(output.attributes.src).toBeUndefined();
22
+ });
23
+
13
24
  it('rendering a null node', function () {
14
25
  const example = { name: 'h1', children: ['test', null] };
15
26
  const output = dynamic(example, React);
@@ -1,5 +1,5 @@
1
1
  import Tag from '../../tag';
2
- import { RenderableTreeNodes, Scalar } from '../../types';
2
+ import { RenderableTreeNodes } from '../../types';
3
3
 
4
4
  import type { createElement, ComponentType, Fragment, ReactNode } from 'react';
5
5
 
@@ -41,9 +41,9 @@ export default function dynamic(
41
41
 
42
42
  if (typeof value !== 'object') return value;
43
43
 
44
- const output: Record<string, Scalar> = {};
45
- for (const [k, v] of Object.entries(value)) output[k] = deepRender(v);
46
- return output;
44
+ return Object.fromEntries(
45
+ Object.entries(value).map(([k, v]) => [k, deepRender(v)])
46
+ );
47
47
  }
48
48
 
49
49
  function render(node: RenderableTreeNodes): ReactNode {
@@ -26,7 +26,7 @@ export default {
26
26
 
27
27
  attributes(node: Node, config: Config = {}) {
28
28
  const schema = this.findSchema(node, config) ?? {};
29
- const output: Record<string, any> = {};
29
+ const output: Record<string, any> = Object.create(null);
30
30
 
31
31
  const attrs = { ...globalAttributes, ...schema.attributes };
32
32
  for (const [key, attr] of Object.entries(attrs)) {
@@ -34,7 +34,9 @@ export default {
34
34
 
35
35
  const name = typeof attr.render === 'string' ? attr.render : key;
36
36
 
37
- let value = node.attributes[key];
37
+ let value = Object.prototype.hasOwnProperty.call(node.attributes, key)
38
+ ? node.attributes[key]
39
+ : undefined;
38
40
  if (typeof attr.type === 'function') {
39
41
  const instance: any = new attr.type();
40
42
  if (instance.transform) {
@@ -1,4 +1,4 @@
1
- import Markdoc, { nodes } from '../index';
1
+ import Markdoc, { Node, Tag, nodes } from '../index';
2
2
  import type { Config, ValidationError } from './types';
3
3
 
4
4
  function validate(string, config) {
@@ -6,6 +6,179 @@ function validate(string, config) {
6
6
  }
7
7
 
8
8
  describe('validate', function () {
9
+ describe('prototype-safe attributes', function () {
10
+ let config: Config;
11
+ let customValidate: jasmine.Spy;
12
+ const unsafe = 'javascript:alert(1)';
13
+ const trusted = 'https://trusted.example/embed';
14
+ const React = {
15
+ Fragment: 'fragment',
16
+ createElement(name, attributes, ...children) {
17
+ return { name, attributes, children };
18
+ },
19
+ };
20
+
21
+ beforeEach(function () {
22
+ customValidate = jasmine.createSpy('validate').and.callFake((value) =>
23
+ value === trusted
24
+ ? []
25
+ : [
26
+ {
27
+ id: 'untrusted-src',
28
+ level: 'critical',
29
+ message: 'Untrusted URL',
30
+ },
31
+ ]
32
+ );
33
+ config = {
34
+ tags: {
35
+ embed: {
36
+ render: 'iframe',
37
+ selfClosing: true,
38
+ attributes: {
39
+ src: {
40
+ type: String,
41
+ required: true,
42
+ matches: /^https:\/\/trusted\.example\//,
43
+ errorLevel: 'critical',
44
+ validate: customValidate,
45
+ },
46
+ },
47
+ },
48
+ },
49
+ };
50
+ });
51
+
52
+ it('validates direct values with the pattern and custom validator', function () {
53
+ const ast = Markdoc.parse(`{% embed src="${unsafe}" /%}`);
54
+ expect(Markdoc.validate(ast, config)).toDeepEqualSubset([
55
+ { error: { id: 'attribute-value-invalid', level: 'critical' } },
56
+ { error: { id: 'untrusted-src', level: 'critical' } },
57
+ ]);
58
+ expect(customValidate).toHaveBeenCalledWith(
59
+ unsafe,
60
+ jasmine.anything(),
61
+ 'src'
62
+ );
63
+ });
64
+
65
+ it('enforces the URL schema when only errors and critical findings block rendering', function () {
66
+ delete config.tags!.embed.attributes!.src.validate;
67
+ const render = (source: string) => {
68
+ const ast = Markdoc.parse(source);
69
+ const findings = Markdoc.validate(ast, config);
70
+ if (
71
+ findings.some(({ error }) =>
72
+ ['error', 'critical'].includes(error.level)
73
+ )
74
+ )
75
+ return null;
76
+ return Markdoc.renderers.html(Markdoc.transform(ast, config));
77
+ };
78
+
79
+ expect(render(`{% embed src="${unsafe}" /%}`)).toBeNull();
80
+ expect(render(`{% embed __proto__={src:"${unsafe}"} /%}`)).toBeNull();
81
+ expect(render(`{% embed src="${trusted}" /%}`)).toBe(
82
+ `<article><iframe src="${trusted}"></iframe></article>`
83
+ );
84
+ });
85
+
86
+ it('blocks prototype attribute smuggling before rendering', function () {
87
+ const ast = Markdoc.parse(`{% embed __proto__={src:"${unsafe}"} /%}`);
88
+ expect(Markdoc.validate(ast, config)).toDeepEqualSubset([
89
+ { error: { id: 'attribute-name-invalid', level: 'critical' } },
90
+ { error: { id: 'attribute-missing-required', level: 'error' } },
91
+ ]);
92
+ expect(customValidate).not.toHaveBeenCalled();
93
+ expect(Object.keys(ast.children[0].attributes)).toEqual([]);
94
+ const output = Markdoc.transform(ast.children[0], config);
95
+ expect(Markdoc.renderers.html(output)).toBe('<iframe></iframe>');
96
+ expect(Markdoc.renderers.react(output, React)).toDeepEqual({
97
+ name: 'iframe',
98
+ attributes: null,
99
+ children: [],
100
+ });
101
+ });
102
+
103
+ it('still applies type validation to own values', function () {
104
+ const ast = Markdoc.parse('{% embed src=42 /%}');
105
+ expect(Markdoc.validate(ast, config)).toDeepEqualSubset([
106
+ { error: { id: 'attribute-type-invalid', level: 'critical' } },
107
+ { error: { id: 'attribute-value-invalid', level: 'critical' } },
108
+ { error: { id: 'untrusted-src', level: 'critical' } },
109
+ ]);
110
+ });
111
+
112
+ it('validates an unsafe own value despite a smuggling attempt', function () {
113
+ const ast = Markdoc.parse(
114
+ `{% embed __proto__={src:"${trusted}"} src="${unsafe}" /%}`
115
+ );
116
+ expect(Markdoc.validate(ast, config)).toDeepEqualSubset([
117
+ { error: { id: 'attribute-name-invalid', level: 'critical' } },
118
+ { error: { id: 'attribute-value-invalid', level: 'critical' } },
119
+ { error: { id: 'untrusted-src', level: 'critical' } },
120
+ ]);
121
+ expect(customValidate).toHaveBeenCalledWith(
122
+ unsafe,
123
+ jasmine.anything(),
124
+ 'src'
125
+ );
126
+ });
127
+
128
+ it('validates and renders an own value despite a smuggling attempt', function () {
129
+ const ast = Markdoc.parse(
130
+ `{% embed __proto__={src:"${unsafe}"} src="${trusted}" /%}`
131
+ );
132
+ expect(Markdoc.validate(ast, config)).toDeepEqualSubset([
133
+ { error: { id: 'attribute-name-invalid', level: 'critical' } },
134
+ ]);
135
+ expect(customValidate).toHaveBeenCalledWith(
136
+ trusted,
137
+ jasmine.anything(),
138
+ 'src'
139
+ );
140
+ expect(
141
+ Markdoc.renderers.html(Markdoc.transform(ast.children[0], config))
142
+ ).toBe(`<iframe src="${trusted}"></iframe>`);
143
+ });
144
+
145
+ it('ignores inherited attributes on a programmatically modified node', function () {
146
+ const node = new Node('tag', {}, [], 'embed');
147
+ node.attributes = Object.create({ src: unsafe });
148
+ expect(Markdoc.validate(node, config)).toDeepEqualSubset([
149
+ { error: { id: 'attribute-missing-required', level: 'error' } },
150
+ ]);
151
+ expect(node.transformAttributes(config).src).toBeUndefined();
152
+ expect(Markdoc.renderers.html(Markdoc.transform(node, config))).toBe(
153
+ '<iframe></iframe>'
154
+ );
155
+ });
156
+
157
+ it('applies defaults instead of inherited values', function () {
158
+ config.tags!.embed.attributes!.src.default = trusted;
159
+ const node = new Node('tag', {}, [], 'embed');
160
+ node.attributes = Object.create({ src: unsafe });
161
+ expect(node.transformAttributes(config).src).toBe(trusted);
162
+ });
163
+
164
+ it('rejects undeclared attributes whose names exist on Object.prototype', function () {
165
+ const ast = Markdoc.parse('{% embed toString="value" /%}');
166
+ expect(Markdoc.validate(ast, config)).toDeepEqualSubset([
167
+ { error: { id: 'attribute-undefined', level: 'error' } },
168
+ { error: { id: 'attribute-missing-required', level: 'error' } },
169
+ ]);
170
+ });
171
+
172
+ it('resolves variables in prototype-free attribute dictionaries', function () {
173
+ config.variables = { url: trusted };
174
+ const ast = Markdoc.parse('{% embed src=$url /%}');
175
+ expect(Markdoc.validate(ast.resolve(config), config)).toEqual([]);
176
+ expect(Markdoc.transform(ast.children[0], config)).toDeepEqual(
177
+ new Tag('iframe', { src: trusted })
178
+ );
179
+ });
180
+ });
181
+
9
182
  describe('function validation', function () {
10
183
  it('ensures that function exists', function () {
11
184
  const example = '{% foo bar=baz() /%}';
package/src/validator.ts CHANGED
@@ -178,7 +178,9 @@ export default function validator(node: Node, config: Config) {
178
178
  }
179
179
 
180
180
  for (let [key, value] of Object.entries(node.attributes)) {
181
- const attrib = attributes[key];
181
+ const attrib = Object.prototype.hasOwnProperty.call(attributes, key)
182
+ ? attributes[key]
183
+ : undefined;
182
184
 
183
185
  if (!attrib) {
184
186
  errors.push({
@@ -259,7 +261,11 @@ export default function validator(node: Node, config: Config) {
259
261
  }
260
262
 
261
263
  for (const [key, { required }] of Object.entries(attributes))
262
- if (required && node.attributes[key] === undefined)
264
+ if (
265
+ required &&
266
+ (!Object.prototype.hasOwnProperty.call(node.attributes, key) ||
267
+ node.attributes[key] === undefined)
268
+ )
263
269
  errors.push({
264
270
  id: 'attribute-missing-required',
265
271
  level: 'error',