@portabletext/block-tools 3.3.3 → 3.4.0
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/lib/index.cjs +142 -124
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +4 -36
- package/lib/index.d.ts +4 -36
- package/lib/index.js +114 -95
- package/lib/index.js.map +1 -1
- package/package.json +7 -6
- package/src/HtmlDeserializer/flatten-nested-blocks.test.ts +112 -1
- package/src/HtmlDeserializer/flatten-nested-blocks.ts +174 -0
- package/src/HtmlDeserializer/helpers.ts +24 -159
- package/src/HtmlDeserializer/index.ts +7 -7
- package/src/HtmlDeserializer/rules/html.ts +12 -0
- package/src/index.ts +1 -1
- package/src/types.ts +1 -1
- package/src/util/normalizeBlock.ts +6 -6
- package/src/types.portable-text.ts +0 -79
package/lib/index.cjs
CHANGED
|
@@ -1,22 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: !0 });
|
|
3
|
-
var sanityBridge = require("@portabletext/sanity-bridge"), flatten = require("lodash/flatten.js"), getRandomValues = require("get-random-values-esm"), isEqual = require("lodash/isEqual.js"), uniq = require("lodash/uniq.js");
|
|
3
|
+
var sanityBridge = require("@portabletext/sanity-bridge"), schema = require("@portabletext/schema"), flatten = require("lodash/flatten.js"), getRandomValues = require("get-random-values-esm"), isEqual = require("lodash/isEqual.js"), uniq = require("lodash/uniq.js");
|
|
4
4
|
function _interopDefaultCompat(e) {
|
|
5
5
|
return e && typeof e == "object" && "default" in e ? e : { default: e };
|
|
6
6
|
}
|
|
7
7
|
var flatten__default = /* @__PURE__ */ _interopDefaultCompat(flatten), getRandomValues__default = /* @__PURE__ */ _interopDefaultCompat(getRandomValues), isEqual__default = /* @__PURE__ */ _interopDefaultCompat(isEqual), uniq__default = /* @__PURE__ */ _interopDefaultCompat(uniq);
|
|
8
|
-
function isArbitraryTypedObject(object) {
|
|
9
|
-
return isRecord(object) && typeof object._type == "string";
|
|
10
|
-
}
|
|
11
|
-
function isRecord(value) {
|
|
12
|
-
return !!value && (typeof value == "object" || typeof value == "function");
|
|
13
|
-
}
|
|
14
|
-
function isTextBlock(schema, block) {
|
|
15
|
-
return !(!isArbitraryTypedObject(block) || block._type !== schema.block.name || !Array.isArray(block.children));
|
|
16
|
-
}
|
|
17
|
-
function isSpan(schema, child) {
|
|
18
|
-
return !(!isArbitraryTypedObject(child) || child._type !== schema.span.name || typeof child.text != "string");
|
|
19
|
-
}
|
|
20
8
|
function keyGenerator() {
|
|
21
9
|
return randomKey(12);
|
|
22
10
|
}
|
|
@@ -48,6 +36,81 @@ function resolveJsType(val) {
|
|
|
48
36
|
}
|
|
49
37
|
return val === null ? "null" : val === void 0 ? "undefined" : val && typeof val == "object" && "nodeType" in val && val.nodeType === 1 ? "element" : val === Object(val) ? "object" : typeof val;
|
|
50
38
|
}
|
|
39
|
+
function isArbitraryTypedObject(object) {
|
|
40
|
+
return isRecord(object) && typeof object._type == "string";
|
|
41
|
+
}
|
|
42
|
+
function isRecord(value) {
|
|
43
|
+
return !!value && (typeof value == "object" || typeof value == "function");
|
|
44
|
+
}
|
|
45
|
+
function flattenNestedBlocks(context, blocks2) {
|
|
46
|
+
return blocks2.flatMap((block) => {
|
|
47
|
+
if (isBlockContainer(block))
|
|
48
|
+
return flattenNestedBlocks(context, [block.block]);
|
|
49
|
+
if (schema.isTextBlock(context, block)) {
|
|
50
|
+
const hasBlockObjects = block.children.some((child) => context.schema.blockObjects.some(
|
|
51
|
+
(blockObject) => blockObject.name === child._type
|
|
52
|
+
)), hasBlocks = block.children.some(
|
|
53
|
+
(child) => child._type === "__block" || child._type === "block"
|
|
54
|
+
);
|
|
55
|
+
if (hasBlockObjects || hasBlocks) {
|
|
56
|
+
const splitChildren = getSplitChildren(context, block);
|
|
57
|
+
return splitChildren.length === 1 && splitChildren[0].type === "children" && isEqual__default.default(splitChildren[0].children, block.children) ? [block] : splitChildren.flatMap((slice) => slice.type === "block object" ? [slice.block] : slice.type === "block" ? flattenNestedBlocks(context, [
|
|
58
|
+
slice.block
|
|
59
|
+
]) : slice.children.length > 0 ? slice.children.every(
|
|
60
|
+
(child) => schema.isSpan(context, child) && child.text.trim() === ""
|
|
61
|
+
) ? [] : flattenNestedBlocks(context, [
|
|
62
|
+
{
|
|
63
|
+
...block,
|
|
64
|
+
children: slice.children
|
|
65
|
+
}
|
|
66
|
+
]) : []);
|
|
67
|
+
}
|
|
68
|
+
return [block];
|
|
69
|
+
}
|
|
70
|
+
return [block];
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
function isBlockContainer(block) {
|
|
74
|
+
return block._type === "__block" && isArbitraryTypedObject(block.block);
|
|
75
|
+
}
|
|
76
|
+
function getSplitChildren(context, block) {
|
|
77
|
+
return block.children.reduce(
|
|
78
|
+
(slices, child) => {
|
|
79
|
+
const knownInlineObject = context.schema.inlineObjects.some(
|
|
80
|
+
(inlineObject) => inlineObject.name === child._type
|
|
81
|
+
), knownBlockObject = context.schema.blockObjects.some(
|
|
82
|
+
(blockObject) => blockObject.name === child._type
|
|
83
|
+
), lastSlice = slices.pop();
|
|
84
|
+
return !schema.isSpan(context, child) && !knownInlineObject && knownBlockObject ? [
|
|
85
|
+
...slices,
|
|
86
|
+
...lastSlice ? [lastSlice] : [],
|
|
87
|
+
{ type: "block object", block: child }
|
|
88
|
+
] : child._type === "__block" ? [
|
|
89
|
+
...slices,
|
|
90
|
+
...lastSlice ? [lastSlice] : [],
|
|
91
|
+
{
|
|
92
|
+
type: "block object",
|
|
93
|
+
block: child.block
|
|
94
|
+
}
|
|
95
|
+
] : child._type === "block" ? [
|
|
96
|
+
...slices,
|
|
97
|
+
...lastSlice ? [lastSlice] : [],
|
|
98
|
+
{ type: "block", block: child }
|
|
99
|
+
] : lastSlice && lastSlice.type === "children" ? [
|
|
100
|
+
...slices,
|
|
101
|
+
{
|
|
102
|
+
type: "children",
|
|
103
|
+
children: [...lastSlice.children, child]
|
|
104
|
+
}
|
|
105
|
+
] : [
|
|
106
|
+
...slices,
|
|
107
|
+
...lastSlice ? [lastSlice] : [],
|
|
108
|
+
{ type: "children", children: [child] }
|
|
109
|
+
];
|
|
110
|
+
},
|
|
111
|
+
[]
|
|
112
|
+
);
|
|
113
|
+
}
|
|
51
114
|
var s = { 0: 8203, 1: 8204, 2: 8205, 3: 8290, 4: 8291, 5: 8288, 6: 65279, 7: 8289, 8: 119155, 9: 119156, a: 119157, b: 119158, c: 119159, d: 119160, e: 119161, f: 119162 }, c = { 0: 8203, 1: 8204, 2: 8205, 3: 65279 };
|
|
52
115
|
new Array(4).fill(String.fromCodePoint(c[0])).join("");
|
|
53
116
|
Object.fromEntries(Object.entries(c).map((t) => t.reverse()));
|
|
@@ -311,73 +374,6 @@ function defaultParseHtml() {
|
|
|
311
374
|
);
|
|
312
375
|
return (html) => new DOMParser().parseFromString(html, "text/html");
|
|
313
376
|
}
|
|
314
|
-
function flattenNestedBlocks(context, blocks2) {
|
|
315
|
-
let depth = 0;
|
|
316
|
-
const flattened = [], traverse = (nodes) => {
|
|
317
|
-
const toRemove = [];
|
|
318
|
-
nodes.forEach((node) => {
|
|
319
|
-
if (depth === 0)
|
|
320
|
-
if (context.schema.blockObjects.length > 0 && isTextBlock(context.schema, node)) {
|
|
321
|
-
const hasBlockObjects = node.children.some((child) => context.schema.blockObjects.some(
|
|
322
|
-
(blockObject) => blockObject.name === child._type
|
|
323
|
-
)), hasBlocks = node.children.some(
|
|
324
|
-
(child) => child._type === "__block"
|
|
325
|
-
);
|
|
326
|
-
if (hasBlockObjects || hasBlocks) {
|
|
327
|
-
node.children.reduce(
|
|
328
|
-
(slices, child) => {
|
|
329
|
-
const knownInlineObject = context.schema.inlineObjects.some(
|
|
330
|
-
(inlineObject) => inlineObject.name === child._type
|
|
331
|
-
), knownBlockObject = context.schema.blockObjects.some(
|
|
332
|
-
(blockObject) => blockObject.name === child._type
|
|
333
|
-
), lastSlice = slices.pop();
|
|
334
|
-
return !isSpan(context.schema, child) && !knownInlineObject && knownBlockObject ? [
|
|
335
|
-
...slices,
|
|
336
|
-
...lastSlice ? [lastSlice] : [],
|
|
337
|
-
{ type: "block object", block: child }
|
|
338
|
-
] : child._type === "__block" ? [
|
|
339
|
-
...slices,
|
|
340
|
-
...lastSlice ? [lastSlice] : [],
|
|
341
|
-
{
|
|
342
|
-
type: "block object",
|
|
343
|
-
block: child.block
|
|
344
|
-
}
|
|
345
|
-
] : lastSlice && lastSlice.type === "children" ? [
|
|
346
|
-
...slices,
|
|
347
|
-
{
|
|
348
|
-
type: "children",
|
|
349
|
-
children: [...lastSlice.children, child]
|
|
350
|
-
}
|
|
351
|
-
] : [
|
|
352
|
-
...slices,
|
|
353
|
-
...lastSlice ? [lastSlice] : [],
|
|
354
|
-
{ type: "children", children: [child] }
|
|
355
|
-
];
|
|
356
|
-
},
|
|
357
|
-
[]
|
|
358
|
-
).forEach((slice) => {
|
|
359
|
-
if (slice.type === "block object")
|
|
360
|
-
flattened.push(slice.block);
|
|
361
|
-
else if (slice.children.length > 0) {
|
|
362
|
-
const newBlock = {
|
|
363
|
-
...node,
|
|
364
|
-
children: slice.children
|
|
365
|
-
};
|
|
366
|
-
flattened.push(newBlock);
|
|
367
|
-
}
|
|
368
|
-
});
|
|
369
|
-
return;
|
|
370
|
-
} else
|
|
371
|
-
flattened.push(node);
|
|
372
|
-
} else
|
|
373
|
-
flattened.push(node);
|
|
374
|
-
isTextBlock(context.schema, node) && (depth > 0 && (toRemove.push(node), flattened.push(node)), depth++, traverse(node.children)), node._type === "__block" && (toRemove.push(node), flattened.push(node.block));
|
|
375
|
-
}), toRemove.forEach((node) => {
|
|
376
|
-
nodes.splice(nodes.indexOf(node), 1);
|
|
377
|
-
}), depth--;
|
|
378
|
-
};
|
|
379
|
-
return traverse(blocks2), flattened;
|
|
380
|
-
}
|
|
381
377
|
function nextSpan(block, index) {
|
|
382
378
|
const next = block.children[index + 1];
|
|
383
379
|
return next && next._type === "span" ? next : null;
|
|
@@ -389,9 +385,9 @@ function prevSpan(block, index) {
|
|
|
389
385
|
function isWhiteSpaceChar(text) {
|
|
390
386
|
return ["\xA0", " "].includes(text);
|
|
391
387
|
}
|
|
392
|
-
function trimWhitespace(schema, blocks2) {
|
|
388
|
+
function trimWhitespace(schema$1, blocks2) {
|
|
393
389
|
return blocks2.forEach((block) => {
|
|
394
|
-
isTextBlock(schema, block) && block.children.forEach((child, index) => {
|
|
390
|
+
schema.isTextBlock({ schema: schema$1 }, block) && block.children.forEach((child, index) => {
|
|
395
391
|
if (!isMinimalSpan(child))
|
|
396
392
|
return;
|
|
397
393
|
const nextChild = nextSpan(block, index), prevChild = prevSpan(block, index);
|
|
@@ -399,20 +395,20 @@ function trimWhitespace(schema, blocks2) {
|
|
|
399
395
|
});
|
|
400
396
|
}), blocks2;
|
|
401
397
|
}
|
|
402
|
-
function ensureRootIsBlocks(schema,
|
|
403
|
-
return
|
|
398
|
+
function ensureRootIsBlocks(schema$1, objects) {
|
|
399
|
+
return objects.reduce((blocks2, node, i, original) => {
|
|
404
400
|
if (node._type === "block")
|
|
405
|
-
return
|
|
401
|
+
return blocks2.push(node), blocks2;
|
|
406
402
|
if (node._type === "__block")
|
|
407
|
-
return
|
|
408
|
-
const lastBlock =
|
|
409
|
-
if (i > 0 && !isTextBlock(schema, original[i - 1]) && isTextBlock(schema, lastBlock))
|
|
410
|
-
return lastBlock.children.push(node),
|
|
403
|
+
return blocks2.push(node.block), blocks2;
|
|
404
|
+
const lastBlock = blocks2[blocks2.length - 1];
|
|
405
|
+
if (i > 0 && !schema.isTextBlock({ schema: schema$1 }, original[i - 1]) && schema.isTextBlock({ schema: schema$1 }, lastBlock))
|
|
406
|
+
return lastBlock.children.push(node), blocks2;
|
|
411
407
|
const block = {
|
|
412
408
|
...DEFAULT_BLOCK,
|
|
413
409
|
children: [node]
|
|
414
410
|
};
|
|
415
|
-
return
|
|
411
|
+
return blocks2.push(block), blocks2;
|
|
416
412
|
}, []);
|
|
417
413
|
}
|
|
418
414
|
function isNodeList(node) {
|
|
@@ -444,7 +440,9 @@ function normalizeWhitespace(rootNode) {
|
|
|
444
440
|
const elm = child;
|
|
445
441
|
isWhitespaceBlock(elm) ? (lastParent && elm.parentElement === lastParent ? (emptyBlockCount++, emptyBlockCount > 1 && nodesToRemove.push(elm)) : emptyBlockCount = 1, lastParent = elm.parentElement) : (normalizeWhitespace(child), emptyBlockCount = 0);
|
|
446
442
|
}
|
|
447
|
-
nodesToRemove.forEach((node) =>
|
|
443
|
+
nodesToRemove.forEach((node) => {
|
|
444
|
+
node.parentElement?.removeChild(node);
|
|
445
|
+
});
|
|
448
446
|
}
|
|
449
447
|
function removeAllWhitespace(rootNode) {
|
|
450
448
|
const nodesToRemove = [];
|
|
@@ -463,7 +461,9 @@ function removeAllWhitespace(rootNode) {
|
|
|
463
461
|
collectNodesToRemove(child);
|
|
464
462
|
}
|
|
465
463
|
}
|
|
466
|
-
collectNodesToRemove(rootNode), nodesToRemove.forEach((node) =>
|
|
464
|
+
collectNodesToRemove(rootNode), nodesToRemove.forEach((node) => {
|
|
465
|
+
node.parentElement?.removeChild(node);
|
|
466
|
+
});
|
|
467
467
|
}
|
|
468
468
|
function isWhitespaceBlock(elm) {
|
|
469
469
|
return ["p", "br"].includes(tagName(elm) || "") && !elm.textContent?.trim();
|
|
@@ -514,11 +514,11 @@ const blocks = {
|
|
|
514
514
|
...HTML_BLOCK_TAGS,
|
|
515
515
|
...HTML_HEADER_TAGS
|
|
516
516
|
};
|
|
517
|
-
function getBlockStyle(
|
|
517
|
+
function getBlockStyle(schema2, el) {
|
|
518
518
|
const childTag = tagName(el.firstChild), block = childTag && blocks[childTag];
|
|
519
|
-
return block &&
|
|
519
|
+
return block && schema2.styles.some((style) => style.name === block.style) ? block.style : BLOCK_DEFAULT_STYLE;
|
|
520
520
|
}
|
|
521
|
-
function createGDocsRules(
|
|
521
|
+
function createGDocsRules(schema2) {
|
|
522
522
|
return [
|
|
523
523
|
{
|
|
524
524
|
deserialize(el, next) {
|
|
@@ -541,7 +541,7 @@ function createGDocsRules(schema) {
|
|
|
541
541
|
...DEFAULT_BLOCK,
|
|
542
542
|
listItem: getListItemStyle$1(el),
|
|
543
543
|
level: getListItemLevel$1(el),
|
|
544
|
-
style: getBlockStyle(
|
|
544
|
+
style: getBlockStyle(schema2, el),
|
|
545
545
|
children: next(el.firstChild?.childNodes || [])
|
|
546
546
|
};
|
|
547
547
|
}
|
|
@@ -579,13 +579,13 @@ const whitespaceTextNodeRule = {
|
|
|
579
579
|
function isWhitespaceTextNode(node) {
|
|
580
580
|
return (node.nodeType === 3 && (node.textContent || "").replace(/[\r\n]/g, " ").replace(/\s\s+/g, " ") === " " && node.nextSibling && node.nextSibling.nodeType !== 3 && node.previousSibling && node.previousSibling.nodeType !== 3 || node.textContent !== " ") && tagName(node.parentNode) !== "body";
|
|
581
581
|
}
|
|
582
|
-
function resolveListItem(
|
|
583
|
-
if (listNodeTagName === "ul" &&
|
|
582
|
+
function resolveListItem(schema2, listNodeTagName) {
|
|
583
|
+
if (listNodeTagName === "ul" && schema2.lists.some((list) => list.name === "bullet"))
|
|
584
584
|
return "bullet";
|
|
585
|
-
if (listNodeTagName === "ol" &&
|
|
585
|
+
if (listNodeTagName === "ol" && schema2.lists.some((list) => list.name === "number"))
|
|
586
586
|
return "number";
|
|
587
587
|
}
|
|
588
|
-
function createHTMLRules(
|
|
588
|
+
function createHTMLRules(schema2, options) {
|
|
589
589
|
return [
|
|
590
590
|
whitespaceTextNodeRule,
|
|
591
591
|
{
|
|
@@ -593,7 +593,7 @@ function createHTMLRules(schema, options) {
|
|
|
593
593
|
deserialize(el) {
|
|
594
594
|
if (tagName(el) !== "pre")
|
|
595
595
|
return;
|
|
596
|
-
const isCodeEnabled =
|
|
596
|
+
const isCodeEnabled = schema2.styles.some(
|
|
597
597
|
(style) => style.name === "code"
|
|
598
598
|
);
|
|
599
599
|
return {
|
|
@@ -653,7 +653,7 @@ function createHTMLRules(schema, options) {
|
|
|
653
653
|
if (el.parentNode && tagName(el.parentNode) === "li")
|
|
654
654
|
return next(el.childNodes);
|
|
655
655
|
const blockStyle = block.style;
|
|
656
|
-
return
|
|
656
|
+
return schema2.styles.some((style) => style.name === blockStyle) || (block = DEFAULT_BLOCK), {
|
|
657
657
|
...block,
|
|
658
658
|
children: next(el.childNodes)
|
|
659
659
|
};
|
|
@@ -699,7 +699,7 @@ function createHTMLRules(schema, options) {
|
|
|
699
699
|
const tag = tagName(el), listItem = tag ? HTML_LIST_ITEM_TAGS[tag] : void 0, parentTag = tagName(el.parentNode) || "";
|
|
700
700
|
if (!listItem || !el.parentNode || !HTML_LIST_CONTAINER_TAGS[parentTag])
|
|
701
701
|
return;
|
|
702
|
-
const enabledListItem = resolveListItem(
|
|
702
|
+
const enabledListItem = resolveListItem(schema2, parentTag);
|
|
703
703
|
return enabledListItem ? (listItem.listItem = enabledListItem, {
|
|
704
704
|
...listItem,
|
|
705
705
|
children: next(el.childNodes)
|
|
@@ -710,7 +710,7 @@ function createHTMLRules(schema, options) {
|
|
|
710
710
|
{
|
|
711
711
|
deserialize(el, next) {
|
|
712
712
|
const decorator = HTML_DECORATOR_TAGS[tagName(el) || ""];
|
|
713
|
-
if (!(!decorator || !
|
|
713
|
+
if (!(!decorator || !schema2.decorators.some(
|
|
714
714
|
(decoratorType) => decoratorType.name === decorator
|
|
715
715
|
)))
|
|
716
716
|
return {
|
|
@@ -726,7 +726,7 @@ function createHTMLRules(schema, options) {
|
|
|
726
726
|
deserialize(el, next) {
|
|
727
727
|
if (tagName(el) !== "a")
|
|
728
728
|
return;
|
|
729
|
-
const linkEnabled =
|
|
729
|
+
const linkEnabled = schema2.annotations.some(
|
|
730
730
|
(annotation) => annotation.name === "link"
|
|
731
731
|
), href = isElement(el) && el.getAttribute("href");
|
|
732
732
|
return href ? linkEnabled ? {
|
|
@@ -740,6 +740,15 @@ function createHTMLRules(schema, options) {
|
|
|
740
740
|
} : el.appendChild(el.ownerDocument.createTextNode(` (${href})`)) && next(el.childNodes) : next(el.childNodes);
|
|
741
741
|
}
|
|
742
742
|
},
|
|
743
|
+
{
|
|
744
|
+
deserialize(el, next) {
|
|
745
|
+
if (isElement(el) && (tagName(el) === "td" || tagName(el) === "th"))
|
|
746
|
+
return {
|
|
747
|
+
...DEFAULT_BLOCK,
|
|
748
|
+
children: next(el.childNodes)
|
|
749
|
+
};
|
|
750
|
+
}
|
|
751
|
+
},
|
|
743
752
|
{
|
|
744
753
|
deserialize(el) {
|
|
745
754
|
if (isElement(el) && tagName(el) === "img") {
|
|
@@ -749,7 +758,7 @@ function createHTMLRules(schema, options) {
|
|
|
749
758
|
if (ancestorOfLonelyChild && !ancestorOfListItem) {
|
|
750
759
|
const image2 = options.matchers?.image?.({
|
|
751
760
|
context: {
|
|
752
|
-
schema,
|
|
761
|
+
schema: schema2,
|
|
753
762
|
keyGenerator: options.keyGenerator ?? keyGenerator
|
|
754
763
|
},
|
|
755
764
|
props: {
|
|
@@ -766,7 +775,7 @@ function createHTMLRules(schema, options) {
|
|
|
766
775
|
}
|
|
767
776
|
const inlineImage = options.matchers?.inlineImage?.({
|
|
768
777
|
context: {
|
|
769
|
-
schema,
|
|
778
|
+
schema: schema2,
|
|
770
779
|
keyGenerator: options.keyGenerator ?? keyGenerator
|
|
771
780
|
},
|
|
772
781
|
props: {
|
|
@@ -779,7 +788,7 @@ function createHTMLRules(schema, options) {
|
|
|
779
788
|
return inlineImage;
|
|
780
789
|
const image = options.matchers?.image?.({
|
|
781
790
|
context: {
|
|
782
|
-
schema,
|
|
791
|
+
schema: schema2,
|
|
783
792
|
keyGenerator: options.keyGenerator ?? keyGenerator
|
|
784
793
|
},
|
|
785
794
|
props: {
|
|
@@ -863,12 +872,12 @@ function createWordRules() {
|
|
|
863
872
|
}
|
|
864
873
|
];
|
|
865
874
|
}
|
|
866
|
-
function createRules(
|
|
875
|
+
function createRules(schema2, options) {
|
|
867
876
|
return [
|
|
868
877
|
...createWordRules(),
|
|
869
878
|
...createNotionRules(),
|
|
870
|
-
...createGDocsRules(
|
|
871
|
-
...createHTMLRules(
|
|
879
|
+
...createGDocsRules(schema2),
|
|
880
|
+
...createHTMLRules(schema2, options)
|
|
872
881
|
];
|
|
873
882
|
}
|
|
874
883
|
class HtmlDeserializer {
|
|
@@ -883,12 +892,12 @@ class HtmlDeserializer {
|
|
|
883
892
|
* @param blockContentType - Schema type for array containing _at least_ a block child type
|
|
884
893
|
* @param options - Options for the deserialization process
|
|
885
894
|
*/
|
|
886
|
-
constructor(
|
|
887
|
-
const { rules = [], unstable_whitespaceOnPasteMode = "preserve" } = options, standardRules = createRules(
|
|
895
|
+
constructor(schema2, options = {}) {
|
|
896
|
+
const { rules = [], unstable_whitespaceOnPasteMode = "preserve" } = options, standardRules = createRules(schema2, {
|
|
888
897
|
keyGenerator: options.keyGenerator,
|
|
889
898
|
matchers: options.matchers
|
|
890
899
|
});
|
|
891
|
-
this.schema =
|
|
900
|
+
this.schema = schema2, this.keyGenerator = options.keyGenerator ?? keyGenerator, this.rules = [...rules, ...standardRules];
|
|
892
901
|
const parseHtml = options.parseHtml || defaultParseHtml();
|
|
893
902
|
this.parseHtml = (html) => preprocess(html, parseHtml, { unstable_whitespaceOnPasteMode }).body;
|
|
894
903
|
}
|
|
@@ -903,14 +912,14 @@ class HtmlDeserializer {
|
|
|
903
912
|
const { parseHtml } = this, fragment = parseHtml(html), children = Array.from(fragment.childNodes), blocks2 = trimWhitespace(
|
|
904
913
|
this.schema,
|
|
905
914
|
flattenNestedBlocks(
|
|
906
|
-
{ schema: this.schema },
|
|
915
|
+
{ schema: this.schema, keyGenerator: this.keyGenerator },
|
|
907
916
|
ensureRootIsBlocks(
|
|
908
917
|
this.schema,
|
|
909
918
|
this.deserializeElements(children)
|
|
910
919
|
)
|
|
911
920
|
)
|
|
912
921
|
);
|
|
913
|
-
return this._markDefs.length > 0 && blocks2.filter((block) => isTextBlock(this.schema, block)).forEach((block) => {
|
|
922
|
+
return this._markDefs.length > 0 && blocks2.filter((block) => schema.isTextBlock({ schema: this.schema }, block)).forEach((block) => {
|
|
914
923
|
block.markDefs = block.markDefs || [], block.markDefs = block.markDefs.concat(
|
|
915
924
|
this._markDefs.filter((def) => flatten__default.default(
|
|
916
925
|
block.children.map((child) => child.marks || [])
|
|
@@ -1030,10 +1039,19 @@ class HtmlDeserializer {
|
|
|
1030
1039
|
};
|
|
1031
1040
|
}
|
|
1032
1041
|
function normalizeBlock(node, options = {}) {
|
|
1033
|
-
const schema = {
|
|
1042
|
+
const schema$1 = {
|
|
1043
|
+
block: {
|
|
1044
|
+
name: options.blockTypeName || "block"
|
|
1045
|
+
},
|
|
1034
1046
|
span: {
|
|
1035
1047
|
name: "span"
|
|
1036
|
-
}
|
|
1048
|
+
},
|
|
1049
|
+
styles: [],
|
|
1050
|
+
lists: [],
|
|
1051
|
+
decorators: [],
|
|
1052
|
+
annotations: [],
|
|
1053
|
+
blockObjects: [],
|
|
1054
|
+
inlineObjects: []
|
|
1037
1055
|
};
|
|
1038
1056
|
if (node._type !== (options.blockTypeName || "block"))
|
|
1039
1057
|
return "_key" in node ? node : {
|
|
@@ -1059,13 +1077,13 @@ function normalizeBlock(node, options = {}) {
|
|
|
1059
1077
|
return block.children = block.children.reduce(
|
|
1060
1078
|
(acc, child) => {
|
|
1061
1079
|
const previousChild = acc[acc.length - 1];
|
|
1062
|
-
return previousChild && isSpan(schema, child) && isSpan(schema, previousChild) && isEqual__default.default(previousChild.marks, child.marks) ? (lastChild && lastChild === child && child.text === "" && block.children.length > 1 || (previousChild.text += child.text), acc) : (acc.push(child), acc);
|
|
1080
|
+
return previousChild && schema.isSpan({ schema: schema$1 }, child) && schema.isSpan({ schema: schema$1 }, previousChild) && isEqual__default.default(previousChild.marks, child.marks) ? (lastChild && lastChild === child && child.text === "" && block.children.length > 1 || (previousChild.text += child.text), acc) : (acc.push(child), acc);
|
|
1063
1081
|
},
|
|
1064
1082
|
[]
|
|
1065
1083
|
).map((child) => {
|
|
1066
1084
|
if (!child)
|
|
1067
1085
|
throw new Error("missing child");
|
|
1068
|
-
return child._key = options.keyGenerator ? options.keyGenerator() : keyGenerator(), isSpan(schema, child) && (child.marks ? allowedDecorators && (child.marks = child.marks.filter((mark) => {
|
|
1086
|
+
return child._key = options.keyGenerator ? options.keyGenerator() : keyGenerator(), schema.isSpan({ schema: schema$1 }, child) && (child.marks ? allowedDecorators && (child.marks = child.marks.filter((mark) => {
|
|
1069
1087
|
const isAllowed = allowedDecorators.includes(mark), isUsed = block.markDefs?.some((def) => def._key === mark);
|
|
1070
1088
|
return isAllowed || isUsed;
|
|
1071
1089
|
})) : child.marks = [], usedMarkDefs.push(...child.marks)), child;
|
|
@@ -1074,11 +1092,11 @@ function normalizeBlock(node, options = {}) {
|
|
|
1074
1092
|
), block;
|
|
1075
1093
|
}
|
|
1076
1094
|
function htmlToBlocks(html, schemaType, options = {}) {
|
|
1077
|
-
const
|
|
1078
|
-
return new HtmlDeserializer(
|
|
1095
|
+
const schema2 = isSanitySchema(schemaType) ? sanityBridge.sanitySchemaToPortableTextSchema(schemaType) : schemaType;
|
|
1096
|
+
return new HtmlDeserializer(schema2, options).deserialize(html).map((block) => normalizeBlock(block, { keyGenerator: options.keyGenerator }));
|
|
1079
1097
|
}
|
|
1080
|
-
function isSanitySchema(
|
|
1081
|
-
return
|
|
1098
|
+
function isSanitySchema(schema2) {
|
|
1099
|
+
return schema2.hasOwnProperty("jsonType");
|
|
1082
1100
|
}
|
|
1083
1101
|
exports.htmlToBlocks = htmlToBlocks;
|
|
1084
1102
|
exports.normalizeBlock = normalizeBlock;
|