Fix escaping markdown by rendering plaintext

We still need to parse "plaintext" messages through the markdown
renderer so that escappes are rendered properly.

Fixes vector-im/riot-web#2870.

Signed-off-by: Johannes Löthberg <johannes@kyriasis.com>
This commit is contained in:
Johannes Löthberg 2016-12-02 19:58:35 +01:00
parent fcb1d7a664
commit 893a5c971f
2 changed files with 25 additions and 15 deletions

View file

@ -56,23 +56,31 @@ export default class Markdown {
return is_plain; return is_plain;
} }
toHTML() { render(html) {
const parser = new commonmark.Parser(); const parser = new commonmark.Parser();
const renderer = new commonmark.HtmlRenderer({safe: true}); const renderer = new commonmark.HtmlRenderer({safe: true});
const real_paragraph = renderer.paragraph; const real_paragraph = renderer.paragraph;
renderer.paragraph = function(node, entering) { if (html) {
// If there is only one top level node, just return the renderer.paragraph = function(node, entering) {
// bare text: it's a single line of text and so should be // If there is only one top level node, just return the
// 'inline', rather than unnecessarily wrapped in its own // bare text: it's a single line of text and so should be
// p tag. If, however, we have multiple nodes, each gets // 'inline', rather than unnecessarily wrapped in its own
// its own p tag to keep them as separate paragraphs. // p tag. If, however, we have multiple nodes, each gets
var par = node; // its own p tag to keep them as separate paragraphs.
while (par.parent) { var par = node;
par = par.parent while (par.parent) {
par = par.parent
}
if (par.firstChild != par.lastChild) {
real_paragraph.call(this, node, entering);
}
} }
if (par.firstChild != par.lastChild) { } else {
real_paragraph.call(this, node, entering); renderer.paragraph = function(node, entering) {
if (entering) {
this.lit('\n\n');
}
} }
} }

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.toHTML()); contentState = RichText.HTMLtoContentState(md.render(true));
} 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') {
@ -523,8 +523,10 @@ 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()) {
contentHTML = md.toHTML(); contentText = md.render(false);
} else {
contentHTML = md.render(true);
} }
} }