@markdoc/markdoc 0.5.11 → 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.
Files changed (48) hide show
  1. package/README.md +43 -13
  2. package/dist/index.d.ts +21 -20
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +86 -62
  5. package/dist/index.js.map +4 -4
  6. package/dist/index.mjs +86 -62
  7. package/dist/index.mjs.map +4 -4
  8. package/dist/react.js +5 -4
  9. package/dist/react.js.map +2 -2
  10. package/dist/src/ast/base.d.ts.map +1 -1
  11. package/dist/src/parser.d.ts.map +1 -1
  12. package/dist/src/renderers/react/react.d.ts.map +1 -1
  13. package/dist/src/tokenizer/index.d.ts +16 -7
  14. package/dist/src/tokenizer/index.d.ts.map +1 -1
  15. package/dist/src/tokenizer/plugins/annotations.d.ts +2 -2
  16. package/dist/src/tokenizer/plugins/annotations.d.ts.map +1 -1
  17. package/dist/src/tokenizer/plugins/comments.d.ts +2 -2
  18. package/dist/src/tokenizer/plugins/comments.d.ts.map +1 -1
  19. package/dist/src/tokenizer/plugins/frontmatter.d.ts +2 -2
  20. package/dist/src/tokenizer/plugins/frontmatter.d.ts.map +1 -1
  21. package/dist/src/tokenizer/plugins/link.d.ts +3 -3
  22. package/dist/src/tokenizer/plugins/link.d.ts.map +1 -1
  23. package/dist/src/transformer.d.ts.map +1 -1
  24. package/dist/src/validator.d.ts.map +1 -1
  25. package/index.ts +1 -0
  26. package/package.json +35 -44
  27. package/src/ast/base.ts +19 -4
  28. package/src/ast/node.test.ts +76 -0
  29. package/src/ast/node.ts +1 -1
  30. package/src/grammar/tag.d.ts +1 -3
  31. package/src/grammar/tag.js +4 -4
  32. package/src/grammar/tag.pegjs +4 -4
  33. package/src/grammar/tag.test.ts +28 -0
  34. package/src/parser.test.ts +35 -0
  35. package/src/parser.ts +10 -1
  36. package/src/renderers/react/react.test.ts +11 -0
  37. package/src/renderers/react/react.ts +12 -10
  38. package/src/renderers/react/static.ts +4 -4
  39. package/src/tag.ts +1 -1
  40. package/src/tokenizer/index.ts +22 -9
  41. package/src/tokenizer/plugins/annotations.ts +11 -7
  42. package/src/tokenizer/plugins/comments.ts +4 -4
  43. package/src/tokenizer/plugins/frontmatter.ts +5 -5
  44. package/src/tokenizer/plugins/link.ts +4 -7
  45. package/src/transformer.ts +4 -2
  46. package/src/validator.test.ts +174 -1
  47. package/src/validator.ts +10 -4
  48. package/spec/marktest/index.ts +0 -136
package/dist/react.js CHANGED
@@ -23,15 +23,16 @@ 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))
32
32
  return React.createElement(React.Fragment, null, ...node2.map(render));
33
- if (node2 === null || typeof node2 !== "object" || !Tag.isTag(node2))
33
+ if (node2 === null || typeof node2 !== "object" || !Tag.isTag(node2)) {
34
34
  return node2;
35
+ }
35
36
  const {
36
37
  name,
37
38
  attributes: { class: className, ...attrs } = {},
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 return node;\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,IAChC,OACA,sBAAsB,WACtB,WAAW,IAAI,IACf,WAAW,IAAI;AACrB;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;AAC9D,aAAOA;AAET,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,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,WAAO,OAAO;AAAA,MACZ,OAAO,QAAQ,KAAK,EAAE,IAAI,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC;AAAA,IAC1D;AAAA,EACF;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;",
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,GAAG,CAAC,CAAC;AAEpC,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,aAuC/D"}
1
+ {"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../../../../src/renderers/react/react.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAElD,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,GAAG,CAAC,CAAC;AAEpC,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,16 +1,25 @@
1
- import MarkdownIt from 'markdown-it';
2
1
  import type { Token } from '../types';
3
2
  export type LinkPluginOptions = {
4
3
  validatedProtocols: string[];
5
4
  };
5
+ export type TokenizerOptions = {
6
+ html?: boolean;
7
+ xhtmlOut?: boolean;
8
+ breaks?: boolean;
9
+ langPrefix?: string;
10
+ linkify?: boolean;
11
+ typographer?: boolean;
12
+ quotes?: string | string[];
13
+ highlight?: ((content: string, language: string, attributes: string) => string) | null;
14
+ maxNesting?: number;
15
+ allowIndentation?: boolean;
16
+ allowComments?: boolean;
17
+ allowLinkValidation?: boolean;
18
+ linkValidationOptions?: LinkPluginOptions;
19
+ };
6
20
  export default class Tokenizer {
7
21
  private parser;
8
- constructor(config?: MarkdownIt.MarkdownItOptions & {
9
- allowIndentation?: boolean;
10
- allowComments?: boolean;
11
- allowLinkValidation?: boolean;
12
- linkValidationOptions?: LinkPluginOptions;
13
- });
22
+ constructor(config?: TokenizerOptions);
14
23
  tokenize(content: string): Token[];
15
24
  }
16
25
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/tokenizer/index.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,aAAa,CAAC;AAKrC,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAEtC,MAAM,MAAM,iBAAiB,GAAG;IAAE,kBAAkB,EAAE,MAAM,EAAE,CAAA;CAAE,CAAC;AAEjE,MAAM,CAAC,OAAO,OAAO,SAAS;IAC5B,OAAO,CAAC,MAAM,CAAwB;gBAGpC,MAAM,GAAE,UAAU,CAAC,iBAAiB,GAAG;QACrC,gBAAgB,CAAC,EAAE,OAAO,CAAC;QAC3B,aAAa,CAAC,EAAE,OAAO,CAAC;QACxB,mBAAmB,CAAC,EAAE,OAAO,CAAC;QAC9B,qBAAqB,CAAC,EAAE,iBAAiB,CAAC;KACtC;IAwBR,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,KAAK,EAAE;CAGnC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/tokenizer/index.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAEtC,MAAM,MAAM,iBAAiB,GAAG;IAAE,kBAAkB,EAAE,MAAM,EAAE,CAAA;CAAE,CAAC;AAIjE,MAAM,MAAM,gBAAgB,GAAG;IAC7B,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAC3B,SAAS,CAAC,EACR,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,KAAK,MAAM,CAAC,GAAG,IAAI,CAAC;IAC7E,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC9B,qBAAqB,CAAC,EAAE,iBAAiB,CAAC;CAC3C,CAAC;AAEF,MAAM,CAAC,OAAO,OAAO,SAAS;IAC5B,OAAO,CAAC,MAAM,CAAmB;gBAErB,MAAM,GAAE,gBAAqB;IAuBzC,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,KAAK,EAAE;CAGnC"}
@@ -1,3 +1,3 @@
1
- import type MarkdownIt from 'markdown-it';
2
- export default function plugin(md: MarkdownIt.MarkdownIt): void;
1
+ import type { MarkdownIt } from 'markdown-it';
2
+ export default function plugin(md: MarkdownIt): void;
3
3
  //# sourceMappingURL=annotations.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"annotations.d.ts","sourceRoot":"","sources":["../../../../src/tokenizer/plugins/annotations.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,UAAU,MAAM,aAAa,CAAC;AA+I1C,MAAM,CAAC,OAAO,UAAU,MAAM,CAAC,EAAE,EAAE,UAAU,CAAC,UAAU,QAMvD"}
1
+ {"version":3,"file":"annotations.d.ts","sourceRoot":"","sources":["../../../../src/tokenizer/plugins/annotations.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAEV,UAAU,EAKX,MAAM,aAAa,CAAC;AA4IrB,MAAM,CAAC,OAAO,UAAU,MAAM,CAAC,EAAE,EAAE,UAAU,QAM5C"}
@@ -1,3 +1,3 @@
1
- import type MarkdownIt from 'markdown-it';
2
- export default function plugin(md: MarkdownIt.MarkdownIt): void;
1
+ import type { MarkdownIt } from 'markdown-it';
2
+ export default function plugin(md: MarkdownIt): void;
3
3
  //# sourceMappingURL=comments.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"comments.d.ts","sourceRoot":"","sources":["../../../../src/tokenizer/plugins/comments.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,UAAU,MAAM,aAAa,CAAC;AA6C1C,MAAM,CAAC,OAAO,UAAU,MAAM,CAAC,EAAE,EAAE,UAAU,CAAC,UAAU,QAGvD"}
1
+ {"version":3,"file":"comments.d.ts","sourceRoot":"","sources":["../../../../src/tokenizer/plugins/comments.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAA2B,MAAM,aAAa,CAAC;AA6CvE,MAAM,CAAC,OAAO,UAAU,MAAM,CAAC,EAAE,EAAE,UAAU,QAG5C"}
@@ -1,3 +1,3 @@
1
- import type MarkdownIt from 'markdown-it';
2
- export default function plugin(md: MarkdownIt.MarkdownIt): void;
1
+ import type { MarkdownIt } from 'markdown-it';
2
+ export default function plugin(md: MarkdownIt): void;
3
3
  //# sourceMappingURL=frontmatter.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"frontmatter.d.ts","sourceRoot":"","sources":["../../../../src/tokenizer/plugins/frontmatter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,UAAU,MAAM,aAAa,CAAC;AAmC1C,MAAM,CAAC,OAAO,UAAU,MAAM,CAAC,EAAE,EAAE,UAAU,CAAC,UAAU,QAEvD"}
1
+ {"version":3,"file":"frontmatter.d.ts","sourceRoot":"","sources":["../../../../src/tokenizer/plugins/frontmatter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAc,MAAM,aAAa,CAAC;AAmC1D,MAAM,CAAC,OAAO,UAAU,MAAM,CAAC,EAAE,EAAE,UAAU,QAE5C"}
@@ -1,4 +1,4 @@
1
- import type MarkdownIt from 'markdown-it';
2
- import { LinkPluginOptions } from '..';
3
- export default function plugin(md: MarkdownIt.MarkdownIt, options: LinkPluginOptions): void;
1
+ import type { MarkdownIt } from 'markdown-it';
2
+ import type { LinkPluginOptions } from '..';
3
+ export default function plugin(md: MarkdownIt, options: LinkPluginOptions): void;
4
4
  //# sourceMappingURL=link.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"link.d.ts","sourceRoot":"","sources":["../../../../src/tokenizer/plugins/link.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,UAAU,MAAM,aAAa,CAAC;AAI1C,OAAO,EAAE,iBAAiB,EAAE,MAAM,IAAI,CAAC;AAyBvC,MAAM,CAAC,OAAO,UAAU,MAAM,CAC5B,EAAE,EAAE,UAAU,CAAC,UAAU,EACzB,OAAO,EAAE,iBAAiB,QAG3B"}
1
+ {"version":3,"file":"link.d.ts","sourceRoot":"","sources":["../../../../src/tokenizer/plugins/link.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAa,MAAM,aAAa,CAAC;AAIzD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,IAAI,CAAC;AAyB5C,MAAM,CAAC,OAAO,UAAU,MAAM,CAAC,EAAE,EAAE,UAAU,EAAE,OAAO,EAAE,iBAAiB,QAExE"}
@@ -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/index.ts CHANGED
@@ -23,6 +23,7 @@ import type {
23
23
  } from './src/types';
24
24
 
25
25
  export * from './src/types';
26
+ export type { TokenizerOptions } from './src/tokenizer';
26
27
 
27
28
  const tokenizer = new Tokenizer();
28
29
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@markdoc/markdoc",
3
3
  "author": "Ryan Paul",
4
- "version": "0.5.11",
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",
@@ -14,21 +14,6 @@
14
14
  ],
15
15
  "sideEffects": false,
16
16
  "license": "MIT",
17
- "scripts": {
18
- "test": "node -r ts-eager/register ./node_modules/.bin/jasmine",
19
- "marktest": "node -r ts-eager/register ${INSPECT:+--inspect-brk} spec/marktest/index.ts spec/marktest/tests.yaml",
20
- "prebuild": "npx patch-package",
21
- "build": "npm run build:common && npm run build:types",
22
- "build:common": "node build.js",
23
- "build:types": "tsc --emitDeclarationOnly --outDir dist",
24
- "lint": "eslint .",
25
- "grammar": "pegjs -o src/grammar/tag.js src/grammar/tag.pegjs",
26
- "postgrammar": "prettier --write src/grammar/tag.js",
27
- "prettier": "prettier --write index.ts src spec",
28
- "prettier:check": "prettier --check index.ts src spec",
29
- "type:check": "tsc --noEmit",
30
- "typedoc": "typedoc index.ts --includeVersion --readme none --excludePrivate --excludeExternals --excludeInternal --sort source-order --out types"
31
- },
32
17
  "engines": {
33
18
  "node": ">=14.7.0"
34
19
  },
@@ -37,46 +22,52 @@
37
22
  "react": "*"
38
23
  },
39
24
  "peerDependenciesMeta": {
40
- "@types/react": {
41
- "optional": true
42
- },
43
25
  "react": {
44
26
  "optional": true
45
27
  }
46
28
  },
47
29
  "devDependencies": {
48
- "@eslint/js": "^9.0.0",
49
- "@types/jasmine": "^5.0.0",
50
- "@types/node": "^18.0.0",
51
- "@types/react": "^17.0.0",
30
+ "@eslint/js": "^10.0.1",
31
+ "@types/jasmine": "^7.0.0",
32
+ "@types/node": "^24.19.2",
33
+ "@types/react": "^19.3.0",
52
34
  "deep-assert": "0.3.0",
53
- "diff": "^4.0.4",
54
- "esbuild": "0.25.0",
55
- "eslint": "10.1.0",
56
- "globals": "^16.0.0",
57
- "jasmine": "5.13.0",
58
- "jest-diff": "^28.1.3",
35
+ "diff": "^9.0.0",
36
+ "esbuild": "0.28.2",
37
+ "eslint": "10.14.0",
38
+ "globals": "^17.14.0",
39
+ "jasmine": "7.0.0",
40
+ "jest-diff": "^30.5.2",
59
41
  "markdown-it": "15.0.1",
60
- "patch-package": "^8.0.0",
61
42
  "pegjs": "0.11.0-master.b7b87ea",
62
- "prettier": "^2.5.1",
63
- "ts-eager": "2.0.2",
64
- "typedoc": "0.28.18",
65
- "typescript": "^5.0.0",
66
- "typescript-eslint": "^8.0.0",
67
- "yaml-js": "^0.2.3"
68
- },
69
- "overrides": {
70
- "ts-eager": {
71
- "esbuild": "0.25.0"
72
- }
43
+ "prettier": "^3.9.10",
44
+ "tsx": "4.23.15",
45
+ "typedoc": "0.28.20",
46
+ "typescript": "^6.0.3",
47
+ "typescript-eslint": "^8.72.1",
48
+ "yaml-js": "^0.3.1"
73
49
  },
74
50
  "repository": {
75
51
  "type": "git",
76
- "url": "git+https://github.com/markdoc/markdoc.git"
52
+ "url": "git+https://github.com/markdoc/markdoc.git",
53
+ "directory": "packages/markdoc"
77
54
  },
78
55
  "bugs": {
79
56
  "url": "https://github.com/markdoc/markdoc/issues"
80
57
  },
81
- "homepage": "https://markdoc.dev"
82
- }
58
+ "homepage": "https://markdoc.dev",
59
+ "scripts": {
60
+ "test": "node --import tsx ./node_modules/jasmine/bin/jasmine.js",
61
+ "test:types": "node spec/types/check.mjs",
62
+ "marktest": "node --import tsx ${INSPECT:+--inspect-brk} spec/marktest/index.ts spec/marktest/tests.yaml",
63
+ "build": "pnpm run build:common && pnpm run build:types",
64
+ "build:common": "node build.js",
65
+ "build:types": "tsc --emitDeclarationOnly --outDir dist",
66
+ "lint": "eslint .",
67
+ "grammar": "pegjs -o src/grammar/tag.js src/grammar/tag.pegjs && prettier --write src/grammar/tag.js",
68
+ "prettier": "prettier --write index.ts src spec",
69
+ "prettier:check": "prettier --check index.ts src spec",
70
+ "type:check": "tsc --noEmit",
71
+ "typedoc": "typedoc index.ts --includeVersion --readme none --excludePrivate --excludeExternals --excludeInternal --sort source-order --out types"
72
+ }
73
+ }
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;
@@ -1,9 +1,7 @@
1
- import type MarkdownIt from 'markdown-it';
1
+ import type { Token } from 'markdown-it';
2
2
  import type AstVariable from '../ast/variable';
3
3
  import type AstFunction from '../ast/function';
4
4
 
5
- type Token = MarkdownIt.Token;
6
-
7
5
  type astTypes = {
8
6
  Variable?: typeof AstVariable;
9
7
  Function?: typeof AstFunction;
@@ -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);