@prismicio/migrate 0.0.1
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/LICENSE +202 -0
- package/README.md +96 -0
- package/dist/errors/PrismicRichTextError.cjs +6 -0
- package/dist/errors/PrismicRichTextError.cjs.map +1 -0
- package/dist/errors/PrismicRichTextError.d.ts +2 -0
- package/dist/errors/PrismicRichTextError.js +6 -0
- package/dist/errors/PrismicRichTextError.js.map +1 -0
- package/dist/errors/PrismicRichTextSerializerError.cjs +7 -0
- package/dist/errors/PrismicRichTextSerializerError.cjs.map +1 -0
- package/dist/errors/PrismicRichTextSerializerError.d.ts +3 -0
- package/dist/errors/PrismicRichTextSerializerError.js +7 -0
- package/dist/errors/PrismicRichTextSerializerError.js.map +1 -0
- package/dist/htmlAsRichText.cjs +11 -0
- package/dist/htmlAsRichText.cjs.map +1 -0
- package/dist/htmlAsRichText.d.ts +78 -0
- package/dist/htmlAsRichText.js +11 -0
- package/dist/htmlAsRichText.js.map +1 -0
- package/dist/index.cjs +9 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/RichTextFieldBuilder.cjs +115 -0
- package/dist/lib/RichTextFieldBuilder.cjs.map +1 -0
- package/dist/lib/RichTextFieldBuilder.d.ts +35 -0
- package/dist/lib/RichTextFieldBuilder.js +115 -0
- package/dist/lib/RichTextFieldBuilder.js.map +1 -0
- package/dist/lib/filterRichTextField.cjs +87 -0
- package/dist/lib/filterRichTextField.cjs.map +1 -0
- package/dist/lib/filterRichTextField.d.ts +10 -0
- package/dist/lib/filterRichTextField.js +87 -0
- package/dist/lib/filterRichTextField.js.map +1 -0
- package/dist/lib/hastSerializerHelpers.cjs +95 -0
- package/dist/lib/hastSerializerHelpers.cjs.map +1 -0
- package/dist/lib/hastSerializerHelpers.d.ts +28 -0
- package/dist/lib/hastSerializerHelpers.js +95 -0
- package/dist/lib/hastSerializerHelpers.js.map +1 -0
- package/dist/lib/hastToRichText.cjs +164 -0
- package/dist/lib/hastToRichText.cjs.map +1 -0
- package/dist/lib/hastToRichText.d.ts +30 -0
- package/dist/lib/hastToRichText.js +164 -0
- package/dist/lib/hastToRichText.js.map +1 -0
- package/dist/lib/rehypeRichText.cjs +59 -0
- package/dist/lib/rehypeRichText.cjs.map +1 -0
- package/dist/lib/rehypeRichText.d.ts +65 -0
- package/dist/lib/rehypeRichText.js +59 -0
- package/dist/lib/rehypeRichText.js.map +1 -0
- package/dist/types.d.ts +57 -0
- package/package.json +95 -0
- package/src/errors/PrismicRichTextError.ts +1 -0
- package/src/errors/PrismicRichTextSerializerError.ts +3 -0
- package/src/htmlAsRichText.ts +117 -0
- package/src/index.ts +7 -0
- package/src/lib/RichTextFieldBuilder.ts +164 -0
- package/src/lib/filterRichTextField.ts +158 -0
- package/src/lib/hastSerializerHelpers.ts +151 -0
- package/src/lib/hastToRichText.ts +294 -0
- package/src/lib/rehypeRichText.ts +185 -0
- package/src/types.ts +72 -0
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { RTInlineNode, RTNode, RichTextNodeTypes } from "@prismicio/client";
|
|
2
|
+
import type { Element } from "hast";
|
|
3
|
+
import type { RTPartialInlineNode } from "./lib/RichTextFieldBuilder";
|
|
4
|
+
/**
|
|
5
|
+
* A shorthand definition for {@link RichTextHTMLMapSerializer} rich text node
|
|
6
|
+
* types.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* The `label` rich text node type is not available as is. Use an object
|
|
10
|
+
* containing your label name to convert to label nodes instead. For example:
|
|
11
|
+
* `u: { label: "underline" }`
|
|
12
|
+
* @remarks
|
|
13
|
+
* The `span` rich text node type is not available as it is not relevant in the
|
|
14
|
+
* context of going from HTML to Prismic rich text.
|
|
15
|
+
*/
|
|
16
|
+
export type RichTextHTMLMapSerializerShorthand = Exclude<RichTextNodeTypes, "label" | "span"> | {
|
|
17
|
+
label: string;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* The payload provided to a {@link RichTextHTMLMapSerializerFunction}.
|
|
21
|
+
*/
|
|
22
|
+
type RichTextHTMLMapSerializerFunctionPayload = {
|
|
23
|
+
/**
|
|
24
|
+
* The hast {@link Element} node to serialize.
|
|
25
|
+
*/
|
|
26
|
+
node: Element;
|
|
27
|
+
/**
|
|
28
|
+
* Additional context information to help with the serialization.
|
|
29
|
+
*/
|
|
30
|
+
context: {
|
|
31
|
+
/**
|
|
32
|
+
* The list type of the last list node encountered if any.
|
|
33
|
+
*/
|
|
34
|
+
listType?: "group-list-item" | "group-o-list-item";
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Serializes a hast {@link Element} node to a
|
|
39
|
+
* {@link RichTextHTMLMapSerializerShorthand} or a rich text node.
|
|
40
|
+
*
|
|
41
|
+
* @remarks
|
|
42
|
+
* Serializing to a rich text node directly is not recommended and is only
|
|
43
|
+
* available as an escape hatch. Prefer returning a
|
|
44
|
+
* {@link RichTextHTMLMapSerializerShorthand} instead.
|
|
45
|
+
*/
|
|
46
|
+
export type RichTextHTMLMapSerializerFunction = (payload: RichTextHTMLMapSerializerFunctionPayload) => RichTextHTMLMapSerializerShorthand | RTNode | RTInlineNode | RTPartialInlineNode | undefined;
|
|
47
|
+
/**
|
|
48
|
+
* Serializes a hast {@link Element} node matching the given HTML tag name or CSS
|
|
49
|
+
* selector to a Prismic rich text node.
|
|
50
|
+
*
|
|
51
|
+
* @remarks
|
|
52
|
+
* This serializer is used to serialize HTML to Prismic rich text. When
|
|
53
|
+
* serializing Prismic rich text to HTML with {@link asHTML}, use the
|
|
54
|
+
* {@link HTMLRichTextMapSerializer} type instead.
|
|
55
|
+
*/
|
|
56
|
+
export type RichTextHTMLMapSerializer = Record<string, RichTextHTMLMapSerializerShorthand | RichTextHTMLMapSerializerFunction>;
|
|
57
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@prismicio/migrate",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Set of helpers to migrate your data to Prismic",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"typescript",
|
|
7
|
+
"prismic"
|
|
8
|
+
],
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "ssh://git@github.com/prismicio/prismic-migrate.git"
|
|
12
|
+
},
|
|
13
|
+
"license": "Apache-2.0",
|
|
14
|
+
"author": "Prismic <contact@prismic.io> (https://prismic.io)",
|
|
15
|
+
"type": "module",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"require": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"default": "./dist/index.cjs"
|
|
21
|
+
},
|
|
22
|
+
"import": {
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"default": "./dist/index.js"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"./package.json": "./package.json"
|
|
28
|
+
},
|
|
29
|
+
"main": "./dist/index.cjs",
|
|
30
|
+
"module": "./dist/index.js",
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"files": [
|
|
33
|
+
"./dist",
|
|
34
|
+
"./src"
|
|
35
|
+
],
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "vite build",
|
|
38
|
+
"dev": "vite build --watch",
|
|
39
|
+
"format": "prettier --write .",
|
|
40
|
+
"prepare": "npm run build",
|
|
41
|
+
"release": "npm run test && standard-version && git push --follow-tags && npm run build && npm publish",
|
|
42
|
+
"release:dry": "standard-version --dry-run",
|
|
43
|
+
"release:alpha": "npm run test && standard-version --release-as major --prerelease alpha && git push --follow-tags && npm run build && npm publish --tag alpha",
|
|
44
|
+
"release:alpha:dry": "standard-version --release-as major --prerelease alpha --dry-run",
|
|
45
|
+
"lint": "eslint --ext .js,.ts .",
|
|
46
|
+
"types": "vitest --typecheck.only --run",
|
|
47
|
+
"types:watch": "vitest --typecheck.only",
|
|
48
|
+
"unit": "vitest run --coverage",
|
|
49
|
+
"unit:watch": "vitest watch",
|
|
50
|
+
"size": "size-limit",
|
|
51
|
+
"test": "npm run lint && npm run types && npm run unit && npm run build && npm run size"
|
|
52
|
+
},
|
|
53
|
+
"dependencies": {
|
|
54
|
+
"hast": "^1.0.0",
|
|
55
|
+
"hast-util-select": "^6.0.2",
|
|
56
|
+
"hast-util-to-html": "^9.0.1",
|
|
57
|
+
"hast-util-to-string": "^3.0.0",
|
|
58
|
+
"rehype-minify-whitespace": "^6.0.0",
|
|
59
|
+
"rehype-parse": "^9.0.0",
|
|
60
|
+
"unified": "^11.0.5",
|
|
61
|
+
"unist-util-remove": "^4.0.0",
|
|
62
|
+
"unist-util-visit": "^5.0.0",
|
|
63
|
+
"vfile": "^6.0.2"
|
|
64
|
+
},
|
|
65
|
+
"devDependencies": {
|
|
66
|
+
"@prismicio/client": "^7.7.4",
|
|
67
|
+
"@size-limit/preset-small-lib": "^11.1.4",
|
|
68
|
+
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
|
|
69
|
+
"@types/node": "^20.14.13",
|
|
70
|
+
"@typescript-eslint/eslint-plugin": "^6.21.0",
|
|
71
|
+
"@typescript-eslint/parser": "^6.21.0",
|
|
72
|
+
"@vitest/coverage-v8": "^2.0.4",
|
|
73
|
+
"eslint": "^8.57.0",
|
|
74
|
+
"eslint-config-prettier": "^9.1.0",
|
|
75
|
+
"eslint-plugin-prettier": "^5.2.1",
|
|
76
|
+
"eslint-plugin-tsdoc": "^0.3.0",
|
|
77
|
+
"prettier": "^3.3.3",
|
|
78
|
+
"prettier-plugin-jsdoc": "^1.3.0",
|
|
79
|
+
"size-limit": "^11.1.4",
|
|
80
|
+
"standard-version": "^9.5.0",
|
|
81
|
+
"typescript": "^5.5.4",
|
|
82
|
+
"vite": "^5.3.4",
|
|
83
|
+
"vite-plugin-sdk": "^0.1.2",
|
|
84
|
+
"vitest": "^2.0.4"
|
|
85
|
+
},
|
|
86
|
+
"peerDependencies": {
|
|
87
|
+
"@prismicio/client": "^7"
|
|
88
|
+
},
|
|
89
|
+
"engines": {
|
|
90
|
+
"node": ">=18.0.0"
|
|
91
|
+
},
|
|
92
|
+
"publishConfig": {
|
|
93
|
+
"access": "public"
|
|
94
|
+
}
|
|
95
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export class PrismicRichTextError extends Error {}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// Used for TSDocs only.
|
|
2
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
3
|
+
import type { HTMLRichTextMapSerializer, asHTML } from "@prismicio/client"
|
|
4
|
+
import type {
|
|
5
|
+
RTInlineNode,
|
|
6
|
+
RTNode,
|
|
7
|
+
RichTextField,
|
|
8
|
+
RichTextNodeTypes,
|
|
9
|
+
} from "@prismicio/client"
|
|
10
|
+
import type { Element } from "hast"
|
|
11
|
+
import rehypeParse from "rehype-parse"
|
|
12
|
+
import { unified } from "unified"
|
|
13
|
+
|
|
14
|
+
import type { RTPartialInlineNode } from "./lib/RichTextFieldBuilder"
|
|
15
|
+
import type { RehypeRichTextConfig } from "./lib/rehypeRichText"
|
|
16
|
+
import { rehypeRichText } from "./lib/rehypeRichText"
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* A shorthand definition for {@link RichTextHTMLMapSerializer} rich text node
|
|
20
|
+
* types.
|
|
21
|
+
*
|
|
22
|
+
* @remarks
|
|
23
|
+
* The `label` rich text node type is not available as is. Use an object
|
|
24
|
+
* containing your label name to convert to label nodes instead. For example:
|
|
25
|
+
* `u: { label: "underline" }`
|
|
26
|
+
* @remarks
|
|
27
|
+
* The `span` rich text node type is not available as it is not relevant in the
|
|
28
|
+
* context of going from HTML to Prismic rich text.
|
|
29
|
+
*/
|
|
30
|
+
export type RichTextHTMLMapSerializerShorthand =
|
|
31
|
+
| Exclude<RichTextNodeTypes, "label" | "span">
|
|
32
|
+
| { label: string }
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The payload provided to a {@link RichTextHTMLMapSerializerFunction}.
|
|
36
|
+
*/
|
|
37
|
+
type RichTextHTMLMapSerializerFunctionPayload = {
|
|
38
|
+
/**
|
|
39
|
+
* The hast {@link Element} node to serialize.
|
|
40
|
+
*/
|
|
41
|
+
node: Element
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Additional context information to help with the serialization.
|
|
45
|
+
*/
|
|
46
|
+
context: {
|
|
47
|
+
/**
|
|
48
|
+
* The list type of the last list node encountered if any.
|
|
49
|
+
*/
|
|
50
|
+
listType?: "group-list-item" | "group-o-list-item"
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Serializes a hast {@link Element} node to a
|
|
56
|
+
* {@link RichTextHTMLMapSerializerShorthand} or a rich text node.
|
|
57
|
+
*
|
|
58
|
+
* @remarks
|
|
59
|
+
* Serializing to a rich text node directly is not recommended and is only
|
|
60
|
+
* available as an escape hatch. Prefer returning a
|
|
61
|
+
* {@link RichTextHTMLMapSerializerShorthand} instead.
|
|
62
|
+
*/
|
|
63
|
+
export type RichTextHTMLMapSerializerFunction = (
|
|
64
|
+
payload: RichTextHTMLMapSerializerFunctionPayload,
|
|
65
|
+
) =>
|
|
66
|
+
| RichTextHTMLMapSerializerShorthand
|
|
67
|
+
| RTNode
|
|
68
|
+
| RTInlineNode
|
|
69
|
+
| RTPartialInlineNode
|
|
70
|
+
| undefined
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Serializes a hast {@link Element} node matching the given HTML tag name or CSS
|
|
74
|
+
* selector to a Prismic rich text node.
|
|
75
|
+
*
|
|
76
|
+
* @remarks
|
|
77
|
+
* This serializer is used to serialize HTML to Prismic rich text. When
|
|
78
|
+
* serializing Prismic rich text to HTML with {@link asHTML}, use the
|
|
79
|
+
* {@link HTMLRichTextMapSerializer} type instead.
|
|
80
|
+
*/
|
|
81
|
+
export type RichTextHTMLMapSerializer = Record<
|
|
82
|
+
string,
|
|
83
|
+
RichTextHTMLMapSerializerShorthand | RichTextHTMLMapSerializerFunction
|
|
84
|
+
>
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Configuration that determines the output of {@link htmlAsRichText}.
|
|
88
|
+
*/
|
|
89
|
+
export type HTMLAsRichTextConfig = RehypeRichTextConfig
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* The return type of {@link htmlAsRichText}.
|
|
93
|
+
*/
|
|
94
|
+
type HTMLAsRichTextReturnType = {
|
|
95
|
+
result: RichTextField
|
|
96
|
+
warnings: string[]
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Converts an HTML string to a rich text field.
|
|
101
|
+
*
|
|
102
|
+
* @param html - An HTML string.
|
|
103
|
+
* @param config - Configuration that determines the output the function.
|
|
104
|
+
*
|
|
105
|
+
* @returns `html` as rich text.
|
|
106
|
+
*/
|
|
107
|
+
export const htmlAsRichText = (
|
|
108
|
+
html: string,
|
|
109
|
+
config?: HTMLAsRichTextConfig,
|
|
110
|
+
): HTMLAsRichTextReturnType => {
|
|
111
|
+
const { result, messages } = unified()
|
|
112
|
+
.use(rehypeParse, { emitParseErrors: true, missingDoctype: 0 })
|
|
113
|
+
.use(rehypeRichText, config)
|
|
114
|
+
.processSync(html)
|
|
115
|
+
|
|
116
|
+
return { result, warnings: messages.map((message) => message.toString()) }
|
|
117
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { htmlAsRichText } from "./htmlAsRichText"
|
|
2
|
+
|
|
3
|
+
export type { RichTextHTMLMapSerializer } from "./types"
|
|
4
|
+
|
|
5
|
+
// Rich Text Errors
|
|
6
|
+
export { PrismicRichTextError } from "./errors/PrismicRichTextError"
|
|
7
|
+
export { PrismicRichTextSerializerError } from "./errors/PrismicRichTextSerializerError"
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
RTInlineNode,
|
|
3
|
+
RTLinkNode,
|
|
4
|
+
RTNode,
|
|
5
|
+
RTTextNode,
|
|
6
|
+
RichTextField,
|
|
7
|
+
} from "@prismicio/client"
|
|
8
|
+
|
|
9
|
+
import { PrismicRichTextError } from "../errors/PrismicRichTextError"
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Omits keys from a type, distributing the operation over a union.
|
|
13
|
+
*
|
|
14
|
+
* Taken from the `type-fest` package.
|
|
15
|
+
*
|
|
16
|
+
* @see https://github.com/sindresorhus/type-fest/blob/8a45ba048767aaffcebc7d190172d814a739feb0/source/distributed-omit.d.ts
|
|
17
|
+
*/
|
|
18
|
+
type DistributedOmit<
|
|
19
|
+
ObjectType,
|
|
20
|
+
KeyType extends keyof ObjectType,
|
|
21
|
+
> = ObjectType extends unknown ? Omit<ObjectType, KeyType> : never
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* An inline node without its `start` and `end` properties.
|
|
25
|
+
*/
|
|
26
|
+
export type RTPartialInlineNode = DistributedOmit<RTInlineNode, "start" | "end">
|
|
27
|
+
|
|
28
|
+
export class RichTextFieldBuilder {
|
|
29
|
+
#nodes: RTNode[] = []
|
|
30
|
+
|
|
31
|
+
get #last(): RTNode | undefined {
|
|
32
|
+
return this.#nodes[this.#nodes.length - 1]
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
appendNode(node: RTNode): void {
|
|
36
|
+
this.cleanupLast()
|
|
37
|
+
|
|
38
|
+
this.#nodes.push(node)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
appendTextNode(
|
|
42
|
+
type: RTTextNode["type"],
|
|
43
|
+
direction: "ltr" | "rtl" = "ltr",
|
|
44
|
+
): void {
|
|
45
|
+
this.appendNode({
|
|
46
|
+
type,
|
|
47
|
+
text: "",
|
|
48
|
+
spans: [],
|
|
49
|
+
direction,
|
|
50
|
+
})
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
appendSpan(span: RTInlineNode): void
|
|
54
|
+
appendSpan(partialSpan: RTPartialInlineNode, length: number): void
|
|
55
|
+
appendSpan(
|
|
56
|
+
spanOrPartialSpan: RTInlineNode | RTPartialInlineNode,
|
|
57
|
+
length?: number,
|
|
58
|
+
): void {
|
|
59
|
+
if (!this.isTextNode(this.#last)) {
|
|
60
|
+
throw new PrismicRichTextError("Cannot add span to non-text last node")
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
let span: RTInlineNode
|
|
64
|
+
if ("start" in spanOrPartialSpan && "end" in spanOrPartialSpan) {
|
|
65
|
+
span = spanOrPartialSpan
|
|
66
|
+
} else {
|
|
67
|
+
const lastLength = this.#last.text.length
|
|
68
|
+
|
|
69
|
+
span = {
|
|
70
|
+
...spanOrPartialSpan,
|
|
71
|
+
start: lastLength,
|
|
72
|
+
end: lastLength + length!,
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
let lastIdenticalSpanIndex = -1
|
|
77
|
+
for (let i = this.#last.spans.length - 1; i >= 0; i--) {
|
|
78
|
+
const lastSpan = this.#last.spans[i]
|
|
79
|
+
|
|
80
|
+
if (span.type === "strong" || span.type === "em") {
|
|
81
|
+
if (lastSpan.type === span.type) {
|
|
82
|
+
lastIdenticalSpanIndex = i
|
|
83
|
+
break
|
|
84
|
+
}
|
|
85
|
+
} else if (span.type === "label") {
|
|
86
|
+
if (
|
|
87
|
+
lastSpan.type === "label" &&
|
|
88
|
+
lastSpan.data.label === span.data.label
|
|
89
|
+
) {
|
|
90
|
+
lastIdenticalSpanIndex = i
|
|
91
|
+
break
|
|
92
|
+
}
|
|
93
|
+
} else if (span.type === "hyperlink") {
|
|
94
|
+
if (lastSpan.type === "hyperlink") {
|
|
95
|
+
// Check that all data fields are the same. Since it's not a
|
|
96
|
+
// deep equal operation, this doesn't guarantee a perfect
|
|
97
|
+
// match with content relationship.
|
|
98
|
+
const isSameLink = (
|
|
99
|
+
Object.entries(span.data) as [keyof RTLinkNode["data"], unknown][]
|
|
100
|
+
).every(([key, value]) => lastSpan.data[key] === value)
|
|
101
|
+
|
|
102
|
+
if (isSameLink) {
|
|
103
|
+
lastIdenticalSpanIndex = i
|
|
104
|
+
break
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Ignore empty spans.
|
|
111
|
+
if (span.start === span.end) {
|
|
112
|
+
return
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Prefer to extend the last span of the same type end
|
|
116
|
+
// position if it ends at the start of the new span.
|
|
117
|
+
if (this.#last.spans[lastIdenticalSpanIndex]?.end === span.start) {
|
|
118
|
+
this.#last.spans[lastIdenticalSpanIndex].end = span.end
|
|
119
|
+
} else {
|
|
120
|
+
this.#last.spans.push(span)
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
appendText(text: string): void {
|
|
125
|
+
if (!this.isTextNode(this.#last)) {
|
|
126
|
+
throw new PrismicRichTextError("Cannot append text to non-text last node")
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (this.#last.text) {
|
|
130
|
+
this.#last.text += text
|
|
131
|
+
} else {
|
|
132
|
+
this.#last.text = text.trimStart()
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
build(): RichTextField {
|
|
137
|
+
// Ensure that the last text node is trimmed.
|
|
138
|
+
this.cleanupLast()
|
|
139
|
+
|
|
140
|
+
// Because `RichTextField` is defined as a non-empty
|
|
141
|
+
// array, we have to cast `RTNode[]` to `RichTextField`.
|
|
142
|
+
return this.#nodes as RichTextField
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Cleans up the current last node.
|
|
147
|
+
*/
|
|
148
|
+
private cleanupLast(): void {
|
|
149
|
+
if (this.isTextNode(this.#last)) {
|
|
150
|
+
this.#last.text = this.#last.text.trimEnd()
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Determines if a node is a text node.
|
|
156
|
+
*
|
|
157
|
+
* @param node - rich text node to check.
|
|
158
|
+
*
|
|
159
|
+
* @returns `true` if `node` is a text node, `false` otherwise.
|
|
160
|
+
*/
|
|
161
|
+
private isTextNode(node?: RTNode): node is RTTextNode {
|
|
162
|
+
return !!node && node.type !== "image" && node.type !== "embed"
|
|
163
|
+
}
|
|
164
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CustomTypeModelRichTextField,
|
|
3
|
+
RTImageNode,
|
|
4
|
+
RTNode,
|
|
5
|
+
RTTextNode,
|
|
6
|
+
RichTextField,
|
|
7
|
+
RichTextNodeTypes,
|
|
8
|
+
TitleField,
|
|
9
|
+
} from "@prismicio/client"
|
|
10
|
+
import { LinkType, RichTextNodeType } from "@prismicio/client"
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Get allowed nodes types from a rich text or title field custom type model.
|
|
14
|
+
*
|
|
15
|
+
* @param model - A rich text or title field custom type model.
|
|
16
|
+
*
|
|
17
|
+
* @returns An array of allowed nodes types.
|
|
18
|
+
*/
|
|
19
|
+
const getAllowedNodeTypes = (
|
|
20
|
+
model: CustomTypeModelRichTextField,
|
|
21
|
+
): RichTextNodeTypes[] => {
|
|
22
|
+
const allowedNodeTypes: RichTextNodeTypes[] = [] // An empty array means no nodes are allowed by default.
|
|
23
|
+
|
|
24
|
+
if (model.config) {
|
|
25
|
+
if ("multi" in model.config && model.config.multi) {
|
|
26
|
+
allowedNodeTypes.push(
|
|
27
|
+
...(model.config.multi.split(",") as RichTextNodeTypes[]),
|
|
28
|
+
)
|
|
29
|
+
} else if ("single" in model.config && model.config.single) {
|
|
30
|
+
allowedNodeTypes.push(
|
|
31
|
+
...(model.config.single.split(",") as RichTextNodeTypes[]),
|
|
32
|
+
)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (model.config.labels?.length) {
|
|
36
|
+
allowedNodeTypes.push("label")
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return allowedNodeTypes
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Filter an image node based on given model.
|
|
45
|
+
*
|
|
46
|
+
* @param node - An image node to filter.
|
|
47
|
+
* @param model - A rich text or title field custom type model.
|
|
48
|
+
*
|
|
49
|
+
* @returns A filtered image node based on the given model.
|
|
50
|
+
*/
|
|
51
|
+
const filterImageNode = (
|
|
52
|
+
node: RTImageNode,
|
|
53
|
+
model: CustomTypeModelRichTextField,
|
|
54
|
+
): RTImageNode => {
|
|
55
|
+
if (
|
|
56
|
+
!model.config?.allowTargetBlank &&
|
|
57
|
+
node.linkTo?.link_type === LinkType.Web
|
|
58
|
+
) {
|
|
59
|
+
return {
|
|
60
|
+
...node,
|
|
61
|
+
linkTo: {
|
|
62
|
+
...node.linkTo,
|
|
63
|
+
target: undefined,
|
|
64
|
+
},
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return node
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Filter a text node based on given model.
|
|
73
|
+
*
|
|
74
|
+
* @param node - A text node to filter.
|
|
75
|
+
* @param model - A rich text or title field custom type model.
|
|
76
|
+
* @param allowedNodeTypes - An array of allowed nodes types.
|
|
77
|
+
*
|
|
78
|
+
* @returns A filtered text node based on the given model.
|
|
79
|
+
*/
|
|
80
|
+
const filterTextNode = (
|
|
81
|
+
node: RTTextNode,
|
|
82
|
+
model: CustomTypeModelRichTextField,
|
|
83
|
+
allowedNodeTypes: RichTextNodeTypes[],
|
|
84
|
+
): RTTextNode => {
|
|
85
|
+
const filteredNode: RTTextNode = {
|
|
86
|
+
...node,
|
|
87
|
+
spans: [],
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
for (let i = 0; i < node.spans.length; i++) {
|
|
91
|
+
const span = node.spans[i]
|
|
92
|
+
|
|
93
|
+
if (allowedNodeTypes.includes(span.type)) {
|
|
94
|
+
if (span.type === RichTextNodeType.hyperlink) {
|
|
95
|
+
if (
|
|
96
|
+
!model.config?.allowTargetBlank &&
|
|
97
|
+
span.data.link_type === LinkType.Web
|
|
98
|
+
) {
|
|
99
|
+
filteredNode.spans.push({
|
|
100
|
+
...span,
|
|
101
|
+
data: {
|
|
102
|
+
...span.data,
|
|
103
|
+
target: undefined,
|
|
104
|
+
},
|
|
105
|
+
})
|
|
106
|
+
} else {
|
|
107
|
+
filteredNode.spans.push(span)
|
|
108
|
+
}
|
|
109
|
+
} else if (span.type === RichTextNodeType.label) {
|
|
110
|
+
if (model.config?.labels?.includes(span.data.label)) {
|
|
111
|
+
filteredNode.spans.push(span)
|
|
112
|
+
}
|
|
113
|
+
} else {
|
|
114
|
+
filteredNode.spans.push(span)
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return filteredNode
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Filter a rich text field based on given model.
|
|
124
|
+
*
|
|
125
|
+
* @param richTextField - A rich text or title field from Prismic.
|
|
126
|
+
* @param model - A rich text or title field custom type model.
|
|
127
|
+
*
|
|
128
|
+
* @returns A rich text or title field filtered based on the given model.
|
|
129
|
+
*/
|
|
130
|
+
export const filterRichTextField = <Field extends RichTextField | TitleField>(
|
|
131
|
+
richTextField: Field,
|
|
132
|
+
model: CustomTypeModelRichTextField,
|
|
133
|
+
): Field => {
|
|
134
|
+
const nodes: RTNode[] = []
|
|
135
|
+
|
|
136
|
+
const allowedNodeTypes = getAllowedNodeTypes(model)
|
|
137
|
+
|
|
138
|
+
for (
|
|
139
|
+
let i = 0;
|
|
140
|
+
// Only process the first node if it's a "single" node rich text type
|
|
141
|
+
i < (model.config && "multi" in model.config ? richTextField.length : 1);
|
|
142
|
+
i++
|
|
143
|
+
) {
|
|
144
|
+
const node = richTextField[i]
|
|
145
|
+
|
|
146
|
+
if (allowedNodeTypes.includes(node.type)) {
|
|
147
|
+
if (node.type === RichTextNodeType.image) {
|
|
148
|
+
nodes.push(filterImageNode(node, model))
|
|
149
|
+
} else if (node.type === RichTextNodeType.embed) {
|
|
150
|
+
nodes.push(node)
|
|
151
|
+
} else {
|
|
152
|
+
nodes.push(filterTextNode(node, model, allowedNodeTypes))
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return nodes as Field
|
|
158
|
+
}
|