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.

60 lines
1.1 KiB

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;