Created
March 28, 2020 10:03
-
-
Save abhishekbhardwaj/168c13869ac043f070e954e0057dcbd7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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