UNPKG

@frontify/fondue

Version:
118 lines (117 loc) 3.32 kB
import { MENTION_DESERIALIZE_REGEX as k, isMaliciousLink as o } from "../utils.es.js"; function x(e, r, l) { var c, u, p; const t = r == null ? void 0 : r.nodeTypes, { linkDestinationKey: m, imageSourceKey: d, imageCaptionKey: g } = K(r); let a = [{ text: "" }]; switch (v(e.children) && (a = e.children.flatMap( (i) => x( { ...i, ordered: e.ordered || !1 }, r, l ) )), e.type) { case "mention": { const i = e.children ? e.children[0].value : void 0, h = i == null ? void 0 : i.match(k); return { type: t.mention, category: h[1], id: h[2], children: [{ text: "" }] }; } case "heading": return { type: t.heading[e.depth || 1], children: a }; case "list": return { type: e.ordered ? t.olList : t.ulList, children: a }; case "listItem": return { type: t.listItem, children: a }; case "listItemChild": return { type: t.listItemChild, children: a }; case "paragraph": return { type: t.paragraph, children: a }; case "link": return { type: t.link, [m]: y(e.url, l == null ? void 0 : l.allowUnsafeLink), target: e.target, children: a }; case "image": return { type: t.image, children: [{ text: "" }], [d]: y(e.url, l == null ? void 0 : l.allowUnsafeLink), [g]: e.alt }; case "blockquote": return { type: t.blockQuote, children: a }; case "code": return { type: t.codeBlock, language: e.lang ?? void 0, children: [{ text: e.value }] }; case "html": return (c = e.value) != null && c.includes("<br>") ? { break: !0, type: t.paragraph, children: [{ text: ((u = e.value) == null ? void 0 : u.replace(/<br>/g, "")) || "" }] } : { type: t.paragraph, children: [{ text: e.value || "" }] }; case "emphasis": return { [t.emphasisMark]: !0, ...n(a), ...s(a) }; case "strong": return { [t.strongMark]: !0, ...n(a), ...s(a) }; case "delete": return { [t.deleteMark]: !0, ...n(a), ...s(a) }; case "inlineCode": return { [t.inlineCodeMark]: !0, text: e.value, ...s(a) }; case "thematicBreak": return { type: t.thematicBreak, children: [{ text: "" }] }; case "text": return { text: ((p = e.value) == null ? void 0 : p.replace(/\u00A0/g, "").length) === 0 ? "" : e.value }; default: return { text: e.value ?? "" }; } } const v = (e) => !!e && Array.isArray(e) && e.length > 0, n = (e) => ({ text: e.map((r) => r == null ? void 0 : r.text).join("") }), s = (e) => e.reduce((r, l) => { for (const t of Object.keys(l)) t === "children" || t === "type" || t === "text" || (r[t] = l[t]); return r; }, {}), K = (e) => { const r = e.linkDestinationKey ?? "link", l = e.imageSourceKey ?? "link", t = e.imageCaptionKey ?? "caption"; return { linkDestinationKey: r, imageSourceKey: l, imageCaptionKey: t }; }, y = (e, r = !1) => !r && e && !o(e) ? e : void 0; export { x as default }; //# sourceMappingURL=deserialize.es.js.map