@prismicio/migrate 0.0.1 → 0.0.2-canary.2da5f89

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/dist/_virtual/_rolldown/runtime.cjs +23 -0
  2. package/dist/errors/PrismicRichTextError.cjs +5 -5
  3. package/dist/errors/PrismicRichTextError.cjs.map +1 -1
  4. package/dist/errors/PrismicRichTextError.d.cts +4 -0
  5. package/dist/errors/PrismicRichTextError.d.cts.map +1 -0
  6. package/dist/errors/PrismicRichTextError.d.ts +4 -2
  7. package/dist/errors/PrismicRichTextError.d.ts.map +1 -0
  8. package/dist/errors/PrismicRichTextError.js +6 -6
  9. package/dist/errors/PrismicRichTextError.js.map +1 -1
  10. package/dist/errors/PrismicRichTextSerializerError.cjs +6 -6
  11. package/dist/errors/PrismicRichTextSerializerError.cjs.map +1 -1
  12. package/dist/errors/PrismicRichTextSerializerError.d.cts +5 -0
  13. package/dist/errors/PrismicRichTextSerializerError.d.cts.map +1 -0
  14. package/dist/errors/PrismicRichTextSerializerError.d.ts +5 -3
  15. package/dist/errors/PrismicRichTextSerializerError.d.ts.map +1 -0
  16. package/dist/errors/PrismicRichTextSerializerError.js +6 -6
  17. package/dist/errors/PrismicRichTextSerializerError.js.map +1 -1
  18. package/dist/htmlAsRichText.cjs +24 -8
  19. package/dist/htmlAsRichText.cjs.map +1 -1
  20. package/dist/htmlAsRichText.d.cts +22 -0
  21. package/dist/htmlAsRichText.d.cts.map +1 -0
  22. package/dist/htmlAsRichText.d.ts +11 -67
  23. package/dist/htmlAsRichText.d.ts.map +1 -0
  24. package/dist/htmlAsRichText.js +21 -7
  25. package/dist/htmlAsRichText.js.map +1 -1
  26. package/dist/index.cjs +6 -8
  27. package/dist/index.d.cts +5 -0
  28. package/dist/index.d.ts +5 -4
  29. package/dist/index.js +2 -7
  30. package/dist/lib/RichTextFieldBuilder.cjs +82 -113
  31. package/dist/lib/RichTextFieldBuilder.cjs.map +1 -1
  32. package/dist/lib/RichTextFieldBuilder.d.cts +14 -0
  33. package/dist/lib/RichTextFieldBuilder.d.cts.map +1 -0
  34. package/dist/lib/RichTextFieldBuilder.d.ts +5 -26
  35. package/dist/lib/RichTextFieldBuilder.d.ts.map +1 -0
  36. package/dist/lib/RichTextFieldBuilder.js +82 -113
  37. package/dist/lib/RichTextFieldBuilder.js.map +1 -1
  38. package/dist/lib/filterRichTextField.cjs +81 -78
  39. package/dist/lib/filterRichTextField.cjs.map +1 -1
  40. package/dist/lib/filterRichTextField.js +82 -79
  41. package/dist/lib/filterRichTextField.js.map +1 -1
  42. package/dist/lib/hastSerializerHelpers.cjs +101 -86
  43. package/dist/lib/hastSerializerHelpers.cjs.map +1 -1
  44. package/dist/lib/hastSerializerHelpers.js +101 -88
  45. package/dist/lib/hastSerializerHelpers.js.map +1 -1
  46. package/dist/lib/hastToRichText.cjs +151 -157
  47. package/dist/lib/hastToRichText.cjs.map +1 -1
  48. package/dist/lib/hastToRichText.d.cts +21 -0
  49. package/dist/lib/hastToRichText.d.cts.map +1 -0
  50. package/dist/lib/hastToRichText.d.ts +19 -28
  51. package/dist/lib/hastToRichText.d.ts.map +1 -0
  52. package/dist/lib/hastToRichText.js +150 -156
  53. package/dist/lib/hastToRichText.js.map +1 -1
  54. package/dist/lib/rehypeRichText.cjs +57 -57
  55. package/dist/lib/rehypeRichText.cjs.map +1 -1
  56. package/dist/lib/rehypeRichText.d.cts +58 -0
  57. package/dist/lib/rehypeRichText.d.cts.map +1 -0
  58. package/dist/lib/rehypeRichText.d.ts +52 -59
  59. package/dist/lib/rehypeRichText.d.ts.map +1 -0
  60. package/dist/lib/rehypeRichText.js +53 -55
  61. package/dist/lib/rehypeRichText.js.map +1 -1
  62. package/dist/types.d.cts +47 -0
  63. package/dist/types.d.cts.map +1 -0
  64. package/dist/types.d.ts +29 -39
  65. package/dist/types.d.ts.map +1 -0
  66. package/package.json +40 -63
  67. package/src/htmlAsRichText.ts +22 -49
  68. package/src/lib/RichTextFieldBuilder.ts +26 -31
  69. package/src/lib/filterRichTextField.ts +6 -25
  70. package/src/lib/hastSerializerHelpers.ts +4 -17
  71. package/src/lib/hastToRichText.ts +17 -39
  72. package/src/lib/rehypeRichText.ts +88 -102
  73. package/src/types.ts +24 -33
  74. package/dist/index.cjs.map +0 -1
  75. package/dist/index.js.map +0 -1
  76. package/dist/lib/filterRichTextField.d.ts +0 -10
  77. package/dist/lib/hastSerializerHelpers.d.ts +0 -28
package/dist/types.d.ts CHANGED
@@ -1,57 +1,47 @@
1
- import type { RTInlineNode, RTNode, RichTextNodeTypes } from "@prismicio/client";
2
- import type { Element } from "hast";
3
- import type { RTPartialInlineNode } from "./lib/RichTextFieldBuilder";
1
+ import { RTPartialInlineNode } from "./lib/RichTextFieldBuilder.js";
2
+ import { InjectMigrationSpecificTypes, RTInlineNode, RTNode, RichTextNodeTypes } from "@prismicio/client";
3
+ import { Element } from "hast";
4
+ //#region src/types.d.ts
4
5
  /**
5
- * A shorthand definition for {@link RichTextHTMLMapSerializer} rich text node
6
- * types.
6
+ * A shorthand definition for {@link RichTextHTMLMapSerializer} rich text node types.
7
7
  *
8
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" }`
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" }`
12
11
  * @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.
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.
15
14
  */
16
15
  export type RichTextHTMLMapSerializerShorthand = Exclude<RichTextNodeTypes, "label" | "span"> | {
17
- label: string;
16
+ label: string;
18
17
  };
19
- /**
20
- * The payload provided to a {@link RichTextHTMLMapSerializerFunction}.
21
- */
18
+ /** The payload provided to a {@link RichTextHTMLMapSerializerFunction}. */
22
19
  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
- };
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
+ };
36
27
  };
37
28
  /**
38
- * Serializes a hast {@link Element} node to a
39
- * {@link RichTextHTMLMapSerializerShorthand} or a rich text node.
29
+ * Serializes a hast {@link Element} node to a {@link RichTextHTMLMapSerializerShorthand} or a rich
30
+ * text node.
40
31
  *
41
32
  * @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.
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.
45
35
  */
46
- export type RichTextHTMLMapSerializerFunction = (payload: RichTextHTMLMapSerializerFunctionPayload) => RichTextHTMLMapSerializerShorthand | RTNode | RTInlineNode | RTPartialInlineNode | undefined;
36
+ export type RichTextHTMLMapSerializerFunction = (payload: RichTextHTMLMapSerializerFunctionPayload) => RichTextHTMLMapSerializerShorthand | InjectMigrationSpecificTypes<RTNode | RTInlineNode | RTPartialInlineNode> | undefined;
47
37
  /**
48
- * Serializes a hast {@link Element} node matching the given HTML tag name or CSS
49
- * selector to a Prismic rich text node.
38
+ * Serializes a hast {@link Element} node matching the given HTML tag name or CSS selector to a
39
+ * Prismic rich text node.
50
40
  *
51
41
  * @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.
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.
55
44
  */
56
45
  export type RichTextHTMLMapSerializer = Record<string, RichTextHTMLMapSerializerShorthand | RichTextHTMLMapSerializerFunction>;
57
- export {};
46
+ //#endregion
47
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","names":[],"sources":["../src/types.ts"],"mappings":";;;;;;;;;;;;;;YAoBY,qCACT,QAAQ;EACN;;;KAGA;;EAEJ,MAAM;;EAGN;;IAEC;;;;;;;;;;;YAYU,qCACX,SAAS,6CAEP,qCACA,6BAA6B,SAAS,eAAe;;;;;;;;;YAW5C,4BAA4B,eAEvC,qCAAqC"}
package/package.json CHANGED
@@ -1,95 +1,72 @@
1
1
  {
2
2
  "name": "@prismicio/migrate",
3
- "version": "0.0.1",
3
+ "version": "0.0.2-canary.2da5f89",
4
4
  "description": "Set of helpers to migrate your data to Prismic",
5
5
  "keywords": [
6
- "typescript",
7
- "prismic"
6
+ "prismic",
7
+ "typescript"
8
8
  ],
9
+ "license": "Apache-2.0",
10
+ "author": "Prismic <contact@prismic.io> (https://prismic.io)",
9
11
  "repository": {
10
12
  "type": "git",
11
13
  "url": "ssh://git@github.com/prismicio/prismic-migrate.git"
12
14
  },
13
- "license": "Apache-2.0",
14
- "author": "Prismic <contact@prismic.io> (https://prismic.io)",
15
+ "files": [
16
+ "./dist",
17
+ "./src"
18
+ ],
15
19
  "type": "module",
20
+ "main": "./dist/index.cjs",
21
+ "module": "./dist/index.js",
22
+ "types": "./dist/index.d.cts",
16
23
  "exports": {
17
24
  ".": {
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
- }
25
+ "import": "./dist/index.js",
26
+ "require": "./dist/index.cjs"
26
27
  },
27
28
  "./package.json": "./package.json"
28
29
  },
29
- "main": "./dist/index.cjs",
30
- "module": "./dist/index.js",
31
- "types": "./dist/index.d.ts",
32
- "files": [
33
- "./dist",
34
- "./src"
35
- ],
30
+ "publishConfig": {
31
+ "access": "public"
32
+ },
36
33
  "scripts": {
37
- "build": "vite build",
38
- "dev": "vite build --watch",
39
- "format": "prettier --write .",
34
+ "build": "tsdown",
35
+ "dev": "tsdown --watch",
36
+ "format": "oxfmt",
40
37
  "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",
38
+ "lint": "oxlint --deny-warnings",
39
+ "types": "tsc --noEmit",
48
40
  "unit": "vitest run --coverage",
49
41
  "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"
42
+ "test": "npm run lint && npm run types && npm run unit && npm run build"
52
43
  },
53
44
  "dependencies": {
54
45
  "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",
46
+ "hast-util-select": "^6.0.4",
47
+ "hast-util-to-html": "^9.0.5",
48
+ "hast-util-to-string": "^3.0.1",
49
+ "rehype-minify-whitespace": "^6.0.2",
50
+ "rehype-parse": "^9.0.1",
60
51
  "unified": "^11.0.5",
61
52
  "unist-util-remove": "^4.0.0",
62
- "unist-util-visit": "^5.0.0",
63
- "vfile": "^6.0.2"
53
+ "unist-util-visit": "^5.1.0",
54
+ "vfile": "^6.0.3"
64
55
  },
65
56
  "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"
57
+ "@prismicio/client": "^7.22.3",
58
+ "@types/node": "^26.6.5",
59
+ "@vitest/coverage-v8": "^5.0.3",
60
+ "oxfmt": "^0.72.0",
61
+ "oxlint": "^1.87.0",
62
+ "tsdown": "^0.23.0",
63
+ "typescript": "^7.0.2",
64
+ "vitest": "^5.0.3"
85
65
  },
86
66
  "peerDependencies": {
87
- "@prismicio/client": "^7"
67
+ "@prismicio/client": "^7.21.8"
88
68
  },
89
69
  "engines": {
90
- "node": ">=18.0.0"
91
- },
92
- "publishConfig": {
93
- "access": "public"
70
+ "node": ">=22"
94
71
  }
95
72
  }
@@ -1,96 +1,70 @@
1
1
  // Used for TSDocs only.
2
2
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
3
3
  import type { HTMLRichTextMapSerializer, asHTML } from "@prismicio/client"
4
- import type {
5
- RTInlineNode,
6
- RTNode,
7
- RichTextField,
8
- RichTextNodeTypes,
9
- } from "@prismicio/client"
4
+ import type { RTInlineNode, RTNode, RichTextField, RichTextNodeTypes } from "@prismicio/client"
10
5
  import type { Element } from "hast"
11
6
  import rehypeParse from "rehype-parse"
12
7
  import { unified } from "unified"
13
8
 
14
- import type { RTPartialInlineNode } from "./lib/RichTextFieldBuilder"
15
9
  import type { RehypeRichTextConfig } from "./lib/rehypeRichText"
16
10
  import { rehypeRichText } from "./lib/rehypeRichText"
11
+ import type { RTPartialInlineNode } from "./lib/RichTextFieldBuilder"
17
12
 
18
13
  /**
19
- * A shorthand definition for {@link RichTextHTMLMapSerializer} rich text node
20
- * types.
14
+ * A shorthand definition for {@link RichTextHTMLMapSerializer} rich text node types.
21
15
  *
22
16
  * @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" }`
17
+ * The `label` rich text node type is not available as is. Use an object containing your label
18
+ * name to convert to label nodes instead. For example: `u: { label: "underline" }`
26
19
  * @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.
20
+ * The `span` rich text node type is not available as it is not relevant in the context of going
21
+ * from HTML to Prismic rich text.
29
22
  */
30
23
  export type RichTextHTMLMapSerializerShorthand =
31
24
  | Exclude<RichTextNodeTypes, "label" | "span">
32
25
  | { label: string }
33
26
 
34
- /**
35
- * The payload provided to a {@link RichTextHTMLMapSerializerFunction}.
36
- */
27
+ /** The payload provided to a {@link RichTextHTMLMapSerializerFunction}. */
37
28
  type RichTextHTMLMapSerializerFunctionPayload = {
38
- /**
39
- * The hast {@link Element} node to serialize.
40
- */
29
+ /** The hast {@link Element} node to serialize. */
41
30
  node: Element
42
31
 
43
- /**
44
- * Additional context information to help with the serialization.
45
- */
32
+ /** Additional context information to help with the serialization. */
46
33
  context: {
47
- /**
48
- * The list type of the last list node encountered if any.
49
- */
34
+ /** The list type of the last list node encountered if any. */
50
35
  listType?: "group-list-item" | "group-o-list-item"
51
36
  }
52
37
  }
53
38
 
54
39
  /**
55
- * Serializes a hast {@link Element} node to a
56
- * {@link RichTextHTMLMapSerializerShorthand} or a rich text node.
40
+ * Serializes a hast {@link Element} node to a {@link RichTextHTMLMapSerializerShorthand} or a rich
41
+ * text node.
57
42
  *
58
43
  * @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.
44
+ * Serializing to a rich text node directly is not recommended and is only available as an escape
45
+ * hatch. Prefer returning a {@link RichTextHTMLMapSerializerShorthand} instead.
62
46
  */
63
47
  export type RichTextHTMLMapSerializerFunction = (
64
48
  payload: RichTextHTMLMapSerializerFunctionPayload,
65
- ) =>
66
- | RichTextHTMLMapSerializerShorthand
67
- | RTNode
68
- | RTInlineNode
69
- | RTPartialInlineNode
70
- | undefined
49
+ ) => RichTextHTMLMapSerializerShorthand | RTNode | RTInlineNode | RTPartialInlineNode | undefined
71
50
 
72
51
  /**
73
- * Serializes a hast {@link Element} node matching the given HTML tag name or CSS
74
- * selector to a Prismic rich text node.
52
+ * Serializes a hast {@link Element} node matching the given HTML tag name or CSS selector to a
53
+ * Prismic rich text node.
75
54
  *
76
55
  * @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.
56
+ * This serializer is used to serialize HTML to Prismic rich text. When serializing Prismic rich
57
+ * text to HTML with {@link asHTML}, use the {@link HTMLRichTextMapSerializer} type instead.
80
58
  */
81
59
  export type RichTextHTMLMapSerializer = Record<
82
60
  string,
83
61
  RichTextHTMLMapSerializerShorthand | RichTextHTMLMapSerializerFunction
84
62
  >
85
63
 
86
- /**
87
- * Configuration that determines the output of {@link htmlAsRichText}.
88
- */
64
+ /** Configuration that determines the output of {@link htmlAsRichText}. */
89
65
  export type HTMLAsRichTextConfig = RehypeRichTextConfig
90
66
 
91
- /**
92
- * The return type of {@link htmlAsRichText}.
93
- */
67
+ /** The return type of {@link htmlAsRichText}. */
94
68
  type HTMLAsRichTextReturnType = {
95
69
  result: RichTextField
96
70
  warnings: string[]
@@ -101,7 +75,6 @@ type HTMLAsRichTextReturnType = {
101
75
  *
102
76
  * @param html - An HTML string.
103
77
  * @param config - Configuration that determines the output the function.
104
- *
105
78
  * @returns `html` as rich text.
106
79
  */
107
80
  export const htmlAsRichText = (
@@ -1,4 +1,5 @@
1
1
  import type {
2
+ InjectMigrationSpecificTypes,
2
3
  RTInlineNode,
3
4
  RTLinkNode,
4
5
  RTNode,
@@ -15,33 +16,27 @@ import { PrismicRichTextError } from "../errors/PrismicRichTextError"
15
16
  *
16
17
  * @see https://github.com/sindresorhus/type-fest/blob/8a45ba048767aaffcebc7d190172d814a739feb0/source/distributed-omit.d.ts
17
18
  */
18
- type DistributedOmit<
19
- ObjectType,
20
- KeyType extends keyof ObjectType,
21
- > = ObjectType extends unknown ? Omit<ObjectType, KeyType> : never
19
+ type DistributedOmit<ObjectType, KeyType extends keyof ObjectType> = ObjectType extends unknown
20
+ ? Omit<ObjectType, KeyType>
21
+ : never
22
22
 
23
- /**
24
- * An inline node without its `start` and `end` properties.
25
- */
23
+ /** An inline node without its `start` and `end` properties. */
26
24
  export type RTPartialInlineNode = DistributedOmit<RTInlineNode, "start" | "end">
27
25
 
28
26
  export class RichTextFieldBuilder {
29
- #nodes: RTNode[] = []
27
+ #nodes: InjectMigrationSpecificTypes<RTNode>[] = []
30
28
 
31
- get #last(): RTNode | undefined {
29
+ get #last(): InjectMigrationSpecificTypes<RTNode> | undefined {
32
30
  return this.#nodes[this.#nodes.length - 1]
33
31
  }
34
32
 
35
- appendNode(node: RTNode): void {
33
+ appendNode(node: InjectMigrationSpecificTypes<RTNode>): void {
36
34
  this.cleanupLast()
37
35
 
38
36
  this.#nodes.push(node)
39
37
  }
40
38
 
41
- appendTextNode(
42
- type: RTTextNode["type"],
43
- direction: "ltr" | "rtl" = "ltr",
44
- ): void {
39
+ appendTextNode(type: RTTextNode["type"], direction: "ltr" | "rtl" = "ltr"): void {
45
40
  this.appendNode({
46
41
  type,
47
42
  text: "",
@@ -50,17 +45,17 @@ export class RichTextFieldBuilder {
50
45
  })
51
46
  }
52
47
 
53
- appendSpan(span: RTInlineNode): void
54
- appendSpan(partialSpan: RTPartialInlineNode, length: number): void
48
+ appendSpan(span: InjectMigrationSpecificTypes<RTInlineNode>): void
49
+ appendSpan(partialSpan: InjectMigrationSpecificTypes<RTPartialInlineNode>, length: number): void
55
50
  appendSpan(
56
- spanOrPartialSpan: RTInlineNode | RTPartialInlineNode,
51
+ spanOrPartialSpan: InjectMigrationSpecificTypes<RTInlineNode | RTPartialInlineNode>,
57
52
  length?: number,
58
53
  ): void {
59
54
  if (!this.isTextNode(this.#last)) {
60
55
  throw new PrismicRichTextError("Cannot add span to non-text last node")
61
56
  }
62
57
 
63
- let span: RTInlineNode
58
+ let span: InjectMigrationSpecificTypes<RTInlineNode>
64
59
  if ("start" in spanOrPartialSpan && "end" in spanOrPartialSpan) {
65
60
  span = spanOrPartialSpan
66
61
  } else {
@@ -83,10 +78,7 @@ export class RichTextFieldBuilder {
83
78
  break
84
79
  }
85
80
  } else if (span.type === "label") {
86
- if (
87
- lastSpan.type === "label" &&
88
- lastSpan.data.label === span.data.label
89
- ) {
81
+ if (lastSpan.type === "label" && lastSpan.data.label === span.data.label) {
90
82
  lastIdenticalSpanIndex = i
91
83
  break
92
84
  }
@@ -95,9 +87,13 @@ export class RichTextFieldBuilder {
95
87
  // Check that all data fields are the same. Since it's not a
96
88
  // deep equal operation, this doesn't guarantee a perfect
97
89
  // 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)
90
+ const isSameLink =
91
+ typeof span.data === "object" && typeof lastSpan.data === "object"
92
+ ? (Object.entries(span.data) as [keyof RTLinkNode["data"], unknown][]).every(
93
+ ([key, value]) =>
94
+ lastSpan.data?.[key as keyof (typeof lastSpan)["data"]] === value,
95
+ )
96
+ : span.data === lastSpan.data
101
97
 
102
98
  if (isSameLink) {
103
99
  lastIdenticalSpanIndex = i
@@ -142,9 +138,7 @@ export class RichTextFieldBuilder {
142
138
  return this.#nodes as RichTextField
143
139
  }
144
140
 
145
- /**
146
- * Cleans up the current last node.
147
- */
141
+ /** Cleans up the current last node. */
148
142
  private cleanupLast(): void {
149
143
  if (this.isTextNode(this.#last)) {
150
144
  this.#last.text = this.#last.text.trimEnd()
@@ -154,11 +148,12 @@ export class RichTextFieldBuilder {
154
148
  /**
155
149
  * Determines if a node is a text node.
156
150
  *
157
- * @param node - rich text node to check.
158
- *
151
+ * @param node - Rich text node to check.
159
152
  * @returns `true` if `node` is a text node, `false` otherwise.
160
153
  */
161
- private isTextNode(node?: RTNode): node is RTTextNode {
154
+ private isTextNode(
155
+ node?: InjectMigrationSpecificTypes<RTNode>,
156
+ ): node is InjectMigrationSpecificTypes<RTTextNode> {
162
157
  return !!node && node.type !== "image" && node.type !== "embed"
163
158
  }
164
159
  }
@@ -13,23 +13,16 @@ import { LinkType, RichTextNodeType } from "@prismicio/client"
13
13
  * Get allowed nodes types from a rich text or title field custom type model.
14
14
  *
15
15
  * @param model - A rich text or title field custom type model.
16
- *
17
16
  * @returns An array of allowed nodes types.
18
17
  */
19
- const getAllowedNodeTypes = (
20
- model: CustomTypeModelRichTextField,
21
- ): RichTextNodeTypes[] => {
18
+ const getAllowedNodeTypes = (model: CustomTypeModelRichTextField): RichTextNodeTypes[] => {
22
19
  const allowedNodeTypes: RichTextNodeTypes[] = [] // An empty array means no nodes are allowed by default.
23
20
 
24
21
  if (model.config) {
25
22
  if ("multi" in model.config && model.config.multi) {
26
- allowedNodeTypes.push(
27
- ...(model.config.multi.split(",") as RichTextNodeTypes[]),
28
- )
23
+ allowedNodeTypes.push(...(model.config.multi.split(",") as RichTextNodeTypes[]))
29
24
  } else if ("single" in model.config && model.config.single) {
30
- allowedNodeTypes.push(
31
- ...(model.config.single.split(",") as RichTextNodeTypes[]),
32
- )
25
+ allowedNodeTypes.push(...(model.config.single.split(",") as RichTextNodeTypes[]))
33
26
  }
34
27
 
35
28
  if (model.config.labels?.length) {
@@ -45,17 +38,10 @@ const getAllowedNodeTypes = (
45
38
  *
46
39
  * @param node - An image node to filter.
47
40
  * @param model - A rich text or title field custom type model.
48
- *
49
41
  * @returns A filtered image node based on the given model.
50
42
  */
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
- ) {
43
+ const filterImageNode = (node: RTImageNode, model: CustomTypeModelRichTextField): RTImageNode => {
44
+ if (!model.config?.allowTargetBlank && node.linkTo?.link_type === LinkType.Web) {
59
45
  return {
60
46
  ...node,
61
47
  linkTo: {
@@ -74,7 +60,6 @@ const filterImageNode = (
74
60
  * @param node - A text node to filter.
75
61
  * @param model - A rich text or title field custom type model.
76
62
  * @param allowedNodeTypes - An array of allowed nodes types.
77
- *
78
63
  * @returns A filtered text node based on the given model.
79
64
  */
80
65
  const filterTextNode = (
@@ -92,10 +77,7 @@ const filterTextNode = (
92
77
 
93
78
  if (allowedNodeTypes.includes(span.type)) {
94
79
  if (span.type === RichTextNodeType.hyperlink) {
95
- if (
96
- !model.config?.allowTargetBlank &&
97
- span.data.link_type === LinkType.Web
98
- ) {
80
+ if (!model.config?.allowTargetBlank && span.data.link_type === LinkType.Web) {
99
81
  filteredNode.spans.push({
100
82
  ...span,
101
83
  data: {
@@ -124,7 +106,6 @@ const filterTextNode = (
124
106
  *
125
107
  * @param richTextField - A rich text or title field from Prismic.
126
108
  * @param model - A rich text or title field custom type model.
127
- *
128
109
  * @returns A rich text or title field filtered based on the given model.
129
110
  */
130
111
  export const filterRichTextField = <Field extends RichTextField | TitleField>(
@@ -4,23 +4,19 @@ import type { Element } from "hast"
4
4
  import { toHtml } from "hast-util-to-html"
5
5
 
6
6
  import { PrismicRichTextSerializerError } from "../errors/PrismicRichTextSerializerError"
7
-
8
7
  import type { RTPartialInlineNode } from "./RichTextFieldBuilder"
9
8
 
10
9
  /**
11
10
  * Serializes a hast {@link Element} node to a {@link RTImageNode}.
12
11
  *
13
12
  * @param node - A hast node to serialize.
14
- *
15
13
  * @returns Equivalent {@link RTImageNode}.
16
14
  */
17
15
  export const serializeImage = (node: Element): RTImageNode => {
18
16
  const src = node.properties?.src as string | undefined
19
17
 
20
18
  if (!src) {
21
- throw new PrismicRichTextSerializerError(
22
- "Element of type `img` is missing an `src` attribute",
23
- )
19
+ throw new PrismicRichTextSerializerError("Element of type `img` is missing an `src` attribute")
24
20
  }
25
21
 
26
22
  const url = new URL(src, "https://noop.com")
@@ -42,9 +38,7 @@ export const serializeImage = (node: Element): RTImageNode => {
42
38
  }
43
39
 
44
40
  if (url.searchParams.has("rect")) {
45
- const [rectX, rextY, rectW, _rectH] = url.searchParams
46
- .get("rect")!
47
- .split(",")
41
+ const [rectX, rextY, rectW, _rectH] = url.searchParams.get("rect")!.split(",")
48
42
 
49
43
  x = Number(rectX)
50
44
  y = Number(rextY)
@@ -74,7 +68,6 @@ export const serializeImage = (node: Element): RTImageNode => {
74
68
  * Serializes a hast {@link Element} node to a {@link RTEmbedNode}.
75
69
  *
76
70
  * @param node - A hast node to serialize.
77
- *
78
71
  * @returns Equivalent {@link RTEmbedNode}.
79
72
  */
80
73
  export const serializeEmbed = (node: Element): RTEmbedNode => {
@@ -117,19 +110,13 @@ export const serializeEmbed = (node: Element): RTEmbedNode => {
117
110
  *
118
111
  * @param node - A hast node to serialize.
119
112
  * @param rtPartialInlineNode - A partial rich text node.
120
- *
121
113
  * @returns Equivalent {@link RTPartialInlineNode}.
122
114
  */
123
115
  export const serializeSpan = (
124
116
  node: Element,
125
- rtPartialInlineNode:
126
- | RTPartialInlineNode
127
- | Omit<RTLinkNode, "start" | "end" | "data">,
117
+ rtPartialInlineNode: RTPartialInlineNode | Omit<RTLinkNode, "start" | "end" | "data">,
128
118
  ): RTPartialInlineNode => {
129
- if (
130
- rtPartialInlineNode.type === "hyperlink" &&
131
- !("data" in rtPartialInlineNode)
132
- ) {
119
+ if (rtPartialInlineNode.type === "hyperlink" && !("data" in rtPartialInlineNode)) {
133
120
  const url = node.properties?.href as string | undefined
134
121
  if (!url) {
135
122
  throw new PrismicRichTextSerializerError(