UNPKG

@prismicio/client

Version:

The official JavaScript + TypeScript client library for Prismic

95 lines (93 loc) 3.74 kB
const require_link = require('../types/value/link.cjs'); const require_asLink = require('../helpers/asLink.cjs'); const require_escapeHTML = require('./escapeHTML.cjs'); //#region src/lib/serializerHelpers.ts const formatAttributes = (node, attributes) => { const _attributes = { ...attributes }; if ("direction" in node && node.direction === "rtl") _attributes.dir = node.direction; if ("data" in node && "label" in node.data && node.data.label) _attributes.class = _attributes.class ? `${_attributes.class} ${node.data.label}` : node.data.label; const result = []; for (const key in _attributes) { const value = _attributes[key]; if (value) if (typeof value === "boolean") result.push(key); else result.push(`${key}="${require_escapeHTML.escapeHTML(value)}"`); } if (result.length) result.unshift(""); return result.join(" "); }; const getGeneralAttributes = (serializerOrShorthand) => { return serializerOrShorthand && typeof serializerOrShorthand !== "function" ? serializerOrShorthand : {}; }; const serializeStandardTag = (tag, serializerOrShorthand) => { const generalAttributes = getGeneralAttributes(serializerOrShorthand); return (({ node, children }) => { return `<${tag}${formatAttributes(node, generalAttributes)}>${children}</${tag}>`; }); }; const serializePreFormatted = (serializerOrShorthand) => { const generalAttributes = getGeneralAttributes(serializerOrShorthand); return ({ node }) => { return `<pre${formatAttributes(node, generalAttributes)}>${require_escapeHTML.escapeHTML(node.text)}</pre>`; }; }; const serializeImage = (linkResolver, serializerOrShorthand) => { const generalAttributes = getGeneralAttributes(serializerOrShorthand); return ({ node }) => { let imageTag = `<img${formatAttributes(node, { ...generalAttributes, src: node.url, alt: node.alt, copyright: node.copyright })} />`; if (node.linkTo) imageTag = serializeHyperlink(linkResolver)({ type: "hyperlink", node: { type: "hyperlink", data: node.linkTo, start: 0, end: 0 }, text: "", children: imageTag, key: "" }); return `<p class="block-img">${imageTag}</p>`; }; }; const serializeEmbed = (serializerOrShorthand) => { const generalAttributes = getGeneralAttributes(serializerOrShorthand); return ({ node }) => { return `<div${formatAttributes(node, { ...generalAttributes, ["data-oembed"]: node.oembed.embed_url, ["data-oembed-type"]: node.oembed.type, ["data-oembed-provider"]: node.oembed.provider_name })}>${node.oembed.html}</div>`; }; }; const serializeHyperlink = (linkResolver, serializerOrShorthand) => { const generalAttributes = getGeneralAttributes(serializerOrShorthand); return ({ node, children }) => { const attributes = { ...generalAttributes }; if (node.data.link_type === require_link.LinkType.Web) { attributes.href = node.data.url; attributes.target = node.data.target; attributes.rel = "noopener noreferrer"; } else if (node.data.link_type === require_link.LinkType.Document) attributes.href = require_asLink.asLink(node.data, { linkResolver }); else if (node.data.link_type === require_link.LinkType.Media) attributes.href = node.data.url; return `<a${formatAttributes(node, attributes)}>${children}</a>`; }; }; const serializeSpan = () => { return ({ text }) => { return text ? require_escapeHTML.escapeHTML(text).replace(/\n/g, "<br />") : ""; }; }; //#endregion exports.serializeEmbed = serializeEmbed; exports.serializeHyperlink = serializeHyperlink; exports.serializeImage = serializeImage; exports.serializePreFormatted = serializePreFormatted; exports.serializeSpan = serializeSpan; exports.serializeStandardTag = serializeStandardTag; //# sourceMappingURL=serializerHelpers.cjs.map