@prismicio/migrate 0.0.1 → 0.0.2-canary.3e6d6e6

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 (77) hide show
  1. package/dist/_virtual/_rolldown/runtime.cjs +23 -0
  2. package/dist/errors/PrismicRichTextError.cjs +5 -5
  3. package/dist/errors/PrismicRichTextError.cjs.map +1 -1
  4. package/dist/errors/PrismicRichTextError.d.cts +5 -0
  5. package/dist/errors/PrismicRichTextError.d.cts.map +1 -0
  6. package/dist/errors/PrismicRichTextError.d.ts +5 -2
  7. package/dist/errors/PrismicRichTextError.d.ts.map +1 -0
  8. package/dist/errors/PrismicRichTextError.js +6 -6
  9. package/dist/errors/PrismicRichTextError.js.map +1 -1
  10. package/dist/errors/PrismicRichTextSerializerError.cjs +6 -6
  11. package/dist/errors/PrismicRichTextSerializerError.cjs.map +1 -1
  12. package/dist/errors/PrismicRichTextSerializerError.d.cts +6 -0
  13. package/dist/errors/PrismicRichTextSerializerError.d.cts.map +1 -0
  14. package/dist/errors/PrismicRichTextSerializerError.d.ts +6 -3
  15. package/dist/errors/PrismicRichTextSerializerError.d.ts.map +1 -0
  16. package/dist/errors/PrismicRichTextSerializerError.js +6 -6
  17. package/dist/errors/PrismicRichTextSerializerError.js.map +1 -1
  18. package/dist/htmlAsRichText.cjs +24 -8
  19. package/dist/htmlAsRichText.cjs.map +1 -1
  20. package/dist/htmlAsRichText.d.cts +23 -0
  21. package/dist/htmlAsRichText.d.cts.map +1 -0
  22. package/dist/htmlAsRichText.d.ts +14 -69
  23. package/dist/htmlAsRichText.d.ts.map +1 -0
  24. package/dist/htmlAsRichText.js +21 -7
  25. package/dist/htmlAsRichText.js.map +1 -1
  26. package/dist/index.cjs +6 -8
  27. package/dist/index.d.cts +5 -0
  28. package/dist/index.d.ts +5 -4
  29. package/dist/index.js +2 -7
  30. package/dist/lib/RichTextFieldBuilder.cjs +82 -113
  31. package/dist/lib/RichTextFieldBuilder.cjs.map +1 -1
  32. package/dist/lib/RichTextFieldBuilder.d.cts +15 -0
  33. package/dist/lib/RichTextFieldBuilder.d.cts.map +1 -0
  34. package/dist/lib/RichTextFieldBuilder.d.ts +7 -27
  35. package/dist/lib/RichTextFieldBuilder.d.ts.map +1 -0
  36. package/dist/lib/RichTextFieldBuilder.js +82 -113
  37. package/dist/lib/RichTextFieldBuilder.js.map +1 -1
  38. package/dist/lib/filterRichTextField.cjs +76 -78
  39. package/dist/lib/filterRichTextField.cjs.map +1 -1
  40. package/dist/lib/filterRichTextField.js +77 -79
  41. package/dist/lib/filterRichTextField.js.map +1 -1
  42. package/dist/lib/hastSerializerHelpers.cjs +101 -86
  43. package/dist/lib/hastSerializerHelpers.cjs.map +1 -1
  44. package/dist/lib/hastSerializerHelpers.js +101 -88
  45. package/dist/lib/hastSerializerHelpers.js.map +1 -1
  46. package/dist/lib/hastToRichText.cjs +151 -157
  47. package/dist/lib/hastToRichText.cjs.map +1 -1
  48. package/dist/lib/hastToRichText.d.cts +22 -0
  49. package/dist/lib/hastToRichText.d.cts.map +1 -0
  50. package/dist/lib/hastToRichText.d.ts +21 -29
  51. package/dist/lib/hastToRichText.d.ts.map +1 -0
  52. package/dist/lib/hastToRichText.js +150 -156
  53. package/dist/lib/hastToRichText.js.map +1 -1
  54. package/dist/lib/rehypeRichText.cjs +57 -57
  55. package/dist/lib/rehypeRichText.cjs.map +1 -1
  56. package/dist/lib/rehypeRichText.d.cts +59 -0
  57. package/dist/lib/rehypeRichText.d.cts.map +1 -0
  58. package/dist/lib/rehypeRichText.d.ts +54 -60
  59. package/dist/lib/rehypeRichText.d.ts.map +1 -0
  60. package/dist/lib/rehypeRichText.js +53 -55
  61. package/dist/lib/rehypeRichText.js.map +1 -1
  62. package/dist/types.d.cts +48 -0
  63. package/dist/types.d.cts.map +1 -0
  64. package/dist/types.d.ts +32 -41
  65. package/dist/types.d.ts.map +1 -0
  66. package/package.json +40 -63
  67. package/src/htmlAsRichText.ts +22 -49
  68. package/src/lib/RichTextFieldBuilder.ts +26 -31
  69. package/src/lib/filterRichTextField.ts +6 -25
  70. package/src/lib/hastSerializerHelpers.ts +4 -17
  71. package/src/lib/hastToRichText.ts +17 -39
  72. package/src/lib/rehypeRichText.ts +88 -102
  73. package/src/types.ts +24 -33
  74. package/dist/index.cjs.map +0 -1
  75. package/dist/index.js.map +0 -1
  76. package/dist/lib/filterRichTextField.d.ts +0 -10
  77. package/dist/lib/hastSerializerHelpers.d.ts +0 -28
@@ -1,59 +1,59 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const hastUtilSelect = require("hast-util-select");
4
- const rehypeMinifyWhitespace = require("rehype-minify-whitespace");
5
- const unistUtilRemove = require("unist-util-remove");
6
- const unistUtilVisit = require("unist-util-visit");
7
- const filterRichTextField = require("./filterRichTextField.cjs");
8
- const hastToRichText = require("./hastToRichText.cjs");
9
- const rehypeRichText = function rehypeRichText2(config) {
10
- const self = this;
11
- self.use(() => {
12
- return (tree) => {
13
- if (config == null ? void 0 : config.container) {
14
- const element = hastUtilSelect.select(config.container, tree);
15
- if (!element) {
16
- throw new Error(`No container matching \`${config == null ? void 0 : config.container}\` could be found in the input AST.`);
17
- }
18
- tree.children = [element];
19
- }
20
- if (config == null ? void 0 : config.exclude) {
21
- const nodesToExclude = hastUtilSelect.selectAll(config.exclude.join(", "), tree);
22
- unistUtilRemove.remove(tree, (node) => nodesToExclude.includes(node));
23
- }
24
- if (config == null ? void 0 : config.include) {
25
- const nodesToInclude = [];
26
- const selector = config.include.join(", ");
27
- const rawNodesToInclude = hastUtilSelect.selectAll(selector, tree);
28
- unistUtilVisit.visit(tree, (node) => {
29
- if (rawNodesToInclude.includes(node)) {
30
- nodesToInclude.push(node);
31
- return unistUtilVisit.SKIP;
32
- }
33
- });
34
- tree.children = nodesToInclude;
35
- }
36
- if (config == null ? void 0 : config.serializer) {
37
- for (const key in config.serializer) {
38
- if (!/^[a-z]+[1-6]?$/.test(key)) {
39
- const matches = hastUtilSelect.selectAll(key, tree);
40
- for (let i = 0; i < matches.length; i++) {
41
- matches[i].matchesSerializer = key;
42
- }
43
- }
44
- }
45
- }
46
- };
47
- });
48
- self.use(rehypeMinifyWhitespace);
49
- self.compiler = compiler;
50
- function compiler(tree, file) {
51
- const richTextField = hastToRichText.hastToRichText(tree, file, config);
52
- if (config == null ? void 0 : config.model) {
53
- return filterRichTextField.filterRichTextField(richTextField, config.model);
54
- }
55
- return richTextField;
56
- }
1
+ const require_runtime = require("../_virtual/_rolldown/runtime.cjs");
2
+ const require_filterRichTextField = require("./filterRichTextField.cjs");
3
+ const require_hastToRichText = require("./hastToRichText.cjs");
4
+ let hast_util_select = require("hast-util-select");
5
+ let rehype_minify_whitespace = require("rehype-minify-whitespace");
6
+ rehype_minify_whitespace = require_runtime.__toESM(rehype_minify_whitespace, 1);
7
+ let unist_util_remove = require("unist-util-remove");
8
+ let unist_util_visit = require("unist-util-visit");
9
+ //#region src/lib/rehypeRichText.ts
10
+ /**
11
+ * A unified plugin that compiles a hast tree to a Prismic rich text field.
12
+ *
13
+ * @param config - Configuration options for the rehype rich text processor.
14
+ */
15
+ const rehypeRichText = function rehypeRichText(config) {
16
+ const self = this;
17
+ self.use(() => {
18
+ return (tree) => {
19
+ if (config?.container) {
20
+ const element = (0, hast_util_select.select)(config.container, tree);
21
+ if (!element) throw new Error(`No container matching \`${config?.container}\` could be found in the input AST.`);
22
+ tree.children = [element];
23
+ }
24
+ if (config?.exclude) {
25
+ const nodesToExclude = (0, hast_util_select.selectAll)(config.exclude.join(", "), tree);
26
+ (0, unist_util_remove.remove)(tree, (node) => nodesToExclude.includes(node));
27
+ }
28
+ if (config?.include) {
29
+ const nodesToInclude = [];
30
+ const selector = config.include.join(", ");
31
+ const rawNodesToInclude = (0, hast_util_select.selectAll)(selector, tree);
32
+ (0, unist_util_visit.visit)(tree, (node) => {
33
+ if (rawNodesToInclude.includes(node)) {
34
+ nodesToInclude.push(node);
35
+ return unist_util_visit.SKIP;
36
+ }
37
+ });
38
+ tree.children = nodesToInclude;
39
+ }
40
+ if (config?.serializer) {
41
+ for (const key in config.serializer) if (!/^[a-z]+[1-6]?$/.test(key)) {
42
+ const matches = (0, hast_util_select.selectAll)(key, tree);
43
+ for (let i = 0; i < matches.length; i++) matches[i].matchesSerializer = key;
44
+ }
45
+ }
46
+ };
47
+ });
48
+ self.use(rehype_minify_whitespace.default);
49
+ self.compiler = compiler;
50
+ function compiler(tree, file) {
51
+ const richTextField = require_hastToRichText.hastToRichText(tree, file, config);
52
+ if (config?.model) return require_filterRichTextField.filterRichTextField(richTextField, config.model);
53
+ return richTextField;
54
+ }
57
55
  };
56
+ //#endregion
58
57
  exports.rehypeRichText = rehypeRichText;
59
- //# sourceMappingURL=rehypeRichText.cjs.map
58
+
59
+ //# sourceMappingURL=rehypeRichText.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"rehypeRichText.cjs","sources":["../../../src/lib/rehypeRichText.ts"],"sourcesContent":["import type {\n\tCustomTypeModelRichTextField,\n\tRichTextField,\n} from \"@prismicio/client\"\nimport type { Element, Root } from \"hast\"\nimport { select, selectAll } from \"hast-util-select\"\nimport rehypeMinifyWhitespace from \"rehype-minify-whitespace\"\nimport type { Plugin, Processor } from \"unified\"\nimport { remove } from \"unist-util-remove\"\nimport { SKIP, visit } from \"unist-util-visit\"\nimport type { VFile } from \"vfile\"\n\nimport { filterRichTextField } from \"./filterRichTextField\"\nimport type { HASTToRichTextConfig } from \"./hastToRichText\"\nimport { hastToRichText } from \"./hastToRichText\"\n\n/**\n * Configuration options for {@link rehypeRichText}.\n */\nexport type RehypeRichTextConfig = {\n\t/**\n\t * A CSS selector that targets the section of the document to convert to rich\n\t * text.\n\t *\n\t * @example `div.post`\n\t *\n\t * @defaultValue `\":root\"` - The top-level element of the document.\n\t */\n\tcontainer?: string\n\n\t/**\n\t * A list of CSS selectors to exclude matching nodes from the document to\n\t * process.\n\t *\n\t * @example `[\".hidden\", \"aside\"]`\n\t *\n\t * @defaultValue `[]` - No nodes are excluded.\n\t */\n\texclude?: string[]\n\n\t/**\n\t * A list of CSS selectors to include only matching nodes from the document to\n\t * process.\n\t *\n\t * @example `[\"p\", \"img\"]`\n\t *\n\t * @defaultValue `[]` - All nodes are included.\n\t */\n\tinclude?: string[]\n\n\t/**\n\t * A rich text or title field model definition. When provided the serializer\n\t * will filter out any node types not allowed by the model.\n\t *\n\t * @defaultValue `undefined` - No filtering is applied.\n\t */\n\tmodel?: CustomTypeModelRichTextField\n} & HASTToRichTextConfig\n\n/**\n * A unified plugin that compiles a hast tree to a Prismic rich text field.\n *\n * @param config - Configuration options for the rehype rich text processor.\n */\n// unified requires the function to be typed directly with the\n// `Plugin` type to properly infer return types on processors.\nexport const rehypeRichText: Plugin<\n\t[config?: RehypeRichTextConfig],\n\tRoot,\n\tRichTextField\n> = function rehypeRichText(config) {\n\t// This is a bit dirty, but it seems like that's how rehype intends\n\t// it to be due to JSDocs limitations(?), see:\n\t// https://github.com/rehypejs/rehype/blob/f6912ac680704f1ef4b558ac57cbf0dd62ed0892/packages/rehype-stringify/lib/index.js#L18-L20\n\t// eslint-disable-next-line @typescript-eslint/no-this-alias\n\tconst self = this as unknown as Processor<\n\t\tundefined,\n\t\tundefined,\n\t\tundefined,\n\t\tRoot,\n\t\tRichTextField\n\t>\n\n\t// We need to exclude nodes _before_ we minify the tree as excluding\n\t// nodes could end up in more whitespaces to trim.\n\tself.use(() => {\n\t\treturn (tree: Root) => {\n\t\t\t// Extract container node if any is specified.\n\t\t\tif (config?.container) {\n\t\t\t\tconst element = select(config.container, tree)\n\n\t\t\t\tif (!element) {\n\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t`No container matching \\`${config?.container}\\` could be found in the input AST.`,\n\t\t\t\t\t)\n\t\t\t\t}\n\n\t\t\t\t// We cannot reassign the tree itself, so we instead replace\n\t\t\t\t// its children with the found element.\n\t\t\t\ttree.children = [element]\n\t\t\t}\n\n\t\t\t// Remove excluded nodes if any are specified\n\t\t\tif (config?.exclude) {\n\t\t\t\t// We join selector to only run one query\n\t\t\t\tconst nodesToExclude = selectAll(config.exclude.join(\", \"), tree)\n\n\t\t\t\tremove(tree, (node) => nodesToExclude.includes(node as Element))\n\t\t\t}\n\n\t\t\t// Include only nodes to include\n\t\t\tif (config?.include) {\n\t\t\t\tconst nodesToInclude: Element[] = []\n\n\t\t\t\t// We join selector to only run one query\n\t\t\t\tconst selector = config.include.join(\", \")\n\t\t\t\tconst rawNodesToInclude = selectAll(selector, tree)\n\n\t\t\t\t// We walk the tree to exclude matching nodes that are children of other matching nodes.\n\t\t\t\tvisit(tree, (node) => {\n\t\t\t\t\tif (rawNodesToInclude.includes(node as Element)) {\n\t\t\t\t\t\tnodesToInclude.push(node as Element)\n\n\t\t\t\t\t\t// Stop traversing this part of the tree since we found its matching parent node.\n\t\t\t\t\t\treturn SKIP\n\t\t\t\t\t}\n\t\t\t\t})\n\n\t\t\t\t// We cannot reassign the tree itself, so we instead replace\n\t\t\t\t// its children with the found element.\n\t\t\t\ttree.children = nodesToInclude\n\t\t\t}\n\n\t\t\t// Mark nodes matching CSS selectors\n\t\t\tif (config?.serializer) {\n\t\t\t\tfor (const key in config.serializer) {\n\t\t\t\t\t// HTML tag names are single word, lowercase strings, a-z and 1-6 (headings).\n\t\t\t\t\t// Here we want to match anything that's not a valid HTML tag name and treat\n\t\t\t\t\t// it as a CSS selector. See: https://regex101.com/r/LILLWH/1\n\t\t\t\t\tif (!/^[a-z]+[1-6]?$/.test(key)) {\n\t\t\t\t\t\tconst matches = selectAll(key, tree)\n\n\t\t\t\t\t\tfor (let i = 0; i < matches.length; i++) {\n\t\t\t\t\t\t\tmatches[i].matchesSerializer = key\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t})\n\n\t// `rehypeRichText` _depends_ on `rehypeMinifyWhitespace`, that's why it's\n\t// registered within the plugin rather than on the processor directly.\n\tself.use(rehypeMinifyWhitespace)\n\n\tself.compiler = compiler\n\n\tfunction compiler(tree: Root, file: VFile): RichTextField {\n\t\tconst richTextField = hastToRichText(tree, file, config)\n\n\t\tif (config?.model) {\n\t\t\treturn filterRichTextField(richTextField, config.model)\n\t\t}\n\n\t\treturn richTextField\n\t}\n}\n\ndeclare module \"unified\" {\n\t// Register unified processor the result type.\n\tinterface CompileResultMap {\n\t\tRichTextField: RichTextField\n\t}\n}\n\ndeclare module \"hast\" {\n\t// Extend hast node with a rich text value.\n\tinterface Element {\n\t\t/**\n\t\t * A serializer this node matches to. Nodes are marked with this property\n\t\t * when they match a CSS selector from the serializer map.\n\t\t */\n\t\tmatchesSerializer?: string\n\t}\n}\n"],"names":["rehypeRichText","select","selectAll","remove","visit","SKIP","hastToRichText","filterRichTextField"],"mappings":";;;;;;;;AAkEa,MAAA,iBAIT,SAASA,gBAAe,QAAM;AAKjC,QAAM,OAAO;AAUb,OAAK,IAAI,MAAK;AACb,WAAO,CAAC,SAAc;AAErB,UAAI,iCAAQ,WAAW;AACtB,cAAM,UAAUC,eAAA,OAAO,OAAO,WAAW,IAAI;AAE7C,YAAI,CAAC,SAAS;AACb,gBAAM,IAAI,MACT,2BAA2B,iCAAQ,SAAS,qCAAqC;AAAA,QAEnF;AAIK,aAAA,WAAW,CAAC,OAAO;AAAA,MACzB;AAGA,UAAI,iCAAQ,SAAS;AAEpB,cAAM,iBAAiBC,eAAU,UAAA,OAAO,QAAQ,KAAK,IAAI,GAAG,IAAI;AAEhEC,wBAAA,OAAO,MAAM,CAAC,SAAS,eAAe,SAAS,IAAe,CAAC;AAAA,MAChE;AAGA,UAAI,iCAAQ,SAAS;AACpB,cAAM,iBAA4B,CAAA;AAGlC,cAAM,WAAW,OAAO,QAAQ,KAAK,IAAI;AACnC,cAAA,oBAAoBD,eAAAA,UAAU,UAAU,IAAI;AAG5CE,6BAAA,MAAM,CAAC,SAAQ;AAChB,cAAA,kBAAkB,SAAS,IAAe,GAAG;AAChD,2BAAe,KAAK,IAAe;AAG5B,mBAAAC;UACR;AAAA,QAAA,CACA;AAID,aAAK,WAAW;AAAA,MACjB;AAGA,UAAI,iCAAQ,YAAY;AACZ,mBAAA,OAAO,OAAO,YAAY;AAIpC,cAAI,CAAC,iBAAiB,KAAK,GAAG,GAAG;AAC1B,kBAAA,UAAUH,eAAAA,UAAU,KAAK,IAAI;AAEnC,qBAAS,IAAI,GAAG,IAAI,QAAQ,QAAQ,KAAK;AAChC,sBAAA,CAAC,EAAE,oBAAoB;AAAA,YAChC;AAAA,UACD;AAAA,QACD;AAAA,MACD;AAAA,IAAA;AAAA,EACD,CACA;AAID,OAAK,IAAI,sBAAsB;AAE/B,OAAK,WAAW;AAEP,WAAA,SAAS,MAAY,MAAW;AACxC,UAAM,gBAAgBI,eAAA,eAAe,MAAM,MAAM,MAAM;AAEvD,QAAI,iCAAQ,OAAO;AACX,aAAAC,wCAAoB,eAAe,OAAO,KAAK;AAAA,IACvD;AAEO,WAAA;AAAA,EACR;AACD;;"}
1
+ {"version":3,"file":"rehypeRichText.cjs","names":["select","selectAll","SKIP","rehypeMinifyWhitespace","hastToRichText","filterRichTextField"],"sources":["../../src/lib/rehypeRichText.ts"],"sourcesContent":["import type { CustomTypeModelRichTextField, RichTextField } from \"@prismicio/client\"\nimport type { Element, Root } from \"hast\"\nimport { select, selectAll } from \"hast-util-select\"\nimport rehypeMinifyWhitespace from \"rehype-minify-whitespace\"\nimport type { Plugin, Processor } from \"unified\"\nimport { remove } from \"unist-util-remove\"\nimport { SKIP, visit } from \"unist-util-visit\"\nimport type { VFile } from \"vfile\"\n\nimport { filterRichTextField } from \"./filterRichTextField\"\nimport type { HASTToRichTextConfig } from \"./hastToRichText\"\nimport { hastToRichText } from \"./hastToRichText\"\n\n/** Configuration options for {@link rehypeRichText}. */\nexport type RehypeRichTextConfig = {\n\t/**\n\t * A CSS selector that targets the section of the document to convert to rich text.\n\t *\n\t * @example\n\t * \t;`div.post`\n\t *\n\t * @defaultValue `\":root\"` - The top-level element of the document.\n\t */\n\tcontainer?: string\n\n\t/**\n\t * A list of CSS selectors to exclude matching nodes from the document to process.\n\t *\n\t * @example\n\t * \t;`[\".hidden\", \"aside\"]`\n\t *\n\t * @defaultValue `[]` - No nodes are excluded.\n\t */\n\texclude?: string[]\n\n\t/**\n\t * A list of CSS selectors to include only matching nodes from the document to process.\n\t *\n\t * @example\n\t * \t;`[\"p\", \"img\"]`\n\t *\n\t * @defaultValue `[]` - All nodes are included.\n\t */\n\tinclude?: string[]\n\n\t/**\n\t * A rich text or title field model definition. When provided the serializer will filter out any\n\t * node types not allowed by the model.\n\t *\n\t * @defaultValue `undefined` - No filtering is applied.\n\t */\n\tmodel?: CustomTypeModelRichTextField\n} & HASTToRichTextConfig\n\n/**\n * A unified plugin that compiles a hast tree to a Prismic rich text field.\n *\n * @param config - Configuration options for the rehype rich text processor.\n */\n// unified requires the function to be typed directly with the\n// `Plugin` type to properly infer return types on processors.\nexport const rehypeRichText: Plugin<[config?: RehypeRichTextConfig], Root, RichTextField> =\n\tfunction rehypeRichText(config) {\n\t\t// This is a bit dirty, but it seems like that's how rehype intends\n\t\t// it to be due to JSDocs limitations(?), see:\n\t\t// https://github.com/rehypejs/rehype/blob/f6912ac680704f1ef4b558ac57cbf0dd62ed0892/packages/rehype-stringify/lib/index.js#L18-L20\n\t\t// eslint-disable-next-line @typescript-eslint/no-this-alias\n\t\tconst self = this as unknown as Processor<undefined, undefined, undefined, Root, RichTextField>\n\n\t\t// We need to exclude nodes _before_ we minify the tree as excluding\n\t\t// nodes could end up in more whitespaces to trim.\n\t\tself.use(() => {\n\t\t\treturn (tree: Root) => {\n\t\t\t\t// Extract container node if any is specified.\n\t\t\t\tif (config?.container) {\n\t\t\t\t\tconst element = select(config.container, tree)\n\n\t\t\t\t\tif (!element) {\n\t\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t\t`No container matching \\`${config?.container}\\` could be found in the input AST.`,\n\t\t\t\t\t\t)\n\t\t\t\t\t}\n\n\t\t\t\t\t// We cannot reassign the tree itself, so we instead replace\n\t\t\t\t\t// its children with the found element.\n\t\t\t\t\ttree.children = [element]\n\t\t\t\t}\n\n\t\t\t\t// Remove excluded nodes if any are specified\n\t\t\t\tif (config?.exclude) {\n\t\t\t\t\t// We join selector to only run one query\n\t\t\t\t\tconst nodesToExclude = selectAll(config.exclude.join(\", \"), tree)\n\n\t\t\t\t\tremove(tree, (node) => nodesToExclude.includes(node as Element))\n\t\t\t\t}\n\n\t\t\t\t// Include only nodes to include\n\t\t\t\tif (config?.include) {\n\t\t\t\t\tconst nodesToInclude: Element[] = []\n\n\t\t\t\t\t// We join selector to only run one query\n\t\t\t\t\tconst selector = config.include.join(\", \")\n\t\t\t\t\tconst rawNodesToInclude = selectAll(selector, tree)\n\n\t\t\t\t\t// We walk the tree to exclude matching nodes that are children of other matching nodes.\n\t\t\t\t\tvisit(tree, (node) => {\n\t\t\t\t\t\tif (rawNodesToInclude.includes(node as Element)) {\n\t\t\t\t\t\t\tnodesToInclude.push(node as Element)\n\n\t\t\t\t\t\t\t// Stop traversing this part of the tree since we found its matching parent node.\n\t\t\t\t\t\t\treturn SKIP\n\t\t\t\t\t\t}\n\t\t\t\t\t})\n\n\t\t\t\t\t// We cannot reassign the tree itself, so we instead replace\n\t\t\t\t\t// its children with the found element.\n\t\t\t\t\ttree.children = nodesToInclude\n\t\t\t\t}\n\n\t\t\t\t// Mark nodes matching CSS selectors\n\t\t\t\tif (config?.serializer) {\n\t\t\t\t\tfor (const key in config.serializer) {\n\t\t\t\t\t\t// HTML tag names are single word, lowercase strings, a-z and 1-6 (headings).\n\t\t\t\t\t\t// Here we want to match anything that's not a valid HTML tag name and treat\n\t\t\t\t\t\t// it as a CSS selector. See: https://regex101.com/r/LILLWH/1\n\t\t\t\t\t\tif (!/^[a-z]+[1-6]?$/.test(key)) {\n\t\t\t\t\t\t\tconst matches = selectAll(key, tree)\n\n\t\t\t\t\t\t\tfor (let i = 0; i < matches.length; i++) {\n\t\t\t\t\t\t\t\tmatches[i].matchesSerializer = key\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\n\t\t// `rehypeRichText` _depends_ on `rehypeMinifyWhitespace`, that's why it's\n\t\t// registered within the plugin rather than on the processor directly.\n\t\tself.use(rehypeMinifyWhitespace)\n\n\t\tself.compiler = compiler\n\n\t\tfunction compiler(tree: Root, file: VFile): RichTextField {\n\t\t\tconst richTextField = hastToRichText(tree, file, config)\n\n\t\t\tif (config?.model) {\n\t\t\t\treturn filterRichTextField(richTextField, config.model)\n\t\t\t}\n\n\t\t\treturn richTextField\n\t\t}\n\t}\n\ndeclare module \"unified\" {\n\t// Register unified processor the result type.\n\tinterface CompileResultMap {\n\t\tRichTextField: RichTextField\n\t}\n}\n\ndeclare module \"hast\" {\n\t// Extend hast node with a rich text value.\n\tinterface Element {\n\t\t/**\n\t\t * A serializer this node matches to. Nodes are marked with this property when they match a CSS\n\t\t * selector from the serializer map.\n\t\t */\n\t\tmatchesSerializer?: string\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;AA6DA,MAAa,iBACZ,SAAS,eAAe,QAAQ;CAK/B,MAAM,OAAO;CAIb,KAAK,UAAU;EACd,QAAQ,SAAe;GAEtB,IAAI,QAAQ,WAAW;IACtB,MAAM,WAAA,GAAUA,iBAAAA,OAAAA,CAAO,OAAO,WAAW,IAAI;IAE7C,IAAI,CAAC,SACJ,MAAM,IAAI,MACT,2BAA2B,QAAQ,UAAU,oCAC9C;IAKD,KAAK,WAAW,CAAC,OAAO;GACzB;GAGA,IAAI,QAAQ,SAAS;IAEpB,MAAM,kBAAA,GAAiBC,iBAAAA,UAAAA,CAAU,OAAO,QAAQ,KAAK,IAAI,GAAG,IAAI;IAEhE,CAAA,GAAA,kBAAA,OAAA,CAAO,OAAO,SAAS,eAAe,SAAS,IAAe,CAAC;GAChE;GAGA,IAAI,QAAQ,SAAS;IACpB,MAAM,iBAA4B,CAAC;IAGnC,MAAM,WAAW,OAAO,QAAQ,KAAK,IAAI;IACzC,MAAM,qBAAA,GAAoBA,iBAAAA,UAAAA,CAAU,UAAU,IAAI;IAGlD,CAAA,GAAA,iBAAA,MAAA,CAAM,OAAO,SAAS;KACrB,IAAI,kBAAkB,SAAS,IAAe,GAAG;MAChD,eAAe,KAAK,IAAe;MAGnC,OAAOC,iBAAAA;KACR;IACD,CAAC;IAID,KAAK,WAAW;GACjB;GAGA,IAAI,QAAQ,YACN;SAAA,MAAM,OAAO,OAAO,YAIxB,IAAI,CAAC,iBAAiB,KAAK,GAAG,GAAG;KAChC,MAAM,WAAA,GAAUD,iBAAAA,UAAAA,CAAU,KAAK,IAAI;KAEnC,KAAK,IAAI,IAAI,GAAG,IAAI,QAAQ,QAAQ,KACnC,QAAQ,EAAE,CAAC,oBAAoB;IAEjC;;EAGH;CACD,CAAC;CAID,KAAK,IAAIE,yBAAAA,OAAsB;CAE/B,KAAK,WAAW;CAEhB,SAAS,SAAS,MAAY,MAA4B;EACzD,MAAM,gBAAgBC,uBAAAA,eAAe,MAAM,MAAM,MAAM;EAEvD,IAAI,QAAQ,OACX,OAAOC,4BAAAA,oBAAoB,eAAe,OAAO,KAAK;EAGvD,OAAO;CACR;AACD"}
@@ -0,0 +1,59 @@
1
+ import { HASTToRichTextConfig } from "./hastToRichText.cjs";
2
+ import { CustomTypeModelRichTextField, RichTextField } from "@prismicio/client";
3
+ import "hast";
4
+ import "unified";
5
+ //#region src/lib/rehypeRichText.d.ts
6
+ /** Configuration options for {@link rehypeRichText}. */
7
+ type RehypeRichTextConfig = {
8
+ /**
9
+ * A CSS selector that targets the section of the document to convert to rich text.
10
+ *
11
+ * @example
12
+ * ;`div.post`
13
+ *
14
+ * @defaultValue `":root"` - The top-level element of the document.
15
+ */
16
+ container?: string;
17
+ /**
18
+ * A list of CSS selectors to exclude matching nodes from the document to process.
19
+ *
20
+ * @example
21
+ * ;`[".hidden", "aside"]`
22
+ *
23
+ * @defaultValue `[]` - No nodes are excluded.
24
+ */
25
+ exclude?: string[];
26
+ /**
27
+ * A list of CSS selectors to include only matching nodes from the document to process.
28
+ *
29
+ * @example
30
+ * ;`["p", "img"]`
31
+ *
32
+ * @defaultValue `[]` - All nodes are included.
33
+ */
34
+ include?: string[];
35
+ /**
36
+ * A rich text or title field model definition. When provided the serializer will filter out any
37
+ * node types not allowed by the model.
38
+ *
39
+ * @defaultValue `undefined` - No filtering is applied.
40
+ */
41
+ model?: CustomTypeModelRichTextField;
42
+ } & HASTToRichTextConfig;
43
+ declare module "unified" {
44
+ interface CompileResultMap {
45
+ RichTextField: RichTextField;
46
+ }
47
+ }
48
+ declare module "hast" {
49
+ interface Element {
50
+ /**
51
+ * A serializer this node matches to. Nodes are marked with this property when they match a CSS
52
+ * selector from the serializer map.
53
+ */
54
+ matchesSerializer?: string;
55
+ }
56
+ }
57
+ //#endregion
58
+ export { RehypeRichTextConfig };
59
+ //# sourceMappingURL=rehypeRichText.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rehypeRichText.d.cts","names":[],"sources":["../../src/lib/rehypeRichText.ts"],"mappings":";;;;;;KAcY;;;;;;;;;EASX;;;;;;;;;EAUA;;;;;;;;;EAUA;;;;;;;EAQA,QAAQ;IACL;;YAwGO;IACT,eAAe;;;;YAMN;;;;;IAKT"}
@@ -1,65 +1,59 @@
1
- import type { CustomTypeModelRichTextField, RichTextField } from "@prismicio/client";
2
- import type { Root } from "hast";
3
- import type { Plugin } from "unified";
4
- import type { HASTToRichTextConfig } from "./hastToRichText";
5
- /**
6
- * Configuration options for {@link rehypeRichText}.
7
- */
8
- export type RehypeRichTextConfig = {
9
- /**
10
- * A CSS selector that targets the section of the document to convert to rich
11
- * text.
12
- *
13
- * @example `div.post`
14
- *
15
- * @defaultValue `":root"` - The top-level element of the document.
16
- */
17
- container?: string;
18
- /**
19
- * A list of CSS selectors to exclude matching nodes from the document to
20
- * process.
21
- *
22
- * @example `[".hidden", "aside"]`
23
- *
24
- * @defaultValue `[]` - No nodes are excluded.
25
- */
26
- exclude?: string[];
27
- /**
28
- * A list of CSS selectors to include only matching nodes from the document to
29
- * process.
30
- *
31
- * @example `["p", "img"]`
32
- *
33
- * @defaultValue `[]` - All nodes are included.
34
- */
35
- include?: string[];
36
- /**
37
- * A rich text or title field model definition. When provided the serializer
38
- * will filter out any node types not allowed by the model.
39
- *
40
- * @defaultValue `undefined` - No filtering is applied.
41
- */
42
- model?: CustomTypeModelRichTextField;
1
+ import { HASTToRichTextConfig } from "./hastToRichText.js";
2
+ import "unified";
3
+ import { CustomTypeModelRichTextField, RichTextField } from "@prismicio/client";
4
+ import "hast";
5
+ //#region src/lib/rehypeRichText.d.ts
6
+ /** Configuration options for {@link rehypeRichText}. */
7
+ type RehypeRichTextConfig = {
8
+ /**
9
+ * A CSS selector that targets the section of the document to convert to rich text.
10
+ *
11
+ * @example
12
+ * ;`div.post`
13
+ *
14
+ * @defaultValue `":root"` - The top-level element of the document.
15
+ */
16
+ container?: string;
17
+ /**
18
+ * A list of CSS selectors to exclude matching nodes from the document to process.
19
+ *
20
+ * @example
21
+ * ;`[".hidden", "aside"]`
22
+ *
23
+ * @defaultValue `[]` - No nodes are excluded.
24
+ */
25
+ exclude?: string[];
26
+ /**
27
+ * A list of CSS selectors to include only matching nodes from the document to process.
28
+ *
29
+ * @example
30
+ * ;`["p", "img"]`
31
+ *
32
+ * @defaultValue `[]` - All nodes are included.
33
+ */
34
+ include?: string[];
35
+ /**
36
+ * A rich text or title field model definition. When provided the serializer will filter out any
37
+ * node types not allowed by the model.
38
+ *
39
+ * @defaultValue `undefined` - No filtering is applied.
40
+ */
41
+ model?: CustomTypeModelRichTextField;
43
42
  } & HASTToRichTextConfig;
44
- /**
45
- * A unified plugin that compiles a hast tree to a Prismic rich text field.
46
- *
47
- * @param config - Configuration options for the rehype rich text processor.
48
- */
49
- export declare const rehypeRichText: Plugin<[
50
- config?: RehypeRichTextConfig
51
- ], Root, RichTextField>;
52
43
  declare module "unified" {
53
- interface CompileResultMap {
54
- RichTextField: RichTextField;
55
- }
44
+ interface CompileResultMap {
45
+ RichTextField: RichTextField;
46
+ }
56
47
  }
57
48
  declare module "hast" {
58
- interface Element {
59
- /**
60
- * A serializer this node matches to. Nodes are marked with this property
61
- * when they match a CSS selector from the serializer map.
62
- */
63
- matchesSerializer?: string;
64
- }
49
+ interface Element {
50
+ /**
51
+ * A serializer this node matches to. Nodes are marked with this property when they match a CSS
52
+ * selector from the serializer map.
53
+ */
54
+ matchesSerializer?: string;
55
+ }
65
56
  }
57
+ //#endregion
58
+ export { RehypeRichTextConfig };
59
+ //# sourceMappingURL=rehypeRichText.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rehypeRichText.d.ts","names":[],"sources":["../../src/lib/rehypeRichText.ts"],"mappings":";;;;;;KAcY;;;;;;;;;EASX;;;;;;;;;EAUA;;;;;;;;;EAUA;;;;;;;EAQA,QAAQ;IACL;;YAwGO;IACT,eAAe;;;;YAMN;;;;;IAKT"}
@@ -1,59 +1,57 @@
1
+ import { filterRichTextField } from "./filterRichTextField.js";
2
+ import { hastToRichText } from "./hastToRichText.js";
1
3
  import { select, selectAll } from "hast-util-select";
2
4
  import rehypeMinifyWhitespace from "rehype-minify-whitespace";
3
5
  import { remove } from "unist-util-remove";
4
- import { visit, SKIP } from "unist-util-visit";
5
- import { filterRichTextField } from "./filterRichTextField.js";
6
- import { hastToRichText } from "./hastToRichText.js";
7
- const rehypeRichText = function rehypeRichText2(config) {
8
- const self = this;
9
- self.use(() => {
10
- return (tree) => {
11
- if (config == null ? void 0 : config.container) {
12
- const element = select(config.container, tree);
13
- if (!element) {
14
- throw new Error(`No container matching \`${config == null ? void 0 : config.container}\` could be found in the input AST.`);
15
- }
16
- tree.children = [element];
17
- }
18
- if (config == null ? void 0 : config.exclude) {
19
- const nodesToExclude = selectAll(config.exclude.join(", "), tree);
20
- remove(tree, (node) => nodesToExclude.includes(node));
21
- }
22
- if (config == null ? void 0 : config.include) {
23
- const nodesToInclude = [];
24
- const selector = config.include.join(", ");
25
- const rawNodesToInclude = selectAll(selector, tree);
26
- visit(tree, (node) => {
27
- if (rawNodesToInclude.includes(node)) {
28
- nodesToInclude.push(node);
29
- return SKIP;
30
- }
31
- });
32
- tree.children = nodesToInclude;
33
- }
34
- if (config == null ? void 0 : config.serializer) {
35
- for (const key in config.serializer) {
36
- if (!/^[a-z]+[1-6]?$/.test(key)) {
37
- const matches = selectAll(key, tree);
38
- for (let i = 0; i < matches.length; i++) {
39
- matches[i].matchesSerializer = key;
40
- }
41
- }
42
- }
43
- }
44
- };
45
- });
46
- self.use(rehypeMinifyWhitespace);
47
- self.compiler = compiler;
48
- function compiler(tree, file) {
49
- const richTextField = hastToRichText(tree, file, config);
50
- if (config == null ? void 0 : config.model) {
51
- return filterRichTextField(richTextField, config.model);
52
- }
53
- return richTextField;
54
- }
55
- };
56
- export {
57
- rehypeRichText
6
+ import { SKIP, visit } from "unist-util-visit";
7
+ //#region src/lib/rehypeRichText.ts
8
+ /**
9
+ * A unified plugin that compiles a hast tree to a Prismic rich text field.
10
+ *
11
+ * @param config - Configuration options for the rehype rich text processor.
12
+ */
13
+ const rehypeRichText = function rehypeRichText(config) {
14
+ const self = this;
15
+ self.use(() => {
16
+ return (tree) => {
17
+ if (config?.container) {
18
+ const element = select(config.container, tree);
19
+ if (!element) throw new Error(`No container matching \`${config?.container}\` could be found in the input AST.`);
20
+ tree.children = [element];
21
+ }
22
+ if (config?.exclude) {
23
+ const nodesToExclude = selectAll(config.exclude.join(", "), tree);
24
+ remove(tree, (node) => nodesToExclude.includes(node));
25
+ }
26
+ if (config?.include) {
27
+ const nodesToInclude = [];
28
+ const selector = config.include.join(", ");
29
+ const rawNodesToInclude = selectAll(selector, tree);
30
+ visit(tree, (node) => {
31
+ if (rawNodesToInclude.includes(node)) {
32
+ nodesToInclude.push(node);
33
+ return SKIP;
34
+ }
35
+ });
36
+ tree.children = nodesToInclude;
37
+ }
38
+ if (config?.serializer) {
39
+ for (const key in config.serializer) if (!/^[a-z]+[1-6]?$/.test(key)) {
40
+ const matches = selectAll(key, tree);
41
+ for (let i = 0; i < matches.length; i++) matches[i].matchesSerializer = key;
42
+ }
43
+ }
44
+ };
45
+ });
46
+ self.use(rehypeMinifyWhitespace);
47
+ self.compiler = compiler;
48
+ function compiler(tree, file) {
49
+ const richTextField = hastToRichText(tree, file, config);
50
+ if (config?.model) return filterRichTextField(richTextField, config.model);
51
+ return richTextField;
52
+ }
58
53
  };
59
- //# sourceMappingURL=rehypeRichText.js.map
54
+ //#endregion
55
+ export { rehypeRichText };
56
+
57
+ //# sourceMappingURL=rehypeRichText.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"rehypeRichText.js","sources":["../../../src/lib/rehypeRichText.ts"],"sourcesContent":["import type {\n\tCustomTypeModelRichTextField,\n\tRichTextField,\n} from \"@prismicio/client\"\nimport type { Element, Root } from \"hast\"\nimport { select, selectAll } from \"hast-util-select\"\nimport rehypeMinifyWhitespace from \"rehype-minify-whitespace\"\nimport type { Plugin, Processor } from \"unified\"\nimport { remove } from \"unist-util-remove\"\nimport { SKIP, visit } from \"unist-util-visit\"\nimport type { VFile } from \"vfile\"\n\nimport { filterRichTextField } from \"./filterRichTextField\"\nimport type { HASTToRichTextConfig } from \"./hastToRichText\"\nimport { hastToRichText } from \"./hastToRichText\"\n\n/**\n * Configuration options for {@link rehypeRichText}.\n */\nexport type RehypeRichTextConfig = {\n\t/**\n\t * A CSS selector that targets the section of the document to convert to rich\n\t * text.\n\t *\n\t * @example `div.post`\n\t *\n\t * @defaultValue `\":root\"` - The top-level element of the document.\n\t */\n\tcontainer?: string\n\n\t/**\n\t * A list of CSS selectors to exclude matching nodes from the document to\n\t * process.\n\t *\n\t * @example `[\".hidden\", \"aside\"]`\n\t *\n\t * @defaultValue `[]` - No nodes are excluded.\n\t */\n\texclude?: string[]\n\n\t/**\n\t * A list of CSS selectors to include only matching nodes from the document to\n\t * process.\n\t *\n\t * @example `[\"p\", \"img\"]`\n\t *\n\t * @defaultValue `[]` - All nodes are included.\n\t */\n\tinclude?: string[]\n\n\t/**\n\t * A rich text or title field model definition. When provided the serializer\n\t * will filter out any node types not allowed by the model.\n\t *\n\t * @defaultValue `undefined` - No filtering is applied.\n\t */\n\tmodel?: CustomTypeModelRichTextField\n} & HASTToRichTextConfig\n\n/**\n * A unified plugin that compiles a hast tree to a Prismic rich text field.\n *\n * @param config - Configuration options for the rehype rich text processor.\n */\n// unified requires the function to be typed directly with the\n// `Plugin` type to properly infer return types on processors.\nexport const rehypeRichText: Plugin<\n\t[config?: RehypeRichTextConfig],\n\tRoot,\n\tRichTextField\n> = function rehypeRichText(config) {\n\t// This is a bit dirty, but it seems like that's how rehype intends\n\t// it to be due to JSDocs limitations(?), see:\n\t// https://github.com/rehypejs/rehype/blob/f6912ac680704f1ef4b558ac57cbf0dd62ed0892/packages/rehype-stringify/lib/index.js#L18-L20\n\t// eslint-disable-next-line @typescript-eslint/no-this-alias\n\tconst self = this as unknown as Processor<\n\t\tundefined,\n\t\tundefined,\n\t\tundefined,\n\t\tRoot,\n\t\tRichTextField\n\t>\n\n\t// We need to exclude nodes _before_ we minify the tree as excluding\n\t// nodes could end up in more whitespaces to trim.\n\tself.use(() => {\n\t\treturn (tree: Root) => {\n\t\t\t// Extract container node if any is specified.\n\t\t\tif (config?.container) {\n\t\t\t\tconst element = select(config.container, tree)\n\n\t\t\t\tif (!element) {\n\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t`No container matching \\`${config?.container}\\` could be found in the input AST.`,\n\t\t\t\t\t)\n\t\t\t\t}\n\n\t\t\t\t// We cannot reassign the tree itself, so we instead replace\n\t\t\t\t// its children with the found element.\n\t\t\t\ttree.children = [element]\n\t\t\t}\n\n\t\t\t// Remove excluded nodes if any are specified\n\t\t\tif (config?.exclude) {\n\t\t\t\t// We join selector to only run one query\n\t\t\t\tconst nodesToExclude = selectAll(config.exclude.join(\", \"), tree)\n\n\t\t\t\tremove(tree, (node) => nodesToExclude.includes(node as Element))\n\t\t\t}\n\n\t\t\t// Include only nodes to include\n\t\t\tif (config?.include) {\n\t\t\t\tconst nodesToInclude: Element[] = []\n\n\t\t\t\t// We join selector to only run one query\n\t\t\t\tconst selector = config.include.join(\", \")\n\t\t\t\tconst rawNodesToInclude = selectAll(selector, tree)\n\n\t\t\t\t// We walk the tree to exclude matching nodes that are children of other matching nodes.\n\t\t\t\tvisit(tree, (node) => {\n\t\t\t\t\tif (rawNodesToInclude.includes(node as Element)) {\n\t\t\t\t\t\tnodesToInclude.push(node as Element)\n\n\t\t\t\t\t\t// Stop traversing this part of the tree since we found its matching parent node.\n\t\t\t\t\t\treturn SKIP\n\t\t\t\t\t}\n\t\t\t\t})\n\n\t\t\t\t// We cannot reassign the tree itself, so we instead replace\n\t\t\t\t// its children with the found element.\n\t\t\t\ttree.children = nodesToInclude\n\t\t\t}\n\n\t\t\t// Mark nodes matching CSS selectors\n\t\t\tif (config?.serializer) {\n\t\t\t\tfor (const key in config.serializer) {\n\t\t\t\t\t// HTML tag names are single word, lowercase strings, a-z and 1-6 (headings).\n\t\t\t\t\t// Here we want to match anything that's not a valid HTML tag name and treat\n\t\t\t\t\t// it as a CSS selector. See: https://regex101.com/r/LILLWH/1\n\t\t\t\t\tif (!/^[a-z]+[1-6]?$/.test(key)) {\n\t\t\t\t\t\tconst matches = selectAll(key, tree)\n\n\t\t\t\t\t\tfor (let i = 0; i < matches.length; i++) {\n\t\t\t\t\t\t\tmatches[i].matchesSerializer = key\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t})\n\n\t// `rehypeRichText` _depends_ on `rehypeMinifyWhitespace`, that's why it's\n\t// registered within the plugin rather than on the processor directly.\n\tself.use(rehypeMinifyWhitespace)\n\n\tself.compiler = compiler\n\n\tfunction compiler(tree: Root, file: VFile): RichTextField {\n\t\tconst richTextField = hastToRichText(tree, file, config)\n\n\t\tif (config?.model) {\n\t\t\treturn filterRichTextField(richTextField, config.model)\n\t\t}\n\n\t\treturn richTextField\n\t}\n}\n\ndeclare module \"unified\" {\n\t// Register unified processor the result type.\n\tinterface CompileResultMap {\n\t\tRichTextField: RichTextField\n\t}\n}\n\ndeclare module \"hast\" {\n\t// Extend hast node with a rich text value.\n\tinterface Element {\n\t\t/**\n\t\t * A serializer this node matches to. Nodes are marked with this property\n\t\t * when they match a CSS selector from the serializer map.\n\t\t */\n\t\tmatchesSerializer?: string\n\t}\n}\n"],"names":["rehypeRichText"],"mappings":";;;;;;AAkEa,MAAA,iBAIT,SAASA,gBAAe,QAAM;AAKjC,QAAM,OAAO;AAUb,OAAK,IAAI,MAAK;AACb,WAAO,CAAC,SAAc;AAErB,UAAI,iCAAQ,WAAW;AACtB,cAAM,UAAU,OAAO,OAAO,WAAW,IAAI;AAE7C,YAAI,CAAC,SAAS;AACb,gBAAM,IAAI,MACT,2BAA2B,iCAAQ,SAAS,qCAAqC;AAAA,QAEnF;AAIK,aAAA,WAAW,CAAC,OAAO;AAAA,MACzB;AAGA,UAAI,iCAAQ,SAAS;AAEpB,cAAM,iBAAiB,UAAU,OAAO,QAAQ,KAAK,IAAI,GAAG,IAAI;AAEhE,eAAO,MAAM,CAAC,SAAS,eAAe,SAAS,IAAe,CAAC;AAAA,MAChE;AAGA,UAAI,iCAAQ,SAAS;AACpB,cAAM,iBAA4B,CAAA;AAGlC,cAAM,WAAW,OAAO,QAAQ,KAAK,IAAI;AACnC,cAAA,oBAAoB,UAAU,UAAU,IAAI;AAG5C,cAAA,MAAM,CAAC,SAAQ;AAChB,cAAA,kBAAkB,SAAS,IAAe,GAAG;AAChD,2BAAe,KAAK,IAAe;AAG5B,mBAAA;AAAA,UACR;AAAA,QAAA,CACA;AAID,aAAK,WAAW;AAAA,MACjB;AAGA,UAAI,iCAAQ,YAAY;AACZ,mBAAA,OAAO,OAAO,YAAY;AAIpC,cAAI,CAAC,iBAAiB,KAAK,GAAG,GAAG;AAC1B,kBAAA,UAAU,UAAU,KAAK,IAAI;AAEnC,qBAAS,IAAI,GAAG,IAAI,QAAQ,QAAQ,KAAK;AAChC,sBAAA,CAAC,EAAE,oBAAoB;AAAA,YAChC;AAAA,UACD;AAAA,QACD;AAAA,MACD;AAAA,IAAA;AAAA,EACD,CACA;AAID,OAAK,IAAI,sBAAsB;AAE/B,OAAK,WAAW;AAEP,WAAA,SAAS,MAAY,MAAW;AACxC,UAAM,gBAAgB,eAAe,MAAM,MAAM,MAAM;AAEvD,QAAI,iCAAQ,OAAO;AACX,aAAA,oBAAoB,eAAe,OAAO,KAAK;AAAA,IACvD;AAEO,WAAA;AAAA,EACR;AACD;"}
1
+ {"version":3,"file":"rehypeRichText.js","names":[],"sources":["../../src/lib/rehypeRichText.ts"],"sourcesContent":["import type { CustomTypeModelRichTextField, RichTextField } from \"@prismicio/client\"\nimport type { Element, Root } from \"hast\"\nimport { select, selectAll } from \"hast-util-select\"\nimport rehypeMinifyWhitespace from \"rehype-minify-whitespace\"\nimport type { Plugin, Processor } from \"unified\"\nimport { remove } from \"unist-util-remove\"\nimport { SKIP, visit } from \"unist-util-visit\"\nimport type { VFile } from \"vfile\"\n\nimport { filterRichTextField } from \"./filterRichTextField\"\nimport type { HASTToRichTextConfig } from \"./hastToRichText\"\nimport { hastToRichText } from \"./hastToRichText\"\n\n/** Configuration options for {@link rehypeRichText}. */\nexport type RehypeRichTextConfig = {\n\t/**\n\t * A CSS selector that targets the section of the document to convert to rich text.\n\t *\n\t * @example\n\t * \t;`div.post`\n\t *\n\t * @defaultValue `\":root\"` - The top-level element of the document.\n\t */\n\tcontainer?: string\n\n\t/**\n\t * A list of CSS selectors to exclude matching nodes from the document to process.\n\t *\n\t * @example\n\t * \t;`[\".hidden\", \"aside\"]`\n\t *\n\t * @defaultValue `[]` - No nodes are excluded.\n\t */\n\texclude?: string[]\n\n\t/**\n\t * A list of CSS selectors to include only matching nodes from the document to process.\n\t *\n\t * @example\n\t * \t;`[\"p\", \"img\"]`\n\t *\n\t * @defaultValue `[]` - All nodes are included.\n\t */\n\tinclude?: string[]\n\n\t/**\n\t * A rich text or title field model definition. When provided the serializer will filter out any\n\t * node types not allowed by the model.\n\t *\n\t * @defaultValue `undefined` - No filtering is applied.\n\t */\n\tmodel?: CustomTypeModelRichTextField\n} & HASTToRichTextConfig\n\n/**\n * A unified plugin that compiles a hast tree to a Prismic rich text field.\n *\n * @param config - Configuration options for the rehype rich text processor.\n */\n// unified requires the function to be typed directly with the\n// `Plugin` type to properly infer return types on processors.\nexport const rehypeRichText: Plugin<[config?: RehypeRichTextConfig], Root, RichTextField> =\n\tfunction rehypeRichText(config) {\n\t\t// This is a bit dirty, but it seems like that's how rehype intends\n\t\t// it to be due to JSDocs limitations(?), see:\n\t\t// https://github.com/rehypejs/rehype/blob/f6912ac680704f1ef4b558ac57cbf0dd62ed0892/packages/rehype-stringify/lib/index.js#L18-L20\n\t\t// eslint-disable-next-line @typescript-eslint/no-this-alias\n\t\tconst self = this as unknown as Processor<undefined, undefined, undefined, Root, RichTextField>\n\n\t\t// We need to exclude nodes _before_ we minify the tree as excluding\n\t\t// nodes could end up in more whitespaces to trim.\n\t\tself.use(() => {\n\t\t\treturn (tree: Root) => {\n\t\t\t\t// Extract container node if any is specified.\n\t\t\t\tif (config?.container) {\n\t\t\t\t\tconst element = select(config.container, tree)\n\n\t\t\t\t\tif (!element) {\n\t\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t\t`No container matching \\`${config?.container}\\` could be found in the input AST.`,\n\t\t\t\t\t\t)\n\t\t\t\t\t}\n\n\t\t\t\t\t// We cannot reassign the tree itself, so we instead replace\n\t\t\t\t\t// its children with the found element.\n\t\t\t\t\ttree.children = [element]\n\t\t\t\t}\n\n\t\t\t\t// Remove excluded nodes if any are specified\n\t\t\t\tif (config?.exclude) {\n\t\t\t\t\t// We join selector to only run one query\n\t\t\t\t\tconst nodesToExclude = selectAll(config.exclude.join(\", \"), tree)\n\n\t\t\t\t\tremove(tree, (node) => nodesToExclude.includes(node as Element))\n\t\t\t\t}\n\n\t\t\t\t// Include only nodes to include\n\t\t\t\tif (config?.include) {\n\t\t\t\t\tconst nodesToInclude: Element[] = []\n\n\t\t\t\t\t// We join selector to only run one query\n\t\t\t\t\tconst selector = config.include.join(\", \")\n\t\t\t\t\tconst rawNodesToInclude = selectAll(selector, tree)\n\n\t\t\t\t\t// We walk the tree to exclude matching nodes that are children of other matching nodes.\n\t\t\t\t\tvisit(tree, (node) => {\n\t\t\t\t\t\tif (rawNodesToInclude.includes(node as Element)) {\n\t\t\t\t\t\t\tnodesToInclude.push(node as Element)\n\n\t\t\t\t\t\t\t// Stop traversing this part of the tree since we found its matching parent node.\n\t\t\t\t\t\t\treturn SKIP\n\t\t\t\t\t\t}\n\t\t\t\t\t})\n\n\t\t\t\t\t// We cannot reassign the tree itself, so we instead replace\n\t\t\t\t\t// its children with the found element.\n\t\t\t\t\ttree.children = nodesToInclude\n\t\t\t\t}\n\n\t\t\t\t// Mark nodes matching CSS selectors\n\t\t\t\tif (config?.serializer) {\n\t\t\t\t\tfor (const key in config.serializer) {\n\t\t\t\t\t\t// HTML tag names are single word, lowercase strings, a-z and 1-6 (headings).\n\t\t\t\t\t\t// Here we want to match anything that's not a valid HTML tag name and treat\n\t\t\t\t\t\t// it as a CSS selector. See: https://regex101.com/r/LILLWH/1\n\t\t\t\t\t\tif (!/^[a-z]+[1-6]?$/.test(key)) {\n\t\t\t\t\t\t\tconst matches = selectAll(key, tree)\n\n\t\t\t\t\t\t\tfor (let i = 0; i < matches.length; i++) {\n\t\t\t\t\t\t\t\tmatches[i].matchesSerializer = key\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\n\t\t// `rehypeRichText` _depends_ on `rehypeMinifyWhitespace`, that's why it's\n\t\t// registered within the plugin rather than on the processor directly.\n\t\tself.use(rehypeMinifyWhitespace)\n\n\t\tself.compiler = compiler\n\n\t\tfunction compiler(tree: Root, file: VFile): RichTextField {\n\t\t\tconst richTextField = hastToRichText(tree, file, config)\n\n\t\t\tif (config?.model) {\n\t\t\t\treturn filterRichTextField(richTextField, config.model)\n\t\t\t}\n\n\t\t\treturn richTextField\n\t\t}\n\t}\n\ndeclare module \"unified\" {\n\t// Register unified processor the result type.\n\tinterface CompileResultMap {\n\t\tRichTextField: RichTextField\n\t}\n}\n\ndeclare module \"hast\" {\n\t// Extend hast node with a rich text value.\n\tinterface Element {\n\t\t/**\n\t\t * A serializer this node matches to. Nodes are marked with this property when they match a CSS\n\t\t * selector from the serializer map.\n\t\t */\n\t\tmatchesSerializer?: string\n\t}\n}\n"],"mappings":";;;;;;;;;;;;AA6DA,MAAa,iBACZ,SAAS,eAAe,QAAQ;CAK/B,MAAM,OAAO;CAIb,KAAK,UAAU;EACd,QAAQ,SAAe;GAEtB,IAAI,QAAQ,WAAW;IACtB,MAAM,UAAU,OAAO,OAAO,WAAW,IAAI;IAE7C,IAAI,CAAC,SACJ,MAAM,IAAI,MACT,2BAA2B,QAAQ,UAAU,oCAC9C;IAKD,KAAK,WAAW,CAAC,OAAO;GACzB;GAGA,IAAI,QAAQ,SAAS;IAEpB,MAAM,iBAAiB,UAAU,OAAO,QAAQ,KAAK,IAAI,GAAG,IAAI;IAEhE,OAAO,OAAO,SAAS,eAAe,SAAS,IAAe,CAAC;GAChE;GAGA,IAAI,QAAQ,SAAS;IACpB,MAAM,iBAA4B,CAAC;IAGnC,MAAM,WAAW,OAAO,QAAQ,KAAK,IAAI;IACzC,MAAM,oBAAoB,UAAU,UAAU,IAAI;IAGlD,MAAM,OAAO,SAAS;KACrB,IAAI,kBAAkB,SAAS,IAAe,GAAG;MAChD,eAAe,KAAK,IAAe;MAGnC,OAAO;KACR;IACD,CAAC;IAID,KAAK,WAAW;GACjB;GAGA,IAAI,QAAQ,YACN;SAAA,MAAM,OAAO,OAAO,YAIxB,IAAI,CAAC,iBAAiB,KAAK,GAAG,GAAG;KAChC,MAAM,UAAU,UAAU,KAAK,IAAI;KAEnC,KAAK,IAAI,IAAI,GAAG,IAAI,QAAQ,QAAQ,KACnC,QAAQ,EAAE,CAAC,oBAAoB;IAEjC;;EAGH;CACD,CAAC;CAID,KAAK,IAAI,sBAAsB;CAE/B,KAAK,WAAW;CAEhB,SAAS,SAAS,MAAY,MAA4B;EACzD,MAAM,gBAAgB,eAAe,MAAM,MAAM,MAAM;EAEvD,IAAI,QAAQ,OACX,OAAO,oBAAoB,eAAe,OAAO,KAAK;EAGvD,OAAO;CACR;AACD"}
@@ -0,0 +1,48 @@
1
+ import { RTPartialInlineNode } from "./lib/RichTextFieldBuilder.cjs";
2
+ import { InjectMigrationSpecificTypes, RTInlineNode, RTNode, RichTextNodeTypes } from "@prismicio/client";
3
+ import { Element } from "hast";
4
+ //#region src/types.d.ts
5
+ /**
6
+ * A shorthand definition for {@link RichTextHTMLMapSerializer} rich text node types.
7
+ *
8
+ * @remarks
9
+ * The `label` rich text node type is not available as is. Use an object containing your label
10
+ * name to convert to label nodes instead. For example: `u: { label: "underline" }`
11
+ * @remarks
12
+ * The `span` rich text node type is not available as it is not relevant in the context of going
13
+ * from HTML to Prismic rich text.
14
+ */
15
+ type RichTextHTMLMapSerializerShorthand = Exclude<RichTextNodeTypes, "label" | "span"> | {
16
+ label: string;
17
+ };
18
+ /** The payload provided to a {@link RichTextHTMLMapSerializerFunction}. */
19
+ type RichTextHTMLMapSerializerFunctionPayload = {
20
+ /** The hast {@link Element} node to serialize. */
21
+ node: Element;
22
+ /** Additional context information to help with the serialization. */
23
+ context: {
24
+ /** The list type of the last list node encountered if any. */
25
+ listType?: "group-list-item" | "group-o-list-item";
26
+ };
27
+ };
28
+ /**
29
+ * Serializes a hast {@link Element} node to a {@link RichTextHTMLMapSerializerShorthand} or a rich
30
+ * text node.
31
+ *
32
+ * @remarks
33
+ * Serializing to a rich text node directly is not recommended and is only available as an escape
34
+ * hatch. Prefer returning a {@link RichTextHTMLMapSerializerShorthand} instead.
35
+ */
36
+ type RichTextHTMLMapSerializerFunction = (payload: RichTextHTMLMapSerializerFunctionPayload) => RichTextHTMLMapSerializerShorthand | InjectMigrationSpecificTypes<RTNode | RTInlineNode | RTPartialInlineNode> | undefined;
37
+ /**
38
+ * Serializes a hast {@link Element} node matching the given HTML tag name or CSS selector to a
39
+ * Prismic rich text node.
40
+ *
41
+ * @remarks
42
+ * This serializer is used to serialize HTML to Prismic rich text. When serializing Prismic rich
43
+ * text to HTML with {@link asHTML}, use the {@link HTMLRichTextMapSerializer} type instead.
44
+ */
45
+ type RichTextHTMLMapSerializer = Record<string, RichTextHTMLMapSerializerShorthand | RichTextHTMLMapSerializerFunction>;
46
+ //#endregion
47
+ export { RichTextHTMLMapSerializer, RichTextHTMLMapSerializerFunction, RichTextHTMLMapSerializerShorthand };
48
+ //# sourceMappingURL=types.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.cts","names":[],"sources":["../src/types.ts"],"mappings":";;;;;;;;;;;;;;KAoBY,qCACT,QAAQ;EACN;;;KAGA;;EAEJ,MAAM;;EAGN;;IAEC;;;;;;;;;;;KAYU,qCACX,SAAS,6CAEP,qCACA,6BAA6B,SAAS,eAAe;;;;;;;;;KAW5C,4BAA4B,eAEvC,qCAAqC"}