Skip to content

Commit

Permalink
fix: Don't crash when rendering invalid slate
Browse files Browse the repository at this point in the history
  • Loading branch information
tiberiuichim authored Feb 19, 2024
1 parent ba82be2 commit 9b7bc96
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/customizations/@plone/volto-slate/editor/render.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ export const Leaf = ({ children, ...rest }) => {
typeof children === 'string' ? (
children.split('\n').map((t, i) => {
// Softbreak support. Should do a plugin?
const hasSoftBreak =
children.indexOf('\n') > -1 && children.split('\n').length - 1 > i;
return (
<React.Fragment key={`${i}`}>
{children.indexOf('\n') > -1 &&
children.split('\n').length - 1 > i ? (
{hasSoftBreak ? (
<>
{klass ? <span className={klass}>{t}</span> : t}
<br />
Expand Down Expand Up @@ -104,7 +105,20 @@ export const serializeNodes = (nodes, getAttributes, extras = {}) => {
const editor = { children: nodes || [] };

const _serializeNodes = (nodes) => {
return (nodes || []).map(([node, path], i) => {
return (nodes || []).map(([node, path]) => {
let _serialized;
const isTextNode = Text.isText(node);
try {
if (!isTextNode) {
_serialized = _serializeNodes(
Array.from(Node.children(editor, path)),
);
}
} catch {
// eslint-disable-next-line no-console
console.error('Error in serializing nodes', editor, path);
}

return Text.isText(node) ? (
<Leaf path={path} leaf={node} text={node} mode="view" key={path}>
{node.text}
Expand All @@ -125,7 +139,7 @@ export const serializeNodes = (nodes, getAttributes, extras = {}) => {
}
extras={extras}
>
{_serializeNodes(Array.from(Node.children(editor, path)))}
{_serialized}
</Element>
);
});
Expand Down

0 comments on commit 9b7bc96

Please sign in to comment.