Latest Threat Research:SANDWORM_MODE: Shai-Hulud-Style npm Worm Hijacks CI Workflows and Poisons AI Toolchains.Details
Socket
Book a DemoInstallSign in
Socket

@portabletext/to-html

Package Overview
Dependencies
Maintainers
9
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@portabletext/to-html - npm Package Compare versions

Comparing version
2.0.17
to
3.0.0
+104
-250
dist/pt-to-html.js
"use strict";
const _excluded = ["block", "list", "listItem", "marks", "types"],
_excluded2 = ["listItem"],
_excluded3 = ["_key"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
Object.defineProperty(exports, "__esModule", {
value: !0
});
Object.defineProperty(exports, "__esModule", { value: !0 });
var toolkit = require("@portabletext/toolkit");
const allowedProtocols = ["http", "https", "mailto", "tel"],
charMap = {
"&": "amp",
"<": "lt",
">": "gt",
'"': "quot",
"'": "#x27"
};
const allowedProtocols = ["http", "https", "mailto", "tel"], charMap = {
"&": "amp",
"<": "lt",
">": "gt",
'"': "quot",
"'": "#x27"
};
function escapeHTML(str) {
return replaceMultipleSpaces(str.replace(/[&<>"']/g, s => `&${charMap[s]};`));
return replaceMultipleSpaces(str.replace(/[&<>"']/g, (s) => `&${charMap[s]};`));
}
function replaceMultipleSpaces(str) {
return str.replace(/ {2,}/g, match => `${"&nbsp;".repeat(match.length - 1)} `);
return str.replace(/ {2,}/g, (match) => `${"&nbsp;".repeat(match.length - 1)} `);
}
function uriLooksSafe(uri) {
const url = (uri || "").trim(),
first = url.charAt(0);
if (first === "#" || first === "/") return !0;
const url = (uri || "").trim(), first = url.charAt(0);
if (first === "#" || first === "/")
return !0;
const colonIndex = url.indexOf(":");
if (colonIndex === -1) return !0;
if (colonIndex === -1)
return !0;
const proto = url.slice(0, colonIndex).toLowerCase();
if (allowedProtocols.indexOf(proto) !== -1) return !0;
if (allowedProtocols.indexOf(proto) !== -1)
return !0;
const queryIndex = url.indexOf("?");
if (queryIndex !== -1 && colonIndex > queryIndex) return !0;
if (queryIndex !== -1 && colonIndex > queryIndex)
return !0;
const hashIndex = url.indexOf("#");

@@ -45,43 +34,15 @@ return hashIndex !== -1 && colonIndex > hashIndex;

const defaultLists = {
number: ({
children
}) => `<ol>${children}</ol>`,
bullet: ({
children
}) => `<ul>${children}</ul>`
},
DefaultListItem = ({
children
}) => `<li>${children}</li>`,
link = ({
children,
value
}) => {
const href = (value == null ? void 0 : value.href) || "";
return uriLooksSafe(href) ? `<a href="${escapeHTML(href)}">${children}</a>` : children;
},
defaultMarks = {
em: ({
children
}) => `<em>${children}</em>`,
strong: ({
children
}) => `<strong>${children}</strong>`,
code: ({
children
}) => `<code>${children}</code>`,
underline: ({
children
}) => `<span style="text-decoration:underline">${children}</span>`,
"strike-through": ({
children
}) => `<del>${children}</del>`,
link
},
getTemplate = (type, prop) => `Unknown ${type}, specify a component for it in the \`components.${prop}\` option`,
unknownTypeWarning = typeName => getTemplate(`block type "${typeName}"`, "types"),
unknownMarkWarning = markType => getTemplate(`mark type "${markType}"`, "marks"),
unknownBlockStyleWarning = blockStyle => getTemplate(`block style "${blockStyle}"`, "block"),
unknownListStyleWarning = listStyle => getTemplate(`list style "${listStyle}"`, "list"),
unknownListItemStyleWarning = listStyle => getTemplate(`list item style "${listStyle}"`, "listItem");
number: ({ children }) => `<ol>${children}</ol>`,
bullet: ({ children }) => `<ul>${children}</ul>`
}, DefaultListItem = ({ children }) => `<li>${children}</li>`, link = ({ children, value }) => {
const href = (value == null ? void 0 : value.href) || "";
return uriLooksSafe(href) ? `<a href="${escapeHTML(href)}">${children}</a>` : children;
}, defaultMarks = {
em: ({ children }) => `<em>${children}</em>`,
strong: ({ children }) => `<strong>${children}</strong>`,
code: ({ children }) => `<code>${children}</code>`,
underline: ({ children }) => `<span style="text-decoration:underline">${children}</span>`,
"strike-through": ({ children }) => `<del>${children}</del>`,
link
}, getTemplate = (type, prop) => `Unknown ${type}, specify a component for it in the \`components.${prop}\` option`, unknownTypeWarning = (typeName) => getTemplate(`block type "${typeName}"`, "types"), unknownMarkWarning = (markType) => getTemplate(`mark type "${markType}"`, "marks"), unknownBlockStyleWarning = (blockStyle) => getTemplate(`block style "${blockStyle}"`, "block"), unknownListStyleWarning = (listStyle) => getTemplate(`list style "${listStyle}"`, "list"), unknownListItemStyleWarning = (listStyle) => getTemplate(`list item style "${listStyle}"`, "listItem");
function printWarning(message) {

@@ -91,72 +52,41 @@ console.warn(message);

const DefaultUnknownType = ({
value,
isInline
}) => {
const warning = unknownTypeWarning(value._type);
return isInline ? `<span style="display:none">${warning}</span>` : `<div style="display:none">${warning}</div>`;
},
DefaultUnknownMark = ({
markType,
children
}) => `<span class="unknown__pt__mark__${markType}">${children}</span>`,
DefaultUnknownBlockStyle = ({
children
}) => `<p>${children}</p>`,
DefaultUnknownList = ({
children
}) => `<ul>${children}</ul>`,
DefaultUnknownListItem = ({
children
}) => `<li>${children}</li>`,
DefaultHardBreak = () => "<br/>",
defaultPortableTextBlockStyles = {
normal: ({
children
}) => `<p>${children}</p>`,
blockquote: ({
children
}) => `<blockquote>${children}</blockquote>`,
h1: ({
children
}) => `<h1>${children}</h1>`,
h2: ({
children
}) => `<h2>${children}</h2>`,
h3: ({
children
}) => `<h3>${children}</h3>`,
h4: ({
children
}) => `<h4>${children}</h4>`,
h5: ({
children
}) => `<h5>${children}</h5>`,
h6: ({
children
}) => `<h6>${children}</h6>`
},
defaultComponents = {
types: {},
block: defaultPortableTextBlockStyles,
marks: defaultMarks,
list: defaultLists,
listItem: DefaultListItem,
hardBreak: DefaultHardBreak,
escapeHTML,
unknownType: DefaultUnknownType,
unknownMark: DefaultUnknownMark,
unknownList: DefaultUnknownList,
unknownListItem: DefaultUnknownListItem,
unknownBlockStyle: DefaultUnknownBlockStyle
};
value,
isInline
}) => {
const warning = unknownTypeWarning(value._type);
return isInline ? `<span style="display:none">${warning}</span>` : `<div style="display:none">${warning}</div>`;
}, DefaultUnknownMark = ({
markType,
children
}) => `<span class="unknown__pt__mark__${markType}">${children}</span>`, DefaultUnknownBlockStyle = ({
children
}) => `<p>${children}</p>`, DefaultUnknownList = ({ children }) => `<ul>${children}</ul>`, DefaultUnknownListItem = ({
children
}) => `<li>${children}</li>`, DefaultHardBreak = () => "<br/>", defaultPortableTextBlockStyles = {
normal: ({ children }) => `<p>${children}</p>`,
blockquote: ({ children }) => `<blockquote>${children}</blockquote>`,
h1: ({ children }) => `<h1>${children}</h1>`,
h2: ({ children }) => `<h2>${children}</h2>`,
h3: ({ children }) => `<h3>${children}</h3>`,
h4: ({ children }) => `<h4>${children}</h4>`,
h5: ({ children }) => `<h5>${children}</h5>`,
h6: ({ children }) => `<h6>${children}</h6>`
}, defaultComponents = {
types: {},
block: defaultPortableTextBlockStyles,
marks: defaultMarks,
list: defaultLists,
listItem: DefaultListItem,
hardBreak: DefaultHardBreak,
escapeHTML,
unknownType: DefaultUnknownType,
unknownMark: DefaultUnknownMark,
unknownList: DefaultUnknownList,
unknownListItem: DefaultUnknownListItem,
unknownBlockStyle: DefaultUnknownBlockStyle
};
function mergeComponents(parent, overrides) {
const {
block,
list,
listItem,
marks,
types
} = overrides,
rest = _objectWithoutProperties(overrides, _excluded);
return _objectSpread(_objectSpread({}, parent), {}, {
const { block, list, listItem, marks, types, ...rest } = overrides;
return {
...parent,
block: mergeDeeply(parent, overrides, "block"),

@@ -166,45 +96,26 @@ list: mergeDeeply(parent, overrides, "list"),

marks: mergeDeeply(parent, overrides, "marks"),
types: mergeDeeply(parent, overrides, "types")
}, rest);
types: mergeDeeply(parent, overrides, "types"),
...rest
};
}
function mergeDeeply(parent, overrides, key) {
const override = overrides[key],
parentVal = parent[key];
return typeof override == "function" || override && typeof parentVal == "function" ? override : override ? _objectSpread(_objectSpread({}, parentVal), override) : parentVal;
const override = overrides[key], parentVal = parent[key];
return typeof override == "function" || override && typeof parentVal == "function" ? override : override ? { ...parentVal, ...override } : parentVal;
}
function toHTML(value, options = {}) {
const {
components: componentOverrides,
onMissingComponent: missingComponentHandler = printWarning
} = options,
handleMissingComponent = missingComponentHandler || noop,
blocks = Array.isArray(value) ? value : [value],
nested = toolkit.nestLists(blocks, "html"),
components = componentOverrides ? mergeComponents(defaultComponents, componentOverrides) : defaultComponents,
renderNode = getNodeRenderer(components, handleMissingComponent);
return nested.map((node, index) => renderNode({
node,
index,
isInline: !1,
renderNode
})).join("");
components: componentOverrides,
onMissingComponent: missingComponentHandler = printWarning
} = options, handleMissingComponent = missingComponentHandler || noop, blocks = Array.isArray(value) ? value : [value], nested = toolkit.nestLists(blocks, "html"), components = componentOverrides ? mergeComponents(defaultComponents, componentOverrides) : defaultComponents, renderNode = getNodeRenderer(components, handleMissingComponent);
return nested.map(
(node, index) => renderNode({ node, index, isInline: !1, renderNode })
).join("");
}
const getNodeRenderer = (components, handleMissingComponent) => {
function renderNode(options) {
const {
node,
index,
isInline
} = options;
const { node, index, isInline } = options;
return toolkit.isPortableTextToolkitList(node) ? renderList(node, index) : toolkit.isPortableTextListItemBlock(node) ? renderListItem(node, index) : toolkit.isPortableTextToolkitSpan(node) ? renderSpan(node) : toolkit.isPortableTextBlock(node) ? renderBlock(node, index, isInline) : toolkit.isPortableTextToolkitTextNode(node) ? renderText(node) : renderCustomBlock(node, index, isInline);
}
function renderListItem(node, index) {
const tree = serializeBlock({
node,
index,
isInline: !1,
renderNode
}),
renderer = components.listItem,
itemHandler = (typeof renderer == "function" ? renderer : renderer[node.listItem]) || components.unknownListItem;
const tree = serializeBlock({ node, index, isInline: !1, renderNode }), renderer = components.listItem, itemHandler = (typeof renderer == "function" ? renderer : renderer[node.listItem]) || components.unknownListItem;
if (itemHandler === components.unknownListItem) {

@@ -219,61 +130,26 @@ const style = node.listItem || "bullet";

if (node.style && node.style !== "normal") {
const {
listItem
} = node,
blockNode = _objectWithoutProperties(node, _excluded2);
children = renderNode({
node: blockNode,
index,
isInline: !1
});
const { listItem, ...blockNode } = node;
children = renderNode({ node: blockNode, index, isInline: !1 });
}
return itemHandler({
value: node,
index,
isInline: !1,
renderNode,
children
});
return itemHandler({ value: node, index, isInline: !1, renderNode, children });
}
function renderList(node, index) {
const children = node.children.map((child, childIndex) => renderNode({
node: child._key ? child : _objectSpread(_objectSpread({}, child), {}, {
_key: `li-${index}-${childIndex}`
}),
const children = node.children.map(
(child, childIndex) => renderNode({
node: child._key ? child : { ...child, _key: `li-${index}-${childIndex}` },
index: childIndex,
isInline: !1
})),
component = components.list,
list = (typeof component == "function" ? component : component[node.listItem]) || components.unknownList;
})
), component = components.list, list = (typeof component == "function" ? component : component[node.listItem]) || components.unknownList;
if (list === components.unknownList) {
const style = node.listItem || "bullet";
handleMissingComponent(unknownListStyleWarning(style), {
nodeType: "listStyle",
type: style
});
handleMissingComponent(unknownListStyleWarning(style), { nodeType: "listStyle", type: style });
}
return list({
value: node,
index,
isInline: !1,
renderNode,
children: children.join("")
});
return list({ value: node, index, isInline: !1, renderNode, children: children.join("") });
}
function renderSpan(node) {
const {
markDef,
markType,
markKey
} = node,
span = components.marks[markType] || components.unknownMark,
children = node.children.map((child, childIndex) => renderNode({
node: child,
index: childIndex,
isInline: !0
}));
return span === components.unknownMark && handleMissingComponent(unknownMarkWarning(markType), {
nodeType: "mark",
type: markType
}), span({
const { markDef, markType, markKey } = node, span = components.marks[markType] || components.unknownMark, children = node.children.map(
(child, childIndex) => renderNode({ node: child, index: childIndex, isInline: !0 })
);
return span === components.unknownMark && handleMissingComponent(unknownMarkWarning(markType), { nodeType: "mark", type: markType }), span({
text: toolkit.spanToPlainText(node),

@@ -288,21 +164,7 @@ value: markDef,

function renderBlock(node, index, isInline) {
const _serializeBlock = serializeBlock({
node,
index,
isInline,
renderNode
}),
{
_key
} = _serializeBlock,
props = _objectWithoutProperties(_serializeBlock, _excluded3),
style = props.node.style || "normal",
block = (typeof components.block == "function" ? components.block : components.block[style]) || components.unknownBlockStyle;
const { _key, ...props } = serializeBlock({ node, index, isInline, renderNode }), style = props.node.style || "normal", block = (typeof components.block == "function" ? components.block : components.block[style]) || components.unknownBlockStyle;
return block === components.unknownBlockStyle && handleMissingComponent(unknownBlockStyleWarning(style), {
nodeType: "blockStyle",
type: style
}), block(_objectSpread(_objectSpread({}, props), {}, {
value: props.node,
renderNode
}));
}), block({ ...props, value: props.node, renderNode });
}

@@ -333,14 +195,5 @@ function renderText(node) {

function serializeBlock(options) {
const {
node,
index,
isInline,
renderNode
} = options,
children = toolkit.buildMarksTree(node).map((child, i) => renderNode({
node: child,
isInline: !0,
index: i,
renderNode
}));
const { node, index, isInline, renderNode } = options, children = toolkit.buildMarksTree(node).map(
(child, i) => renderNode({ node: child, isInline: !0, index: i, renderNode })
);
return {

@@ -354,3 +207,4 @@ _key: node._key || `block-${index}`,

}
function noop() {}
function noop() {
}
exports.defaultComponents = defaultComponents;

@@ -357,0 +211,0 @@ exports.escapeHTML = escapeHTML;

@@ -1,36 +0,28 @@

const _excluded = ["block", "list", "listItem", "marks", "types"],
_excluded2 = ["listItem"],
_excluded3 = ["_key"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
import { nestLists, isPortableTextToolkitList, isPortableTextListItemBlock, isPortableTextToolkitSpan, isPortableTextBlock, isPortableTextToolkitTextNode, spanToPlainText, buildMarksTree } from "@portabletext/toolkit";
const allowedProtocols = ["http", "https", "mailto", "tel"],
charMap = {
"&": "amp",
"<": "lt",
">": "gt",
'"': "quot",
"'": "#x27"
};
const allowedProtocols = ["http", "https", "mailto", "tel"], charMap = {
"&": "amp",
"<": "lt",
">": "gt",
'"': "quot",
"'": "#x27"
};
function escapeHTML(str) {
return replaceMultipleSpaces(str.replace(/[&<>"']/g, s => `&${charMap[s]};`));
return replaceMultipleSpaces(str.replace(/[&<>"']/g, (s) => `&${charMap[s]};`));
}
function replaceMultipleSpaces(str) {
return str.replace(/ {2,}/g, match => `${"&nbsp;".repeat(match.length - 1)} `);
return str.replace(/ {2,}/g, (match) => `${"&nbsp;".repeat(match.length - 1)} `);
}
function uriLooksSafe(uri) {
const url = (uri || "").trim(),
first = url.charAt(0);
if (first === "#" || first === "/") return !0;
const url = (uri || "").trim(), first = url.charAt(0);
if (first === "#" || first === "/")
return !0;
const colonIndex = url.indexOf(":");
if (colonIndex === -1) return !0;
if (colonIndex === -1)
return !0;
const proto = url.slice(0, colonIndex).toLowerCase();
if (allowedProtocols.indexOf(proto) !== -1) return !0;
if (allowedProtocols.indexOf(proto) !== -1)
return !0;
const queryIndex = url.indexOf("?");
if (queryIndex !== -1 && colonIndex > queryIndex) return !0;
if (queryIndex !== -1 && colonIndex > queryIndex)
return !0;
const hashIndex = url.indexOf("#");

@@ -40,43 +32,15 @@ return hashIndex !== -1 && colonIndex > hashIndex;

const defaultLists = {
number: ({
children
}) => `<ol>${children}</ol>`,
bullet: ({
children
}) => `<ul>${children}</ul>`
},
DefaultListItem = ({
children
}) => `<li>${children}</li>`,
link = ({
children,
value
}) => {
const href = (value == null ? void 0 : value.href) || "";
return uriLooksSafe(href) ? `<a href="${escapeHTML(href)}">${children}</a>` : children;
},
defaultMarks = {
em: ({
children
}) => `<em>${children}</em>`,
strong: ({
children
}) => `<strong>${children}</strong>`,
code: ({
children
}) => `<code>${children}</code>`,
underline: ({
children
}) => `<span style="text-decoration:underline">${children}</span>`,
"strike-through": ({
children
}) => `<del>${children}</del>`,
link
},
getTemplate = (type, prop) => `Unknown ${type}, specify a component for it in the \`components.${prop}\` option`,
unknownTypeWarning = typeName => getTemplate(`block type "${typeName}"`, "types"),
unknownMarkWarning = markType => getTemplate(`mark type "${markType}"`, "marks"),
unknownBlockStyleWarning = blockStyle => getTemplate(`block style "${blockStyle}"`, "block"),
unknownListStyleWarning = listStyle => getTemplate(`list style "${listStyle}"`, "list"),
unknownListItemStyleWarning = listStyle => getTemplate(`list item style "${listStyle}"`, "listItem");
number: ({ children }) => `<ol>${children}</ol>`,
bullet: ({ children }) => `<ul>${children}</ul>`
}, DefaultListItem = ({ children }) => `<li>${children}</li>`, link = ({ children, value }) => {
const href = (value == null ? void 0 : value.href) || "";
return uriLooksSafe(href) ? `<a href="${escapeHTML(href)}">${children}</a>` : children;
}, defaultMarks = {
em: ({ children }) => `<em>${children}</em>`,
strong: ({ children }) => `<strong>${children}</strong>`,
code: ({ children }) => `<code>${children}</code>`,
underline: ({ children }) => `<span style="text-decoration:underline">${children}</span>`,
"strike-through": ({ children }) => `<del>${children}</del>`,
link
}, getTemplate = (type, prop) => `Unknown ${type}, specify a component for it in the \`components.${prop}\` option`, unknownTypeWarning = (typeName) => getTemplate(`block type "${typeName}"`, "types"), unknownMarkWarning = (markType) => getTemplate(`mark type "${markType}"`, "marks"), unknownBlockStyleWarning = (blockStyle) => getTemplate(`block style "${blockStyle}"`, "block"), unknownListStyleWarning = (listStyle) => getTemplate(`list style "${listStyle}"`, "list"), unknownListItemStyleWarning = (listStyle) => getTemplate(`list item style "${listStyle}"`, "listItem");
function printWarning(message) {

@@ -86,72 +50,41 @@ console.warn(message);

const DefaultUnknownType = ({
value,
isInline
}) => {
const warning = unknownTypeWarning(value._type);
return isInline ? `<span style="display:none">${warning}</span>` : `<div style="display:none">${warning}</div>`;
},
DefaultUnknownMark = ({
markType,
children
}) => `<span class="unknown__pt__mark__${markType}">${children}</span>`,
DefaultUnknownBlockStyle = ({
children
}) => `<p>${children}</p>`,
DefaultUnknownList = ({
children
}) => `<ul>${children}</ul>`,
DefaultUnknownListItem = ({
children
}) => `<li>${children}</li>`,
DefaultHardBreak = () => "<br/>",
defaultPortableTextBlockStyles = {
normal: ({
children
}) => `<p>${children}</p>`,
blockquote: ({
children
}) => `<blockquote>${children}</blockquote>`,
h1: ({
children
}) => `<h1>${children}</h1>`,
h2: ({
children
}) => `<h2>${children}</h2>`,
h3: ({
children
}) => `<h3>${children}</h3>`,
h4: ({
children
}) => `<h4>${children}</h4>`,
h5: ({
children
}) => `<h5>${children}</h5>`,
h6: ({
children
}) => `<h6>${children}</h6>`
},
defaultComponents = {
types: {},
block: defaultPortableTextBlockStyles,
marks: defaultMarks,
list: defaultLists,
listItem: DefaultListItem,
hardBreak: DefaultHardBreak,
escapeHTML,
unknownType: DefaultUnknownType,
unknownMark: DefaultUnknownMark,
unknownList: DefaultUnknownList,
unknownListItem: DefaultUnknownListItem,
unknownBlockStyle: DefaultUnknownBlockStyle
};
value,
isInline
}) => {
const warning = unknownTypeWarning(value._type);
return isInline ? `<span style="display:none">${warning}</span>` : `<div style="display:none">${warning}</div>`;
}, DefaultUnknownMark = ({
markType,
children
}) => `<span class="unknown__pt__mark__${markType}">${children}</span>`, DefaultUnknownBlockStyle = ({
children
}) => `<p>${children}</p>`, DefaultUnknownList = ({ children }) => `<ul>${children}</ul>`, DefaultUnknownListItem = ({
children
}) => `<li>${children}</li>`, DefaultHardBreak = () => "<br/>", defaultPortableTextBlockStyles = {
normal: ({ children }) => `<p>${children}</p>`,
blockquote: ({ children }) => `<blockquote>${children}</blockquote>`,
h1: ({ children }) => `<h1>${children}</h1>`,
h2: ({ children }) => `<h2>${children}</h2>`,
h3: ({ children }) => `<h3>${children}</h3>`,
h4: ({ children }) => `<h4>${children}</h4>`,
h5: ({ children }) => `<h5>${children}</h5>`,
h6: ({ children }) => `<h6>${children}</h6>`
}, defaultComponents = {
types: {},
block: defaultPortableTextBlockStyles,
marks: defaultMarks,
list: defaultLists,
listItem: DefaultListItem,
hardBreak: DefaultHardBreak,
escapeHTML,
unknownType: DefaultUnknownType,
unknownMark: DefaultUnknownMark,
unknownList: DefaultUnknownList,
unknownListItem: DefaultUnknownListItem,
unknownBlockStyle: DefaultUnknownBlockStyle
};
function mergeComponents(parent, overrides) {
const {
block,
list,
listItem,
marks,
types
} = overrides,
rest = _objectWithoutProperties(overrides, _excluded);
return _objectSpread(_objectSpread({}, parent), {}, {
const { block, list, listItem, marks, types, ...rest } = overrides;
return {
...parent,
block: mergeDeeply(parent, overrides, "block"),

@@ -161,45 +94,26 @@ list: mergeDeeply(parent, overrides, "list"),

marks: mergeDeeply(parent, overrides, "marks"),
types: mergeDeeply(parent, overrides, "types")
}, rest);
types: mergeDeeply(parent, overrides, "types"),
...rest
};
}
function mergeDeeply(parent, overrides, key) {
const override = overrides[key],
parentVal = parent[key];
return typeof override == "function" || override && typeof parentVal == "function" ? override : override ? _objectSpread(_objectSpread({}, parentVal), override) : parentVal;
const override = overrides[key], parentVal = parent[key];
return typeof override == "function" || override && typeof parentVal == "function" ? override : override ? { ...parentVal, ...override } : parentVal;
}
function toHTML(value, options = {}) {
const {
components: componentOverrides,
onMissingComponent: missingComponentHandler = printWarning
} = options,
handleMissingComponent = missingComponentHandler || noop,
blocks = Array.isArray(value) ? value : [value],
nested = nestLists(blocks, "html"),
components = componentOverrides ? mergeComponents(defaultComponents, componentOverrides) : defaultComponents,
renderNode = getNodeRenderer(components, handleMissingComponent);
return nested.map((node, index) => renderNode({
node,
index,
isInline: !1,
renderNode
})).join("");
components: componentOverrides,
onMissingComponent: missingComponentHandler = printWarning
} = options, handleMissingComponent = missingComponentHandler || noop, blocks = Array.isArray(value) ? value : [value], nested = nestLists(blocks, "html"), components = componentOverrides ? mergeComponents(defaultComponents, componentOverrides) : defaultComponents, renderNode = getNodeRenderer(components, handleMissingComponent);
return nested.map(
(node, index) => renderNode({ node, index, isInline: !1, renderNode })
).join("");
}
const getNodeRenderer = (components, handleMissingComponent) => {
function renderNode(options) {
const {
node,
index,
isInline
} = options;
const { node, index, isInline } = options;
return isPortableTextToolkitList(node) ? renderList(node, index) : isPortableTextListItemBlock(node) ? renderListItem(node, index) : isPortableTextToolkitSpan(node) ? renderSpan(node) : isPortableTextBlock(node) ? renderBlock(node, index, isInline) : isPortableTextToolkitTextNode(node) ? renderText(node) : renderCustomBlock(node, index, isInline);
}
function renderListItem(node, index) {
const tree = serializeBlock({
node,
index,
isInline: !1,
renderNode
}),
renderer = components.listItem,
itemHandler = (typeof renderer == "function" ? renderer : renderer[node.listItem]) || components.unknownListItem;
const tree = serializeBlock({ node, index, isInline: !1, renderNode }), renderer = components.listItem, itemHandler = (typeof renderer == "function" ? renderer : renderer[node.listItem]) || components.unknownListItem;
if (itemHandler === components.unknownListItem) {

@@ -214,61 +128,26 @@ const style = node.listItem || "bullet";

if (node.style && node.style !== "normal") {
const {
listItem
} = node,
blockNode = _objectWithoutProperties(node, _excluded2);
children = renderNode({
node: blockNode,
index,
isInline: !1
});
const { listItem, ...blockNode } = node;
children = renderNode({ node: blockNode, index, isInline: !1 });
}
return itemHandler({
value: node,
index,
isInline: !1,
renderNode,
children
});
return itemHandler({ value: node, index, isInline: !1, renderNode, children });
}
function renderList(node, index) {
const children = node.children.map((child, childIndex) => renderNode({
node: child._key ? child : _objectSpread(_objectSpread({}, child), {}, {
_key: `li-${index}-${childIndex}`
}),
const children = node.children.map(
(child, childIndex) => renderNode({
node: child._key ? child : { ...child, _key: `li-${index}-${childIndex}` },
index: childIndex,
isInline: !1
})),
component = components.list,
list = (typeof component == "function" ? component : component[node.listItem]) || components.unknownList;
})
), component = components.list, list = (typeof component == "function" ? component : component[node.listItem]) || components.unknownList;
if (list === components.unknownList) {
const style = node.listItem || "bullet";
handleMissingComponent(unknownListStyleWarning(style), {
nodeType: "listStyle",
type: style
});
handleMissingComponent(unknownListStyleWarning(style), { nodeType: "listStyle", type: style });
}
return list({
value: node,
index,
isInline: !1,
renderNode,
children: children.join("")
});
return list({ value: node, index, isInline: !1, renderNode, children: children.join("") });
}
function renderSpan(node) {
const {
markDef,
markType,
markKey
} = node,
span = components.marks[markType] || components.unknownMark,
children = node.children.map((child, childIndex) => renderNode({
node: child,
index: childIndex,
isInline: !0
}));
return span === components.unknownMark && handleMissingComponent(unknownMarkWarning(markType), {
nodeType: "mark",
type: markType
}), span({
const { markDef, markType, markKey } = node, span = components.marks[markType] || components.unknownMark, children = node.children.map(
(child, childIndex) => renderNode({ node: child, index: childIndex, isInline: !0 })
);
return span === components.unknownMark && handleMissingComponent(unknownMarkWarning(markType), { nodeType: "mark", type: markType }), span({
text: spanToPlainText(node),

@@ -283,21 +162,7 @@ value: markDef,

function renderBlock(node, index, isInline) {
const _serializeBlock = serializeBlock({
node,
index,
isInline,
renderNode
}),
{
_key
} = _serializeBlock,
props = _objectWithoutProperties(_serializeBlock, _excluded3),
style = props.node.style || "normal",
block = (typeof components.block == "function" ? components.block : components.block[style]) || components.unknownBlockStyle;
const { _key, ...props } = serializeBlock({ node, index, isInline, renderNode }), style = props.node.style || "normal", block = (typeof components.block == "function" ? components.block : components.block[style]) || components.unknownBlockStyle;
return block === components.unknownBlockStyle && handleMissingComponent(unknownBlockStyleWarning(style), {
nodeType: "blockStyle",
type: style
}), block(_objectSpread(_objectSpread({}, props), {}, {
value: props.node,
renderNode
}));
}), block({ ...props, value: props.node, renderNode });
}

@@ -328,14 +193,5 @@ function renderText(node) {

function serializeBlock(options) {
const {
node,
index,
isInline,
renderNode
} = options,
children = buildMarksTree(node).map((child, i) => renderNode({
node: child,
isInline: !0,
index: i,
renderNode
}));
const { node, index, isInline, renderNode } = options, children = buildMarksTree(node).map(
(child, i) => renderNode({ node: child, isInline: !0, index: i, renderNode })
);
return {

@@ -349,4 +205,11 @@ _key: node._key || `block-${index}`,

}
function noop() {}
export { defaultComponents, escapeHTML, mergeComponents, toHTML, uriLooksSafe };
function noop() {
}
export {
defaultComponents,
escapeHTML,
mergeComponents,
toHTML,
uriLooksSafe
};
//# sourceMappingURL=pt-to-html.mjs.map
{
"name": "@portabletext/to-html",
"version": "2.0.17",
"version": "3.0.0",
"description": "Render Portable Text to HTML",

@@ -68,8 +68,6 @@ "keywords": [

"dependencies": {
"@portabletext/toolkit": "^2.0.18",
"@portabletext/toolkit": "^3.0.0",
"@portabletext/types": "^2.0.15"
},
"devDependencies": {
"@babel/core": "^7.28.3",
"@babel/plugin-proposal-object-rest-spread": "^7.20.7",
"@changesets/changelog-github": "^0.5.1",

@@ -76,0 +74,0 @@ "@changesets/cli": "^2.29.6",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet