From b7ec49e060b43c8dd043ba970aed99b53eea698a Mon Sep 17 00:00:00 2001 From: Benjy Cui Date: Wed, 6 Apr 2016 16:14:00 +0800 Subject: [PATCH] site: update code to be compatible with new deps --- site/component/Article/ImagePreview.jsx | 12 +++--------- site/component/Article/VideoPlayer.jsx | 13 +++++-------- site/component/Article/index.jsx | 24 +++++++++--------------- site/component/utils.js | 13 ++++++++----- 4 files changed, 25 insertions(+), 37 deletions(-) diff --git a/site/component/Article/ImagePreview.jsx b/site/component/Article/ImagePreview.jsx index 6b94e0897c..23906ba01a 100644 --- a/site/component/Article/ImagePreview.jsx +++ b/site/component/Article/ImagePreview.jsx @@ -61,17 +61,11 @@ export default class ImagePreview extends React.Component { render() { const { imgs } = this.props; const imgsMeta = imgs.map((img) => { - const span = document.createElement('span'); - span.innerHTML = img; - const imgNode = span.children[0]; - const attributes = imgNode.attributes; - const { alt, description, src } = attributes; - const imgClassName = attributes.class.nodeValue; + const { alt, description, src } = img; + const imgClassName = img.class; return { className: imgClassName, - alt: alt && alt.nodeValue, - description: description && description.nodeValue, - src: src.nodeValue, + alt, description, src, isGood: isGood(imgClassName), isBad: isBad(imgClassName), }; diff --git a/site/component/Article/VideoPlayer.jsx b/site/component/Article/VideoPlayer.jsx index 8d5e2776db..87484ce931 100644 --- a/site/component/Article/VideoPlayer.jsx +++ b/site/component/Article/VideoPlayer.jsx @@ -4,20 +4,17 @@ import SublimeVideo from 'react-sublime-video'; export default class VideoPlayer extends React.Component { render() { const video = this.props.video; - const span = document.createElement('span'); - span.innerHTML = video; - const attributes = span.children[0].attributes; - const { alt, description, src } = attributes; - const videoClassName = attributes.class.nodeValue; + const { alt, description, src } = video; + const videoClassName = video.class; return (
- +
-
{alt && alt.nodeValue}
+
{alt}
+ dangerouslySetInnerHTML={{ __html: description }} />
); } diff --git a/site/component/Article/index.jsx b/site/component/Article/index.jsx index e0bcd6cac2..d086fa9c7c 100644 --- a/site/component/Article/index.jsx +++ b/site/component/Article/index.jsx @@ -1,5 +1,6 @@ import React from 'react'; import { Link } from 'react-router'; +import toReactComponent from 'jsonml-to-react-component'; import ImagePreview from './ImagePreview'; import VideoPlayer from './VideoPlayer'; import * as utils from '../utils'; @@ -20,28 +21,20 @@ export default class Article extends React.Component { utils.setTitle(`${chinese || english} - Ant Design`); } - isPreviewImg(string) { - return /^ n[1]) - .filter((img) => img); + .filter((img) => img[1]) + .map((n) => n[1]); return ; } return node; } - isVideo(string) { - return /^; } return node; @@ -54,8 +47,9 @@ export default class Article extends React.Component { }).map((node) => { return (
  • - + + {toReactComponent([], node[1])} +
  • ); }); diff --git a/site/component/utils.js b/site/component/utils.js index d034c1c18f..007521c8ba 100644 --- a/site/component/utils.js +++ b/site/component/utils.js @@ -1,6 +1,7 @@ import React from 'react'; import ReactDOM from 'react-dom'; import { Link } from 'react-router'; +import { getTagName, getAttributes, getChildren } from 'jsonml.js/lib/utils'; import toReactComponent from 'jsonml-to-react-component'; function isHeading(type) { @@ -15,13 +16,15 @@ export function jsonmlToComponent(pathname, jsonml) { [(node) => typeof node === 'function', (node, index) => { return React.cloneElement(node(React, ReactDOM), { key: index }); }], - [(node) => isHeading(node[0]), (node, index) => { - return React.createElement(node[0], { + [(node) => isHeading(getTagName(node)), (node, index) => { + const children = getChildren(node); + return React.createElement(getTagName(node), { key: index, - id: node[1], + id: children, + ...getAttributes(node), }, [ - , - #, + { children.map(toReactComponent.bind(null, [])) }, + #, ]); }], ], jsonml);