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>pull/21833/head
							parent
							
								
									fcb1d7a664
								
							
						
					
					
						commit
						893a5c971f
					
				| 
						 | 
				
			
			@ -56,11 +56,12 @@ export default class Markdown {
 | 
			
		|||
        return is_plain;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    toHTML() {
 | 
			
		||||
    render(html) {
 | 
			
		||||
        const parser = new commonmark.Parser();
 | 
			
		||||
 | 
			
		||||
        const renderer = new commonmark.HtmlRenderer({safe: true});
 | 
			
		||||
        const real_paragraph = renderer.paragraph;
 | 
			
		||||
        if (html) {
 | 
			
		||||
            renderer.paragraph = function(node, entering) {
 | 
			
		||||
                // If there is only one top level node, just return the
 | 
			
		||||
                // bare text: it's a single line of text and so should be
 | 
			
		||||
| 
						 | 
				
			
			@ -75,6 +76,13 @@ export default class Markdown {
 | 
			
		|||
                    real_paragraph.call(this, node, entering);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            renderer.paragraph = function(node, entering) {
 | 
			
		||||
                if (entering) {
 | 
			
		||||
                    this.lit('\n\n');
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        var parsed = parser.parse(this.input);
 | 
			
		||||
        return renderer.render(parsed);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -401,7 +401,7 @@ export default class MessageComposerInput extends React.Component {
 | 
			
		|||
        let contentState = null;
 | 
			
		||||
        if (enabled) {
 | 
			
		||||
            const md = new Markdown(this.state.editorState.getCurrentContent().getPlainText());
 | 
			
		||||
            contentState = RichText.HTMLtoContentState(md.toHTML());
 | 
			
		||||
            contentState = RichText.HTMLtoContentState(md.render(true));
 | 
			
		||||
        } else {
 | 
			
		||||
            let markdown = stateToMarkdown(this.state.editorState.getCurrentContent());
 | 
			
		||||
            if (markdown[markdown.length - 1] === '\n') {
 | 
			
		||||
| 
						 | 
				
			
			@ -523,8 +523,10 @@ export default class MessageComposerInput extends React.Component {
 | 
			
		|||
            );
 | 
			
		||||
        } else {
 | 
			
		||||
            const md = new Markdown(contentText);
 | 
			
		||||
            if (!md.isPlainText()) {
 | 
			
		||||
                contentHTML = md.toHTML();
 | 
			
		||||
            if (md.isPlainText()) {
 | 
			
		||||
                contentText = md.render(false);
 | 
			
		||||
            } else {
 | 
			
		||||
                contentHTML = md.render(true);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue