2016-02-29 14:08:40 +08:00
|
|
|
import React from 'react';
|
2016-03-02 17:12:43 +08:00
|
|
|
import classNames from 'classnames';
|
|
|
|
import ImagePreview from './ImagePreview';
|
2016-02-29 14:08:40 +08:00
|
|
|
import * as utils from '../utils';
|
|
|
|
|
2016-03-02 17:12:43 +08:00
|
|
|
function isPreviewImg(string) {
|
|
|
|
return /^<img\s/i.test(string) && /preview-img/gi.test(string);
|
|
|
|
}
|
|
|
|
|
|
|
|
function imgToPreview(node) {
|
|
|
|
if (!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} />;
|
|
|
|
}
|
|
|
|
|
2016-02-29 14:08:40 +08:00
|
|
|
export default class Article extends React.Component {
|
|
|
|
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>;
|
|
|
|
});
|
2016-02-29 14:34:51 +08:00
|
|
|
|
2016-03-02 17:12:43 +08:00
|
|
|
content.description = content.description.map(imgToPreview);
|
|
|
|
|
2016-02-29 14:08:40 +08:00
|
|
|
return (
|
|
|
|
<article className="markdown">
|
2016-03-03 11:12:46 +08:00
|
|
|
<h1>{ content.meta.chinese || content.meta.english }</h1>
|
2016-02-29 14:08:40 +08:00
|
|
|
{
|
|
|
|
jumper.length > 0 ?
|
2016-02-29 14:34:51 +08:00
|
|
|
<section className="toc"><ul>{ jumper }</ul></section> :
|
2016-02-29 14:08:40 +08:00
|
|
|
null
|
|
|
|
}
|
|
|
|
{ content.description.map(utils.objectToComponent) }
|
|
|
|
</article>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|