ant-design/site/component/Article/index.jsx
2016-03-10 10:50:39 +08:00

55 lines
1.5 KiB
JavaScript

import React from 'react';
import classNames from 'classnames';
import ImagePreview from './ImagePreview';
import * as utils from '../utils';
export default class Article extends React.Component {
constructor(props) {
super(props);
this.imgToPreview = this.imgToPreview.bind(this);
}
isPreviewImg(string) {
return /^<img\s/i.test(string) && /preview-img/gi.test(string);
}
imgToPreview(node) {
if (!this.isPreviewImg(node.children)) {
return node;
}
const imgs = node.children.split(/\r|\n/);
const hasPopup = imgs.length > 1;
const previewClassName = classNames({
'preview-image-boxes': true,
clearfix: true,
'preview-image-boxes-with-popup': hasPopup,
});
return <ImagePreview className={previewClassName} imgs={imgs} />;
}
render() {
const content = this.props.content;
const jumper = content.description.filter((node) => {
return node.type === 'h2';
}).map((node) => {
return <li key={node.children}><a href={`#${node.children}`}>{ node.children }</a></li>;
});
content.description = content.description.map(this.imgToPreview);
return (
<article className="markdown">
<h1>{ content.meta.chinese || content.meta.english }</h1>
{
jumper.length > 0 ?
<section className="toc"><ul>{ jumper }</ul></section> :
null
}
{ content.description.map(utils.objectToComponent) }
</article>
);
}
}