import { escapeHTML } from "./escapeHTML"; import { RTBlockNode, RTInlineNode, RTPreformattedNode, RTImageNode, RTEmbedNode, RTLinkNode, LinkType, RichTextNodeType, } from "@prismicio/types"; import { asLink } from "../asLink"; import { LinkResolverFunction } from "../types"; export const getLabel = (node: RTBlockNode | RTInlineNode): string => { return "data" in node && "label" in node.data ? ` class="${node.data.label}"` : ""; }; export const serializeStandardTag = ( tag: string, node: RTBlockNode | RTInlineNode, children: string[], ): string => { return `<${tag}${getLabel(node)}>${children.join("")}`; }; export const serializePreFormatted = (node: RTPreformattedNode): string => { return `${escapeHTML(node.text)}`; }; export const serializeImage = ( linkResolver: LinkResolverFunction | undefined | null, node: RTImageNode, ): string => { let imageTag = `${escapeHTML(node.alt)}`; // If the image has a link, we wrap it with an anchor tag if (node.linkTo) { imageTag = serializeHyperlink( linkResolver, { type: RichTextNodeType.hyperlink, data: node.linkTo, start: 0, end: 0, }, [imageTag], ); } return `

${imageTag}

`; }; export const serializeEmbed = (node: RTEmbedNode): string => { return `
${ node.oembed.html }
`; }; export const serializeHyperlink = ( linkResolver: LinkResolverFunction | undefined | null, node: RTLinkNode, children: string[], ): string => { switch (node.data.link_type) { case LinkType.Web: { return `${children.join("")}`; } case LinkType.Document: { return `${children.join("")}`; } case LinkType.Media: { return `${children.join( "", )}`; } } }; export const serializeSpan = (content?: string): string => { return content ? escapeHTML(content).replace(/\n/g, "
") : ""; };