Markdown: Split up render function into toHTML/toPlaintext

Signed-off-by: Johannes Löthberg <johannes@kyriasis.com>
pull/21833/head
Johannes Löthberg 2017-01-18 19:29:11 +01:00
parent 6d2e521421
commit 30bd01cdf2
3 changed files with 61 additions and 47 deletions

View File

@ -23,7 +23,9 @@ import commonmark from 'commonmark';
*/ */
export default class Markdown { export default class Markdown {
constructor(input) { constructor(input) {
this.input = input this.input = input;
this.parser = new commonmark.Parser();
this.renderer = new commonmark.HtmlRenderer({safe: false});
} }
isPlainText() { isPlainText() {
@ -57,13 +59,10 @@ export default class Markdown {
return is_plain; return is_plain;
} }
render(html) { toHTML(html) {
const parser = new commonmark.Parser(); const real_paragraph = this.renderer.paragraph;
const renderer = new commonmark.HtmlRenderer({safe: true}); this.renderer.paragraph = function(node, entering) {
const real_paragraph = renderer.paragraph;
if (html) {
renderer.paragraph = function(node, entering) {
// If there is only one top level node, just return the // If there is only one top level node, just return the
// bare text: it's a single line of text and so should be // bare text: it's a single line of text and so should be
// 'inline', rather than unnecessarily wrapped in its own // 'inline', rather than unnecessarily wrapped in its own
@ -77,15 +76,27 @@ export default class Markdown {
real_paragraph.call(this, node, entering); real_paragraph.call(this, node, entering);
} }
} }
} else {
var parsed = this.parser.parse(this.input);
var rendered = this.renderer.render(parsed);
this.renderer.paragraph = real_paragraph;
return rendered;
}
toPlaintext() {
const real_paragraph = this.renderer.paragraph;
// The default `out` function only sends the input through an XML // The default `out` function only sends the input through an XML
// escaping function, which causes messages to be entity encoded, // escaping function, which causes messages to be entity encoded,
// which we don't want in this case. // which we don't want in this case.
renderer.out = function(s) { this.renderer.out = function(s) {
// The `lit` function adds a string literal to the output buffer.
this.lit(s); this.lit(s);
} }
renderer.paragraph = function(node, entering) { this.renderer.paragraph = function(node, entering) {
// If there is only one top level node, just return the // If there is only one top level node, just return the
// bare text: it's a single line of text and so should be // bare text: it's a single line of text and so should be
// 'inline', rather than unnecessarily wrapped in its own // 'inline', rather than unnecessarily wrapped in its own
@ -102,9 +113,12 @@ export default class Markdown {
} }
} }
} }
}
var parsed = parser.parse(this.input); var parsed = this.parser.parse(this.input);
return renderer.render(parsed); var rendered = this.renderer.render(parsed);
this.renderer.paragraph = real_paragraph;
return rendered;
} }
} }

View File

@ -401,7 +401,7 @@ export default class MessageComposerInput extends React.Component {
let contentState = null; let contentState = null;
if (enabled) { if (enabled) {
const md = new Markdown(this.state.editorState.getCurrentContent().getPlainText()); const md = new Markdown(this.state.editorState.getCurrentContent().getPlainText());
contentState = RichText.HTMLtoContentState(md.render(true)); contentState = RichText.HTMLtoContentState(md.toHTML());
} else { } else {
let markdown = stateToMarkdown(this.state.editorState.getCurrentContent()); let markdown = stateToMarkdown(this.state.editorState.getCurrentContent());
if (markdown[markdown.length - 1] === '\n') { if (markdown[markdown.length - 1] === '\n') {
@ -524,9 +524,9 @@ export default class MessageComposerInput extends React.Component {
} else { } else {
const md = new Markdown(contentText); const md = new Markdown(contentText);
if (md.isPlainText()) { if (md.isPlainText()) {
contentText = md.render(false); contentText = md.toPlaintext();
} else { } else {
contentHTML = md.render(true); contentHTML = md.toHTML(true);
} }
} }

View File

@ -325,13 +325,13 @@ module.exports = React.createClass({
} }
if (send_markdown) { if (send_markdown) {
const htmlText = mdown.render(true); const htmlText = mdown.toHTML();
sendMessagePromise = isEmote ? sendMessagePromise = isEmote ?
MatrixClientPeg.get().sendHtmlEmote(this.props.room.roomId, contentText, htmlText) : MatrixClientPeg.get().sendHtmlEmote(this.props.room.roomId, contentText, htmlText) :
MatrixClientPeg.get().sendHtmlMessage(this.props.room.roomId, contentText, htmlText); MatrixClientPeg.get().sendHtmlMessage(this.props.room.roomId, contentText, htmlText);
} }
else { else {
const contentText = mdown.render(false); const contentText = mdown.toPlaintext(false);
sendMessagePromise = isEmote ? sendMessagePromise = isEmote ?
MatrixClientPeg.get().sendEmoteMessage(this.props.room.roomId, contentText) : MatrixClientPeg.get().sendEmoteMessage(this.props.room.roomId, contentText) :
MatrixClientPeg.get().sendTextMessage(this.props.room.roomId, contentText); MatrixClientPeg.get().sendTextMessage(this.props.room.roomId, contentText);