You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
895 B
38 lines
895 B
4 years ago
|
function CompileHTML(ast) {
|
||
|
let str = "<p>";
|
||
|
const OpenTags = {
|
||
|
bold: () => `<span class="bold">`,
|
||
|
text: () => `<span class="text">`,
|
||
|
italics: () => `<span class="italics">`,
|
||
|
strikethrough: () => `<span class="strikethrough">`,
|
||
|
underline: () => `<span class="underline">`,
|
||
|
url: url => `<a href="${url}" class="url">`,
|
||
|
};
|
||
|
|
||
|
const CloseTags = {
|
||
|
text: "</span>",
|
||
|
bold: "</span>",
|
||
|
italics: "</span>",
|
||
|
strikethrough: "</span>",
|
||
|
underline: "</span>",
|
||
|
url: "</a>",
|
||
|
};
|
||
|
const mapper = node => {
|
||
|
node.forEach(element => {
|
||
|
if (typeof element === "string") {
|
||
|
str += `${element}`;
|
||
|
} else if (typeof element === "object") {
|
||
|
str += OpenTags[element.type]("url" in element ? element.url : null);
|
||
|
mapper(element.children);
|
||
|
str += CloseTags[element.type];
|
||
|
}
|
||
|
});
|
||
|
return "";
|
||
|
};
|
||
|
mapper(ast);
|
||
|
str += "</p>";
|
||
|
return str;
|
||
|
}
|
||
|
|
||
|
module.exports = CompileHTML;
|