From 5cbfad3433ee0260df78e7267056eb7954740d40 Mon Sep 17 00:00:00 2001 From: ClemDee Date: Thu, 25 Jul 2019 11:48:16 +0200 Subject: [PATCH 1/3] Fix markdown unwanted renders in chats --- www/common/diffMarked.js | 27 +++++++++++++++++++++++++-- www/common/messenger-ui.js | 4 ++-- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/www/common/diffMarked.js b/www/common/diffMarked.js index 35633c4cd..a3ae55e06 100644 --- a/www/common/diffMarked.js +++ b/www/common/diffMarked.js @@ -15,6 +15,7 @@ define([ var DiffDOM = window.diffDOM; var renderer = new Marked.Renderer(); + var restrictedRenderer = new Marked.Renderer(); var Mermaid = { init: function () {} @@ -61,13 +62,20 @@ define([ return h('div.cp-md-toc', content).outerHTML; }; - DiffMd.render = function (md, sanitize) { + DiffMd.render = function (md, sanitize, restrictedMd) { + console.error("DIFFMD RENDER", restrictedMd); + console.log("md:\n", md); + Marked.setOptions({ + renderer: restrictedMd ? restrictedRenderer : renderer, + }); var r = Marked(md, { sanitize: sanitize }); // Add Table of Content - r = r.replace(/
<\/div>/g, getTOC()); + if (!restrictedMd) { + r = r.replace(/
<\/div>/g, getTOC()); + } toc = []; return r; @@ -83,6 +91,7 @@ define([ return defaultCode.apply(renderer, arguments); } }; + restrictedRenderer.code = renderer.code; var stripTags = function (text) { var div = document.createElement("div"); @@ -109,6 +118,9 @@ define([ }); return "" + text + ""; }; + restrictedRenderer.heading = function (text) { + return text; + }; // Tasks list var checkedTaskItemPtn = /^\s*(

)?\[[xX]\](<\/p>)?\s*/; @@ -138,6 +150,13 @@ define([ var cls = (isCheckedTaskItem || isUncheckedTaskItem || hasBogusInput) ? ' class="todo-list-item"' : ''; return '' + text + '\n'; }; + restrictedRenderer.listitem = function (text) { + if (bogusCheckPtn.test(text)) { + text = text.replace(bogusCheckPtn, ''); + } + return '

  • ' + text + '
  • \n'; + } + renderer.image = function (href, title, text) { if (href.slice(0,6) === '/file/') { // DEPRECATED @@ -162,6 +181,7 @@ define([ out += this.options.xhtml ? '/>' : '>'; return out; }; + restrictedRenderer.image = renderer.image; renderer.paragraph = function (p) { if (p === '[TOC]') { @@ -169,6 +189,9 @@ define([ } return //i.test(p)? p + '\n': '

    ' + p + '

    \n'; }; + restrictedRenderer.paragraph = function (p) { + return //i.test(p)? p + '\n': '

    ' + p + '

    \n'; + }; var MutationObserver = window.MutationObserver; var forbiddenTags = [ diff --git a/www/common/messenger-ui.js b/www/common/messenger-ui.js index 682535d98..223f67bb3 100644 --- a/www/common/messenger-ui.js +++ b/www/common/messenger-ui.js @@ -151,7 +151,7 @@ define([ }); try { var $d = $(d); - DiffMd.apply(DiffMd.render(md || '', true), $d, common); + DiffMd.apply(DiffMd.render(md || '', true, true), $d, common); $d.addClass("cp-app-contacts-content"); // override link clicking, because we're in an iframe @@ -197,7 +197,7 @@ define([ var getChat = function (id) { return $messages.find(dataQuery(id)); }; - + var scrollChatToBottom = function () { var $messagebox = $('.cp-app-contacts-messages'); $messagebox.scrollTop($messagebox[0].scrollHeight); From a204740ac2ebfcfab2cccd5bd1d65f4bf114eb77 Mon Sep 17 00:00:00 2001 From: ClemDee Date: Thu, 25 Jul 2019 11:50:19 +0200 Subject: [PATCH 2/3] Fix one annoying missing semicolon --- www/common/diffMarked.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/common/diffMarked.js b/www/common/diffMarked.js index a3ae55e06..5c4942dbc 100644 --- a/www/common/diffMarked.js +++ b/www/common/diffMarked.js @@ -155,7 +155,7 @@ define([ text = text.replace(bogusCheckPtn, ''); } return '
  • ' + text + '
  • \n'; - } + }; renderer.image = function (href, title, text) { if (href.slice(0,6) === '/file/') { From 5679dd9feeb58274249f92db044c4b6d03898de8 Mon Sep 17 00:00:00 2001 From: ClemDee Date: Thu, 8 Aug 2019 16:34:26 +0200 Subject: [PATCH 3/3] Clean code --- www/common/diffMarked.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/www/common/diffMarked.js b/www/common/diffMarked.js index 5c4942dbc..1b7f21e2b 100644 --- a/www/common/diffMarked.js +++ b/www/common/diffMarked.js @@ -63,8 +63,6 @@ define([ }; DiffMd.render = function (md, sanitize, restrictedMd) { - console.error("DIFFMD RENDER", restrictedMd); - console.log("md:\n", md); Marked.setOptions({ renderer: restrictedMd ? restrictedRenderer : renderer, }); @@ -183,14 +181,17 @@ define([ }; restrictedRenderer.image = renderer.image; + var renderParagraph = function (p) { + return //i.test(p)? p + '\n': '

    ' + p + '

    \n'; + }; renderer.paragraph = function (p) { if (p === '[TOC]') { return '

    '; } - return //i.test(p)? p + '\n': '

    ' + p + '

    \n'; + return renderParagraph(p); }; restrictedRenderer.paragraph = function (p) { - return //i.test(p)? p + '\n': '

    ' + p + '

    \n'; + return renderParagraph(p); }; var MutationObserver = window.MutationObserver;