@prismicio/client 7.22.3-pr.472.b6003a6 → 7.23.0-pr.473.7aca245
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/package.cjs +1 -1
- package/dist/package.js +1 -1
- package/dist/richtext/asTree.cjs +38 -0
- package/dist/richtext/asTree.cjs.map +1 -1
- package/dist/richtext/asTree.js +38 -0
- package/dist/richtext/asTree.js.map +1 -1
- package/dist/types/value/richText.cjs.map +1 -1
- package/dist/types/value/richText.d.cts +4 -0
- package/dist/types/value/richText.d.cts.map +1 -1
- package/dist/types/value/richText.d.ts +4 -0
- package/dist/types/value/richText.d.ts.map +1 -1
- package/dist/types/value/richText.js.map +1 -1
- package/package.json +1 -1
- package/src/richtext/asTree.ts +133 -55
- package/src/types/value/richText.ts +6 -3
package/dist/package.cjs
CHANGED
package/dist/package.js
CHANGED
package/dist/richtext/asTree.cjs
CHANGED
|
@@ -14,6 +14,7 @@ uuid.i = 0;
|
|
|
14
14
|
* @returns Tree from given rich text or title field
|
|
15
15
|
*/
|
|
16
16
|
const asTree = (nodes) => {
|
|
17
|
+
if (nodes.some((node) => (node.type === require_richText.RichTextNodeType.listItem || node.type === require_richText.RichTextNodeType.oListItem) && (node.depth ?? 0) > 0)) return nestedListsToTree(nodes);
|
|
17
18
|
const preparedNodes = prepareNodes(nodes);
|
|
18
19
|
const children = [];
|
|
19
20
|
for (let i = 0; i < preparedNodes.length; i++) children.push(nodeToTreeNode(preparedNodes[i]));
|
|
@@ -22,6 +23,43 @@ const asTree = (nodes) => {
|
|
|
22
23
|
children
|
|
23
24
|
};
|
|
24
25
|
};
|
|
26
|
+
const nestedListsToTree = (nodes) => {
|
|
27
|
+
const children = [];
|
|
28
|
+
const levels = [];
|
|
29
|
+
for (const node of nodes) {
|
|
30
|
+
if (node.type !== require_richText.RichTextNodeType.listItem && node.type !== require_richText.RichTextNodeType.oListItem) {
|
|
31
|
+
levels.length = 0;
|
|
32
|
+
children.push(nodeToTreeNode(node));
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
const requested = node.depth ?? 0;
|
|
36
|
+
const depth = Number.isSafeInteger(requested) && requested >= 0 ? Math.min(requested, levels.length) : 0;
|
|
37
|
+
levels.length = Math.min(levels.length, depth + 1);
|
|
38
|
+
const listType = node.type === require_richText.RichTextNodeType.listItem ? require_richText.RichTextNodeType.list : require_richText.RichTextNodeType.oList;
|
|
39
|
+
let level = levels[depth];
|
|
40
|
+
if (!level || level.list.type !== listType) {
|
|
41
|
+
const list = createTreeNode(listType === require_richText.RichTextNodeType.list ? {
|
|
42
|
+
type: listType,
|
|
43
|
+
items: []
|
|
44
|
+
} : {
|
|
45
|
+
type: listType,
|
|
46
|
+
items: []
|
|
47
|
+
});
|
|
48
|
+
(depth === 0 ? children : levels[depth - 1].lastItem.children).push(list);
|
|
49
|
+
level = { list };
|
|
50
|
+
levels[depth] = level;
|
|
51
|
+
}
|
|
52
|
+
const item = nodeToTreeNode(node);
|
|
53
|
+
level.list.children.push(item);
|
|
54
|
+
if (level.list.node.type === require_richText.RichTextNodeType.list && node.type === require_richText.RichTextNodeType.listItem) level.list.node.items.push(node);
|
|
55
|
+
if (level.list.node.type === require_richText.RichTextNodeType.oList && node.type === require_richText.RichTextNodeType.oListItem) level.list.node.items.push(node);
|
|
56
|
+
level.lastItem = item;
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
key: uuid(),
|
|
60
|
+
children
|
|
61
|
+
};
|
|
62
|
+
};
|
|
25
63
|
const createTreeNode = (node, children = []) => {
|
|
26
64
|
return {
|
|
27
65
|
key: uuid(),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"asTree.cjs","names":["RichTextNodeType"],"sources":["../../src/richtext/asTree.ts"],"sourcesContent":["import type {\n\tRTAnyNode,\n\tRTBlockNode,\n\tRTInlineNode,\n\tRTListItemNode,\n\tRTNode,\n\tRTOListItemNode,\n\tRTTextNode,\n} from \"../types/value/richText\"\nimport { RichTextNodeType } from \"../types/value/richText\"\nimport type { Tree, TreeNode } from \"./types\"\n\nconst uuid = (): string => {\n\treturn (++uuid.i).toString()\n}\nuuid.i = 0\n\n/**\n * Parses a rich text or title field into a tree\n *\n * @remarks\n * This is a low level helper mainly intended to be used by higher level packages. Most users\n * aren't expected to this function directly.\n * @param nodes - A rich text or title field from Prismic\n * @returns Tree from given rich text or title field\n */\nexport const asTree = (nodes: RTNode[]): Tree => {\n\tconst preparedNodes = prepareNodes(nodes)\n\n\tconst children: TreeNode[] = []\n\tfor (let i = 0; i < preparedNodes.length; i++) {\n\t\tchildren.push(nodeToTreeNode(preparedNodes[i]))\n\t}\n\n\treturn {\n\t\tkey: uuid(),\n\t\tchildren,\n\t}\n}\n\nconst createTreeNode = (node: RTAnyNode, children: TreeNode[] = []): TreeNode => {\n\treturn {\n\t\tkey: uuid(),\n\t\ttype: node.type,\n\t\ttext: \"text\" in node ? node.text : undefined,\n\t\tnode,\n\t\tchildren,\n\t}\n}\n\nconst createTextTreeNode = (text: string): TreeNode => {\n\treturn createTreeNode({\n\t\ttype: RichTextNodeType.span,\n\t\ttext,\n\t\tspans: [],\n\t})\n}\n\nconst prepareNodes = (nodes: RTNode[]): RTBlockNode[] => {\n\tconst mutNodes: RTBlockNode[] = nodes.slice(0)\n\n\tfor (let i = 0; i < mutNodes.length; i++) {\n\t\tconst node = mutNodes[i]\n\n\t\tif (node.type === RichTextNodeType.listItem || node.type === RichTextNodeType.oListItem) {\n\t\t\tconst items: (RTListItemNode | RTOListItemNode)[] = [node as RTListItemNode | RTOListItemNode]\n\n\t\t\twhile (mutNodes[i + 1] && mutNodes[i + 1].type === node.type) {\n\t\t\t\titems.push(mutNodes[i + 1] as RTListItemNode | RTOListItemNode)\n\t\t\t\tmutNodes.splice(i, 1)\n\t\t\t}\n\n\t\t\tif (node.type === RichTextNodeType.listItem) {\n\t\t\t\tmutNodes[i] = {\n\t\t\t\t\ttype: RichTextNodeType.list,\n\t\t\t\t\titems: items as RTListItemNode[],\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tmutNodes[i] = {\n\t\t\t\t\ttype: RichTextNodeType.oList,\n\t\t\t\t\titems: items as RTOListItemNode[],\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\treturn mutNodes\n}\n\nconst nodeToTreeNode = (node: RTBlockNode): TreeNode => {\n\tif (\"text\" in node) {\n\t\treturn createTreeNode(node, textNodeSpansToTreeNodeChildren(node.spans, node))\n\t}\n\n\tif (\"items\" in node) {\n\t\tconst children: TreeNode[] = []\n\t\tfor (let i = 0; i < node.items.length; i++) {\n\t\t\tchildren.push(nodeToTreeNode(node.items[i]))\n\t\t}\n\n\t\treturn createTreeNode(node, children)\n\t}\n\n\treturn createTreeNode(node)\n}\n\nconst textNodeSpansToTreeNodeChildren = (\n\tspans: RTInlineNode[],\n\tnode: RTTextNode,\n\tparentSpan?: RTInlineNode,\n): TreeNode[] => {\n\tif (!spans.length) {\n\t\treturn [createTextTreeNode(node.text)]\n\t}\n\n\tconst mutSpans: RTInlineNode[] = spans.slice(0)\n\n\t// Sort spans using the following criteria:\n\t//\n\t// 1. By start index (ascending)\n\t// 2. If start indices are equal, by end index (descending)\n\t//\n\t// If start and end indices of more than one span are equal, use what\n\t// the API gives without modifications.\n\t//\n\t// Sorting using this algorithm ensures proper detection of child\n\t// spans.\n\tmutSpans.sort((a, b) => a.start - b.start || b.end - a.end)\n\n\tconst children: TreeNode[] = []\n\n\tfor (let i = 0; i < mutSpans.length; i++) {\n\t\tconst span = mutSpans[i]\n\t\tconst parentSpanStart = (parentSpan && parentSpan.start) || 0\n\t\tconst spanStart = span.start - parentSpanStart\n\t\tconst spanEnd = span.end - parentSpanStart\n\t\tconst text = node.text.slice(spanStart, spanEnd)\n\n\t\tconst childSpans: RTInlineNode[] = []\n\t\tfor (let j = i; j < mutSpans.length; j++) {\n\t\t\tconst siblingSpan = mutSpans[j]\n\n\t\t\tif (siblingSpan !== span) {\n\t\t\t\tif (siblingSpan.start >= span.start && siblingSpan.end <= span.end) {\n\t\t\t\t\tchildSpans.push(siblingSpan)\n\t\t\t\t\tmutSpans.splice(j, 1)\n\t\t\t\t\tj--\n\t\t\t\t} else if (siblingSpan.start < span.end && siblingSpan.end > span.start) {\n\t\t\t\t\tchildSpans.push({\n\t\t\t\t\t\t...siblingSpan,\n\t\t\t\t\t\tend: span.end,\n\t\t\t\t\t})\n\t\t\t\t\tmutSpans[j] = {\n\t\t\t\t\t\t...siblingSpan,\n\t\t\t\t\t\tstart: span.end,\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (i === 0 && spanStart > 0) {\n\t\t\tchildren.push(createTextTreeNode(node.text.slice(0, spanStart)))\n\t\t}\n\n\t\tconst spanWithText = { ...span, text }\n\t\tchildren.push(\n\t\t\tcreateTreeNode(\n\t\t\t\tspanWithText,\n\t\t\t\ttextNodeSpansToTreeNodeChildren(\n\t\t\t\t\tchildSpans,\n\t\t\t\t\t{\n\t\t\t\t\t\t...node,\n\t\t\t\t\t\ttext,\n\t\t\t\t\t},\n\t\t\t\t\tspan,\n\t\t\t\t),\n\t\t\t),\n\t\t)\n\n\t\tif (spanEnd < node.text.length) {\n\t\t\tchildren.push(\n\t\t\t\tcreateTextTreeNode(\n\t\t\t\t\tnode.text.slice(\n\t\t\t\t\t\tspanEnd,\n\t\t\t\t\t\tmutSpans[i + 1] ? mutSpans[i + 1].start - parentSpanStart : undefined,\n\t\t\t\t\t),\n\t\t\t\t),\n\t\t\t)\n\t\t}\n\t}\n\n\treturn children\n}\n"],"mappings":";;AAYA,MAAM,aAAqB;CAC1B,QAAQ,EAAE,KAAK,EAAA,CAAG,SAAS;AAC5B;AACA,KAAK,IAAI;;;;;;;;;;AAWT,MAAa,UAAU,UAA0B;CAChD,MAAM,gBAAgB,aAAa,KAAK;CAExC,MAAM,WAAuB,CAAC;CAC9B,KAAK,IAAI,IAAI,GAAG,IAAI,cAAc,QAAQ,KACzC,S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AAK,KAAK,MACT,SACA,SAAS,IAAI,KAAK,SAAS,IAAI,EAAE,CAAC,QAAQ,kBAAkB,KAAA,CAC7D,CACD,CACD;CAEF;CAEA,OAAO;AACR"}
|
|
1
|
+
{"version":3,"file":"asTree.cjs","names":["RichTextNodeType"],"sources":["../../src/richtext/asTree.ts"],"sourcesContent":["import type {\n\tRTAnyNode,\n\tRTBlockNode,\n\tRTInlineNode,\n\tRTListItemNode,\n\tRTNode,\n\tRTOListItemNode,\n\tRTTextNode,\n} from \"../types/value/richText\";\nimport { RichTextNodeType } from \"../types/value/richText\";\nimport type { Tree, TreeNode } from \"./types\";\n\nconst uuid = (): string => {\n\treturn (++uuid.i).toString();\n};\nuuid.i = 0;\n\n/**\n * Parses a rich text or title field into a tree\n *\n * @remarks\n * This is a low level helper mainly intended to be used by higher level packages. Most users\n * aren't expected to this function directly.\n * @param nodes - A rich text or title field from Prismic\n * @returns Tree from given rich text or title field\n */\nexport const asTree = (nodes: RTNode[]): Tree => {\n\tif (\n\t\tnodes.some(\n\t\t\t(node) =>\n\t\t\t\t(node.type === RichTextNodeType.listItem ||\n\t\t\t\t\tnode.type === RichTextNodeType.oListItem) &&\n\t\t\t\t(node.depth ?? 0) > 0,\n\t\t)\n\t) {\n\t\treturn nestedListsToTree(nodes);\n\t}\n\tconst preparedNodes = prepareNodes(nodes);\n\n\tconst children: TreeNode[] = [];\n\tfor (let i = 0; i < preparedNodes.length; i++) {\n\t\tchildren.push(nodeToTreeNode(preparedNodes[i]));\n\t}\n\n\treturn {\n\t\tkey: uuid(),\n\t\tchildren,\n\t};\n};\n\nconst nestedListsToTree = (nodes: RTNode[]): Tree => {\n\tconst children: TreeNode[] = [];\n\tconst levels: { list: TreeNode; lastItem?: TreeNode }[] = [];\n\tfor (const node of nodes) {\n\t\tif (\n\t\t\tnode.type !== RichTextNodeType.listItem &&\n\t\t\tnode.type !== RichTextNodeType.oListItem\n\t\t) {\n\t\t\tlevels.length = 0;\n\t\t\tchildren.push(nodeToTreeNode(node));\n\t\t\tcontinue;\n\t\t}\n\t\tconst requested = node.depth ?? 0;\n\t\tconst depth =\n\t\t\tNumber.isSafeInteger(requested) && requested >= 0\n\t\t\t\t? Math.min(requested, levels.length)\n\t\t\t\t: 0;\n\t\tlevels.length = Math.min(levels.length, depth + 1);\n\t\tconst listType =\n\t\t\tnode.type === RichTextNodeType.listItem\n\t\t\t\t? RichTextNodeType.list\n\t\t\t\t: RichTextNodeType.oList;\n\t\tlet level = levels[depth];\n\t\tif (!level || level.list.type !== listType) {\n\t\t\tconst list = createTreeNode(\n\t\t\t\tlistType === RichTextNodeType.list\n\t\t\t\t\t? { type: listType, items: [] }\n\t\t\t\t\t: { type: listType, items: [] },\n\t\t\t);\n\t\t\tconst siblings =\n\t\t\t\tdepth === 0 ? children : levels[depth - 1].lastItem!.children;\n\t\t\tsiblings.push(list);\n\t\t\tlevel = { list };\n\t\t\tlevels[depth] = level;\n\t\t}\n\t\tconst item = nodeToTreeNode(node);\n\t\tlevel.list.children.push(item);\n\t\tif (\n\t\t\tlevel.list.node.type === RichTextNodeType.list &&\n\t\t\tnode.type === RichTextNodeType.listItem\n\t\t)\n\t\t\tlevel.list.node.items.push(node);\n\t\tif (\n\t\t\tlevel.list.node.type === RichTextNodeType.oList &&\n\t\t\tnode.type === RichTextNodeType.oListItem\n\t\t)\n\t\t\tlevel.list.node.items.push(node);\n\t\tlevel.lastItem = item;\n\t}\n\treturn { key: uuid(), children };\n};\n\nconst createTreeNode = (\n\tnode: RTAnyNode,\n\tchildren: TreeNode[] = [],\n): TreeNode => {\n\treturn {\n\t\tkey: uuid(),\n\t\ttype: node.type,\n\t\ttext: \"text\" in node ? node.text : undefined,\n\t\tnode,\n\t\tchildren,\n\t};\n};\n\nconst createTextTreeNode = (text: string): TreeNode => {\n\treturn createTreeNode({\n\t\ttype: RichTextNodeType.span,\n\t\ttext,\n\t\tspans: [],\n\t});\n};\n\nconst prepareNodes = (nodes: RTNode[]): RTBlockNode[] => {\n\tconst mutNodes: RTBlockNode[] = nodes.slice(0);\n\n\tfor (let i = 0; i < mutNodes.length; i++) {\n\t\tconst node = mutNodes[i];\n\n\t\tif (\n\t\t\tnode.type === RichTextNodeType.listItem ||\n\t\t\tnode.type === RichTextNodeType.oListItem\n\t\t) {\n\t\t\tconst items: (RTListItemNode | RTOListItemNode)[] = [\n\t\t\t\tnode as RTListItemNode | RTOListItemNode,\n\t\t\t];\n\n\t\t\twhile (mutNodes[i + 1] && mutNodes[i + 1].type === node.type) {\n\t\t\t\titems.push(mutNodes[i + 1] as RTListItemNode | RTOListItemNode);\n\t\t\t\tmutNodes.splice(i, 1);\n\t\t\t}\n\n\t\t\tif (node.type === RichTextNodeType.listItem) {\n\t\t\t\tmutNodes[i] = {\n\t\t\t\t\ttype: RichTextNodeType.list,\n\t\t\t\t\titems: items as RTListItemNode[],\n\t\t\t\t};\n\t\t\t} else {\n\t\t\t\tmutNodes[i] = {\n\t\t\t\t\ttype: RichTextNodeType.oList,\n\t\t\t\t\titems: items as RTOListItemNode[],\n\t\t\t\t};\n\t\t\t}\n\t\t}\n\t}\n\n\treturn mutNodes;\n};\n\nconst nodeToTreeNode = (node: RTBlockNode): TreeNode => {\n\tif (\"text\" in node) {\n\t\treturn createTreeNode(\n\t\t\tnode,\n\t\t\ttextNodeSpansToTreeNodeChildren(node.spans, node),\n\t\t);\n\t}\n\n\tif (\"items\" in node) {\n\t\tconst children: TreeNode[] = [];\n\t\tfor (let i = 0; i < node.items.length; i++) {\n\t\t\tchildren.push(nodeToTreeNode(node.items[i]));\n\t\t}\n\n\t\treturn createTreeNode(node, children);\n\t}\n\n\treturn createTreeNode(node);\n};\n\nconst textNodeSpansToTreeNodeChildren = (\n\tspans: RTInlineNode[],\n\tnode: RTTextNode,\n\tparentSpan?: RTInlineNode,\n): TreeNode[] => {\n\tif (!spans.length) {\n\t\treturn [createTextTreeNode(node.text)];\n\t}\n\n\tconst mutSpans: RTInlineNode[] = spans.slice(0);\n\n\t// Sort spans using the following criteria:\n\t//\n\t// 1. By start index (ascending)\n\t// 2. If start indices are equal, by end index (descending)\n\t//\n\t// If start and end indices of more than one span are equal, use what\n\t// the API gives without modifications.\n\t//\n\t// Sorting using this algorithm ensures proper detection of child\n\t// spans.\n\tmutSpans.sort((a, b) => a.start - b.start || b.end - a.end);\n\n\tconst children: TreeNode[] = [];\n\n\tfor (let i = 0; i < mutSpans.length; i++) {\n\t\tconst span = mutSpans[i];\n\t\tconst parentSpanStart = (parentSpan && parentSpan.start) || 0;\n\t\tconst spanStart = span.start - parentSpanStart;\n\t\tconst spanEnd = span.end - parentSpanStart;\n\t\tconst text = node.text.slice(spanStart, spanEnd);\n\n\t\tconst childSpans: RTInlineNode[] = [];\n\t\tfor (let j = i; j < mutSpans.length; j++) {\n\t\t\tconst siblingSpan = mutSpans[j];\n\n\t\t\tif (siblingSpan !== span) {\n\t\t\t\tif (siblingSpan.start >= span.start && siblingSpan.end <= span.end) {\n\t\t\t\t\tchildSpans.push(siblingSpan);\n\t\t\t\t\tmutSpans.splice(j, 1);\n\t\t\t\t\tj--;\n\t\t\t\t} else if (\n\t\t\t\t\tsiblingSpan.start < span.end &&\n\t\t\t\t\tsiblingSpan.end > span.start\n\t\t\t\t) {\n\t\t\t\t\tchildSpans.push({\n\t\t\t\t\t\t...siblingSpan,\n\t\t\t\t\t\tend: span.end,\n\t\t\t\t\t});\n\t\t\t\t\tmutSpans[j] = {\n\t\t\t\t\t\t...siblingSpan,\n\t\t\t\t\t\tstart: span.end,\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (i === 0 && spanStart > 0) {\n\t\t\tchildren.push(createTextTreeNode(node.text.slice(0, spanStart)));\n\t\t}\n\n\t\tconst spanWithText = { ...span, text };\n\t\tchildren.push(\n\t\t\tcreateTreeNode(\n\t\t\t\tspanWithText,\n\t\t\t\ttextNodeSpansToTreeNodeChildren(\n\t\t\t\t\tchildSpans,\n\t\t\t\t\t{\n\t\t\t\t\t\t...node,\n\t\t\t\t\t\ttext,\n\t\t\t\t\t},\n\t\t\t\t\tspan,\n\t\t\t\t),\n\t\t\t),\n\t\t);\n\n\t\tif (spanEnd < node.text.length) {\n\t\t\tchildren.push(\n\t\t\t\tcreateTextTreeNode(\n\t\t\t\t\tnode.text.slice(\n\t\t\t\t\t\tspanEnd,\n\t\t\t\t\t\tmutSpans[i + 1]\n\t\t\t\t\t\t\t? mutSpans[i + 1].start - parentSpanStart\n\t\t\t\t\t\t\t: undefined,\n\t\t\t\t\t),\n\t\t\t\t),\n\t\t\t);\n\t\t}\n\t}\n\n\treturn children;\n};\n"],"mappings":";;AAYA,MAAM,aAAqB;CAC1B,QAAQ,EAAE,KAAK,EAAA,CAAG,SAAS;AAC5B;AACA,KAAK,IAAI;;;;;;;;;;AAWT,MAAa,UAAU,UAA0B;CAChD,IACC,MAAM,MACJ,UACC,KAAK,SAASA,iBAAAA,iBAAiB,YAC/B,KAAK,SAASA,iBAAAA,iBAAiB,eAC/B,KAAK,SAAS,KAAK,CACtB,GAEA,OAAO,kBAAkB,KAAK;CAE/B,MAAM,gBAAgB,aAAa,KAAK;CAExC,MAAM,WAAuB,CAAC;CAC9B,KAAK,IAAI,IAAI,GAAG,IAAI,cAAc,QAAQ,KACzC,SAAS,KAAK,eAAe,cAAc,EAAE,CAAC;CAG/C,OAAO;EACN,KAAK,KAAK;EACV;CACD;AACD;AAEA,MAAM,qBAAqB,UAA0B;CACpD,MAAM,WAAuB,CAAC;CAC9B,MAAM,SAAoD,CAAC;CAC3D,KAAK,MAAM,QAAQ,OAAO;EACzB,IACC,KAAK,SAASA,iBAAAA,iBAAiB,YAC/B,KAAK,SAASA,iBAAAA,iBAAiB,WAC9B;GACD,OAAO,SAAS;GAChB,SAAS,KAAK,eAAe,IAAI,CAAC;GAClC;EACD;EACA,MAAM,YAAY,KAAK,SAAS;EAChC,MAAM,QACL,OAAO,cAAc,SAAS,KAAK,aAAa,IAC7C,KAAK,IAAI,WAAW,OAAO,MAAM,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,KAAK,QAAQ;EAC/B,MAAM,UAAU,KAAK,MAAM;EAC3B,MAAM,OAAO,KAAK,KAAK,MAAM,WAAW,OAAO;EAE/C,MAAM,aAA6B,CAAC;EACpC,KAAK,IAAI,IAAI,GAAG,IAAI,SAAS,QAAQ,KAAK;GACzC,MAAM,cAAc,SAAS;GAE7B,IAAI,gBAAgB,MAAM;IACzB,IAAI,YAAY,SAAS,KAAK,SAAS,YAAY,OAAO,KAAK,KAAK;KACnE,WAAW,KAAK,WAAW;KAC3B,SAAS,OAAO,GAAG,CAAC;KACpB;IACD,OAAO,IACN,YAAY,QAAQ,KAAK,OACzB,YAAY,MAAM,KAAK,OACtB;KACD,WAAW,KAAK;MACf,GAAG;MACH,KAAK,KAAK;KACX,CAAC;KACD,SAAS,KAAK;MACb,GAAG;MACH,OAAO,KAAK;KACb;IACD;GACD;EACD;EAEA,IAAI,MAAM,KAAK,YAAY,GAC1B,SAAS,KAAK,mBAAmB,KAAK,KAAK,MAAM,GAAG,SAAS,CAAC,CAAC;EAGhE,MAAM,eAAe;GAAE,GAAG;GAAM;EAAK;EACrC,SAAS,KACR,eACC,cACA,gCACC,YACA;GACC,GAAG;GACH;EACD,GACA,IACD,CACD,CACD;EAEA,IAAI,UAAU,KAAK,KAAK,QACvB,SAAS,KACR,mBACC,KAAK,KAAK,MACT,SACA,SAAS,IAAI,KACV,SAAS,IAAI,EAAE,CAAC,QAAQ,kBACxB,KAAA,CACJ,CACD,CACD;CAEF;CAEA,OAAO;AACR"}
|
package/dist/richtext/asTree.js
CHANGED
|
@@ -14,6 +14,7 @@ uuid.i = 0;
|
|
|
14
14
|
* @returns Tree from given rich text or title field
|
|
15
15
|
*/
|
|
16
16
|
const asTree = (nodes) => {
|
|
17
|
+
if (nodes.some((node) => (node.type === RichTextNodeType.listItem || node.type === RichTextNodeType.oListItem) && (node.depth ?? 0) > 0)) return nestedListsToTree(nodes);
|
|
17
18
|
const preparedNodes = prepareNodes(nodes);
|
|
18
19
|
const children = [];
|
|
19
20
|
for (let i = 0; i < preparedNodes.length; i++) children.push(nodeToTreeNode(preparedNodes[i]));
|
|
@@ -22,6 +23,43 @@ const asTree = (nodes) => {
|
|
|
22
23
|
children
|
|
23
24
|
};
|
|
24
25
|
};
|
|
26
|
+
const nestedListsToTree = (nodes) => {
|
|
27
|
+
const children = [];
|
|
28
|
+
const levels = [];
|
|
29
|
+
for (const node of nodes) {
|
|
30
|
+
if (node.type !== RichTextNodeType.listItem && node.type !== RichTextNodeType.oListItem) {
|
|
31
|
+
levels.length = 0;
|
|
32
|
+
children.push(nodeToTreeNode(node));
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
const requested = node.depth ?? 0;
|
|
36
|
+
const depth = Number.isSafeInteger(requested) && requested >= 0 ? Math.min(requested, levels.length) : 0;
|
|
37
|
+
levels.length = Math.min(levels.length, depth + 1);
|
|
38
|
+
const listType = node.type === RichTextNodeType.listItem ? RichTextNodeType.list : RichTextNodeType.oList;
|
|
39
|
+
let level = levels[depth];
|
|
40
|
+
if (!level || level.list.type !== listType) {
|
|
41
|
+
const list = createTreeNode(listType === RichTextNodeType.list ? {
|
|
42
|
+
type: listType,
|
|
43
|
+
items: []
|
|
44
|
+
} : {
|
|
45
|
+
type: listType,
|
|
46
|
+
items: []
|
|
47
|
+
});
|
|
48
|
+
(depth === 0 ? children : levels[depth - 1].lastItem.children).push(list);
|
|
49
|
+
level = { list };
|
|
50
|
+
levels[depth] = level;
|
|
51
|
+
}
|
|
52
|
+
const item = nodeToTreeNode(node);
|
|
53
|
+
level.list.children.push(item);
|
|
54
|
+
if (level.list.node.type === RichTextNodeType.list && node.type === RichTextNodeType.listItem) level.list.node.items.push(node);
|
|
55
|
+
if (level.list.node.type === RichTextNodeType.oList && node.type === RichTextNodeType.oListItem) level.list.node.items.push(node);
|
|
56
|
+
level.lastItem = item;
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
key: uuid(),
|
|
60
|
+
children
|
|
61
|
+
};
|
|
62
|
+
};
|
|
25
63
|
const createTreeNode = (node, children = []) => {
|
|
26
64
|
return {
|
|
27
65
|
key: uuid(),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"asTree.js","names":[],"sources":["../../src/richtext/asTree.ts"],"sourcesContent":["import type {\n\tRTAnyNode,\n\tRTBlockNode,\n\tRTInlineNode,\n\tRTListItemNode,\n\tRTNode,\n\tRTOListItemNode,\n\tRTTextNode,\n} from \"../types/value/richText\"\nimport { RichTextNodeType } from \"../types/value/richText\"\nimport type { Tree, TreeNode } from \"./types\"\n\nconst uuid = (): string => {\n\treturn (++uuid.i).toString()\n}\nuuid.i = 0\n\n/**\n * Parses a rich text or title field into a tree\n *\n * @remarks\n * This is a low level helper mainly intended to be used by higher level packages. Most users\n * aren't expected to this function directly.\n * @param nodes - A rich text or title field from Prismic\n * @returns Tree from given rich text or title field\n */\nexport const asTree = (nodes: RTNode[]): Tree => {\n\tconst preparedNodes = prepareNodes(nodes)\n\n\tconst children: TreeNode[] = []\n\tfor (let i = 0; i < preparedNodes.length; i++) {\n\t\tchildren.push(nodeToTreeNode(preparedNodes[i]))\n\t}\n\n\treturn {\n\t\tkey: uuid(),\n\t\tchildren,\n\t}\n}\n\nconst createTreeNode = (node: RTAnyNode, children: TreeNode[] = []): TreeNode => {\n\treturn {\n\t\tkey: uuid(),\n\t\ttype: node.type,\n\t\ttext: \"text\" in node ? node.text : undefined,\n\t\tnode,\n\t\tchildren,\n\t}\n}\n\nconst createTextTreeNode = (text: string): TreeNode => {\n\treturn createTreeNode({\n\t\ttype: RichTextNodeType.span,\n\t\ttext,\n\t\tspans: [],\n\t})\n}\n\nconst prepareNodes = (nodes: RTNode[]): RTBlockNode[] => {\n\tconst mutNodes: RTBlockNode[] = nodes.slice(0)\n\n\tfor (let i = 0; i < mutNodes.length; i++) {\n\t\tconst node = mutNodes[i]\n\n\t\tif (node.type === RichTextNodeType.listItem || node.type === RichTextNodeType.oListItem) {\n\t\t\tconst items: (RTListItemNode | RTOListItemNode)[] = [node as RTListItemNode | RTOListItemNode]\n\n\t\t\twhile (mutNodes[i + 1] && mutNodes[i + 1].type === node.type) {\n\t\t\t\titems.push(mutNodes[i + 1] as RTListItemNode | RTOListItemNode)\n\t\t\t\tmutNodes.splice(i, 1)\n\t\t\t}\n\n\t\t\tif (node.type === RichTextNodeType.listItem) {\n\t\t\t\tmutNodes[i] = {\n\t\t\t\t\ttype: RichTextNodeType.list,\n\t\t\t\t\titems: items as RTListItemNode[],\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tmutNodes[i] = {\n\t\t\t\t\ttype: RichTextNodeType.oList,\n\t\t\t\t\titems: items as RTOListItemNode[],\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\treturn mutNodes\n}\n\nconst nodeToTreeNode = (node: RTBlockNode): TreeNode => {\n\tif (\"text\" in node) {\n\t\treturn createTreeNode(node, textNodeSpansToTreeNodeChildren(node.spans, node))\n\t}\n\n\tif (\"items\" in node) {\n\t\tconst children: TreeNode[] = []\n\t\tfor (let i = 0; i < node.items.length; i++) {\n\t\t\tchildren.push(nodeToTreeNode(node.items[i]))\n\t\t}\n\n\t\treturn createTreeNode(node, children)\n\t}\n\n\treturn createTreeNode(node)\n}\n\nconst textNodeSpansToTreeNodeChildren = (\n\tspans: RTInlineNode[],\n\tnode: RTTextNode,\n\tparentSpan?: RTInlineNode,\n): TreeNode[] => {\n\tif (!spans.length) {\n\t\treturn [createTextTreeNode(node.text)]\n\t}\n\n\tconst mutSpans: RTInlineNode[] = spans.slice(0)\n\n\t// Sort spans using the following criteria:\n\t//\n\t// 1. By start index (ascending)\n\t// 2. If start indices are equal, by end index (descending)\n\t//\n\t// If start and end indices of more than one span are equal, use what\n\t// the API gives without modifications.\n\t//\n\t// Sorting using this algorithm ensures proper detection of child\n\t// spans.\n\tmutSpans.sort((a, b) => a.start - b.start || b.end - a.end)\n\n\tconst children: TreeNode[] = []\n\n\tfor (let i = 0; i < mutSpans.length; i++) {\n\t\tconst span = mutSpans[i]\n\t\tconst parentSpanStart = (parentSpan && parentSpan.start) || 0\n\t\tconst spanStart = span.start - parentSpanStart\n\t\tconst spanEnd = span.end - parentSpanStart\n\t\tconst text = node.text.slice(spanStart, spanEnd)\n\n\t\tconst childSpans: RTInlineNode[] = []\n\t\tfor (let j = i; j < mutSpans.length; j++) {\n\t\t\tconst siblingSpan = mutSpans[j]\n\n\t\t\tif (siblingSpan !== span) {\n\t\t\t\tif (siblingSpan.start >= span.start && siblingSpan.end <= span.end) {\n\t\t\t\t\tchildSpans.push(siblingSpan)\n\t\t\t\t\tmutSpans.splice(j, 1)\n\t\t\t\t\tj--\n\t\t\t\t} else if (siblingSpan.start < span.end && siblingSpan.end > span.start) {\n\t\t\t\t\tchildSpans.push({\n\t\t\t\t\t\t...siblingSpan,\n\t\t\t\t\t\tend: span.end,\n\t\t\t\t\t})\n\t\t\t\t\tmutSpans[j] = {\n\t\t\t\t\t\t...siblingSpan,\n\t\t\t\t\t\tstart: span.end,\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (i === 0 && spanStart > 0) {\n\t\t\tchildren.push(createTextTreeNode(node.text.slice(0, spanStart)))\n\t\t}\n\n\t\tconst spanWithText = { ...span, text }\n\t\tchildren.push(\n\t\t\tcreateTreeNode(\n\t\t\t\tspanWithText,\n\t\t\t\ttextNodeSpansToTreeNodeChildren(\n\t\t\t\t\tchildSpans,\n\t\t\t\t\t{\n\t\t\t\t\t\t...node,\n\t\t\t\t\t\ttext,\n\t\t\t\t\t},\n\t\t\t\t\tspan,\n\t\t\t\t),\n\t\t\t),\n\t\t)\n\n\t\tif (spanEnd < node.text.length) {\n\t\t\tchildren.push(\n\t\t\t\tcreateTextTreeNode(\n\t\t\t\t\tnode.text.slice(\n\t\t\t\t\t\tspanEnd,\n\t\t\t\t\t\tmutSpans[i + 1] ? mutSpans[i + 1].start - parentSpanStart : undefined,\n\t\t\t\t\t),\n\t\t\t\t),\n\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AS,KAAK,mBAAmB,KAAK,KAAK,MAAM,GAAG,SAAS,CAAC,CAAC;EAGhE,MAAM,eAAe;GAAE,GAAG;GAAM;EAAK;EACrC,SAAS,KACR,eACC,cACA,gCACC,YACA;GACC,GAAG;GACH;EACD,GACA,IACD,CACD,CACD;EAEA,IAAI,UAAU,KAAK,KAAK,QACvB,SAAS,KACR,mBACC,KAAK,KAAK,MACT,SACA,SAAS,IAAI,KAAK,SAAS,IAAI,EAAE,CAAC,QAAQ,kBAAkB,KAAA,CAC7D,CACD,CACD;CAEF;CAEA,OAAO;AACR"}
|
|
1
|
+
{"version":3,"file":"asTree.js","names":[],"sources":["../../src/richtext/asTree.ts"],"sourcesContent":["import type {\n\tRTAnyNode,\n\tRTBlockNode,\n\tRTInlineNode,\n\tRTListItemNode,\n\tRTNode,\n\tRTOListItemNode,\n\tRTTextNode,\n} from \"../types/value/richText\";\nimport { RichTextNodeType } from \"../types/value/richText\";\nimport type { Tree, TreeNode } from \"./types\";\n\nconst uuid = (): string => {\n\treturn (++uuid.i).toString();\n};\nuuid.i = 0;\n\n/**\n * Parses a rich text or title field into a tree\n *\n * @remarks\n * This is a low level helper mainly intended to be used by higher level packages. Most users\n * aren't expected to this function directly.\n * @param nodes - A rich text or title field from Prismic\n * @returns Tree from given rich text or title field\n */\nexport const asTree = (nodes: RTNode[]): Tree => {\n\tif (\n\t\tnodes.some(\n\t\t\t(node) =>\n\t\t\t\t(node.type === RichTextNodeType.listItem ||\n\t\t\t\t\tnode.type === RichTextNodeType.oListItem) &&\n\t\t\t\t(node.depth ?? 0) > 0,\n\t\t)\n\t) {\n\t\treturn nestedListsToTree(nodes);\n\t}\n\tconst preparedNodes = prepareNodes(nodes);\n\n\tconst children: TreeNode[] = [];\n\tfor (let i = 0; i < preparedNodes.length; i++) {\n\t\tchildren.push(nodeToTreeNode(preparedNodes[i]));\n\t}\n\n\treturn {\n\t\tkey: uuid(),\n\t\tchildren,\n\t};\n};\n\nconst nestedListsToTree = (nodes: RTNode[]): Tree => {\n\tconst children: TreeNode[] = [];\n\tconst levels: { list: TreeNode; lastItem?: TreeNode }[] = [];\n\tfor (const node of nodes) {\n\t\tif (\n\t\t\tnode.type !== RichTextNodeType.listItem &&\n\t\t\tnode.type !== RichTextNodeType.oListItem\n\t\t) {\n\t\t\tlevels.length = 0;\n\t\t\tchildren.push(nodeToTreeNode(node));\n\t\t\tcontinue;\n\t\t}\n\t\tconst requested = node.depth ?? 0;\n\t\tconst depth =\n\t\t\tNumber.isSafeInteger(requested) && requested >= 0\n\t\t\t\t? Math.min(requested, levels.length)\n\t\t\t\t: 0;\n\t\tlevels.length = Math.min(levels.length, depth + 1);\n\t\tconst listType =\n\t\t\tnode.type === RichTextNodeType.listItem\n\t\t\t\t? RichTextNodeType.list\n\t\t\t\t: RichTextNodeType.oList;\n\t\tlet level = levels[depth];\n\t\tif (!level || level.list.type !== listType) {\n\t\t\tconst list = createTreeNode(\n\t\t\t\tlistType === RichTextNodeType.list\n\t\t\t\t\t? { type: listType, items: [] }\n\t\t\t\t\t: { type: listType, items: [] },\n\t\t\t);\n\t\t\tconst siblings =\n\t\t\t\tdepth === 0 ? children : levels[depth - 1].lastItem!.children;\n\t\t\tsiblings.push(list);\n\t\t\tlevel = { list };\n\t\t\tlevels[depth] = level;\n\t\t}\n\t\tconst item = nodeToTreeNode(node);\n\t\tlevel.list.children.push(item);\n\t\tif (\n\t\t\tlevel.list.node.type === RichTextNodeType.list &&\n\t\t\tnode.type === RichTextNodeType.listItem\n\t\t)\n\t\t\tlevel.list.node.items.push(node);\n\t\tif (\n\t\t\tlevel.list.node.type === RichTextNodeType.oList &&\n\t\t\tnode.type === RichTextNodeType.oListItem\n\t\t)\n\t\t\tlevel.list.node.items.push(node);\n\t\tlevel.lastItem = item;\n\t}\n\treturn { key: uuid(), children };\n};\n\nconst createTreeNode = (\n\tnode: RTAnyNode,\n\tchildren: TreeNode[] = [],\n): TreeNode => {\n\treturn {\n\t\tkey: uuid(),\n\t\ttype: node.type,\n\t\ttext: \"text\" in node ? node.text : undefined,\n\t\tnode,\n\t\tchildren,\n\t};\n};\n\nconst createTextTreeNode = (text: string): TreeNode => {\n\treturn createTreeNode({\n\t\ttype: RichTextNodeType.span,\n\t\ttext,\n\t\tspans: [],\n\t});\n};\n\nconst prepareNodes = (nodes: RTNode[]): RTBlockNode[] => {\n\tconst mutNodes: RTBlockNode[] = nodes.slice(0);\n\n\tfor (let i = 0; i < mutNodes.length; i++) {\n\t\tconst node = mutNodes[i];\n\n\t\tif (\n\t\t\tnode.type === RichTextNodeType.listItem ||\n\t\t\tnode.type === RichTextNodeType.oListItem\n\t\t) {\n\t\t\tconst items: (RTListItemNode | RTOListItemNode)[] = [\n\t\t\t\tnode as RTListItemNode | RTOListItemNode,\n\t\t\t];\n\n\t\t\twhile (mutNodes[i + 1] && mutNodes[i + 1].type === node.type) {\n\t\t\t\titems.push(mutNodes[i + 1] as RTListItemNode | RTOListItemNode);\n\t\t\t\tmutNodes.splice(i, 1);\n\t\t\t}\n\n\t\t\tif (node.type === RichTextNodeType.listItem) {\n\t\t\t\tmutNodes[i] = {\n\t\t\t\t\ttype: RichTextNodeType.list,\n\t\t\t\t\titems: items as RTListItemNode[],\n\t\t\t\t};\n\t\t\t} else {\n\t\t\t\tmutNodes[i] = {\n\t\t\t\t\ttype: RichTextNodeType.oList,\n\t\t\t\t\titems: items as RTOListItemNode[],\n\t\t\t\t};\n\t\t\t}\n\t\t}\n\t}\n\n\treturn mutNodes;\n};\n\nconst nodeToTreeNode = (node: RTBlockNode): TreeNode => {\n\tif (\"text\" in node) {\n\t\treturn createTreeNode(\n\t\t\tnode,\n\t\t\ttextNodeSpansToTreeNodeChildren(node.spans, node),\n\t\t);\n\t}\n\n\tif (\"items\" in node) {\n\t\tconst children: TreeNode[] = [];\n\t\tfor (let i = 0; i < node.items.length; i++) {\n\t\t\tchildren.push(nodeToTreeNode(node.items[i]));\n\t\t}\n\n\t\treturn createTreeNode(node, children);\n\t}\n\n\treturn createTreeNode(node);\n};\n\nconst textNodeSpansToTreeNodeChildren = (\n\tspans: RTInlineNode[],\n\tnode: RTTextNode,\n\tparentSpan?: RTInlineNode,\n): TreeNode[] => {\n\tif (!spans.length) {\n\t\treturn [createTextTreeNode(node.text)];\n\t}\n\n\tconst mutSpans: RTInlineNode[] = spans.slice(0);\n\n\t// Sort spans using the following criteria:\n\t//\n\t// 1. By start index (ascending)\n\t// 2. If start indices are equal, by end index (descending)\n\t//\n\t// If start and end indices of more than one span are equal, use what\n\t// the API gives without modifications.\n\t//\n\t// Sorting using this algorithm ensures proper detection of child\n\t// spans.\n\tmutSpans.sort((a, b) => a.start - b.start || b.end - a.end);\n\n\tconst children: TreeNode[] = [];\n\n\tfor (let i = 0; i < mutSpans.length; i++) {\n\t\tconst span = mutSpans[i];\n\t\tconst parentSpanStart = (parentSpan && parentSpan.start) || 0;\n\t\tconst spanStart = span.start - parentSpanStart;\n\t\tconst spanEnd = span.end - parentSpanStart;\n\t\tconst text = node.text.slice(spanStart, spanEnd);\n\n\t\tconst childSpans: RTInlineNode[] = [];\n\t\tfor (let j = i; j < mutSpans.length; j++) {\n\t\t\tconst siblingSpan = mutSpans[j];\n\n\t\t\tif (siblingSpan !== span) {\n\t\t\t\tif (siblingSpan.start >= span.start && siblingSpan.end <= span.end) {\n\t\t\t\t\tchildSpans.push(siblingSpan);\n\t\t\t\t\tmutSpans.splice(j, 1);\n\t\t\t\t\tj--;\n\t\t\t\t} else if (\n\t\t\t\t\tsiblingSpan.start < span.end &&\n\t\t\t\t\tsiblingSpan.end > span.start\n\t\t\t\t) {\n\t\t\t\t\tchildSpans.push({\n\t\t\t\t\t\t...siblingSpan,\n\t\t\t\t\t\tend: span.end,\n\t\t\t\t\t});\n\t\t\t\t\tmutSpans[j] = {\n\t\t\t\t\t\t...siblingSpan,\n\t\t\t\t\t\tstart: span.end,\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (i === 0 && spanStart > 0) {\n\t\t\tchildren.push(createTextTreeNode(node.text.slice(0, spanStart)));\n\t\t}\n\n\t\tconst spanWithText = { ...span, text };\n\t\tchildren.push(\n\t\t\tcreateTreeNode(\n\t\t\t\tspanWithText,\n\t\t\t\ttextNodeSpansToTreeNodeChildren(\n\t\t\t\t\tchildSpans,\n\t\t\t\t\t{\n\t\t\t\t\t\t...node,\n\t\t\t\t\t\ttext,\n\t\t\t\t\t},\n\t\t\t\t\tspan,\n\t\t\t\t),\n\t\t\t),\n\t\t);\n\n\t\tif (spanEnd < node.text.length) {\n\t\t\tchildren.push(\n\t\t\t\tcreateTextTreeNode(\n\t\t\t\t\tnode.text.slice(\n\t\t\t\t\t\tspanEnd,\n\t\t\t\t\t\tmutSpans[i + 1]\n\t\t\t\t\t\t\t? mutSpans[i + 1].start - parentSpanStart\n\t\t\t\t\t\t\t: undefined,\n\t\t\t\t\t),\n\t\t\t\t),\n\t\t\t);\n\t\t}\n\t}\n\n\treturn children;\n};\n"],"mappings":";;AAYA,MAAM,aAAqB;CAC1B,QAAQ,EAAE,KAAK,EAAA,CAAG,SAAS;AAC5B;AACA,KAAK,IAAI;;;;;;;;;;AAWT,MAAa,UAAU,UAA0B;CAChD,IACC,MAAM,MACJ,UACC,KAAK,SAAS,iBAAiB,YAC/B,KAAK,SAAS,iBAAiB,eAC/B,KAAK,SAAS,KAAK,CACtB,GAEA,OAAO,kBAAkB,KAAK;CAE/B,MAAM,gBAAgB,aAAa,KAAK;CAExC,MAAM,WAAuB,CAAC;CAC9B,KAAK,IAAI,IAAI,GAAG,IAAI,cAAc,QAAQ,KACzC,SAAS,KAAK,eAAe,cAAc,EAAE,CAAC;CAG/C,OAAO;EACN,KAAK,KAAK;EACV;CACD;AACD;AAEA,MAAM,qBAAqB,UAA0B;CACpD,MAAM,WAAuB,CAAC;CAC9B,MAAM,SAAoD,CAAC;CAC3D,KAAK,MAAM,QAAQ,OAAO;EACzB,IACC,KAAK,SAAS,iBAAiB,YAC/B,KAAK,SAAS,iBAA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SAAS;EACtB,MAAM,kBAAmB,cAAc,WAAW,SAAU;EAC5D,MAAM,YAAY,KAAK,QAAQ;EAC/B,MAAM,UAAU,KAAK,MAAM;EAC3B,MAAM,OAAO,KAAK,KAAK,MAAM,WAAW,OAAO;EAE/C,MAAM,aAA6B,CAAC;EACpC,KAAK,IAAI,IAAI,GAAG,IAAI,SAAS,QAAQ,KAAK;GACzC,MAAM,cAAc,SAAS;GAE7B,IAAI,gBAAgB,MAAM;IACzB,IAAI,YAAY,SAAS,KAAK,SAAS,YAAY,OAAO,KAAK,KAAK;KACnE,WAAW,KAAK,WAAW;KAC3B,SAAS,OAAO,GAAG,CAAC;KACpB;IACD,OAAO,IACN,YAAY,QAAQ,KAAK,OACzB,YAAY,MAAM,KAAK,OACtB;KACD,WAAW,KAAK;MACf,GAAG;MACH,KAAK,KAAK;KACX,CAAC;KACD,SAAS,KAAK;MACb,GAAG;MACH,OAAO,KAAK;KACb;IACD;GACD;EACD;EAEA,IAAI,MAAM,KAAK,YAAY,GAC1B,SAAS,KAAK,mBAAmB,KAAK,KAAK,MAAM,GAAG,SAAS,CAAC,CAAC;EAGhE,MAAM,eAAe;GAAE,GAAG;GAAM;EAAK;EACrC,SAAS,KACR,eACC,cACA,gCACC,YACA;GACC,GAAG;GACH;EACD,GACA,IACD,CACD,CACD;EAEA,IAAI,UAAU,KAAK,KAAK,QACvB,SAAS,KACR,mBACC,KAAK,KAAK,MACT,SACA,SAAS,IAAI,KACV,SAAS,IAAI,EAAE,CAAC,QAAQ,kBACxB,KAAA,CACJ,CACD,CACD;CAEF;CAEA,OAAO;AACR"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"richText.cjs","names":[],"sources":["../../../src/types/value/richText.ts"],"sourcesContent":["import type { EmbedField } from \"./embed\"\nimport type { FilledLinkField } from \"./link\"\nimport type { FieldState } from \"./types\"\n\n/**\n * Types enum for RichTextNodes\n *\n * @see More details: {@link https://prismic.io/docs/rich-text-title}\n */\nexport const RichTextNodeType = {\n\theading1: \"heading1\",\n\theading2: \"heading2\",\n\theading3: \"heading3\",\n\theading4: \"heading4\",\n\theading5: \"heading5\",\n\theading6: \"heading6\",\n\tparagraph: \"paragraph\",\n\tpreformatted: \"preformatted\",\n\tstrong: \"strong\",\n\tem: \"em\",\n\tlistItem: \"list-item\",\n\toListItem: \"o-list-item\",\n\tlist: \"group-list-item\",\n\toList: \"group-o-list-item\",\n\timage: \"image\",\n\tembed: \"embed\",\n\thyperlink: \"hyperlink\",\n\tlabel: \"label\",\n\tspan: \"span\",\n} as const\n\n/**\n * Types for RichTextNodes\n *\n * @see More details: {@link https://prismic.io/docs/rich-text-title}\n */\nexport type RichTextNodeTypes = (typeof RichTextNodeType)[keyof typeof RichTextNodeType]\n\n// Text nodes\n\n/** Base to be extended by other rich text nodes. */\nexport interface RTTextNodeBase {\n\ttext: string\n\tspans: RTInlineNode[]\n\tdirection?: \"ltr\" | \"rtl\"\n}\n\n/** Rich text `heading1` node */\nexport interface RTHeading1Node extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.heading1\n}\n\n/** Rich text `heading2` node */\nexport interface RTHeading2Node extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.heading2\n}\n\n/** Rich text `heading3` node */\nexport interface RTHeading3Node extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.heading3\n}\n\n/** Rich text `heading4` node */\nexport interface RTHeading4Node extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.heading4\n}\n\n/** Rich text `heading5` node */\nexport interface RTHeading5Node extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.heading5\n}\n\n/** Rich text `heading6` node */\nexport interface RTHeading6Node extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.heading6\n}\n\n/** Rich text `paragraph` node */\nexport interface RTParagraphNode extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.paragraph\n}\n\n/** Rich text `preformatted` node */\nexport interface RTPreformattedNode extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.preformatted\n}\n\n/** Rich text `list-item` node */\nexport interface RTListItemNode extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.listItem\n}\n\n/** Rich text `o-list-item` node for ordered lists */\nexport interface RTOListItemNode extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.oListItem\n}\n\n// Span nodes\n\n/** @internal Span Node base to be extended for other Span nodes */\nexport interface RTSpanNodeBase {\n\tstart: number\n\tend: number\n}\n/** Rich text `strong` node */\nexport interface RTStrongNode extends RTSpanNodeBase {\n\ttype: typeof RichTextNodeType.strong\n}\n\n/** Rich text `embed` node */\nexport interface RTEmNode extends RTSpanNodeBase {\n\ttype: typeof RichTextNodeType.em\n}\n\n/** Rich text `label` node */\nexport interface RTLabelNode extends RTSpanNodeBase {\n\ttype: typeof RichTextNodeType.label\n\tdata: {\n\t\tlabel: string\n\t}\n}\n\n// Media nodes\n\n/** Rich text `image` nodes. They could link to other documents, external web links and media fields */\nexport type RTImageNode = {\n\ttype: typeof RichTextNodeType.image\n\tid: string\n\turl: string\n\talt: string | null\n\tcopyright: string | null\n\tdimensions: {\n\t\twidth: number\n\t\theight: number\n\t}\n\tedit: {\n\t\tx: number\n\t\ty: number\n\t\tzoom: number\n\t\tbackground: string\n\t}\n\tlinkTo?: FilledLinkField\n}\n\n/** Rich text `embed` node */\nexport type RTEmbedNode = {\n\ttype: typeof RichTextNodeType.embed\n\toembed: EmbedField\n}\n\n// Link nodes\n\n/**\n * Rich text `a` node\n *\n * @see More details: {@link https://prismic.io/docs/rich-text-title#elements-and-styles}\n */\nexport interface RTLinkNode extends RTSpanNodeBase {\n\ttype: typeof RichTextNodeType.hyperlink\n\tdata: FilledLinkField\n}\n\n// Serialization related nodes\n\n/** Rich text `list` node */\nexport interface RTListNode {\n\ttype: typeof RichTextNodeType.list\n\titems: RTListItemNode[]\n}\n\n/** Rich text o-lost node */\nexport interface RTOListNode {\n\ttype: typeof RichTextNodeType.oList\n\titems: RTOListItemNode[]\n}\n\n// This one is confusing but it's actually the inner content of a block\n/** Rich text `span` node */\nexport interface RTSpanNode extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.span\n}\n\n// Helpers\n\n/** Nodes from a rich text field */\nexport type RTNode =\n\t| RTHeading1Node\n\t| RTHeading2Node\n\t| RTHeading3Node\n\t| RTHeading4Node\n\t| RTHeading5Node\n\t| RTHeading6Node\n\t| RTParagraphNode\n\t| RTPreformattedNode\n\t| RTListItemNode\n\t| RTOListItemNode\n\t| RTImageNode\n\t| RTEmbedNode\n\n/** Rich text nodes with text */\nexport type RTTextNode =\n\t| RTHeading1Node\n\t| RTHeading2Node\n\t| RTHeading3Node\n\t| RTHeading4Node\n\t| RTHeading5Node\n\t| RTHeading6Node\n\t| RTParagraphNode\n\t| RTPreformattedNode\n\t| RTListItemNode\n\t| RTOListItemNode\n\n/** Rich text block nodes */\nexport type RTBlockNode =\n\t| RTHeading1Node\n\t| RTHeading2Node\n\t| RTHeading3Node\n\t| RTHeading4Node\n\t| RTHeading5Node\n\t| RTHeading6Node\n\t| RTParagraphNode\n\t| RTPreformattedNode\n\t| RTListItemNode\n\t| RTOListItemNode\n\t| RTListNode\n\t| RTOListNode\n\t| RTImageNode\n\t| RTEmbedNode\n\n/** Inline rich text nodes */\nexport type RTInlineNode = RTStrongNode | RTEmNode | RTLabelNode | RTLinkNode\n\n/** All rich text nodes */\nexport type RTAnyNode = RTBlockNode | RTInlineNode | RTSpanNode\n\n/**\n * A rich text field.\n *\n * @see Rich text field documentation: {@link https://prismic.io/docs/rich-text-title}\n */\nexport type RichTextField<State extends FieldState = FieldState>
|
|
1
|
+
{"version":3,"file":"richText.cjs","names":[],"sources":["../../../src/types/value/richText.ts"],"sourcesContent":["import type { EmbedField } from \"./embed\"\nimport type { FilledLinkField } from \"./link\"\nimport type { FieldState } from \"./types\"\n\n/**\n * Types enum for RichTextNodes\n *\n * @see More details: {@link https://prismic.io/docs/rich-text-title}\n */\nexport const RichTextNodeType = {\n\theading1: \"heading1\",\n\theading2: \"heading2\",\n\theading3: \"heading3\",\n\theading4: \"heading4\",\n\theading5: \"heading5\",\n\theading6: \"heading6\",\n\tparagraph: \"paragraph\",\n\tpreformatted: \"preformatted\",\n\tstrong: \"strong\",\n\tem: \"em\",\n\tlistItem: \"list-item\",\n\toListItem: \"o-list-item\",\n\tlist: \"group-list-item\",\n\toList: \"group-o-list-item\",\n\timage: \"image\",\n\tembed: \"embed\",\n\thyperlink: \"hyperlink\",\n\tlabel: \"label\",\n\tspan: \"span\",\n} as const\n\n/**\n * Types for RichTextNodes\n *\n * @see More details: {@link https://prismic.io/docs/rich-text-title}\n */\nexport type RichTextNodeTypes = (typeof RichTextNodeType)[keyof typeof RichTextNodeType]\n\n// Text nodes\n\n/** Base to be extended by other rich text nodes. */\nexport interface RTTextNodeBase {\n\ttext: string\n\tspans: RTInlineNode[]\n\tdirection?: \"ltr\" | \"rtl\"\n}\n\n/** Rich text `heading1` node */\nexport interface RTHeading1Node extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.heading1\n}\n\n/** Rich text `heading2` node */\nexport interface RTHeading2Node extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.heading2\n}\n\n/** Rich text `heading3` node */\nexport interface RTHeading3Node extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.heading3\n}\n\n/** Rich text `heading4` node */\nexport interface RTHeading4Node extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.heading4\n}\n\n/** Rich text `heading5` node */\nexport interface RTHeading5Node extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.heading5\n}\n\n/** Rich text `heading6` node */\nexport interface RTHeading6Node extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.heading6\n}\n\n/** Rich text `paragraph` node */\nexport interface RTParagraphNode extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.paragraph\n}\n\n/** Rich text `preformatted` node */\nexport interface RTPreformattedNode extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.preformatted\n}\n\n/** Rich text `list-item` node */\nexport interface RTListItemNode extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.listItem\n\t/** Indentation level; omitted for a root list item. */\n\tdepth?: number\n}\n\n/** Rich text `o-list-item` node for ordered lists */\nexport interface RTOListItemNode extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.oListItem\n\t/** Indentation level; omitted for a root list item. */\n\tdepth?: number\n}\n\n// Span nodes\n\n/** @internal Span Node base to be extended for other Span nodes */\nexport interface RTSpanNodeBase {\n\tstart: number\n\tend: number\n}\n/** Rich text `strong` node */\nexport interface RTStrongNode extends RTSpanNodeBase {\n\ttype: typeof RichTextNodeType.strong\n}\n\n/** Rich text `embed` node */\nexport interface RTEmNode extends RTSpanNodeBase {\n\ttype: typeof RichTextNodeType.em\n}\n\n/** Rich text `label` node */\nexport interface RTLabelNode extends RTSpanNodeBase {\n\ttype: typeof RichTextNodeType.label\n\tdata: {\n\t\tlabel: string\n\t}\n}\n\n// Media nodes\n\n/** Rich text `image` nodes. They could link to other documents, external web links and media fields */\nexport type RTImageNode = {\n\ttype: typeof RichTextNodeType.image\n\tid: string\n\turl: string\n\talt: string | null\n\tcopyright: string | null\n\tdimensions: {\n\t\twidth: number\n\t\theight: number\n\t}\n\tedit: {\n\t\tx: number\n\t\ty: number\n\t\tzoom: number\n\t\tbackground: string\n\t}\n\tlinkTo?: FilledLinkField\n}\n\n/** Rich text `embed` node */\nexport type RTEmbedNode = {\n\ttype: typeof RichTextNodeType.embed\n\toembed: EmbedField\n}\n\n// Link nodes\n\n/**\n * Rich text `a` node\n *\n * @see More details: {@link https://prismic.io/docs/rich-text-title#elements-and-styles}\n */\nexport interface RTLinkNode extends RTSpanNodeBase {\n\ttype: typeof RichTextNodeType.hyperlink\n\tdata: FilledLinkField\n}\n\n// Serialization related nodes\n\n/** Rich text `list` node */\nexport interface RTListNode {\n\ttype: typeof RichTextNodeType.list\n\titems: RTListItemNode[]\n}\n\n/** Rich text o-lost node */\nexport interface RTOListNode {\n\ttype: typeof RichTextNodeType.oList\n\titems: RTOListItemNode[]\n}\n\n// This one is confusing but it's actually the inner content of a block\n/** Rich text `span` node */\nexport interface RTSpanNode extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.span\n}\n\n// Helpers\n\n/** Nodes from a rich text field */\nexport type RTNode =\n\t| RTHeading1Node\n\t| RTHeading2Node\n\t| RTHeading3Node\n\t| RTHeading4Node\n\t| RTHeading5Node\n\t| RTHeading6Node\n\t| RTParagraphNode\n\t| RTPreformattedNode\n\t| RTListItemNode\n\t| RTOListItemNode\n\t| RTImageNode\n\t| RTEmbedNode\n\n/** Rich text nodes with text */\nexport type RTTextNode =\n\t| RTHeading1Node\n\t| RTHeading2Node\n\t| RTHeading3Node\n\t| RTHeading4Node\n\t| RTHeading5Node\n\t| RTHeading6Node\n\t| RTParagraphNode\n\t| RTPreformattedNode\n\t| RTListItemNode\n\t| RTOListItemNode\n\n/** Rich text block nodes */\nexport type RTBlockNode =\n\t| RTHeading1Node\n\t| RTHeading2Node\n\t| RTHeading3Node\n\t| RTHeading4Node\n\t| RTHeading5Node\n\t| RTHeading6Node\n\t| RTParagraphNode\n\t| RTPreformattedNode\n\t| RTListItemNode\n\t| RTOListItemNode\n\t| RTListNode\n\t| RTOListNode\n\t| RTImageNode\n\t| RTEmbedNode\n\n/** Inline rich text nodes */\nexport type RTInlineNode = RTStrongNode | RTEmNode | RTLabelNode | RTLinkNode\n\n/** All rich text nodes */\nexport type RTAnyNode = RTBlockNode | RTInlineNode | RTSpanNode\n\n/**\n * A rich text field.\n *\n * @see Rich text field documentation: {@link https://prismic.io/docs/rich-text-title}\n */\nexport type RichTextField<State extends FieldState = FieldState> =\n\tState extends \"empty\" ? [] : [RTNode, ...RTNode[]]\n"],"mappings":";;;;;;AASA,MAAa,mBAAmB;CAC/B,UAAU;CACV,UAAU;CACV,UAAU;CACV,UAAU;CACV,UAAU;CACV,UAAU;CACV,WAAW;CACX,cAAc;CACd,QAAQ;CACR,IAAI;CACJ,UAAU;CACV,WAAW;CACX,MAAM;CACN,OAAO;CACP,OAAO;CACP,OAAO;CACP,WAAW;CACX,OAAO;CACP,MAAM;AACP"}
|
|
@@ -75,10 +75,14 @@ export interface RTPreformattedNode extends RTTextNodeBase {
|
|
|
75
75
|
/** Rich text `list-item` node */
|
|
76
76
|
export interface RTListItemNode extends RTTextNodeBase {
|
|
77
77
|
type: typeof RichTextNodeType.listItem;
|
|
78
|
+
/** Indentation level; omitted for a root list item. */
|
|
79
|
+
depth?: number;
|
|
78
80
|
}
|
|
79
81
|
/** Rich text `o-list-item` node for ordered lists */
|
|
80
82
|
export interface RTOListItemNode extends RTTextNodeBase {
|
|
81
83
|
type: typeof RichTextNodeType.oListItem;
|
|
84
|
+
/** Indentation level; omitted for a root list item. */
|
|
85
|
+
depth?: number;
|
|
82
86
|
}
|
|
83
87
|
/** @internal Span Node base to be extended for other Span nodes */
|
|
84
88
|
export interface RTSpanNodeBase {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"richText.d.cts","names":[],"sources":["../../../src/types/value/richText.ts"],"mappings":";;;;;;;;;qBASa;WACZ;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;;;;;;;YAQW,4BAA4B,+BAA+B;;iBAKtD;EAChB;EACA,OAAO;EACP;;;iBAIgB,uBAAuB;EACvC,aAAa,iBAAiB;;;iBAId,uBAAuB;EACvC,aAAa,iBAAiB;;;iBAId,uBAAuB;EACvC,aAAa,iBAAiB;;;iBAId,uBAAuB;EACvC,aAAa,iBAAiB;;;iBAId,uBAAuB;EACvC,aAAa,iBAAiB;;;iBAId,uBAAuB;EACvC,aAAa,iBAAiB;;;iBAId,wBAAwB;EACxC,aAAa,iBAAiB;;;iBAId,2BAA2B;EAC3C,aAAa,iBAAiB;;;iBAId,uBAAuB;EACvC,aAAa,iBAAiB;;;
|
|
1
|
+
{"version":3,"file":"richText.d.cts","names":[],"sources":["../../../src/types/value/rich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BACA,iBACA,iBACA,kBACA,qBACA,iBACA,kBACA,aACA,cACA,cACA;;YAGS,eAAe,eAAe,WAAW,cAAc;;YAGvD,YAAY,cAAc,eAAe;;;;;;YAOzC,cAAc,cAAc,aAAa,cACpD,8BAA8B,WAAW"}
|
|
@@ -75,10 +75,14 @@ export interface RTPreformattedNode extends RTTextNodeBase {
|
|
|
75
75
|
/** Rich text `list-item` node */
|
|
76
76
|
export interface RTListItemNode extends RTTextNodeBase {
|
|
77
77
|
type: typeof RichTextNodeType.listItem;
|
|
78
|
+
/** Indentation level; omitted for a root list item. */
|
|
79
|
+
depth?: number;
|
|
78
80
|
}
|
|
79
81
|
/** Rich text `o-list-item` node for ordered lists */
|
|
80
82
|
export interface RTOListItemNode extends RTTextNodeBase {
|
|
81
83
|
type: typeof RichTextNodeType.oListItem;
|
|
84
|
+
/** Indentation level; omitted for a root list item. */
|
|
85
|
+
depth?: number;
|
|
82
86
|
}
|
|
83
87
|
/** @internal Span Node base to be extended for other Span nodes */
|
|
84
88
|
export interface RTSpanNodeBase {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"richText.d.ts","names":[],"sources":["../../../src/types/value/richText.ts"],"mappings":";;;;;;;;;qBASa;WACZ;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;;;;;;;YAQW,4BAA4B,+BAA+B;;iBAKtD;EAChB;EACA,OAAO;EACP;;;iBAIgB,uBAAuB;EACvC,aAAa,iBAAiB;;;iBAId,uBAAuB;EACvC,aAAa,iBAAiB;;;iBAId,uBAAuB;EACvC,aAAa,iBAAiB;;;iBAId,uBAAuB;EACvC,aAAa,iBAAiB;;;iBAId,uBAAuB;EACvC,aAAa,iBAAiB;;;iBAId,uBAAuB;EACvC,aAAa,iBAAiB;;;iBAId,wBAAwB;EACxC,aAAa,iBAAiB;;;iBAId,2BAA2B;EAC3C,aAAa,iBAAiB;;;iBAId,uBAAuB;EACvC,aAAa,iBAAiB;;;
|
|
1
|
+
{"version":3,"file":"richText.d.ts","names":[],"sources":["../../../src/types/value/richText.ts"],"mappings":";;;;;;;;;qBASa;WACZ;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;;;;;;;YAQW,4BAA4B,+BAA+B;;iBAKtD;EAChB;EACA,OAAO;EACP;;;iBAIgB,uBAAuB;EACvC,aAAa,iBAAiB;;;iBAId,uBAAuB;EACvC,aAAa,iBAAiB;;;iBAId,uBAAuB;EACvC,aAAa,iBAAiB;;;iBAId,uBAAuB;EACvC,aAAa,iBAAiB;;;iBAId,uBAAuB;EACvC,aAAa,iBAAiB;;;iBAId,uBAAuB;EACvC,aAAa,iBAAiB;;;iBAId,wBAAwB;EACxC,aAAa,iBAAiB;;;iBAId,2BAA2B;EAC3C,aAAa,iBAAiB;;;iBAId,uBAAuB;EACvC,aAAa,iBAAiB;;EAE9B;;;iBAIgB,wBAAwB;EACxC,aAAa,iBAAiB;;EAE9B;;;iBAMgB;EAChB;EACA;;;iBAGgB,qBAAqB;EACrC,aAAa,iBAAiB;;;iBAId,iBAAiB;EACjC,aAAa,iBAAiB;;;iBAId,oBAAoB;EACpC,aAAa,iBAAiB;EAC9B;IACC;;;;YAOU;EACX,aAAa,iBAAiB;EAC9B;EACA;EACA;EACA;EACA;IACC;IACA;;EAED;IACC;IACA;IACA;IACA;;EAED,SAAS;;;YAIE;EACX,aAAa,iBAAiB;EAC9B,QAAQ;;;;;;;iBAUQ,mBAAmB;EACnC,aAAa,iBAAiB;EAC9B,MAAM;;;iBAMU;EAChB,aAAa,iBAAiB;EAC9B,OAAO;;;iBAIS;EAChB,aAAa,iBAAiB;EAC9B,OAAO;;;iBAKS,mBAAmB;EACnC,aAAa,iBAAiB;;;YAMnB,SACT,iBACA,iBACA,iBACA,iBACA,iBACA,iBACA,kBACA,qBACA,iBACA,kBACA,cACA;;YAGS,aACT,iBACA,iBACA,iBACA,iBACA,iBACA,iBACA,kBACA,qBACA,iBACA;;YAGS,cACT,iBACA,iBACA,iBACA,iBACA,iBACA,iBACA,kBACA,qBACA,iBACA,kBACA,aACA,cACA,cACA;;YAGS,eAAe,eAAe,WAAW,cAAc;;YAGvD,YAAY,cAAc,eAAe;;;;;;YAOzC,cAAc,cAAc,aAAa,cACpD,8BAA8B,WAAW"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"richText.js","names":[],"sources":["../../../src/types/value/richText.ts"],"sourcesContent":["import type { EmbedField } from \"./embed\"\nimport type { FilledLinkField } from \"./link\"\nimport type { FieldState } from \"./types\"\n\n/**\n * Types enum for RichTextNodes\n *\n * @see More details: {@link https://prismic.io/docs/rich-text-title}\n */\nexport const RichTextNodeType = {\n\theading1: \"heading1\",\n\theading2: \"heading2\",\n\theading3: \"heading3\",\n\theading4: \"heading4\",\n\theading5: \"heading5\",\n\theading6: \"heading6\",\n\tparagraph: \"paragraph\",\n\tpreformatted: \"preformatted\",\n\tstrong: \"strong\",\n\tem: \"em\",\n\tlistItem: \"list-item\",\n\toListItem: \"o-list-item\",\n\tlist: \"group-list-item\",\n\toList: \"group-o-list-item\",\n\timage: \"image\",\n\tembed: \"embed\",\n\thyperlink: \"hyperlink\",\n\tlabel: \"label\",\n\tspan: \"span\",\n} as const\n\n/**\n * Types for RichTextNodes\n *\n * @see More details: {@link https://prismic.io/docs/rich-text-title}\n */\nexport type RichTextNodeTypes = (typeof RichTextNodeType)[keyof typeof RichTextNodeType]\n\n// Text nodes\n\n/** Base to be extended by other rich text nodes. */\nexport interface RTTextNodeBase {\n\ttext: string\n\tspans: RTInlineNode[]\n\tdirection?: \"ltr\" | \"rtl\"\n}\n\n/** Rich text `heading1` node */\nexport interface RTHeading1Node extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.heading1\n}\n\n/** Rich text `heading2` node */\nexport interface RTHeading2Node extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.heading2\n}\n\n/** Rich text `heading3` node */\nexport interface RTHeading3Node extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.heading3\n}\n\n/** Rich text `heading4` node */\nexport interface RTHeading4Node extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.heading4\n}\n\n/** Rich text `heading5` node */\nexport interface RTHeading5Node extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.heading5\n}\n\n/** Rich text `heading6` node */\nexport interface RTHeading6Node extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.heading6\n}\n\n/** Rich text `paragraph` node */\nexport interface RTParagraphNode extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.paragraph\n}\n\n/** Rich text `preformatted` node */\nexport interface RTPreformattedNode extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.preformatted\n}\n\n/** Rich text `list-item` node */\nexport interface RTListItemNode extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.listItem\n}\n\n/** Rich text `o-list-item` node for ordered lists */\nexport interface RTOListItemNode extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.oListItem\n}\n\n// Span nodes\n\n/** @internal Span Node base to be extended for other Span nodes */\nexport interface RTSpanNodeBase {\n\tstart: number\n\tend: number\n}\n/** Rich text `strong` node */\nexport interface RTStrongNode extends RTSpanNodeBase {\n\ttype: typeof RichTextNodeType.strong\n}\n\n/** Rich text `embed` node */\nexport interface RTEmNode extends RTSpanNodeBase {\n\ttype: typeof RichTextNodeType.em\n}\n\n/** Rich text `label` node */\nexport interface RTLabelNode extends RTSpanNodeBase {\n\ttype: typeof RichTextNodeType.label\n\tdata: {\n\t\tlabel: string\n\t}\n}\n\n// Media nodes\n\n/** Rich text `image` nodes. They could link to other documents, external web links and media fields */\nexport type RTImageNode = {\n\ttype: typeof RichTextNodeType.image\n\tid: string\n\turl: string\n\talt: string | null\n\tcopyright: string | null\n\tdimensions: {\n\t\twidth: number\n\t\theight: number\n\t}\n\tedit: {\n\t\tx: number\n\t\ty: number\n\t\tzoom: number\n\t\tbackground: string\n\t}\n\tlinkTo?: FilledLinkField\n}\n\n/** Rich text `embed` node */\nexport type RTEmbedNode = {\n\ttype: typeof RichTextNodeType.embed\n\toembed: EmbedField\n}\n\n// Link nodes\n\n/**\n * Rich text `a` node\n *\n * @see More details: {@link https://prismic.io/docs/rich-text-title#elements-and-styles}\n */\nexport interface RTLinkNode extends RTSpanNodeBase {\n\ttype: typeof RichTextNodeType.hyperlink\n\tdata: FilledLinkField\n}\n\n// Serialization related nodes\n\n/** Rich text `list` node */\nexport interface RTListNode {\n\ttype: typeof RichTextNodeType.list\n\titems: RTListItemNode[]\n}\n\n/** Rich text o-lost node */\nexport interface RTOListNode {\n\ttype: typeof RichTextNodeType.oList\n\titems: RTOListItemNode[]\n}\n\n// This one is confusing but it's actually the inner content of a block\n/** Rich text `span` node */\nexport interface RTSpanNode extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.span\n}\n\n// Helpers\n\n/** Nodes from a rich text field */\nexport type RTNode =\n\t| RTHeading1Node\n\t| RTHeading2Node\n\t| RTHeading3Node\n\t| RTHeading4Node\n\t| RTHeading5Node\n\t| RTHeading6Node\n\t| RTParagraphNode\n\t| RTPreformattedNode\n\t| RTListItemNode\n\t| RTOListItemNode\n\t| RTImageNode\n\t| RTEmbedNode\n\n/** Rich text nodes with text */\nexport type RTTextNode =\n\t| RTHeading1Node\n\t| RTHeading2Node\n\t| RTHeading3Node\n\t| RTHeading4Node\n\t| RTHeading5Node\n\t| RTHeading6Node\n\t| RTParagraphNode\n\t| RTPreformattedNode\n\t| RTListItemNode\n\t| RTOListItemNode\n\n/** Rich text block nodes */\nexport type RTBlockNode =\n\t| RTHeading1Node\n\t| RTHeading2Node\n\t| RTHeading3Node\n\t| RTHeading4Node\n\t| RTHeading5Node\n\t| RTHeading6Node\n\t| RTParagraphNode\n\t| RTPreformattedNode\n\t| RTListItemNode\n\t| RTOListItemNode\n\t| RTListNode\n\t| RTOListNode\n\t| RTImageNode\n\t| RTEmbedNode\n\n/** Inline rich text nodes */\nexport type RTInlineNode = RTStrongNode | RTEmNode | RTLabelNode | RTLinkNode\n\n/** All rich text nodes */\nexport type RTAnyNode = RTBlockNode | RTInlineNode | RTSpanNode\n\n/**\n * A rich text field.\n *\n * @see Rich text field documentation: {@link https://prismic.io/docs/rich-text-title}\n */\nexport type RichTextField<State extends FieldState = FieldState>
|
|
1
|
+
{"version":3,"file":"richText.js","names":[],"sources":["../../../src/types/value/richText.ts"],"sourcesContent":["import type { EmbedField } from \"./embed\"\nimport type { FilledLinkField } from \"./link\"\nimport type { FieldState } from \"./types\"\n\n/**\n * Types enum for RichTextNodes\n *\n * @see More details: {@link https://prismic.io/docs/rich-text-title}\n */\nexport const RichTextNodeType = {\n\theading1: \"heading1\",\n\theading2: \"heading2\",\n\theading3: \"heading3\",\n\theading4: \"heading4\",\n\theading5: \"heading5\",\n\theading6: \"heading6\",\n\tparagraph: \"paragraph\",\n\tpreformatted: \"preformatted\",\n\tstrong: \"strong\",\n\tem: \"em\",\n\tlistItem: \"list-item\",\n\toListItem: \"o-list-item\",\n\tlist: \"group-list-item\",\n\toList: \"group-o-list-item\",\n\timage: \"image\",\n\tembed: \"embed\",\n\thyperlink: \"hyperlink\",\n\tlabel: \"label\",\n\tspan: \"span\",\n} as const\n\n/**\n * Types for RichTextNodes\n *\n * @see More details: {@link https://prismic.io/docs/rich-text-title}\n */\nexport type RichTextNodeTypes = (typeof RichTextNodeType)[keyof typeof RichTextNodeType]\n\n// Text nodes\n\n/** Base to be extended by other rich text nodes. */\nexport interface RTTextNodeBase {\n\ttext: string\n\tspans: RTInlineNode[]\n\tdirection?: \"ltr\" | \"rtl\"\n}\n\n/** Rich text `heading1` node */\nexport interface RTHeading1Node extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.heading1\n}\n\n/** Rich text `heading2` node */\nexport interface RTHeading2Node extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.heading2\n}\n\n/** Rich text `heading3` node */\nexport interface RTHeading3Node extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.heading3\n}\n\n/** Rich text `heading4` node */\nexport interface RTHeading4Node extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.heading4\n}\n\n/** Rich text `heading5` node */\nexport interface RTHeading5Node extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.heading5\n}\n\n/** Rich text `heading6` node */\nexport interface RTHeading6Node extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.heading6\n}\n\n/** Rich text `paragraph` node */\nexport interface RTParagraphNode extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.paragraph\n}\n\n/** Rich text `preformatted` node */\nexport interface RTPreformattedNode extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.preformatted\n}\n\n/** Rich text `list-item` node */\nexport interface RTListItemNode extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.listItem\n\t/** Indentation level; omitted for a root list item. */\n\tdepth?: number\n}\n\n/** Rich text `o-list-item` node for ordered lists */\nexport interface RTOListItemNode extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.oListItem\n\t/** Indentation level; omitted for a root list item. */\n\tdepth?: number\n}\n\n// Span nodes\n\n/** @internal Span Node base to be extended for other Span nodes */\nexport interface RTSpanNodeBase {\n\tstart: number\n\tend: number\n}\n/** Rich text `strong` node */\nexport interface RTStrongNode extends RTSpanNodeBase {\n\ttype: typeof RichTextNodeType.strong\n}\n\n/** Rich text `embed` node */\nexport interface RTEmNode extends RTSpanNodeBase {\n\ttype: typeof RichTextNodeType.em\n}\n\n/** Rich text `label` node */\nexport interface RTLabelNode extends RTSpanNodeBase {\n\ttype: typeof RichTextNodeType.label\n\tdata: {\n\t\tlabel: string\n\t}\n}\n\n// Media nodes\n\n/** Rich text `image` nodes. They could link to other documents, external web links and media fields */\nexport type RTImageNode = {\n\ttype: typeof RichTextNodeType.image\n\tid: string\n\turl: string\n\talt: string | null\n\tcopyright: string | null\n\tdimensions: {\n\t\twidth: number\n\t\theight: number\n\t}\n\tedit: {\n\t\tx: number\n\t\ty: number\n\t\tzoom: number\n\t\tbackground: string\n\t}\n\tlinkTo?: FilledLinkField\n}\n\n/** Rich text `embed` node */\nexport type RTEmbedNode = {\n\ttype: typeof RichTextNodeType.embed\n\toembed: EmbedField\n}\n\n// Link nodes\n\n/**\n * Rich text `a` node\n *\n * @see More details: {@link https://prismic.io/docs/rich-text-title#elements-and-styles}\n */\nexport interface RTLinkNode extends RTSpanNodeBase {\n\ttype: typeof RichTextNodeType.hyperlink\n\tdata: FilledLinkField\n}\n\n// Serialization related nodes\n\n/** Rich text `list` node */\nexport interface RTListNode {\n\ttype: typeof RichTextNodeType.list\n\titems: RTListItemNode[]\n}\n\n/** Rich text o-lost node */\nexport interface RTOListNode {\n\ttype: typeof RichTextNodeType.oList\n\titems: RTOListItemNode[]\n}\n\n// This one is confusing but it's actually the inner content of a block\n/** Rich text `span` node */\nexport interface RTSpanNode extends RTTextNodeBase {\n\ttype: typeof RichTextNodeType.span\n}\n\n// Helpers\n\n/** Nodes from a rich text field */\nexport type RTNode =\n\t| RTHeading1Node\n\t| RTHeading2Node\n\t| RTHeading3Node\n\t| RTHeading4Node\n\t| RTHeading5Node\n\t| RTHeading6Node\n\t| RTParagraphNode\n\t| RTPreformattedNode\n\t| RTListItemNode\n\t| RTOListItemNode\n\t| RTImageNode\n\t| RTEmbedNode\n\n/** Rich text nodes with text */\nexport type RTTextNode =\n\t| RTHeading1Node\n\t| RTHeading2Node\n\t| RTHeading3Node\n\t| RTHeading4Node\n\t| RTHeading5Node\n\t| RTHeading6Node\n\t| RTParagraphNode\n\t| RTPreformattedNode\n\t| RTListItemNode\n\t| RTOListItemNode\n\n/** Rich text block nodes */\nexport type RTBlockNode =\n\t| RTHeading1Node\n\t| RTHeading2Node\n\t| RTHeading3Node\n\t| RTHeading4Node\n\t| RTHeading5Node\n\t| RTHeading6Node\n\t| RTParagraphNode\n\t| RTPreformattedNode\n\t| RTListItemNode\n\t| RTOListItemNode\n\t| RTListNode\n\t| RTOListNode\n\t| RTImageNode\n\t| RTEmbedNode\n\n/** Inline rich text nodes */\nexport type RTInlineNode = RTStrongNode | RTEmNode | RTLabelNode | RTLinkNode\n\n/** All rich text nodes */\nexport type RTAnyNode = RTBlockNode | RTInlineNode | RTSpanNode\n\n/**\n * A rich text field.\n *\n * @see Rich text field documentation: {@link https://prismic.io/docs/rich-text-title}\n */\nexport type RichTextField<State extends FieldState = FieldState> =\n\tState extends \"empty\" ? [] : [RTNode, ...RTNode[]]\n"],"mappings":";;;;;;AASA,MAAa,mBAAmB;CAC/B,UAAU;CACV,UAAU;CACV,UAAU;CACV,UAAU;CACV,UAAU;CACV,UAAU;CACV,WAAW;CACX,cAAc;CACd,QAAQ;CACR,IAAI;CACJ,UAAU;CACV,WAAW;CACX,MAAM;CACN,OAAO;CACP,OAAO;CACP,OAAO;CACP,WAAW;CACX,OAAO;CACP,MAAM;AACP"}
|
package/package.json
CHANGED
package/src/richtext/asTree.ts
CHANGED
|
@@ -6,14 +6,14 @@ import type {
|
|
|
6
6
|
RTNode,
|
|
7
7
|
RTOListItemNode,
|
|
8
8
|
RTTextNode,
|
|
9
|
-
} from "../types/value/richText"
|
|
10
|
-
import { RichTextNodeType } from "../types/value/richText"
|
|
11
|
-
import type { Tree, TreeNode } from "./types"
|
|
9
|
+
} from "../types/value/richText";
|
|
10
|
+
import { RichTextNodeType } from "../types/value/richText";
|
|
11
|
+
import type { Tree, TreeNode } from "./types";
|
|
12
12
|
|
|
13
13
|
const uuid = (): string => {
|
|
14
|
-
return (++uuid.i).toString()
|
|
15
|
-
}
|
|
16
|
-
uuid.i = 0
|
|
14
|
+
return (++uuid.i).toString();
|
|
15
|
+
};
|
|
16
|
+
uuid.i = 0;
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
19
|
* Parses a rich text or title field into a tree
|
|
@@ -25,84 +25,157 @@ uuid.i = 0
|
|
|
25
25
|
* @returns Tree from given rich text or title field
|
|
26
26
|
*/
|
|
27
27
|
export const asTree = (nodes: RTNode[]): Tree => {
|
|
28
|
-
|
|
28
|
+
if (
|
|
29
|
+
nodes.some(
|
|
30
|
+
(node) =>
|
|
31
|
+
(node.type === RichTextNodeType.listItem ||
|
|
32
|
+
node.type === RichTextNodeType.oListItem) &&
|
|
33
|
+
(node.depth ?? 0) > 0,
|
|
34
|
+
)
|
|
35
|
+
) {
|
|
36
|
+
return nestedListsToTree(nodes);
|
|
37
|
+
}
|
|
38
|
+
const preparedNodes = prepareNodes(nodes);
|
|
29
39
|
|
|
30
|
-
const children: TreeNode[] = []
|
|
40
|
+
const children: TreeNode[] = [];
|
|
31
41
|
for (let i = 0; i < preparedNodes.length; i++) {
|
|
32
|
-
children.push(nodeToTreeNode(preparedNodes[i]))
|
|
42
|
+
children.push(nodeToTreeNode(preparedNodes[i]));
|
|
33
43
|
}
|
|
34
44
|
|
|
35
45
|
return {
|
|
36
46
|
key: uuid(),
|
|
37
47
|
children,
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const nestedListsToTree = (nodes: RTNode[]): Tree => {
|
|
52
|
+
const children: TreeNode[] = [];
|
|
53
|
+
const levels: { list: TreeNode; lastItem?: TreeNode }[] = [];
|
|
54
|
+
for (const node of nodes) {
|
|
55
|
+
if (
|
|
56
|
+
node.type !== RichTextNodeType.listItem &&
|
|
57
|
+
node.type !== RichTextNodeType.oListItem
|
|
58
|
+
) {
|
|
59
|
+
levels.length = 0;
|
|
60
|
+
children.push(nodeToTreeNode(node));
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
const requested = node.depth ?? 0;
|
|
64
|
+
const depth =
|
|
65
|
+
Number.isSafeInteger(requested) && requested >= 0
|
|
66
|
+
? Math.min(requested, levels.length)
|
|
67
|
+
: 0;
|
|
68
|
+
levels.length = Math.min(levels.length, depth + 1);
|
|
69
|
+
const listType =
|
|
70
|
+
node.type === RichTextNodeType.listItem
|
|
71
|
+
? RichTextNodeType.list
|
|
72
|
+
: RichTextNodeType.oList;
|
|
73
|
+
let level = levels[depth];
|
|
74
|
+
if (!level || level.list.type !== listType) {
|
|
75
|
+
const list = createTreeNode(
|
|
76
|
+
listType === RichTextNodeType.list
|
|
77
|
+
? { type: listType, items: [] }
|
|
78
|
+
: { type: listType, items: [] },
|
|
79
|
+
);
|
|
80
|
+
const siblings =
|
|
81
|
+
depth === 0 ? children : levels[depth - 1].lastItem!.children;
|
|
82
|
+
siblings.push(list);
|
|
83
|
+
level = { list };
|
|
84
|
+
levels[depth] = level;
|
|
85
|
+
}
|
|
86
|
+
const item = nodeToTreeNode(node);
|
|
87
|
+
level.list.children.push(item);
|
|
88
|
+
if (
|
|
89
|
+
level.list.node.type === RichTextNodeType.list &&
|
|
90
|
+
node.type === RichTextNodeType.listItem
|
|
91
|
+
)
|
|
92
|
+
level.list.node.items.push(node);
|
|
93
|
+
if (
|
|
94
|
+
level.list.node.type === RichTextNodeType.oList &&
|
|
95
|
+
node.type === RichTextNodeType.oListItem
|
|
96
|
+
)
|
|
97
|
+
level.list.node.items.push(node);
|
|
98
|
+
level.lastItem = item;
|
|
38
99
|
}
|
|
39
|
-
}
|
|
100
|
+
return { key: uuid(), children };
|
|
101
|
+
};
|
|
40
102
|
|
|
41
|
-
const createTreeNode = (
|
|
103
|
+
const createTreeNode = (
|
|
104
|
+
node: RTAnyNode,
|
|
105
|
+
children: TreeNode[] = [],
|
|
106
|
+
): TreeNode => {
|
|
42
107
|
return {
|
|
43
108
|
key: uuid(),
|
|
44
109
|
type: node.type,
|
|
45
110
|
text: "text" in node ? node.text : undefined,
|
|
46
111
|
node,
|
|
47
112
|
children,
|
|
48
|
-
}
|
|
49
|
-
}
|
|
113
|
+
};
|
|
114
|
+
};
|
|
50
115
|
|
|
51
116
|
const createTextTreeNode = (text: string): TreeNode => {
|
|
52
117
|
return createTreeNode({
|
|
53
118
|
type: RichTextNodeType.span,
|
|
54
119
|
text,
|
|
55
120
|
spans: [],
|
|
56
|
-
})
|
|
57
|
-
}
|
|
121
|
+
});
|
|
122
|
+
};
|
|
58
123
|
|
|
59
124
|
const prepareNodes = (nodes: RTNode[]): RTBlockNode[] => {
|
|
60
|
-
const mutNodes: RTBlockNode[] = nodes.slice(0)
|
|
125
|
+
const mutNodes: RTBlockNode[] = nodes.slice(0);
|
|
61
126
|
|
|
62
127
|
for (let i = 0; i < mutNodes.length; i++) {
|
|
63
|
-
const node = mutNodes[i]
|
|
128
|
+
const node = mutNodes[i];
|
|
64
129
|
|
|
65
|
-
if (
|
|
66
|
-
|
|
130
|
+
if (
|
|
131
|
+
node.type === RichTextNodeType.listItem ||
|
|
132
|
+
node.type === RichTextNodeType.oListItem
|
|
133
|
+
) {
|
|
134
|
+
const items: (RTListItemNode | RTOListItemNode)[] = [
|
|
135
|
+
node as RTListItemNode | RTOListItemNode,
|
|
136
|
+
];
|
|
67
137
|
|
|
68
138
|
while (mutNodes[i + 1] && mutNodes[i + 1].type === node.type) {
|
|
69
|
-
items.push(mutNodes[i + 1] as RTListItemNode | RTOListItemNode)
|
|
70
|
-
mutNodes.splice(i, 1)
|
|
139
|
+
items.push(mutNodes[i + 1] as RTListItemNode | RTOListItemNode);
|
|
140
|
+
mutNodes.splice(i, 1);
|
|
71
141
|
}
|
|
72
142
|
|
|
73
143
|
if (node.type === RichTextNodeType.listItem) {
|
|
74
144
|
mutNodes[i] = {
|
|
75
145
|
type: RichTextNodeType.list,
|
|
76
146
|
items: items as RTListItemNode[],
|
|
77
|
-
}
|
|
147
|
+
};
|
|
78
148
|
} else {
|
|
79
149
|
mutNodes[i] = {
|
|
80
150
|
type: RichTextNodeType.oList,
|
|
81
151
|
items: items as RTOListItemNode[],
|
|
82
|
-
}
|
|
152
|
+
};
|
|
83
153
|
}
|
|
84
154
|
}
|
|
85
155
|
}
|
|
86
156
|
|
|
87
|
-
return mutNodes
|
|
88
|
-
}
|
|
157
|
+
return mutNodes;
|
|
158
|
+
};
|
|
89
159
|
|
|
90
160
|
const nodeToTreeNode = (node: RTBlockNode): TreeNode => {
|
|
91
161
|
if ("text" in node) {
|
|
92
|
-
return createTreeNode(
|
|
162
|
+
return createTreeNode(
|
|
163
|
+
node,
|
|
164
|
+
textNodeSpansToTreeNodeChildren(node.spans, node),
|
|
165
|
+
);
|
|
93
166
|
}
|
|
94
167
|
|
|
95
168
|
if ("items" in node) {
|
|
96
|
-
const children: TreeNode[] = []
|
|
169
|
+
const children: TreeNode[] = [];
|
|
97
170
|
for (let i = 0; i < node.items.length; i++) {
|
|
98
|
-
children.push(nodeToTreeNode(node.items[i]))
|
|
171
|
+
children.push(nodeToTreeNode(node.items[i]));
|
|
99
172
|
}
|
|
100
173
|
|
|
101
|
-
return createTreeNode(node, children)
|
|
174
|
+
return createTreeNode(node, children);
|
|
102
175
|
}
|
|
103
176
|
|
|
104
|
-
return createTreeNode(node)
|
|
105
|
-
}
|
|
177
|
+
return createTreeNode(node);
|
|
178
|
+
};
|
|
106
179
|
|
|
107
180
|
const textNodeSpansToTreeNodeChildren = (
|
|
108
181
|
spans: RTInlineNode[],
|
|
@@ -110,10 +183,10 @@ const textNodeSpansToTreeNodeChildren = (
|
|
|
110
183
|
parentSpan?: RTInlineNode,
|
|
111
184
|
): TreeNode[] => {
|
|
112
185
|
if (!spans.length) {
|
|
113
|
-
return [createTextTreeNode(node.text)]
|
|
186
|
+
return [createTextTreeNode(node.text)];
|
|
114
187
|
}
|
|
115
188
|
|
|
116
|
-
const mutSpans: RTInlineNode[] = spans.slice(0)
|
|
189
|
+
const mutSpans: RTInlineNode[] = spans.slice(0);
|
|
117
190
|
|
|
118
191
|
// Sort spans using the following criteria:
|
|
119
192
|
//
|
|
@@ -125,44 +198,47 @@ const textNodeSpansToTreeNodeChildren = (
|
|
|
125
198
|
//
|
|
126
199
|
// Sorting using this algorithm ensures proper detection of child
|
|
127
200
|
// spans.
|
|
128
|
-
mutSpans.sort((a, b) => a.start - b.start || b.end - a.end)
|
|
201
|
+
mutSpans.sort((a, b) => a.start - b.start || b.end - a.end);
|
|
129
202
|
|
|
130
|
-
const children: TreeNode[] = []
|
|
203
|
+
const children: TreeNode[] = [];
|
|
131
204
|
|
|
132
205
|
for (let i = 0; i < mutSpans.length; i++) {
|
|
133
|
-
const span = mutSpans[i]
|
|
134
|
-
const parentSpanStart = (parentSpan && parentSpan.start) || 0
|
|
135
|
-
const spanStart = span.start - parentSpanStart
|
|
136
|
-
const spanEnd = span.end - parentSpanStart
|
|
137
|
-
const text = node.text.slice(spanStart, spanEnd)
|
|
206
|
+
const span = mutSpans[i];
|
|
207
|
+
const parentSpanStart = (parentSpan && parentSpan.start) || 0;
|
|
208
|
+
const spanStart = span.start - parentSpanStart;
|
|
209
|
+
const spanEnd = span.end - parentSpanStart;
|
|
210
|
+
const text = node.text.slice(spanStart, spanEnd);
|
|
138
211
|
|
|
139
|
-
const childSpans: RTInlineNode[] = []
|
|
212
|
+
const childSpans: RTInlineNode[] = [];
|
|
140
213
|
for (let j = i; j < mutSpans.length; j++) {
|
|
141
|
-
const siblingSpan = mutSpans[j]
|
|
214
|
+
const siblingSpan = mutSpans[j];
|
|
142
215
|
|
|
143
216
|
if (siblingSpan !== span) {
|
|
144
217
|
if (siblingSpan.start >= span.start && siblingSpan.end <= span.end) {
|
|
145
|
-
childSpans.push(siblingSpan)
|
|
146
|
-
mutSpans.splice(j, 1)
|
|
147
|
-
j
|
|
148
|
-
} else if (
|
|
218
|
+
childSpans.push(siblingSpan);
|
|
219
|
+
mutSpans.splice(j, 1);
|
|
220
|
+
j--;
|
|
221
|
+
} else if (
|
|
222
|
+
siblingSpan.start < span.end &&
|
|
223
|
+
siblingSpan.end > span.start
|
|
224
|
+
) {
|
|
149
225
|
childSpans.push({
|
|
150
226
|
...siblingSpan,
|
|
151
227
|
end: span.end,
|
|
152
|
-
})
|
|
228
|
+
});
|
|
153
229
|
mutSpans[j] = {
|
|
154
230
|
...siblingSpan,
|
|
155
231
|
start: span.end,
|
|
156
|
-
}
|
|
232
|
+
};
|
|
157
233
|
}
|
|
158
234
|
}
|
|
159
235
|
}
|
|
160
236
|
|
|
161
237
|
if (i === 0 && spanStart > 0) {
|
|
162
|
-
children.push(createTextTreeNode(node.text.slice(0, spanStart)))
|
|
238
|
+
children.push(createTextTreeNode(node.text.slice(0, spanStart)));
|
|
163
239
|
}
|
|
164
240
|
|
|
165
|
-
const spanWithText = { ...span, text }
|
|
241
|
+
const spanWithText = { ...span, text };
|
|
166
242
|
children.push(
|
|
167
243
|
createTreeNode(
|
|
168
244
|
spanWithText,
|
|
@@ -175,19 +251,21 @@ const textNodeSpansToTreeNodeChildren = (
|
|
|
175
251
|
span,
|
|
176
252
|
),
|
|
177
253
|
),
|
|
178
|
-
)
|
|
254
|
+
);
|
|
179
255
|
|
|
180
256
|
if (spanEnd < node.text.length) {
|
|
181
257
|
children.push(
|
|
182
258
|
createTextTreeNode(
|
|
183
259
|
node.text.slice(
|
|
184
260
|
spanEnd,
|
|
185
|
-
mutSpans[i + 1]
|
|
261
|
+
mutSpans[i + 1]
|
|
262
|
+
? mutSpans[i + 1].start - parentSpanStart
|
|
263
|
+
: undefined,
|
|
186
264
|
),
|
|
187
265
|
),
|
|
188
|
-
)
|
|
266
|
+
);
|
|
189
267
|
}
|
|
190
268
|
}
|
|
191
269
|
|
|
192
|
-
return children
|
|
193
|
-
}
|
|
270
|
+
return children;
|
|
271
|
+
};
|
|
@@ -88,11 +88,15 @@ export interface RTPreformattedNode extends RTTextNodeBase {
|
|
|
88
88
|
/** Rich text `list-item` node */
|
|
89
89
|
export interface RTListItemNode extends RTTextNodeBase {
|
|
90
90
|
type: typeof RichTextNodeType.listItem
|
|
91
|
+
/** Indentation level; omitted for a root list item. */
|
|
92
|
+
depth?: number
|
|
91
93
|
}
|
|
92
94
|
|
|
93
95
|
/** Rich text `o-list-item` node for ordered lists */
|
|
94
96
|
export interface RTOListItemNode extends RTTextNodeBase {
|
|
95
97
|
type: typeof RichTextNodeType.oListItem
|
|
98
|
+
/** Indentation level; omitted for a root list item. */
|
|
99
|
+
depth?: number
|
|
96
100
|
}
|
|
97
101
|
|
|
98
102
|
// Span nodes
|
|
@@ -238,6 +242,5 @@ export type RTAnyNode = RTBlockNode | RTInlineNode | RTSpanNode
|
|
|
238
242
|
*
|
|
239
243
|
* @see Rich text field documentation: {@link https://prismic.io/docs/rich-text-title}
|
|
240
244
|
*/
|
|
241
|
-
export type RichTextField<State extends FieldState = FieldState> =
|
|
242
|
-
? []
|
|
243
|
-
: [RTNode, ...RTNode[]]
|
|
245
|
+
export type RichTextField<State extends FieldState = FieldState> =
|
|
246
|
+
State extends "empty" ? [] : [RTNode, ...RTNode[]]
|