@portabletext/block-tools
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -1,8 +0,6 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: !0 }); | ||
var flatten = require("lodash/flatten.js"), types = require("@sanity/types"), isEqual = require("lodash/isEqual.js"), uniq = require("lodash/uniq.js"), getRandomValues = require("get-random-values-esm"); | ||
function _interopDefaultCompat(e) { | ||
return e && typeof e == "object" && "default" in e ? e : { default: e }; | ||
} | ||
var flatten__default = /* @__PURE__ */ _interopDefaultCompat(flatten), isEqual__default = /* @__PURE__ */ _interopDefaultCompat(isEqual), uniq__default = /* @__PURE__ */ _interopDefaultCompat(uniq), getRandomValues__default = /* @__PURE__ */ _interopDefaultCompat(getRandomValues); | ||
import flatten from "lodash/flatten.js"; | ||
import { isBlockSchemaType, isBlockChildrenObjectField, isObjectSchemaType, isBlockStyleObjectField, isBlockListObjectField, isTitledListValue, isPortableTextTextBlock, isPortableTextSpan } from "@sanity/types"; | ||
import isEqual from "lodash/isEqual.js"; | ||
import uniq from "lodash/uniq.js"; | ||
import getRandomValues from "get-random-values-esm"; | ||
function findBlockType(type) { | ||
@@ -95,6 +93,6 @@ return type.type ? findBlockType(type.type) : type.name === "block"; | ||
}; | ||
uniq__default.default( | ||
uniq( | ||
Object.values(ELEMENT_MAP).filter((tag) => "style" in tag).map((tag) => tag.style) | ||
); | ||
uniq__default.default( | ||
uniq( | ||
Object.values(HTML_DECORATOR_TAGS) | ||
@@ -106,5 +104,5 @@ ); | ||
const blockType = blockContentType.of.find(findBlockType); | ||
if (!types.isBlockSchemaType(blockType)) | ||
if (!isBlockSchemaType(blockType)) | ||
throw new Error("'block' type is not defined in this schema (required)."); | ||
const ofType = blockType.fields.find(types.isBlockChildrenObjectField)?.type?.of; | ||
const ofType = blockType.fields.find(isBlockChildrenObjectField)?.type?.of; | ||
if (!ofType) | ||
@@ -120,5 +118,5 @@ throw new Error("No `of` declaration found for blocks `children` field"); | ||
const inlineObjectTypes = ofType.filter( | ||
(inlineType) => inlineType.name !== "span" && types.isObjectSchemaType(inlineType) | ||
(inlineType) => inlineType.name !== "span" && isObjectSchemaType(inlineType) | ||
), blockObjectTypes = blockContentType.of.filter( | ||
(memberType) => memberType.name !== blockType.name && types.isObjectSchemaType(memberType) | ||
(memberType) => memberType.name !== blockType.name && isObjectSchemaType(memberType) | ||
); | ||
@@ -139,3 +137,3 @@ return { | ||
function resolveEnabledStyles(blockType) { | ||
const styleField = blockType.fields.find(types.isBlockStyleObjectField); | ||
const styleField = blockType.fields.find(isBlockStyleObjectField); | ||
if (!styleField) | ||
@@ -166,3 +164,3 @@ throw new Error( | ||
function resolveEnabledListItems(blockType) { | ||
const listField = blockType.fields.find(types.isBlockListObjectField); | ||
const listField = blockType.fields.find(isBlockListObjectField); | ||
if (!listField) | ||
@@ -182,3 +180,3 @@ throw new Error( | ||
return Array.isArray(list) ? list.map( | ||
(item) => types.isTitledListValue(item) ? item : { title: item, value: item } | ||
(item) => isTitledListValue(item) ? item : { title: item, value: item } | ||
) : []; | ||
@@ -406,3 +404,3 @@ } | ||
nodes.forEach((node) => { | ||
depth === 0 && flattened.push(node), types.isPortableTextTextBlock(node) && (depth > 0 && (toRemove.push(node), flattened.push(node)), depth++, traverse(node.children)), node._type === "__block" && (toRemove.push(node), flattened.push(node.block)); | ||
depth === 0 && flattened.push(node), isPortableTextTextBlock(node) && (depth > 0 && (toRemove.push(node), flattened.push(node)), depth++, traverse(node.children)), node._type === "__block" && (toRemove.push(node), flattened.push(node.block)); | ||
}), toRemove.forEach((node) => { | ||
@@ -427,7 +425,7 @@ nodes.splice(nodes.indexOf(node), 1); | ||
return blocks2.forEach((block) => { | ||
types.isPortableTextTextBlock(block) && block.children.forEach((child, index) => { | ||
isPortableTextTextBlock(block) && block.children.forEach((child, index) => { | ||
if (!isMinimalSpan(child)) | ||
return; | ||
const nextChild = nextSpan(block, index), prevChild = prevSpan(block, index); | ||
index === 0 && (child.text = child.text.replace(/^[^\S\n]+/g, "")), index === block.children.length - 1 && (child.text = child.text.replace(/[^\S\n]+$/g, "")), /\s/.test(child.text.slice(Math.max(0, child.text.length - 1))) && nextChild && isMinimalSpan(nextChild) && /\s/.test(nextChild.text.slice(0, 1)) && (child.text = child.text.replace(/[^\S\n]+$/g, "")), /\s/.test(child.text.slice(0, 1)) && prevChild && isMinimalSpan(prevChild) && /\s/.test(prevChild.text.slice(Math.max(0, prevChild.text.length - 1))) && (child.text = child.text.replace(/^[^\S\n]+/g, "")), child.text || block.children.splice(index, 1), prevChild && isEqual__default.default(prevChild.marks, child.marks) && isWhiteSpaceChar(child.text) ? (prevChild.text += " ", block.children.splice(index, 1)) : nextChild && isEqual__default.default(nextChild.marks, child.marks) && isWhiteSpaceChar(child.text) && (nextChild.text = ` ${nextChild.text}`, block.children.splice(index, 1)); | ||
index === 0 && (child.text = child.text.replace(/^[^\S\n]+/g, "")), index === block.children.length - 1 && (child.text = child.text.replace(/[^\S\n]+$/g, "")), /\s/.test(child.text.slice(Math.max(0, child.text.length - 1))) && nextChild && isMinimalSpan(nextChild) && /\s/.test(nextChild.text.slice(0, 1)) && (child.text = child.text.replace(/[^\S\n]+$/g, "")), /\s/.test(child.text.slice(0, 1)) && prevChild && isMinimalSpan(prevChild) && /\s/.test(prevChild.text.slice(Math.max(0, prevChild.text.length - 1))) && (child.text = child.text.replace(/^[^\S\n]+/g, "")), child.text || block.children.splice(index, 1), prevChild && isEqual(prevChild.marks, child.marks) && isWhiteSpaceChar(child.text) ? (prevChild.text += " ", block.children.splice(index, 1)) : nextChild && isEqual(nextChild.marks, child.marks) && isWhiteSpaceChar(child.text) && (nextChild.text = ` ${nextChild.text}`, block.children.splice(index, 1)); | ||
}); | ||
@@ -443,3 +441,3 @@ }), blocks2; | ||
const lastBlock = memo[memo.length - 1]; | ||
if (i > 0 && !types.isPortableTextTextBlock(original[i - 1]) && types.isPortableTextTextBlock(lastBlock)) | ||
if (i > 0 && !isPortableTextTextBlock(original[i - 1]) && isPortableTextTextBlock(lastBlock)) | ||
return lastBlock.children.push(node), memo; | ||
@@ -605,3 +603,3 @@ const block = { | ||
const rnds8 = new Uint8Array(length); | ||
return getRandomValues__default.default(rnds8), rnds8; | ||
return getRandomValues(rnds8), rnds8; | ||
} | ||
@@ -900,3 +898,3 @@ const byteToHex = []; | ||
block.markDefs = block.markDefs || [], block.markDefs = block.markDefs.concat( | ||
this._markDefs.filter((def) => flatten__default.default( | ||
this._markDefs.filter((def) => flatten( | ||
block.children.map((child) => child.marks || []) | ||
@@ -1043,3 +1041,3 @@ ).includes(def._key)) | ||
const previousChild = acc[acc.length - 1]; | ||
return previousChild && types.isPortableTextSpan(child) && types.isPortableTextSpan(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); | ||
return previousChild && isPortableTextSpan(child) && isPortableTextSpan(previousChild) && isEqual(previousChild.marks, child.marks) ? (lastChild && lastChild === child && child.text === "" && block.children.length > 1 || (previousChild.text += child.text), acc) : (acc.push(child), acc); | ||
}, | ||
@@ -1050,3 +1048,3 @@ [] | ||
throw new Error("missing child"); | ||
return child._key = `${block._key}${index}`, types.isPortableTextSpan(child) && (child.marks ? allowedDecorators && (child.marks = child.marks.filter((mark) => { | ||
return child._key = `${block._key}${index}`, isPortableTextSpan(child) && (child.marks ? allowedDecorators && (child.marks = child.marks.filter((mark) => { | ||
const isAllowed = allowedDecorators.includes(mark), isUsed = block.markDefs?.some((def) => def._key === mark); | ||
@@ -1065,6 +1063,8 @@ return isAllowed || isUsed; | ||
} | ||
exports.getBlockContentFeatures = getBlockContentFeatures; | ||
exports.htmlToBlocks = htmlToBlocks; | ||
exports.normalizeBlock = normalizeBlock; | ||
exports.randomKey = randomKey; | ||
export { | ||
getBlockContentFeatures, | ||
htmlToBlocks, | ||
normalizeBlock, | ||
randomKey | ||
}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@portabletext/block-tools", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Can format HTML, Slate JSON or Sanity block array into any other format.", | ||
@@ -21,8 +21,8 @@ "keywords": [ | ||
"sideEffects": false, | ||
"type": "commonjs", | ||
"type": "module", | ||
"exports": { | ||
".": { | ||
"source": "./src/index.ts", | ||
"import": "./lib/index.mjs", | ||
"require": "./lib/index.js", | ||
"import": "./lib/index.js", | ||
"require": "./lib/index.cjs", | ||
"default": "./lib/index.js" | ||
@@ -32,4 +32,4 @@ }, | ||
}, | ||
"main": "./lib/index.js", | ||
"module": "./lib/index.mjs", | ||
"main": "./lib/index.cjs", | ||
"module": "./lib/index.js", | ||
"types": "./lib/index.d.ts", | ||
@@ -67,3 +67,3 @@ "files": [ | ||
"check:lint": "biome lint .", | ||
"check:types": "tsc --project tsconfig.lib.json", | ||
"check:types": "tsc", | ||
"clean": "del .turbo && del lib && del node_modules", | ||
@@ -70,0 +70,0 @@ "dev": "pkg-utils watch", |
Sorry, the diff of this file is not supported yet
Yes
330305