@prismicio/client
Version:
The official JavaScript + TypeScript client library for Prismic
1 lines • 8.04 kB
Source Map (JSON)
{"version":3,"file":"serializerHelpers.cjs","sources":["../../../src/lib/serializerHelpers.ts"],"sourcesContent":["import type { RichTextMapSerializer } from \"../richtext/types\"\nimport { LinkType } from \"../types/value/link\"\nimport type { RTAnyNode } from \"../types/value/richText\"\n\nimport type {\n\tHTMLRichTextMapSerializer,\n\tHTMLStrictRichTextMapSerializer,\n} from \"../helpers/asHTML\"\nimport type { LinkResolverFunction } from \"../helpers/asLink\"\nimport { asLink } from \"../helpers/asLink\"\n\nimport { escapeHTML } from \"./escapeHTML\"\n\ntype Attributes = Record<string, string | boolean | null | undefined>\nconst formatAttributes = (node: RTAnyNode, attributes: Attributes): string => {\n\tconst _attributes = { ...attributes }\n\n\t// Respect `ltr` and `rtl` direction\n\tif (\"direction\" in node && node.direction === \"rtl\") {\n\t\t_attributes.dir = node.direction\n\t}\n\n\t// Add label to attributes\n\tif (\"data\" in node && \"label\" in node.data && node.data.label) {\n\t\t_attributes.class = _attributes.class\n\t\t\t? `${_attributes.class} ${node.data.label}`\n\t\t\t: node.data.label\n\t}\n\n\tconst result = []\n\n\tfor (const key in _attributes) {\n\t\tconst value = _attributes[key]\n\n\t\tif (value) {\n\t\t\tif (typeof value === \"boolean\") {\n\t\t\t\tresult.push(key)\n\t\t\t} else {\n\t\t\t\tresult.push(`${key}=\"${escapeHTML(value)}\"`)\n\t\t\t}\n\t\t}\n\t}\n\n\t// Add a space at the beginning if there's any result\n\tif (result.length) {\n\t\tresult.unshift(\"\")\n\t}\n\n\treturn result.join(\" \")\n}\n\nconst getGeneralAttributes = (\n\tserializerOrShorthand?: HTMLRichTextMapSerializer[keyof HTMLRichTextMapSerializer],\n): Attributes => {\n\treturn serializerOrShorthand && typeof serializerOrShorthand !== \"function\"\n\t\t? serializerOrShorthand\n\t\t: {}\n}\n\nexport const serializeStandardTag = <\n\tBlockType extends keyof RichTextMapSerializer<string>,\n>(\n\ttag: string,\n\tserializerOrShorthand?: HTMLRichTextMapSerializer[BlockType],\n): NonNullable<HTMLStrictRichTextMapSerializer[BlockType]> => {\n\tconst generalAttributes = getGeneralAttributes(serializerOrShorthand)\n\n\treturn (({ node, children }) => {\n\t\treturn `<${tag}${formatAttributes(\n\t\t\tnode,\n\t\t\tgeneralAttributes,\n\t\t)}>${children}</${tag}>`\n\t}) as NonNullable<HTMLStrictRichTextMapSerializer[BlockType]>\n}\n\nexport const serializePreFormatted = (\n\tserializerOrShorthand?: HTMLRichTextMapSerializer[\"preformatted\"],\n): NonNullable<HTMLStrictRichTextMapSerializer[\"preformatted\"]> => {\n\tconst generalAttributes = getGeneralAttributes(serializerOrShorthand)\n\n\treturn ({ node }) => {\n\t\treturn `<pre${formatAttributes(node, generalAttributes)}>${escapeHTML(\n\t\t\tnode.text,\n\t\t)}</pre>`\n\t}\n}\n\nexport const serializeImage = (\n\tlinkResolver:\n\t\t| LinkResolverFunction<string | null | undefined>\n\t\t| undefined\n\t\t| null,\n\tserializerOrShorthand?: HTMLRichTextMapSerializer[\"image\"],\n): NonNullable<HTMLStrictRichTextMapSerializer[\"image\"]> => {\n\tconst generalAttributes = getGeneralAttributes(serializerOrShorthand)\n\n\treturn ({ node }) => {\n\t\tconst attributes = {\n\t\t\t...generalAttributes,\n\t\t\tsrc: node.url,\n\t\t\talt: node.alt,\n\t\t\tcopyright: node.copyright,\n\t\t}\n\n\t\tlet imageTag = `<img${formatAttributes(node, attributes)} />`\n\n\t\t// If the image has a link, we wrap it with an anchor tag\n\t\tif (node.linkTo) {\n\t\t\timageTag = serializeHyperlink(linkResolver)({\n\t\t\t\ttype: \"hyperlink\",\n\t\t\t\tnode: {\n\t\t\t\t\ttype: \"hyperlink\",\n\t\t\t\t\tdata: node.linkTo,\n\t\t\t\t\tstart: 0,\n\t\t\t\t\tend: 0,\n\t\t\t\t},\n\t\t\t\ttext: \"\",\n\t\t\t\tchildren: imageTag,\n\t\t\t\tkey: \"\",\n\t\t\t})!\n\t\t}\n\n\t\treturn `<p class=\"block-img\">${imageTag}</p>`\n\t}\n}\n\nexport const serializeEmbed = (\n\tserializerOrShorthand?: HTMLRichTextMapSerializer[\"embed\"],\n): NonNullable<HTMLStrictRichTextMapSerializer[\"embed\"]> => {\n\tconst generalAttributes = getGeneralAttributes(serializerOrShorthand)\n\n\treturn ({ node }) => {\n\t\tconst attributes = {\n\t\t\t...generalAttributes,\n\t\t\t[\"data-oembed\"]: node.oembed.embed_url,\n\t\t\t[\"data-oembed-type\"]: node.oembed.type,\n\t\t\t[\"data-oembed-provider\"]: node.oembed.provider_name,\n\t\t}\n\n\t\treturn `<div${formatAttributes(node, attributes)}>${node.oembed.html}</div>`\n\t}\n}\n\nexport const serializeHyperlink = (\n\tlinkResolver:\n\t\t| LinkResolverFunction<string | null | undefined>\n\t\t| undefined\n\t\t| null,\n\tserializerOrShorthand?: HTMLRichTextMapSerializer[\"hyperlink\"],\n): NonNullable<HTMLStrictRichTextMapSerializer[\"hyperlink\"]> => {\n\tconst generalAttributes = getGeneralAttributes(serializerOrShorthand)\n\n\treturn ({ node, children }): string => {\n\t\tconst attributes = {\n\t\t\t...generalAttributes,\n\t\t}\n\n\t\tif (node.data.link_type === LinkType.Web) {\n\t\t\tattributes.href = node.data.url\n\t\t\tattributes.target = node.data.target\n\t\t\tattributes.rel = \"noopener noreferrer\"\n\t\t} else if (node.data.link_type === LinkType.Document) {\n\t\t\tattributes.href = asLink(node.data, { linkResolver })\n\t\t} else if (node.data.link_type === LinkType.Media) {\n\t\t\tattributes.href = node.data.url\n\t\t}\n\n\t\treturn `<a${formatAttributes(node, attributes)}>${children}</a>`\n\t}\n}\n\nexport const serializeSpan = (): NonNullable<\n\tHTMLStrictRichTextMapSerializer[\"span\"]\n> => {\n\treturn ({ text }): string => {\n\t\treturn text ? escapeHTML(text).replace(/\\n/g, \"<br />\") : \"\"\n\t}\n}\n"],"names":["escapeHTML","LinkType","asLink"],"mappings":";;;;;AAcA,MAAM,mBAAmB,CA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