@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.
- package/dist/_virtual/_rolldown/runtime.cjs +23 -0
- package/dist/errors/PrismicRichTextError.cjs +5 -5
- package/dist/errors/PrismicRichTextError.cjs.map +1 -1
- package/dist/errors/PrismicRichTextError.d.cts +5 -0
- package/dist/errors/PrismicRichTextError.d.cts.map +1 -0
- package/dist/errors/PrismicRichTextError.d.ts +5 -2
- package/dist/errors/PrismicRichTextError.d.ts.map +1 -0
- package/dist/errors/PrismicRichTextError.js +6 -6
- package/dist/errors/PrismicRichTextError.js.map +1 -1
- package/dist/errors/PrismicRichTextSerializerError.cjs +6 -6
- package/dist/errors/PrismicRichTextSerializerError.cjs.map +1 -1
- package/dist/errors/PrismicRichTextSerializerError.d.cts +6 -0
- package/dist/errors/PrismicRichTextSerializerError.d.cts.map +1 -0
- package/dist/errors/PrismicRichTextSerializerError.d.ts +6 -3
- package/dist/errors/PrismicRichTextSerializerError.d.ts.map +1 -0
- package/dist/errors/PrismicRichTextSerializerError.js +6 -6
- package/dist/errors/PrismicRichTextSerializerError.js.map +1 -1
- package/dist/htmlAsRichText.cjs +24 -8
- package/dist/htmlAsRichText.cjs.map +1 -1
- package/dist/htmlAsRichText.d.cts +23 -0
- package/dist/htmlAsRichText.d.cts.map +1 -0
- package/dist/htmlAsRichText.d.ts +14 -69
- package/dist/htmlAsRichText.d.ts.map +1 -0
- package/dist/htmlAsRichText.js +21 -7
- package/dist/htmlAsRichText.js.map +1 -1
- package/dist/index.cjs +6 -8
- package/dist/index.d.cts +5 -0
- package/dist/index.d.ts +5 -4
- package/dist/index.js +2 -7
- package/dist/lib/RichTextFieldBuilder.cjs +82 -113
- package/dist/lib/RichTextFieldBuilder.cjs.map +1 -1
- package/dist/lib/RichTextFieldBuilder.d.cts +15 -0
- package/dist/lib/RichTextFieldBuilder.d.cts.map +1 -0
- package/dist/lib/RichTextFieldBuilder.d.ts +7 -27
- package/dist/lib/RichTextFieldBuilder.d.ts.map +1 -0
- package/dist/lib/RichTextFieldBuilder.js +82 -113
- package/dist/lib/RichTextFieldBuilder.js.map +1 -1
- package/dist/lib/filterRichTextField.cjs +76 -78
- package/dist/lib/filterRichTextField.cjs.map +1 -1
- package/dist/lib/filterRichTextField.js +77 -79
- package/dist/lib/filterRichTextField.js.map +1 -1
- package/dist/lib/hastSerializerHelpers.cjs +101 -86
- package/dist/lib/hastSerializerHelpers.cjs.map +1 -1
- package/dist/lib/hastSerializerHelpers.js +101 -88
- package/dist/lib/hastSerializerHelpers.js.map +1 -1
- package/dist/lib/hastToRichText.cjs +151 -157
- package/dist/lib/hastToRichText.cjs.map +1 -1
- package/dist/lib/hastToRichText.d.cts +22 -0
- package/dist/lib/hastToRichText.d.cts.map +1 -0
- package/dist/lib/hastToRichText.d.ts +21 -29
- package/dist/lib/hastToRichText.d.ts.map +1 -0
- package/dist/lib/hastToRichText.js +150 -156
- package/dist/lib/hastToRichText.js.map +1 -1
- package/dist/lib/rehypeRichText.cjs +57 -57
- package/dist/lib/rehypeRichText.cjs.map +1 -1
- package/dist/lib/rehypeRichText.d.cts +59 -0
- package/dist/lib/rehypeRichText.d.cts.map +1 -0
- package/dist/lib/rehypeRichText.d.ts +54 -60
- package/dist/lib/rehypeRichText.d.ts.map +1 -0
- package/dist/lib/rehypeRichText.js +53 -55
- package/dist/lib/rehypeRichText.js.map +1 -1
- package/dist/types.d.cts +48 -0
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.ts +32 -41
- package/dist/types.d.ts.map +1 -0
- package/package.json +40 -63
- package/src/htmlAsRichText.ts +22 -49
- package/src/lib/RichTextFieldBuilder.ts +26 -31
- package/src/lib/filterRichTextField.ts +6 -25
- package/src/lib/hastSerializerHelpers.ts +4 -17
- package/src/lib/hastToRichText.ts +17 -39
- package/src/lib/rehypeRichText.ts +88 -102
- package/src/types.ts +24 -33
- package/dist/index.cjs.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/lib/filterRichTextField.d.ts +0 -10
- package/dist/lib/hastSerializerHelpers.d.ts +0 -28
|
@@ -1,59 +1,59 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
-
|
|
58
|
+
|
|
59
|
+
//# sourceMappingURL=rehypeRichText.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rehypeRichText.cjs","sources":["
|
|
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
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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
|
-
|
|
54
|
-
|
|
55
|
-
|
|
44
|
+
interface CompileResultMap {
|
|
45
|
+
RichTextField: RichTextField;
|
|
46
|
+
}
|
|
56
47
|
}
|
|
57
48
|
declare module "hast" {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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 {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
-
//#
|
|
54
|
+
//#endregion
|
|
55
|
+
export { rehypeRichText };
|
|
56
|
+
|
|
57
|
+
//# sourceMappingURL=rehypeRichText.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rehypeRichText.js","sources":["
|
|
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"}
|
package/dist/types.d.cts
ADDED
|
@@ -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"}
|