ant-design/site/component/utils.js

36 lines
1.1 KiB
JavaScript
Raw Normal View History

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';
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) {
2016-04-11 10:12:48 +08:00
return toReactComponent(jsonml, [
2016-04-01 16:52:47 +08:00
[(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 });
}],
[(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,
id: children,
...getAttributes(node),
2016-04-01 16:52:47 +08:00
}, [
2016-04-11 10:12:48 +08:00
<span key="title">{ children.map((child) => toReactComponent(child)) }</span>,
<Link to={{ pathname, query: { scrollTo: children } }} className="anchor" key="anchor">#</Link>,
2016-04-01 16:52:47 +08:00
]);
}],
2016-04-11 10:12:48 +08:00
]);
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
}