arunkumar
4 years ago
5 changed files with 128 additions and 8 deletions
@ -0,0 +1,17 @@ |
|||||
|
const BOLD = "bold"; |
||||
|
const TEXT = "text"; |
||||
|
const ITALICS = "italics"; |
||||
|
const STRIKETHROUGH = "strikethrough"; |
||||
|
const UNDERLINE = "underline"; |
||||
|
const URL = "url"; |
||||
|
const PARAGRAPH = "paragraph"; |
||||
|
|
||||
|
module.exports = { |
||||
|
BOLD, |
||||
|
TEXT, |
||||
|
ITALICS, |
||||
|
STRIKETHROUGH, |
||||
|
UNDERLINE, |
||||
|
URL, |
||||
|
PARAGRAPH, |
||||
|
}; |
@ -0,0 +1,59 @@ |
|||||
|
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; |
Loading…
Reference in new issue