Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit ddd7467

Browse files
committed
Fix escaping markdown by rendering plaintext
We still need to parse "plaintext" messages through the markdown renderer so that escappes are rendered properly. Signed-off-by: Johannes Löthberg <[email protected]>
1 parent fcb1d7a commit ddd7467

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

src/Markdown.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,31 @@ export default class Markdown {
5656
return is_plain;
5757
}
5858

59-
toHTML() {
59+
render(html) {
6060
const parser = new commonmark.Parser();
6161

6262
const renderer = new commonmark.HtmlRenderer({safe: true});
6363
const real_paragraph = renderer.paragraph;
64-
renderer.paragraph = function(node, entering) {
65-
// If there is only one top level node, just return the
66-
// bare text: it's a single line of text and so should be
67-
// 'inline', rather than unnecessarily wrapped in its own
68-
// p tag. If, however, we have multiple nodes, each gets
69-
// its own p tag to keep them as separate paragraphs.
70-
var par = node;
71-
while (par.parent) {
72-
par = par.parent
64+
if (html) {
65+
renderer.paragraph = function(node, entering) {
66+
// If there is only one top level node, just return the
67+
// bare text: it's a single line of text and so should be
68+
// 'inline', rather than unnecessarily wrapped in its own
69+
// p tag. If, however, we have multiple nodes, each gets
70+
// its own p tag to keep them as separate paragraphs.
71+
var par = node;
72+
while (par.parent) {
73+
par = par.parent
74+
}
75+
if (par.firstChild != par.lastChild) {
76+
real_paragraph.call(this, node, entering);
77+
}
7378
}
74-
if (par.firstChild != par.lastChild) {
75-
real_paragraph.call(this, node, entering);
79+
} else {
80+
renderer.paragraph = function(node, entering) {
81+
if (entering) {
82+
this.lit('\n\n');
83+
}
7684
}
7785
}
7886

src/components/views/rooms/MessageComposerInput.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ export default class MessageComposerInput extends React.Component {
401401
let contentState = null;
402402
if (enabled) {
403403
const md = new Markdown(this.state.editorState.getCurrentContent().getPlainText());
404-
contentState = RichText.HTMLtoContentState(md.toHTML());
404+
contentState = RichText.HTMLtoContentState(md.render(true));
405405
} else {
406406
let markdown = stateToMarkdown(this.state.editorState.getCurrentContent());
407407
if (markdown[markdown.length - 1] === '\n') {
@@ -523,8 +523,10 @@ export default class MessageComposerInput extends React.Component {
523523
);
524524
} else {
525525
const md = new Markdown(contentText);
526-
if (!md.isPlainText()) {
527-
contentHTML = md.toHTML();
526+
if (md.isPlainText()) {
527+
contentText = md.render(false);
528+
} else {
529+
contentHTML = md.render(true);
528530
}
529531
}
530532

0 commit comments

Comments
 (0)