Skip to content

Commit

Permalink
refactor: extract serializer helpers into a dedicated file
Browse files Browse the repository at this point in the history
  • Loading branch information
lihbr committed Jun 25, 2021
1 parent a565d02 commit 5d1d2a8
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 89 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
"test": "yarn lint && yarn unit"
},
"dependencies": {
"@prismicio/richtext": "^2.0.0-alpha.0",
"@prismicio/types": "~0.0.10",
"@prismicio/richtext": "^2.0.0-alpha.1",
"@prismicio/types": "~0.0.11",
"escape-html": "^1.0.3"
},
"devDependencies": {
Expand Down
86 changes: 8 additions & 78 deletions src/asHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,92 +4,22 @@ import {
composeSerializers,
wrapMapSerializer
} from "@prismicio/richtext";
import { RichTextField } from "@prismicio/types";

import {
LinkType,
RichTextField,
RTBlockNode,
RTEmbedNode,
RTImageNode,
RTInlineNode,
RTLinkNode,
RTPreformattedNode
} from "@prismicio/types";
serializeStandardTag,
serializePreFormatted,
serializeImage,
serializeEmbed,
serializeHyperlink,
serializeSpan
} from "./lib/serializerHelpers";
import {
HTMLSerializerFunction,
HTMLSerializerMap,
LinkResolverFunction
} from "./types";

import escapeHtml from "escape-html";
import { asLink } from "./asLink";

function label(node: RTBlockNode | RTInlineNode) {
return "data" in node && "label" in node.data
? ` class="${node.data.label}"`
: "";
}

function serializeStandardTag(
tag: string,
node: RTBlockNode | RTInlineNode,
children: string[]
) {
return `<${tag}${label(node)}>${children.join("")}</${tag}>`;
}

function serializePreFormatted(node: RTPreformattedNode) {
return `<pre${label(node)}>${escapeHtml(node.text)}</pre>`;
}

// TODO: Check link behavior with image + maybe rewrap with paragraph
function serializeImage(
_linkResolver: LinkResolverFunction<string>,
node: RTImageNode
) {
return `<img src="${node.url}" alt="${node.alt}"${
node.copyright ? ` copyright="${node.copyright}"` : ""
} />`;
}

function serializeEmbed(node: RTEmbedNode) {
return `<div data-oembed="${node.oembed.embed_url}" data-oembed-type="${
node.oembed.type
}" data-oembed-provider="${node.oembed.provider_name}"${label(node)}>${
node.oembed.html
}</div>`;
}

function serializeHyperlink(
linkResolver: LinkResolverFunction,
node: RTLinkNode,
children: string[]
) {
switch (node.data.link_type) {
case LinkType.Web: {
return `<a href="${node.data.url}" target="${
node.data.target
}" rel="noopener noreferrer"${label(node)}>${children.join("")}</a>`;
}

case LinkType.Document: {
return `<a href="${asLink(node.data, linkResolver)}"${label(
node
)}>${children.join("")}</a>`;
}

case LinkType.Media: {
return `<a href="${node.data.url}"${label(node)}>${children.join(
""
)}</a>`;
}
}
}

function serializeSpan(content?: string) {
return content ? escapeHtml(content).replace(/\n/g, "<br />") : "";
}

function defaultHTMLSerializer(
linkResolver: LinkResolverFunction<string>,
_type: Parameters<HTMLSerializerFunction>[0],
Expand Down
79 changes: 79 additions & 0 deletions src/lib/serializerHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import escapeHtml from "escape-html";
import {
RTBlockNode,
RTInlineNode,
RTPreformattedNode,
RTImageNode,
RTEmbedNode,
RTLinkNode,
LinkType
} 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("")}</${tag}>`;
};

export const serializePreFormatted = (node: RTPreformattedNode): string => {
return `<pre${getLabel(node)}>${escapeHtml(node.text)}</pre>`;
};

// TODO: Check link behavior with image + maybe rewrap with paragraph
export const serializeImage = (
_linkResolver: LinkResolverFunction<string>,
node: RTImageNode
): string => {
return `<img src="${node.url}" alt="${node.alt}"${
node.copyright ? ` copyright="${node.copyright}"` : ""
} />`;
};

export const serializeEmbed = (node: RTEmbedNode): string => {
return `<div data-oembed="${node.oembed.embed_url}" data-oembed-type="${
node.oembed.type
}" data-oembed-provider="${node.oembed.provider_name}"${getLabel(node)}>${
node.oembed.html
}</div>`;
};

export const serializeHyperlink = (
linkResolver: LinkResolverFunction,
node: RTLinkNode,
children: string[]
): string => {
switch (node.data.link_type) {
case LinkType.Web: {
return `<a href="${node.data.url}" target="${
node.data.target
}" rel="noopener noreferrer"${getLabel(node)}>${children.join("")}</a>`;
}

case LinkType.Document: {
return `<a href="${asLink(node.data, linkResolver)}"${getLabel(
node
)}>${children.join("")}</a>`;
}

case LinkType.Media: {
return `<a href="${node.data.url}"${getLabel(node)}>${children.join(
""
)}</a>`;
}
}
};

export const serializeSpan = (content?: string): string => {
return content ? escapeHtml(content).replace(/\n/g, "<br />") : "";
};
18 changes: 9 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -267,17 +267,17 @@
"@nodelib/fs.scandir" "2.1.5"
fastq "^1.6.0"

"@prismicio/richtext@^2.0.0-alpha.0":
version "2.0.0-alpha.0"
resolved "https://registry.yarnpkg.com/@prismicio/richtext/-/richtext-2.0.0-alpha.0.tgz#2f64af8a4893b1333f3836867d8531b8d90941db"
integrity sha512-hscYpg7vpLyduybiAK+8kNZPJaQ9sgyqJxO9vOoHXULdIAZYc3wGgZxKl+/JbhPia+1wtdirG7JIpWnZ1w4xPw==
"@prismicio/richtext@^2.0.0-alpha.1":
version "2.0.0-alpha.1"
resolved "https://registry.yarnpkg.com/@prismicio/richtext/-/richtext-2.0.0-alpha.1.tgz#29756f4dc78bbcedf16714cca1f61ec9dd1a9e69"
integrity sha512-tTSxp8XySM9j5CkMyWywl9R74PsqYi8vJ8LiZy3PR9NysE28TVSBn3Glgn+3YBwBLIV1VkwbKLPWEHf0oZ3Pyg==
dependencies:
"@prismicio/types" "^0.0.10"
"@prismicio/types" "^0.0.11"

"@prismicio/types@^0.0.10", "@prismicio/types@~0.0.10":
version "0.0.10"
resolved "https://registry.yarnpkg.com/@prismicio/types/-/types-0.0.10.tgz#b3b93a460393c650d8a0ce95ea8620f1e08f49da"
integrity sha512-B55j/YEZbhYWjpli70+RbuX0NHFRlqM6q0tQHw7Dr4ovGqHNnTJqGH8zVFuKqlmH5SLk/7jGSi7fcWconS94Tw==
"@prismicio/types@^0.0.11", "@prismicio/types@~0.0.11":
version "0.0.11"
resolved "https://registry.yarnpkg.com/@prismicio/types/-/types-0.0.11.tgz#97d73ef5abed5ce9623bf7c8fcacf4f9bac8d9a3"
integrity sha512-QsGW0quZ2NJDaDl+XvUhiafo7xUWOCKkJlvWwNaG1vWXB73b5cj0dk0UAPmoVOY3xx65JXTGKhBBkNKna8DhfA==

"@rollup/plugin-alias@^3.1.2":
version "3.1.2"
Expand Down

0 comments on commit 5d1d2a8

Please sign in to comment.