2016-02-29 14:08:40 +08:00
|
|
|
import React from 'react';
|
2016-03-04 15:19:23 +08:00
|
|
|
import ReactDOM from 'react-dom';
|
2016-03-07 17:33:38 +08:00
|
|
|
import { Link } from 'react-router';
|
2016-04-06 16:14:00 +08:00
|
|
|
import { getTagName, getAttributes, getChildren } from 'jsonml.js/lib/utils';
|
2016-04-01 16:52:47 +08:00
|
|
|
import toReactComponent from 'jsonml-to-react-component';
|
2016-02-29 14:08:40 +08:00
|
|
|
|
2016-03-01 16:20:32 +08:00
|
|
|
function isHeading(type) {
|
|
|
|
return /h[1-6]/i.test(type);
|
|
|
|
}
|
|
|
|
|
2016-04-01 16:52:47 +08:00
|
|
|
export function jsonmlToComponent(pathname, jsonml) {
|
|
|
|
return toReactComponent([
|
|
|
|
[(node) => React.isValidElement(node), (node, index) => {
|
|
|
|
return React.cloneElement(node, { key: index });
|
|
|
|
}],
|
|
|
|
[(node) => typeof node === 'function', (node, index) => {
|
|
|
|
return React.cloneElement(node(React, ReactDOM), { key: index });
|
|
|
|
}],
|
2016-04-06 16:14:00 +08:00
|
|
|
[(node) => isHeading(getTagName(node)), (node, index) => {
|
|
|
|
const children = getChildren(node);
|
|
|
|
return React.createElement(getTagName(node), {
|
2016-04-01 16:52:47 +08:00
|
|
|
key: index,
|
2016-04-06 16:14:00 +08:00
|
|
|
id: children,
|
|
|
|
...getAttributes(node),
|
2016-04-01 16:52:47 +08:00
|
|
|
}, [
|
2016-04-06 16:14:00 +08:00
|
|
|
<span key="title">{ children.map(toReactComponent.bind(null, [])) }</span>,
|
|
|
|
<Link to={{ pathname, query: { scrollTo: children } }} className="anchor" key="anchor">#</Link>,
|
2016-04-01 16:52:47 +08:00
|
|
|
]);
|
|
|
|
}],
|
|
|
|
], jsonml);
|
2016-02-29 14:08:40 +08:00
|
|
|
}
|
2016-03-23 14:15:00 +08:00
|
|
|
|
|
|
|
export function setTitle(title) {
|
2016-03-23 14:29:16 +08:00
|
|
|
document.title = title;
|
2016-03-23 14:15:00 +08:00
|
|
|
}
|