Skip to content

Instantly share code, notes, and snippets.

@abhishekbhardwaj
Created March 28, 2020 10:03
Show Gist options
  • Save abhishekbhardwaj/168c13869ac043f070e954e0057dcbd7 to your computer and use it in GitHub Desktop.
Save abhishekbhardwaj/168c13869ac043f070e954e0057dcbd7 to your computer and use it in GitHub Desktop.
import escapeHtml from "escape-html";
import { Text } from "slate";
export const serialize = node => {
let nodeText = escapeHtml(node.text);
if (Text.isText(node)) {
if (node["bold"]) {
nodeText = `<strong>` + nodeText + `</strong>`;
}
if (node["italic"]) {
nodeText = `<em>` + nodeText + `</em>`;
}
if (node["underlined"]) {
nodeText = `<u>` + nodeText + `</u>`;
}
// Other marks should go here like above
return nodeText;
}
if (Array.isArray(node)) {
return node.map(subNode => serializeSubNode(subNode)).join("");
}
return serializeSubNode(node);
};
const serializeSubNode = node => {
const children = node.children.map(n => serialize(n)).join("");
switch (node.type) {
case "link":
return `<a href="${escapeHtml(node.url)}">${children}</a>`;
default:
return `<p>${children}</p>`;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment