|
|
@ -1,6 +1,7 @@ |
|
|
|
import React from 'react'; |
|
|
|
import ReactDOM from 'react-dom'; |
|
|
|
import { Link } from 'react-router'; |
|
|
|
import { getTagName, getAttributes, getChildren } from 'jsonml.js/lib/utils'; |
|
|
|
import toReactComponent from 'jsonml-to-react-component'; |
|
|
|
|
|
|
|
function isHeading(type) { |
|
|
@ -15,13 +16,15 @@ export function jsonmlToComponent(pathname, jsonml) { |
|
|
|
[(node) => typeof node === 'function', (node, index) => { |
|
|
|
return React.cloneElement(node(React, ReactDOM), { key: index }); |
|
|
|
}], |
|
|
|
[(node) => isHeading(node[0]), (node, index) => { |
|
|
|
return React.createElement(node[0], { |
|
|
|
[(node) => isHeading(getTagName(node)), (node, index) => { |
|
|
|
const children = getChildren(node); |
|
|
|
return React.createElement(getTagName(node), { |
|
|
|
key: index, |
|
|
|
id: node[1], |
|
|
|
id: children, |
|
|
|
...getAttributes(node), |
|
|
|
}, [ |
|
|
|
<span key="title" dangerouslySetInnerHTML={{ __html: node[1] }} />, |
|
|
|
<Link to={{ pathname, query: { scrollTo: node[1] } }} className="anchor" key="anchor">#</Link>, |
|
|
|
<span key="title">{ children.map(toReactComponent.bind(null, [])) }</span>, |
|
|
|
<Link to={{ pathname, query: { scrollTo: children } }} className="anchor" key="anchor">#</Link>, |
|
|
|
]); |
|
|
|
}], |
|
|
|
], jsonml); |
|
|
|