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.
 

47 lines
1010 B

const {
BOLD,
TEXT,
ITALICS,
STRIKETHROUGH,
UNDERLINE,
URL,
} = require("../constants");
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;