2016-04-18 16:59:04 +08:00
|
|
|
import React, { Children, cloneElement } from 'react';
|
2016-03-07 17:33:38 +08:00
|
|
|
import { Link } from 'react-router';
|
2016-02-29 14:08:40 +08:00
|
|
|
import * as utils from '../utils';
|
2016-04-13 16:48:56 +08:00
|
|
|
import { getTagName, getChildren } from 'jsonml.js/lib/utils';
|
2016-04-18 16:59:04 +08:00
|
|
|
import { Timeline } from 'antd';
|
2016-02-29 14:08:40 +08:00
|
|
|
|
2016-03-07 15:20:18 +08:00
|
|
|
export default class Article extends React.Component {
|
2016-03-23 14:15:00 +08:00
|
|
|
componentDidMount() {
|
|
|
|
this.componentDidUpdate();
|
|
|
|
}
|
|
|
|
componentDidUpdate() {
|
|
|
|
const { chinese, english } = this.props.content.meta;
|
|
|
|
utils.setTitle(`${chinese || english} - Ant Design`);
|
|
|
|
}
|
2016-04-18 16:59:04 +08:00
|
|
|
getTimelineFromArticle(article) {
|
|
|
|
const { content } = this.props;
|
|
|
|
const { meta } = content;
|
|
|
|
if (!meta.timeline) {
|
|
|
|
return article;
|
|
|
|
}
|
|
|
|
const timelineItems = [];
|
|
|
|
let temp = [];
|
|
|
|
Children.forEach(article.props.children, (child, i) => {
|
|
|
|
if (child.type === 'h2' && temp.length > 0) {
|
|
|
|
timelineItems.push(<Timeline.Item key={i}>{temp}</Timeline.Item>);
|
|
|
|
temp = [];
|
|
|
|
}
|
|
|
|
temp.push(child);
|
|
|
|
});
|
|
|
|
return cloneElement(article, {
|
|
|
|
children: <Timeline>{timelineItems}</Timeline>,
|
|
|
|
});
|
|
|
|
}
|
2016-02-29 14:08:40 +08:00
|
|
|
render() {
|
2016-03-07 17:33:38 +08:00
|
|
|
const { content, location } = this.props;
|
2016-02-29 14:08:40 +08:00
|
|
|
const jumper = content.description.filter((node) => {
|
2016-04-13 10:13:07 +08:00
|
|
|
return getTagName(node) === 'h2';
|
2016-02-29 14:08:40 +08:00
|
|
|
}).map((node) => {
|
2016-03-07 17:33:38 +08:00
|
|
|
return (
|
2016-04-13 10:13:07 +08:00
|
|
|
<li key={getChildren(node)[0]}>
|
|
|
|
<Link to={{ pathname: location.pathname, query: { scrollTo: getChildren(node)[0] } }}>
|
2016-04-29 12:13:27 +08:00
|
|
|
{utils.jsonmlToComponent(location.pathname, getChildren(node)[0])}
|
2016-04-06 16:14:00 +08:00
|
|
|
</Link>
|
2016-03-07 17:33:38 +08:00
|
|
|
</li>
|
|
|
|
);
|
2016-02-29 14:08:40 +08:00
|
|
|
});
|
2016-02-29 14:34:51 +08:00
|
|
|
|
2016-04-13 16:48:56 +08:00
|
|
|
const { meta, intro, description } = content;
|
2016-03-02 17:12:43 +08:00
|
|
|
|
2016-02-29 14:08:40 +08:00
|
|
|
return (
|
|
|
|
<article className="markdown">
|
2016-03-15 11:19:57 +08:00
|
|
|
<h1>
|
2016-04-18 16:59:04 +08:00
|
|
|
{meta.english} {meta.chinese}
|
2016-03-15 11:19:57 +08:00
|
|
|
{
|
2016-04-01 16:52:47 +08:00
|
|
|
!meta.subtitle ? null :
|
2016-04-29 12:13:27 +08:00
|
|
|
<span className="subtitle">{meta.subtitle}</span>
|
2016-03-15 11:19:57 +08:00
|
|
|
}
|
|
|
|
</h1>
|
|
|
|
{
|
2016-04-01 16:52:47 +08:00
|
|
|
!intro ? null :
|
|
|
|
utils.jsonmlToComponent(
|
|
|
|
location.pathname,
|
|
|
|
['section', { className: 'markdown' }].concat(intro)
|
|
|
|
)
|
2016-03-15 11:19:57 +08:00
|
|
|
}
|
2016-02-29 14:08:40 +08:00
|
|
|
{
|
2016-04-18 16:59:04 +08:00
|
|
|
(jumper.length > 0 && meta.toc !== false) ?
|
2016-04-29 12:13:27 +08:00
|
|
|
<section className="toc"><ul>{jumper}</ul></section> :
|
2016-02-29 14:08:40 +08:00
|
|
|
null
|
|
|
|
}
|
2016-04-01 16:52:47 +08:00
|
|
|
{
|
2016-04-18 16:59:04 +08:00
|
|
|
this.getTimelineFromArticle(utils.jsonmlToComponent(
|
2016-04-01 16:52:47 +08:00
|
|
|
location.pathname,
|
|
|
|
['section', { className: 'markdown' }].concat(description)
|
2016-04-18 16:59:04 +08:00
|
|
|
))
|
2016-04-01 16:52:47 +08:00
|
|
|
}
|
2016-02-29 14:08:40 +08:00
|
|
|
</article>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|