|
| 1 | +import escape from "lodash/escape" |
| 2 | +import toArray from "lib/x/toArray" |
| 3 | +import toTree from "Editor/components/toReactTree/toTree" |
| 4 | + |
| 5 | +// Reads text content from tree-shaped text nodes. |
| 6 | +function textContent(treeTextNodes) { |
| 7 | + let str = "" |
| 8 | + for (const each of toArray(treeTextNodes)) { |
| 9 | + if (typeof each === "string") { |
| 10 | + str += each |
| 11 | + continue |
| 12 | + } |
| 13 | + str += each.props.children && |
| 14 | + textContent(each.props.children) |
| 15 | + } |
| 16 | + return str |
| 17 | +} |
| 18 | + |
| 19 | +// Resolves tree-shaped children to a resolver-type. |
| 20 | +function toInnerText(children, resolver) { |
| 21 | + let str = "" |
| 22 | + for (const each of toArray(children)) { |
| 23 | + if (typeof each === "string") { |
| 24 | + str += resolver !== markup ? each : escape(each) |
| 25 | + continue |
| 26 | + } |
| 27 | + str += resolver[each.type](each) |
| 28 | + } |
| 29 | + return str |
| 30 | +} |
| 31 | + |
| 32 | +// Converts to tree-shaped text nodes. |
| 33 | +function toText(children, resolver) { |
| 34 | + return toInnerText(toTree(children), resolver) |
| 35 | +} |
| 36 | + |
| 37 | +const markdown = { |
| 38 | + em: el => `_${toInnerText(el.props.children, markdown)}_`, |
| 39 | + strong: el => `**${toInnerText(el.props.children, markdown)}**`, |
| 40 | + code: el => `\`${textContent(el.props.children)}\``, |
| 41 | + strike: el => `~~${toInnerText(el.props.children, markdown)}~~`, |
| 42 | + |
| 43 | + // TODO |
| 44 | + a: el => `[${toInnerText(el.props.children, markdown)}](${el.props.href || "TODO"})`, |
| 45 | + |
| 46 | + "h1": el => `# ${toText(el.props.children, markdown)}`, |
| 47 | + "h2": el => `## ${toText(el.props.children, markdown)}`, |
| 48 | + "h3": el => `### ${toText(el.props.children, markdown)}`, |
| 49 | + "h4": el => `#### ${toText(el.props.children, markdown)}`, |
| 50 | + "h5": el => `##### ${toText(el.props.children, markdown)}`, |
| 51 | + "h6": el => `###### ${toText(el.props.children, markdown)}`, |
| 52 | + "p": el => toText(el.props.children, markdown), |
| 53 | +} |
| 54 | + |
| 55 | +const markup = { |
| 56 | + em: el => `<em>${toInnerText(el.props.children, markup)}</em>`, |
| 57 | + strong: el => `<strong>${toInnerText(el.props.children, markup)}</strong>`, |
| 58 | + code: el => `<code>${toInnerText(el.props.children, markup)}</code>`, |
| 59 | + strike: el => `<strike>${toInnerText(el.props.children, markup)}</strike>`, |
| 60 | + |
| 61 | + // TODO |
| 62 | + a: el => `<a href="${el.props.href}">${toInnerText(el.props.children, markup)}</a>`, |
| 63 | + |
| 64 | + "h1": el => `<h1>\n\t${toText(el.props.children, markup) || "<br />"}\n</h1>`, |
| 65 | + "h2": el => `<h2>\n\t${toText(el.props.children, markup) || "<br />"}\n</h2>`, |
| 66 | + "h3": el => `<h3>\n\t${toText(el.props.children, markup) || "<br />"}\n</h3>`, |
| 67 | + "h4": el => `<h4>\n\t${toText(el.props.children, markup) || "<br />"}\n</h4>`, |
| 68 | + "h5": el => `<h5>\n\t${toText(el.props.children, markup) || "<br />"}\n</h5>`, |
| 69 | + "h6": el => `<h6>\n\t${toText(el.props.children, markup) || "<br />"}\n</h6>`, |
| 70 | + "p": el => `<p>\n\t${toText(el.props.children, markup) || "<br />"}\n</p>`, |
| 71 | +} |
| 72 | + |
| 73 | +// Converts to GFM. |
| 74 | +export function toMarkdown(elements) { |
| 75 | + let str = "" |
| 76 | + for (const each of elements) { |
| 77 | + str += markdown[each.type](each) |
| 78 | + if (each !== elements[elements.length - 1]) { |
| 79 | + str += "\n" |
| 80 | + } |
| 81 | + } |
| 82 | + return str |
| 83 | +} |
| 84 | + |
| 85 | +// Converts to HTML. |
| 86 | +export function toMarkup(elements) { |
| 87 | + let str = "" |
| 88 | + for (const each of elements) { |
| 89 | + str += markup[each.type](each) |
| 90 | + if (each !== elements[elements.length - 1]) { |
| 91 | + str += "\n" |
| 92 | + } |
| 93 | + } |
| 94 | + return str |
| 95 | +} |
0 commit comments