const h = require("react").createElement; const { BOLD, TEXT, ITALICS, STRIKETHROUGH, UNDERLINE, URL, PARAGRAPH, } = require("../constants"); function CompileReact(ast) { const OpenTags = { bold: "span", text: "span", italics: "span", strikethrough: "span", underline: "span", url: "a", paragraph: "p", }; const classNames = { bold: BOLD, text: TEXT, italics: ITALICS, strikethrough: STRIKETHROUGH, underline: UNDERLINE, url: URL, paragraph: PARAGRAPH, }; const mapper = node => node.map((element, index) => { if (typeof element === "string") { return element; } else if (typeof element === "object") { const makeProps = () => { let props = {}; if (element.type === "url") { props.href = element.url; } props.className = classNames[element.type]; props.key = element.type + "-" + index; return props; }; return h( OpenTags[element.type], makeProps(), element.children && mapper(element.children), ); } }); return h("p", { className: PARAGRAPH }, mapper(ast)); } module.exports = CompileReact;