UNPKG

@dossierhq/core

Version:

The core Dossier library used by clients and server alike, used to interact with schema and entities directly, as well as remotely through a client.

61 lines 2.43 kB
/// <reference types="./RichTextTransformer.d.ts" /> import { notOk, ok } from '../ErrorResult.js'; import { contentValuePathToString } from './ContentPath.js'; import { isRichTextElementNode, isRichTextParagraphNode, isRichTextRootNode, } from './ContentTypeUtils.js'; import { checkRichTextNodeTraversable } from './ContentUtils.js'; export function transformRichText(path, richText, transformer) { const transformResult = transformNode(path, richText.root, transformer); if (transformResult.isError()) return transformResult; const newRoot = transformResult.value; if (newRoot === null) return ok(null); if (!isRichTextRootNode(newRoot)) { return notOk.Generic('Rich text transformer didn’t return a root node'); } // normalize empty rich text if (newRoot.children.length === 0 || (newRoot.children.length === 1 && isRichTextParagraphNode(newRoot.children[0]) && newRoot.children[0].children.length === 0)) { return ok(null); } if (newRoot === richText.root) { return ok(richText); } return ok({ ...richText, root: newRoot }); } function transformNode(path, node, transformer) { const traversableError = checkRichTextNodeTraversable(node); if (traversableError) { return notOk.BadRequest(`${contentValuePathToString([...path, ...traversableError.path])}: ${traversableError.message}`); } const transformResult = transformer(path, node); if (transformResult.isError()) return transformResult; const newNode = transformResult.value; if (!newNode || !isRichTextElementNode(newNode)) { return ok(newNode); } const newChildren = []; let childrenHasChanged = false; for (let index = 0; index < newNode.children.length; index += 1) { const child = newNode.children[index]; const childPath = [...path, index]; const childResult = transformNode(childPath, child, transformer); if (childResult.isError()) return childResult; const newChild = childResult.value; if (newChild !== child) { childrenHasChanged = true; } if (newChild) { newChildren.push(newChild); } } if (!childrenHasChanged) { return ok(newNode); } return ok({ ...newNode, children: newChildren }); } //# sourceMappingURL=RichTextTransformer.js.map