@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,87 +1,85 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
let _prismicio_client = require("@prismicio/client");
|
|
2
|
+
//#region src/lib/filterRichTextField.ts
|
|
3
|
+
/**
|
|
4
|
+
* Get allowed nodes types from a rich text or title field custom type model.
|
|
5
|
+
*
|
|
6
|
+
* @param model - A rich text or title field custom type model.
|
|
7
|
+
* @returns An array of allowed nodes types.
|
|
8
|
+
*/
|
|
4
9
|
const getAllowedNodeTypes = (model) => {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
if ((_a = model.config.labels) == null ? void 0 : _a.length) {
|
|
14
|
-
allowedNodeTypes.push("label");
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
return allowedNodeTypes;
|
|
10
|
+
const allowedNodeTypes = [];
|
|
11
|
+
if (model.config) {
|
|
12
|
+
if ("multi" in model.config && model.config.multi) allowedNodeTypes.push(...model.config.multi.split(","));
|
|
13
|
+
else if ("single" in model.config && model.config.single) allowedNodeTypes.push(...model.config.single.split(","));
|
|
14
|
+
if (model.config.labels?.length) allowedNodeTypes.push("label");
|
|
15
|
+
}
|
|
16
|
+
return allowedNodeTypes;
|
|
18
17
|
};
|
|
18
|
+
/**
|
|
19
|
+
* Filter an image node based on given model.
|
|
20
|
+
*
|
|
21
|
+
* @param node - An image node to filter.
|
|
22
|
+
* @param model - A rich text or title field custom type model.
|
|
23
|
+
* @returns A filtered image node based on the given model.
|
|
24
|
+
*/
|
|
19
25
|
const filterImageNode = (node, model) => {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
return node;
|
|
26
|
+
if (!model.config?.allowTargetBlank && node.linkTo?.link_type === _prismicio_client.LinkType.Web) return {
|
|
27
|
+
...node,
|
|
28
|
+
linkTo: {
|
|
29
|
+
...node.linkTo,
|
|
30
|
+
target: void 0
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
return node;
|
|
31
34
|
};
|
|
35
|
+
/**
|
|
36
|
+
* Filter a text node based on given model.
|
|
37
|
+
*
|
|
38
|
+
* @param node - A text node to filter.
|
|
39
|
+
* @param model - A rich text or title field custom type model.
|
|
40
|
+
* @param allowedNodeTypes - An array of allowed nodes types.
|
|
41
|
+
* @returns A filtered text node based on the given model.
|
|
42
|
+
*/
|
|
32
43
|
const filterTextNode = (node, model, allowedNodeTypes) => {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
53
|
-
} else if (span.type === client.RichTextNodeType.label) {
|
|
54
|
-
if ((_c = (_b = model.config) == null ? void 0 : _b.labels) == null ? void 0 : _c.includes(span.data.label)) {
|
|
55
|
-
filteredNode.spans.push(span);
|
|
56
|
-
}
|
|
57
|
-
} else {
|
|
58
|
-
filteredNode.spans.push(span);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
return filteredNode;
|
|
44
|
+
const filteredNode = {
|
|
45
|
+
...node,
|
|
46
|
+
spans: []
|
|
47
|
+
};
|
|
48
|
+
for (let i = 0; i < node.spans.length; i++) {
|
|
49
|
+
const span = node.spans[i];
|
|
50
|
+
if (allowedNodeTypes.includes(span.type)) if (span.type === _prismicio_client.RichTextNodeType.hyperlink) if (!model.config?.allowTargetBlank && span.data.link_type === _prismicio_client.LinkType.Web) filteredNode.spans.push({
|
|
51
|
+
...span,
|
|
52
|
+
data: {
|
|
53
|
+
...span.data,
|
|
54
|
+
target: void 0
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
else filteredNode.spans.push(span);
|
|
58
|
+
else if (span.type === _prismicio_client.RichTextNodeType.label) {
|
|
59
|
+
if (model.config?.labels?.includes(span.data.label)) filteredNode.spans.push(span);
|
|
60
|
+
} else filteredNode.spans.push(span);
|
|
61
|
+
}
|
|
62
|
+
return filteredNode;
|
|
63
63
|
};
|
|
64
|
+
/**
|
|
65
|
+
* Filter a rich text field based on given model.
|
|
66
|
+
*
|
|
67
|
+
* @param richTextField - A rich text or title field from Prismic.
|
|
68
|
+
* @param model - A rich text or title field custom type model.
|
|
69
|
+
* @returns A rich text or title field filtered based on the given model.
|
|
70
|
+
*/
|
|
64
71
|
const filterRichTextField = (richTextField, model) => {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
if (allowedNodeTypes.includes(node.type)) {
|
|
75
|
-
if (node.type === client.RichTextNodeType.image) {
|
|
76
|
-
nodes.push(filterImageNode(node, model));
|
|
77
|
-
} else if (node.type === client.RichTextNodeType.embed) {
|
|
78
|
-
nodes.push(node);
|
|
79
|
-
} else {
|
|
80
|
-
nodes.push(filterTextNode(node, model, allowedNodeTypes));
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
return nodes;
|
|
72
|
+
const nodes = [];
|
|
73
|
+
const allowedNodeTypes = getAllowedNodeTypes(model);
|
|
74
|
+
for (let i = 0; i < (model.config && "multi" in model.config ? richTextField.length : 1); i++) {
|
|
75
|
+
const node = richTextField[i];
|
|
76
|
+
if (allowedNodeTypes.includes(node.type)) if (node.type === _prismicio_client.RichTextNodeType.image) nodes.push(filterImageNode(node, model));
|
|
77
|
+
else if (node.type === _prismicio_client.RichTextNodeType.embed) nodes.push(node);
|
|
78
|
+
else nodes.push(filterTextNode(node, model, allowedNodeTypes));
|
|
79
|
+
}
|
|
80
|
+
return nodes;
|
|
85
81
|
};
|
|
82
|
+
//#endregion
|
|
86
83
|
exports.filterRichTextField = filterRichTextField;
|
|
87
|
-
|
|
84
|
+
|
|
85
|
+
//# sourceMappingURL=filterRichTextField.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"filterRichTextField.cjs","sources":["
|
|
1
|
+
{"version":3,"file":"filterRichTextField.cjs","names":["LinkType","RichTextNodeType"],"sources":["../../src/lib/filterRichTextField.ts"],"sourcesContent":["import type {\n\tCustomTypeModelRichTextField,\n\tRTImageNode,\n\tRTNode,\n\tRTTextNode,\n\tRichTextField,\n\tRichTextNodeTypes,\n\tTitleField,\n} from \"@prismicio/client\"\nimport { LinkType, RichTextNodeType } from \"@prismicio/client\"\n\n/**\n * Get allowed nodes types from a rich text or title field custom type model.\n *\n * @param model - A rich text or title field custom type model.\n * @returns An array of allowed nodes types.\n */\nconst getAllowedNodeTypes = (model: CustomTypeModelRichTextField): RichTextNodeTypes[] => {\n\tconst allowedNodeTypes: RichTextNodeTypes[] = [] // An empty array means no nodes are allowed by default.\n\n\tif (model.config) {\n\t\tif (\"multi\" in model.config && model.config.multi) {\n\t\t\tallowedNodeTypes.push(...(model.config.multi.split(\",\") as RichTextNodeTypes[]))\n\t\t} else if (\"single\" in model.config && model.config.single) {\n\t\t\tallowedNodeTypes.push(...(model.config.single.split(\",\") as RichTextNodeTypes[]))\n\t\t}\n\n\t\tif (model.config.labels?.length) {\n\t\t\tallowedNodeTypes.push(\"label\")\n\t\t}\n\t}\n\n\treturn allowedNodeTypes\n}\n\n/**\n * Filter an image node based on given model.\n *\n * @param node - An image node to filter.\n * @param model - A rich text or title field custom type model.\n * @returns A filtered image node based on the given model.\n */\nconst filterImageNode = (node: RTImageNode, model: CustomTypeModelRichTextField): RTImageNode => {\n\tif (!model.config?.allowTargetBlank && node.linkTo?.link_type === LinkType.Web) {\n\t\treturn {\n\t\t\t...node,\n\t\t\tlinkTo: {\n\t\t\t\t...node.linkTo,\n\t\t\t\ttarget: undefined,\n\t\t\t},\n\t\t}\n\t}\n\n\treturn node\n}\n\n/**\n * Filter a text node based on given model.\n *\n * @param node - A text node to filter.\n * @param model - A rich text or title field custom type model.\n * @param allowedNodeTypes - An array of allowed nodes types.\n * @returns A filtered text node based on the given model.\n */\nconst filterTextNode = (\n\tnode: RTTextNode,\n\tmodel: CustomTypeModelRichTextField,\n\tallowedNodeTypes: RichTextNodeTypes[],\n): RTTextNode => {\n\tconst filteredNode: RTTextNode = {\n\t\t...node,\n\t\tspans: [],\n\t}\n\n\tfor (let i = 0; i < node.spans.length; i++) {\n\t\tconst span = node.spans[i]\n\n\t\tif (allowedNodeTypes.includes(span.type)) {\n\t\t\tif (span.type === RichTextNodeType.hyperlink) {\n\t\t\t\tif (!model.config?.allowTargetBlank && span.data.link_type === LinkType.Web) {\n\t\t\t\t\tfilteredNode.spans.push({\n\t\t\t\t\t\t...span,\n\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\t...span.data,\n\t\t\t\t\t\t\ttarget: undefined,\n\t\t\t\t\t\t},\n\t\t\t\t\t})\n\t\t\t\t} else {\n\t\t\t\t\tfilteredNode.spans.push(span)\n\t\t\t\t}\n\t\t\t} else if (span.type === RichTextNodeType.label) {\n\t\t\t\tif (model.config?.labels?.includes(span.data.label)) {\n\t\t\t\t\tfilteredNode.spans.push(span)\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tfilteredNode.spans.push(span)\n\t\t\t}\n\t\t}\n\t}\n\n\treturn filteredNode\n}\n\n/**\n * Filter a rich text field based on given model.\n *\n * @param richTextField - A rich text or title field from Prismic.\n * @param model - A rich text or title field custom type model.\n * @returns A rich text or title field filtered based on the given model.\n */\nexport const filterRichTextField = <Field extends RichTextField | TitleField>(\n\trichTextField: Field,\n\tmodel: CustomTypeModelRichTextField,\n): Field => {\n\tconst nodes: RTNode[] = []\n\n\tconst allowedNodeTypes = getAllowedNodeTypes(model)\n\n\tfor (\n\t\tlet i = 0;\n\t\t// Only process the first node if it's a \"single\" node rich text type\n\t\ti < (model.config && \"multi\" in model.config ? richTextField.length : 1);\n\t\ti++\n\t) {\n\t\tconst node = richTextField[i]\n\n\t\tif (allowedNodeTypes.includes(node.type)) {\n\t\t\tif (node.type === RichTextNodeType.image) {\n\t\t\t\tnodes.push(filterImageNode(node, model))\n\t\t\t} else if (node.type === RichTextNodeType.embed) {\n\t\t\t\tnodes.push(node)\n\t\t\t} else {\n\t\t\t\tnodes.push(filterTextNode(node, model, allowedNodeTypes))\n\t\t\t}\n\t\t}\n\t}\n\n\treturn nodes as Field\n}\n"],"mappings":";;;;;;;;AAiBA,MAAM,uBAAuB,UAA6D;CACzF,MAAM,mBAAwC,CAAC;CAE/C,IAAI,MAAM,QAAQ;EACjB,IAAI,WAAW,MAAM,UAAU,MAAM,OAAO,OAC3C,iBAAiB,KAAK,GAAI,MAAM,OAAO,MAAM,MAAM,GAAG,CAAyB;OACzE,IAAI,YAAY,MAAM,UAAU,MAAM,OAAO,QACnD,iBAAiB,KAAK,GAAI,MAAM,OAAO,OAAO,MAAM,GAAG,CAAyB;EAGjF,IAAI,MAAM,OAAO,QAAQ,QACxB,iBAAiB,KAAK,OAAO;CAE/B;CAEA,OAAO;AACR;;;;;;;;AASA,MAAM,mBAAmB,MAAmB,UAAqD;CAChG,IAAI,CAAC,MAAM,QAAQ,oBAAoB,KAAK,QAAQ,cAAcA,kBAAAA,SAAS,KAC1E,OAAO;EACN,GAAG;EACH,QAAQ;GACP,GAAG,KAAK;GACR,QAAQ,KAAA;EACT;CACD;CAGD,OAAO;AACR;;;;;;;;;AAUA,MAAM,kBACL,MACA,OACA,qBACgB;CAChB,MAAM,eAA2B;EAChC,GAAG;EACH,OAAO,CAAC;CACT;CAEA,KAAK,IAAI,IAAI,GAAG,IAAI,KAAK,MAAM,QAAQ,KAAK;EAC3C,MAAM,OAAO,KAAK,MAAM;EAExB,IAAI,iBAAiB,SAAS,KAAK,IAAI,GACtC,IAAI,KAAK,SAASC,kBAAAA,iBAAiB,WAClC,IAAI,CAAC,MAAM,QAAQ,oBAAoB,KAAK,KAAK,cAAcD,kBAAAA,SAAS,KACvE,aAAa,MAAM,KAAK;GACvB,GAAG;GACH,MAAM;IACL,GAAG,KAAK;IACR,QAAQ,KAAA;GACT;EACD,CAAC;OAED,aAAa,MAAM,KAAK,IAAI;OAEvB,IAAI,KAAK,SAASC,kBAAAA,iBAAiB,OACrC;OAAA,MAAM,QAAQ,QAAQ,SAAS,KAAK,KAAK,KAAK,GACjD,aAAa,MAAM,KAAK,IAAI;EAAA,OAG7B,aAAa,MAAM,KAAK,IAAI;CAG/B;CAEA,OAAO;AACR;;;;;;;;AASA,MAAa,uBACZ,eACA,UACW;CACX,MAAM,QAAkB,CAAC;CAEzB,MAAM,mBAAmB,oBAAoB,KAAK;CAElD,KACC,IAAI,IAAI,GAER,KAAK,MAAM,UAAU,WAAW,MAAM,SAAS,cAAc,SAAS,IACtE,KACC;EACD,MAAM,OAAO,cAAc;EAE3B,IAAI,iBAAiB,SAAS,KAAK,IAAI,GACtC,IAAI,KAAK,SAASA,kBAAAA,iBAAiB,OAClC,MAAM,KAAK,gBAAgB,MAAM,KAAK,CAAC;OACjC,IAAI,KAAK,SAASA,kBAAAA,iBAAiB,OACzC,MAAM,KAAK,IAAI;OAEf,MAAM,KAAK,eAAe,MAAM,OAAO,gBAAgB,CAAC;CAG3D;CAEA,OAAO;AACR"}
|
|
@@ -1,87 +1,85 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LinkType, RichTextNodeType } from "@prismicio/client";
|
|
2
|
+
//#region src/lib/filterRichTextField.ts
|
|
3
|
+
/**
|
|
4
|
+
* Get allowed nodes types from a rich text or title field custom type model.
|
|
5
|
+
*
|
|
6
|
+
* @param model - A rich text or title field custom type model.
|
|
7
|
+
* @returns An array of allowed nodes types.
|
|
8
|
+
*/
|
|
2
9
|
const getAllowedNodeTypes = (model) => {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
11
|
-
if ((_a = model.config.labels) == null ? void 0 : _a.length) {
|
|
12
|
-
allowedNodeTypes.push("label");
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
return allowedNodeTypes;
|
|
10
|
+
const allowedNodeTypes = [];
|
|
11
|
+
if (model.config) {
|
|
12
|
+
if ("multi" in model.config && model.config.multi) allowedNodeTypes.push(...model.config.multi.split(","));
|
|
13
|
+
else if ("single" in model.config && model.config.single) allowedNodeTypes.push(...model.config.single.split(","));
|
|
14
|
+
if (model.config.labels?.length) allowedNodeTypes.push("label");
|
|
15
|
+
}
|
|
16
|
+
return allowedNodeTypes;
|
|
16
17
|
};
|
|
18
|
+
/**
|
|
19
|
+
* Filter an image node based on given model.
|
|
20
|
+
*
|
|
21
|
+
* @param node - An image node to filter.
|
|
22
|
+
* @param model - A rich text or title field custom type model.
|
|
23
|
+
* @returns A filtered image node based on the given model.
|
|
24
|
+
*/
|
|
17
25
|
const filterImageNode = (node, model) => {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
return node;
|
|
26
|
+
if (!model.config?.allowTargetBlank && node.linkTo?.link_type === LinkType.Web) return {
|
|
27
|
+
...node,
|
|
28
|
+
linkTo: {
|
|
29
|
+
...node.linkTo,
|
|
30
|
+
target: void 0
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
return node;
|
|
29
34
|
};
|
|
35
|
+
/**
|
|
36
|
+
* Filter a text node based on given model.
|
|
37
|
+
*
|
|
38
|
+
* @param node - A text node to filter.
|
|
39
|
+
* @param model - A rich text or title field custom type model.
|
|
40
|
+
* @param allowedNodeTypes - An array of allowed nodes types.
|
|
41
|
+
* @returns A filtered text node based on the given model.
|
|
42
|
+
*/
|
|
30
43
|
const filterTextNode = (node, model, allowedNodeTypes) => {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
51
|
-
} else if (span.type === RichTextNodeType.label) {
|
|
52
|
-
if ((_c = (_b = model.config) == null ? void 0 : _b.labels) == null ? void 0 : _c.includes(span.data.label)) {
|
|
53
|
-
filteredNode.spans.push(span);
|
|
54
|
-
}
|
|
55
|
-
} else {
|
|
56
|
-
filteredNode.spans.push(span);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
return filteredNode;
|
|
44
|
+
const filteredNode = {
|
|
45
|
+
...node,
|
|
46
|
+
spans: []
|
|
47
|
+
};
|
|
48
|
+
for (let i = 0; i < node.spans.length; i++) {
|
|
49
|
+
const span = node.spans[i];
|
|
50
|
+
if (allowedNodeTypes.includes(span.type)) if (span.type === RichTextNodeType.hyperlink) if (!model.config?.allowTargetBlank && span.data.link_type === LinkType.Web) filteredNode.spans.push({
|
|
51
|
+
...span,
|
|
52
|
+
data: {
|
|
53
|
+
...span.data,
|
|
54
|
+
target: void 0
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
else filteredNode.spans.push(span);
|
|
58
|
+
else if (span.type === RichTextNodeType.label) {
|
|
59
|
+
if (model.config?.labels?.includes(span.data.label)) filteredNode.spans.push(span);
|
|
60
|
+
} else filteredNode.spans.push(span);
|
|
61
|
+
}
|
|
62
|
+
return filteredNode;
|
|
61
63
|
};
|
|
64
|
+
/**
|
|
65
|
+
* Filter a rich text field based on given model.
|
|
66
|
+
*
|
|
67
|
+
* @param richTextField - A rich text or title field from Prismic.
|
|
68
|
+
* @param model - A rich text or title field custom type model.
|
|
69
|
+
* @returns A rich text or title field filtered based on the given model.
|
|
70
|
+
*/
|
|
62
71
|
const filterRichTextField = (richTextField, model) => {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
if (allowedNodeTypes.includes(node.type)) {
|
|
73
|
-
if (node.type === RichTextNodeType.image) {
|
|
74
|
-
nodes.push(filterImageNode(node, model));
|
|
75
|
-
} else if (node.type === RichTextNodeType.embed) {
|
|
76
|
-
nodes.push(node);
|
|
77
|
-
} else {
|
|
78
|
-
nodes.push(filterTextNode(node, model, allowedNodeTypes));
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
return nodes;
|
|
72
|
+
const nodes = [];
|
|
73
|
+
const allowedNodeTypes = getAllowedNodeTypes(model);
|
|
74
|
+
for (let i = 0; i < (model.config && "multi" in model.config ? richTextField.length : 1); i++) {
|
|
75
|
+
const node = richTextField[i];
|
|
76
|
+
if (allowedNodeTypes.includes(node.type)) if (node.type === RichTextNodeType.image) nodes.push(filterImageNode(node, model));
|
|
77
|
+
else if (node.type === RichTextNodeType.embed) nodes.push(node);
|
|
78
|
+
else nodes.push(filterTextNode(node, model, allowedNodeTypes));
|
|
79
|
+
}
|
|
80
|
+
return nodes;
|
|
83
81
|
};
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
//# sourceMappingURL=filterRichTextField.js.map
|
|
82
|
+
//#endregion
|
|
83
|
+
export { filterRichTextField };
|
|
84
|
+
|
|
85
|
+
//# sourceMappingURL=filterRichTextField.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"filterRichTextField.js","sources":["
|
|
1
|
+
{"version":3,"file":"filterRichTextField.js","names":[],"sources":["../../src/lib/filterRichTextField.ts"],"sourcesContent":["import type {\n\tCustomTypeModelRichTextField,\n\tRTImageNode,\n\tRTNode,\n\tRTTextNode,\n\tRichTextField,\n\tRichTextNodeTypes,\n\tTitleField,\n} from \"@prismicio/client\"\nimport { LinkType, RichTextNodeType } from \"@prismicio/client\"\n\n/**\n * Get allowed nodes types from a rich text or title field custom type model.\n *\n * @param model - A rich text or title field custom type model.\n * @returns An array of allowed nodes types.\n */\nconst getAllowedNodeTypes = (model: CustomTypeModelRichTextField): RichTextNodeTypes[] => {\n\tconst allowedNodeTypes: RichTextNodeTypes[] = [] // An empty array means no nodes are allowed by default.\n\n\tif (model.config) {\n\t\tif (\"multi\" in model.config && model.config.multi) {\n\t\t\tallowedNodeTypes.push(...(model.config.multi.split(\",\") as RichTextNodeTypes[]))\n\t\t} else if (\"single\" in model.config && model.config.single) {\n\t\t\tallowedNodeTypes.push(...(model.config.single.split(\",\") as RichTextNodeTypes[]))\n\t\t}\n\n\t\tif (model.config.labels?.length) {\n\t\t\tallowedNodeTypes.push(\"label\")\n\t\t}\n\t}\n\n\treturn allowedNodeTypes\n}\n\n/**\n * Filter an image node based on given model.\n *\n * @param node - An image node to filter.\n * @param model - A rich text or title field custom type model.\n * @returns A filtered image node based on the given model.\n */\nconst filterImageNode = (node: RTImageNode, model: CustomTypeModelRichTextField): RTImageNode => {\n\tif (!model.config?.allowTargetBlank && node.linkTo?.link_type === LinkType.Web) {\n\t\treturn {\n\t\t\t...node,\n\t\t\tlinkTo: {\n\t\t\t\t...node.linkTo,\n\t\t\t\ttarget: undefined,\n\t\t\t},\n\t\t}\n\t}\n\n\treturn node\n}\n\n/**\n * Filter a text node based on given model.\n *\n * @param node - A text node to filter.\n * @param model - A rich text or title field custom type model.\n * @param allowedNodeTypes - An array of allowed nodes types.\n * @returns A filtered text node based on the given model.\n */\nconst filterTextNode = (\n\tnode: RTTextNode,\n\tmodel: CustomTypeModelRichTextField,\n\tallowedNodeTypes: RichTextNodeTypes[],\n): RTTextNode => {\n\tconst filteredNode: RTTextNode = {\n\t\t...node,\n\t\tspans: [],\n\t}\n\n\tfor (let i = 0; i < node.spans.length; i++) {\n\t\tconst span = node.spans[i]\n\n\t\tif (allowedNodeTypes.includes(span.type)) {\n\t\t\tif (span.type === RichTextNodeType.hyperlink) {\n\t\t\t\tif (!model.config?.allowTargetBlank && span.data.link_type === LinkType.Web) {\n\t\t\t\t\tfilteredNode.spans.push({\n\t\t\t\t\t\t...span,\n\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\t...span.data,\n\t\t\t\t\t\t\ttarget: undefined,\n\t\t\t\t\t\t},\n\t\t\t\t\t})\n\t\t\t\t} else {\n\t\t\t\t\tfilteredNode.spans.push(span)\n\t\t\t\t}\n\t\t\t} else if (span.type === RichTextNodeType.label) {\n\t\t\t\tif (model.config?.labels?.includes(span.data.label)) {\n\t\t\t\t\tfilteredNode.spans.push(span)\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tfilteredNode.spans.push(span)\n\t\t\t}\n\t\t}\n\t}\n\n\treturn filteredNode\n}\n\n/**\n * Filter a rich text field based on given model.\n *\n * @param richTextField - A rich text or title field from Prismic.\n * @param model - A rich text or title field custom type model.\n * @returns A rich text or title field filtered based on the given model.\n */\nexport const filterRichTextField = <Field extends RichTextField | TitleField>(\n\trichTextField: Field,\n\tmodel: CustomTypeModelRichTextField,\n): Field => {\n\tconst nodes: RTNode[] = []\n\n\tconst allowedNodeTypes = getAllowedNodeTypes(model)\n\n\tfor (\n\t\tlet i = 0;\n\t\t// Only process the first node if it's a \"single\" node rich text type\n\t\ti < (model.config && \"multi\" in model.config ? richTextField.length : 1);\n\t\ti++\n\t) {\n\t\tconst node = richTextField[i]\n\n\t\tif (allowedNodeTypes.includes(node.type)) {\n\t\t\tif (node.type === RichTextNodeType.image) {\n\t\t\t\tnodes.push(filterImageNode(node, model))\n\t\t\t} else if (node.type === RichTextNodeType.embed) {\n\t\t\t\tnodes.push(node)\n\t\t\t} else {\n\t\t\t\tnodes.push(filterTextNode(node, model, allowedNodeTypes))\n\t\t\t}\n\t\t}\n\t}\n\n\treturn nodes as Field\n}\n"],"mappings":";;;;;;;;AAiBA,MAAM,uBAAuB,UAA6D;CACzF,MAAM,mBAAwC,CAAC;CAE/C,IAAI,MAAM,QAAQ;EACjB,IAAI,WAAW,MAAM,UAAU,MAAM,OAAO,OAC3C,iBAAiB,KAAK,GAAI,MAAM,OAAO,MAAM,MAAM,GAAG,CAAyB;OACzE,IAAI,YAAY,MAAM,UAAU,MAAM,OAAO,QACnD,iBAAiB,KAAK,GAAI,MAAM,OAAO,OAAO,MAAM,GAAG,CAAyB;EAGjF,IAAI,MAAM,OAAO,QAAQ,QACxB,iBAAiB,KAAK,OAAO;CAE/B;CAEA,OAAO;AACR;;;;;;;;AASA,MAAM,mBAAmB,MAAmB,UAAqD;CAChG,IAAI,CAAC,MAAM,QAAQ,oBAAoB,KAAK,QAAQ,cAAc,SAAS,KAC1E,OAAO;EACN,GAAG;EACH,QAAQ;GACP,GAAG,KAAK;GACR,QAAQ,KAAA;EACT;CACD;CAGD,OAAO;AACR;;;;;;;;;AAUA,MAAM,kBACL,MACA,OACA,qBACgB;CAChB,MAAM,eAA2B;EAChC,GAAG;EACH,OAAO,CAAC;CACT;CAEA,KAAK,IAAI,IAAI,GAAG,IAAI,KAAK,MAAM,QAAQ,KAAK;EAC3C,MAAM,OAAO,KAAK,MAAM;EAExB,IAAI,iBAAiB,SAAS,KAAK,IAAI,GACtC,IAAI,KAAK,SAAS,iBAAiB,WAClC,IAAI,CAAC,MAAM,QAAQ,oBAAoB,KAAK,KAAK,cAAc,SAAS,KACvE,aAAa,MAAM,KAAK;GACvB,GAAG;GACH,MAAM;IACL,GAAG,KAAK;IACR,QAAQ,KAAA;GACT;EACD,CAAC;OAED,aAAa,MAAM,KAAK,IAAI;OAEvB,IAAI,KAAK,SAAS,iBAAiB,OACrC;OAAA,MAAM,QAAQ,QAAQ,SAAS,KAAK,KAAK,KAAK,GACjD,aAAa,MAAM,KAAK,IAAI;EAAA,OAG7B,aAAa,MAAM,KAAK,IAAI;CAG/B;CAEA,OAAO;AACR;;;;;;;;AASA,MAAa,uBACZ,eACA,UACW;CACX,MAAM,QAAkB,CAAC;CAEzB,MAAM,mBAAmB,oBAAoB,KAAK;CAElD,KACC,IAAI,IAAI,GAER,KAAK,MAAM,UAAU,WAAW,MAAM,SAAS,cAAc,SAAS,IACtE,KACC;EACD,MAAM,OAAO,cAAc;EAE3B,IAAI,iBAAiB,SAAS,KAAK,IAAI,GACtC,IAAI,KAAK,SAAS,iBAAiB,OAClC,MAAM,KAAK,gBAAgB,MAAM,KAAK,CAAC;OACjC,IAAI,KAAK,SAAS,iBAAiB,OACzC,MAAM,KAAK,IAAI;OAEf,MAAM,KAAK,eAAe,MAAM,OAAO,gBAAgB,CAAC;CAG3D;CAEA,OAAO;AACR"}
|
|
@@ -1,95 +1,110 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
const require_PrismicRichTextSerializerError = require("../errors/PrismicRichTextSerializerError.cjs");
|
|
2
|
+
let _prismicio_client = require("@prismicio/client");
|
|
3
|
+
let hast_util_to_html = require("hast-util-to-html");
|
|
4
|
+
//#region src/lib/hastSerializerHelpers.ts
|
|
5
|
+
/**
|
|
6
|
+
* Serializes a hast {@link Element} node to a {@link RTImageNode}.
|
|
7
|
+
*
|
|
8
|
+
* @param node - A hast node to serialize.
|
|
9
|
+
* @returns Equivalent {@link RTImageNode}.
|
|
10
|
+
*/
|
|
6
11
|
const serializeImage = (node) => {
|
|
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
|
-
// See: https://github.com/prismicio/prismic-client/pull/342#discussion_r1683650185
|
|
43
|
-
dimensions: { width, height },
|
|
44
|
-
edit: { x, y, zoom, background: "transparent" }
|
|
45
|
-
};
|
|
12
|
+
const src = node.properties?.src;
|
|
13
|
+
if (!src) throw new require_PrismicRichTextSerializerError.PrismicRichTextSerializerError("Element of type `img` is missing an `src` attribute");
|
|
14
|
+
const url = new URL(src, "https://noop.com");
|
|
15
|
+
let width = node.properties?.width;
|
|
16
|
+
let height = node.properties?.height;
|
|
17
|
+
let x = 0;
|
|
18
|
+
let y = 0;
|
|
19
|
+
let zoom = 1;
|
|
20
|
+
if (url.hostname === "images.prismic.io") {
|
|
21
|
+
if (url.searchParams.has("w")) width = Number(url.searchParams.get("w"));
|
|
22
|
+
if (url.searchParams.has("h")) height = Number(url.searchParams.get("h"));
|
|
23
|
+
if (url.searchParams.has("rect")) {
|
|
24
|
+
const [rectX, rextY, rectW, _rectH] = url.searchParams.get("rect").split(",");
|
|
25
|
+
x = Number(rectX);
|
|
26
|
+
y = Number(rextY);
|
|
27
|
+
if (width) zoom = Math.max(1, width / Number(rectW));
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return {
|
|
31
|
+
type: "image",
|
|
32
|
+
id: "",
|
|
33
|
+
url: src,
|
|
34
|
+
alt: node.properties?.alt ?? "",
|
|
35
|
+
copyright: node.properties?.copyright ?? null,
|
|
36
|
+
dimensions: {
|
|
37
|
+
width,
|
|
38
|
+
height
|
|
39
|
+
},
|
|
40
|
+
edit: {
|
|
41
|
+
x,
|
|
42
|
+
y,
|
|
43
|
+
zoom,
|
|
44
|
+
background: "transparent"
|
|
45
|
+
}
|
|
46
|
+
};
|
|
46
47
|
};
|
|
48
|
+
/**
|
|
49
|
+
* Serializes a hast {@link Element} node to a {@link RTEmbedNode}.
|
|
50
|
+
*
|
|
51
|
+
* @param node - A hast node to serialize.
|
|
52
|
+
* @returns Equivalent {@link RTEmbedNode}.
|
|
53
|
+
*/
|
|
47
54
|
const serializeEmbed = (node) => {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
55
|
+
const src = node.properties?.src;
|
|
56
|
+
if (!src) throw new require_PrismicRichTextSerializerError.PrismicRichTextSerializerError("Element of type `embed` is missing an `src` attribute");
|
|
57
|
+
const oembedBase = {
|
|
58
|
+
version: "1.0",
|
|
59
|
+
embed_url: src,
|
|
60
|
+
html: (0, hast_util_to_html.toHtml)(node),
|
|
61
|
+
title: node.properties?.title
|
|
62
|
+
};
|
|
63
|
+
const width = node.properties?.width;
|
|
64
|
+
const height = node.properties?.height;
|
|
65
|
+
node.children = [];
|
|
66
|
+
if (width && height) return {
|
|
67
|
+
type: "embed",
|
|
68
|
+
oembed: {
|
|
69
|
+
...oembedBase,
|
|
70
|
+
type: _prismicio_client.OEmbedType.Rich,
|
|
71
|
+
width,
|
|
72
|
+
height
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
else return {
|
|
76
|
+
type: "embed",
|
|
77
|
+
oembed: {
|
|
78
|
+
...oembedBase,
|
|
79
|
+
type: _prismicio_client.OEmbedType.Link
|
|
80
|
+
}
|
|
81
|
+
};
|
|
73
82
|
};
|
|
83
|
+
/**
|
|
84
|
+
* Serializes a hast {@link Element} node to a {@link RTPartialInlineNode}.
|
|
85
|
+
*
|
|
86
|
+
* @param node - A hast node to serialize.
|
|
87
|
+
* @param rtPartialInlineNode - A partial rich text node.
|
|
88
|
+
* @returns Equivalent {@link RTPartialInlineNode}.
|
|
89
|
+
*/
|
|
74
90
|
const serializeSpan = (node, rtPartialInlineNode) => {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
return rtPartialInlineNode;
|
|
91
|
+
if (rtPartialInlineNode.type === "hyperlink" && !("data" in rtPartialInlineNode)) {
|
|
92
|
+
const url = node.properties?.href;
|
|
93
|
+
if (!url) throw new require_PrismicRichTextSerializerError.PrismicRichTextSerializerError("Element of type `hyperlink` is missing an `href` attribute");
|
|
94
|
+
return {
|
|
95
|
+
type: "hyperlink",
|
|
96
|
+
data: {
|
|
97
|
+
link_type: _prismicio_client.LinkType.Web,
|
|
98
|
+
url,
|
|
99
|
+
target: node.properties?.target
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
return rtPartialInlineNode;
|
|
91
104
|
};
|
|
105
|
+
//#endregion
|
|
92
106
|
exports.serializeEmbed = serializeEmbed;
|
|
93
107
|
exports.serializeImage = serializeImage;
|
|
94
108
|
exports.serializeSpan = serializeSpan;
|
|
95
|
-
|
|
109
|
+
|
|
110
|
+
//# sourceMappingURL=hastSerializerHelpers.cjs.map
|