Preview using MarkupIt
This commit is contained in:
parent
95fdba9953
commit
b4f3a38bd5
@ -82,6 +82,11 @@
|
|||||||
},
|
},
|
||||||
toBlock: function(obj) {
|
toBlock: function(obj) {
|
||||||
return '{{< youtube ' + obj.id + ' >}}';
|
return '{{< youtube ' + obj.id + ' >}}';
|
||||||
|
},
|
||||||
|
toPreview: function(obj) {
|
||||||
|
return (
|
||||||
|
'<img src="http://img.youtube.com/vi/' + obj.id + '/maxresdefault.jpg" alt="Youtube Video"/>'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
@ -67,8 +67,6 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"bricks.js": "^1.7.0",
|
"bricks.js": "^1.7.0",
|
||||||
"commonmark": "^0.24.0",
|
|
||||||
"commonmark-react-renderer": "^4.1.2",
|
|
||||||
"fuzzy": "^0.1.1",
|
"fuzzy": "^0.1.1",
|
||||||
"js-base64": "^2.1.9",
|
"js-base64": "^2.1.9",
|
||||||
"json-loader": "^0.5.4",
|
"json-loader": "^0.5.4",
|
||||||
|
@ -1,17 +1,25 @@
|
|||||||
import React, { PropTypes } from 'react';
|
import React, { PropTypes } from 'react';
|
||||||
import CommonMark from 'commonmark';
|
import MarkupIt from 'markup-it';
|
||||||
import ReactRenderer from 'commonmark-react-renderer';
|
import { getSyntaxes } from './richText';
|
||||||
|
|
||||||
const parser = new CommonMark.Parser();
|
|
||||||
const renderer = new ReactRenderer();
|
|
||||||
|
|
||||||
export default class MarkdownPreview extends React.Component {
|
export default class MarkdownPreview extends React.Component {
|
||||||
|
|
||||||
|
constructor(props) {
|
||||||
|
super(props);
|
||||||
|
|
||||||
|
const { markdown, html } = getSyntaxes();
|
||||||
|
this.markdown = new MarkupIt(markdown);
|
||||||
|
this.html = new MarkupIt(html);
|
||||||
|
}
|
||||||
render() {
|
render() {
|
||||||
const { value } = this.props;
|
const { value } = this.props;
|
||||||
if (value == null) { return null; }
|
if (value == null) { return null; }
|
||||||
|
const content = this.markdown.toContent(value);
|
||||||
|
const contentHtml = { __html: this.html.toText(content) };
|
||||||
|
|
||||||
const ast = parser.parse(value);
|
return (
|
||||||
return React.createElement.apply(React, ['div', {}].concat(renderer.render(ast)));
|
<div dangerouslySetInnerHTML={contentHtml} />
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,15 +26,17 @@ function processEditorPlugins(plugins) {
|
|||||||
if (plugins === processedPlugins) return;
|
if (plugins === processedPlugins) return;
|
||||||
|
|
||||||
plugins.forEach(plugin => {
|
plugins.forEach(plugin => {
|
||||||
const markdownRule = MarkupIt.Rule(plugin.id)
|
const basicRule = MarkupIt.Rule(plugin.id).regExp(plugin.pattern, (state, match) => (
|
||||||
.regExp(plugin.pattern, function(state, match) {
|
{ data: plugin.fromBlock(match) }
|
||||||
return { data: plugin.fromBlock(match) };
|
));
|
||||||
})
|
|
||||||
.toText(function(state, token) { return plugin.toBlock(token.getData().toObject()) + '\n\n'; });
|
|
||||||
|
|
||||||
const htmlRule = MarkupIt.Rule(plugin.id)
|
const markdownRule = basicRule.toText((state, token) => (
|
||||||
.regExp(plugin.pattern, function(state, match) { return plugin.fromBlock(match); })
|
plugin.toBlock(token.getData().toObject()) + '\n\n'
|
||||||
.toText(function(state, token) { return plugin.toPreview(token.getData()); });
|
));
|
||||||
|
|
||||||
|
const htmlRule = basicRule.toText((state, token) => (
|
||||||
|
plugin.toPreview(token.getData().toObject())
|
||||||
|
));
|
||||||
|
|
||||||
const nodeRenderer = (props) => {
|
const nodeRenderer = (props) => {
|
||||||
const { node, state } = props;
|
const { node, state } = props;
|
||||||
@ -61,8 +63,7 @@ function processEditorPlugins(plugins) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function processMediaProxyPlugins(getMedia) {
|
function processMediaProxyPlugins(getMedia) {
|
||||||
const customImageRule = MarkupIt.Rule('mediaproxy')
|
const mediaProxyRule = MarkupIt.Rule('mediaproxy').regExp(reInline.link, (state, match) => {
|
||||||
.regExp(reInline.link, function(state, match) {
|
|
||||||
if (match[0].charAt(0) !== '!') {
|
if (match[0].charAt(0) !== '!') {
|
||||||
// Return if this is not an image
|
// Return if this is not an image
|
||||||
return;
|
return;
|
||||||
@ -70,15 +71,15 @@ function processMediaProxyPlugins(getMedia) {
|
|||||||
|
|
||||||
var imgData = Map({
|
var imgData = Map({
|
||||||
alt: match[1],
|
alt: match[1],
|
||||||
src: getMedia(match[2]),
|
src: match[2],
|
||||||
title: match[3]
|
title: match[3]
|
||||||
}).filter(Boolean);
|
}).filter(Boolean);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
data: imgData
|
data: imgData
|
||||||
};
|
};
|
||||||
})
|
});
|
||||||
.toText(function(state, token) {
|
const mediaProxyMarkdownRule = mediaProxyRule.toText((state, token) => {
|
||||||
var data = token.getData();
|
var data = token.getData();
|
||||||
var alt = data.get('alt', '');
|
var alt = data.get('alt', '');
|
||||||
var src = getMedia(data.get('src', ''));
|
var src = getMedia(data.get('src', ''));
|
||||||
@ -90,6 +91,12 @@ function processMediaProxyPlugins(getMedia) {
|
|||||||
return '![' + alt + '](' + src + ')';
|
return '![' + alt + '](' + src + ')';
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
const mediaProxyHTMLRule = mediaProxyRule.toText((state, token) => {
|
||||||
|
var data = token.getData();
|
||||||
|
var alt = data.get('alt', '');
|
||||||
|
var src = data.get('src', '');
|
||||||
|
return `<img src=${src} alt=${alt} />`;
|
||||||
|
});
|
||||||
|
|
||||||
nodes['mediaproxy'] = (props) => {
|
nodes['mediaproxy'] = (props) => {
|
||||||
/* eslint react/prop-types: 0 */
|
/* eslint react/prop-types: 0 */
|
||||||
@ -101,7 +108,8 @@ function processMediaProxyPlugins(getMedia) {
|
|||||||
<img {...props.attributes} src={getMedia(src)} className={className} />
|
<img {...props.attributes} src={getMedia(src)} className={className} />
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
augmentedMarkdownSyntax = augmentedMarkdownSyntax.addInlineRules(customImageRule);
|
augmentedMarkdownSyntax = augmentedMarkdownSyntax.addInlineRules(mediaProxyMarkdownRule);
|
||||||
|
augmentedHTMLSyntax = augmentedHTMLSyntax.addInlineRules(mediaProxyHTMLRule);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getPlugins() {
|
function getPlugins() {
|
||||||
@ -115,7 +123,9 @@ function getNodes() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getSyntaxes(getMedia) {
|
function getSyntaxes(getMedia) {
|
||||||
|
if (getMedia) {
|
||||||
processMediaProxyPlugins(getMedia);
|
processMediaProxyPlugins(getMedia);
|
||||||
|
}
|
||||||
return { markdown: augmentedMarkdownSyntax, html:augmentedHTMLSyntax };
|
return { markdown: augmentedMarkdownSyntax, html:augmentedHTMLSyntax };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user