@portabletext/block-tools
Advanced tools
Comparing version 1.1.3 to 1.1.4
@@ -605,2 +605,14 @@ import flatten from "lodash/flatten.js"; | ||
} | ||
const whitespaceTextNodeRule = { | ||
deserialize(node) { | ||
return node.nodeName === "#text" && isWhitespaceTextNode(node) ? { | ||
...DEFAULT_SPAN, | ||
marks: [], | ||
text: (node.textContent ?? "").replace(/\s\s+/g, " ") | ||
} : void 0; | ||
} | ||
}; | ||
function isWhitespaceTextNode(node) { | ||
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"; | ||
} | ||
function resolveListItem(listNodeTagName, enabledListTypes) { | ||
@@ -614,19 +626,6 @@ if (listNodeTagName === "ul" && enabledListTypes.includes("bullet")) | ||
return [ | ||
// Text nodes | ||
whitespaceTextNodeRule, | ||
{ | ||
// Pre element | ||
deserialize(el) { | ||
if (tagName(el) === "pre") | ||
return; | ||
const isValidText = (el.nodeType === 3 && (el.textContent || "").replace(/[\r\n]/g, " ").replace(/\s\s+/g, " ") === " " && el.nextSibling && el.nextSibling.nodeType !== 3 && el.previousSibling && el.previousSibling.nodeType !== 3 || el.textContent !== " ") && tagName(el.parentNode) !== "body"; | ||
if (el.nodeName === "#text" && isValidText) | ||
return { | ||
...DEFAULT_SPAN, | ||
marks: [], | ||
text: (el.textContent || "").replace(/\s\s+/g, " ") | ||
}; | ||
} | ||
}, | ||
// Pre element | ||
{ | ||
deserialize(el) { | ||
if (tagName(el) !== "pre") | ||
@@ -633,0 +632,0 @@ return; |
{ | ||
"name": "@portabletext/block-tools", | ||
"version": "1.1.3", | ||
"version": "1.1.4", | ||
"description": "Can format HTML, Slate JSON or Sanity block array into any other format.", | ||
@@ -43,5 +43,5 @@ "keywords": [ | ||
"devDependencies": { | ||
"@sanity/pkg-utils": "^7.0.2", | ||
"@sanity/schema": "^3.71.2", | ||
"@sanity/types": "^3.71.2", | ||
"@sanity/pkg-utils": "^7.0.3", | ||
"@sanity/schema": "^3.72.1", | ||
"@sanity/types": "^3.72.1", | ||
"@types/jsdom": "^20.0.0", | ||
@@ -51,9 +51,9 @@ "@types/lodash": "^4.17.7", | ||
"@vercel/stega": "0.1.2", | ||
"@vitest/coverage-v8": "^3.0.2", | ||
"@vitest/coverage-v8": "^3.0.4", | ||
"jsdom": "^26.0.0", | ||
"typescript": "^5.7.3", | ||
"vitest": "^3.0.2" | ||
"vitest": "^3.0.4" | ||
}, | ||
"peerDependencies": { | ||
"@sanity/types": "^3.71.2", | ||
"@sanity/types": "^3.72.1", | ||
"@types/react": "18 || 19" | ||
@@ -72,4 +72,6 @@ }, | ||
"test": "vitest --run", | ||
"test:watch": "vitest" | ||
"test:watch": "vitest", | ||
"test:unit": "vitest --run --project unit", | ||
"test:unit:watch": "vitest --project unit" | ||
} | ||
} |
@@ -16,2 +16,3 @@ import type {ArraySchemaType, TypedObject} from '@sanity/types' | ||
import {isElement, tagName} from '../helpers' | ||
import {whitespaceTextNodeRule} from './whitespace-text-node' | ||
@@ -36,32 +37,6 @@ export function resolveListItem( | ||
return [ | ||
// Text nodes | ||
whitespaceTextNodeRule, | ||
{ | ||
// Pre element | ||
deserialize(el) { | ||
if (tagName(el) === 'pre') { | ||
return undefined | ||
} | ||
const isValidWhiteSpace = | ||
el.nodeType === 3 && | ||
(el.textContent || '') | ||
.replace(/[\r\n]/g, ' ') | ||
.replace(/\s\s+/g, ' ') === ' ' && | ||
el.nextSibling && | ||
el.nextSibling.nodeType !== 3 && | ||
el.previousSibling && | ||
el.previousSibling.nodeType !== 3 | ||
const isValidText = | ||
(isValidWhiteSpace || el.textContent !== ' ') && | ||
tagName(el.parentNode) !== 'body' | ||
if (el.nodeName === '#text' && isValidText) { | ||
return { | ||
...DEFAULT_SPAN, | ||
marks: [], | ||
text: (el.textContent || '').replace(/\s\s+/g, ' '), | ||
} | ||
} | ||
return undefined | ||
}, | ||
}, // Pre element | ||
{ | ||
deserialize(el) { | ||
if (tagName(el) !== 'pre') { | ||
@@ -68,0 +43,0 @@ return undefined |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
334177
32
4344