ant-design/site/component/Article/index.jsx

80 lines
2.0 KiB
React
Raw Normal View History

2016-02-29 14:08:40 +08:00
import React from 'react';
2016-03-07 17:33:38 +08:00
import { Link } from 'react-router';
2016-03-02 17:12:43 +08:00
import ImagePreview from './ImagePreview';
2016-03-16 17:46:35 +08:00
import VideoPlayer from './VideoPlayer';
2016-02-29 14:08:40 +08:00
import * as utils from '../utils';
2016-03-07 15:20:18 +08:00
export default class Article extends React.Component {
constructor(props) {
super(props);
2016-03-02 17:12:43 +08:00
2016-03-07 15:20:18 +08:00
this.imgToPreview = this.imgToPreview.bind(this);
2016-03-16 17:46:35 +08:00
this.enhanceVideo = this.enhanceVideo.bind(this);
2016-03-07 15:20:18 +08:00
}
2016-03-07 17:33:38 +08:00
2016-03-07 15:20:18 +08:00
isPreviewImg(string) {
return /^<img\s/i.test(string) && /preview-img/gi.test(string);
2016-03-02 17:12:43 +08:00
}
2016-03-07 15:20:18 +08:00
imgToPreview(node) {
if (!this.isPreviewImg(node.children)) {
return node;
}
const imgs = node.children.split(/\r|\n/);
2016-03-17 15:00:06 +08:00
return <ImagePreview imgs={imgs} />;
2016-03-07 15:20:18 +08:00
}
2016-03-02 17:12:43 +08:00
2016-03-16 17:46:35 +08:00
isVideo(string) {
return /^<video\s/i.test(string);
}
enhanceVideo(node) {
if (!this.isVideo(node.children)) {
return node;
}
return <VideoPlayer video={node.children} />;
}
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) => {
return node.type === 'h2';
}).map((node) => {
2016-03-07 17:33:38 +08:00
return (
<li key={node.children}>
2016-03-15 16:51:22 +08:00
<Link to={{ pathname: location.pathname, query: { scrollTo: node.children } }}
dangerouslySetInnerHTML={{ __html: node.children }} />
2016-03-07 17:33:38 +08:00
</li>
);
2016-02-29 14:08:40 +08:00
});
2016-03-16 17:46:35 +08:00
content.description = content.description
.map(this.imgToPreview)
.map(this.enhanceVideo);
2016-03-02 17:12:43 +08:00
2016-02-29 14:08:40 +08:00
return (
<article className="markdown">
<h1>
{ content.meta.chinese || content.meta.english }
{
!content.meta.subtitle ? null :
<span className="subtitle">{ content.meta.subtitle }</span>
}
</h1>
{
!content.intro ? null :
content.intro.map(utils.objectToComponent.bind(null, location.pathname))
}
2016-02-29 14:08:40 +08:00
{
jumper.length > 0 ?
<section className="toc"><ul>{ jumper }</ul></section> :
2016-02-29 14:08:40 +08:00
null
}
2016-03-07 17:33:38 +08:00
{ content.description.map(utils.objectToComponent.bind(null, location.pathname)) }
2016-02-29 14:08:40 +08:00
</article>
);
}
}