From a504faa2f6f80fa879d46e10009c45e4474aa257 Mon Sep 17 00:00:00 2001 From: "J. Ryan Stinnett" Date: Wed, 22 Jan 2020 22:08:34 +0000 Subject: [PATCH] Treat links as external in report content admin message This marks all the links in the report content admin message (in Markdown format) as external so they open in a new tab. --- src/Markdown.js | 20 ++++++++++++++++++- .../views/dialogs/ReportEventDialog.js | 2 +- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/Markdown.js b/src/Markdown.js index acfea52100..437ceec88b 100644 --- a/src/Markdown.js +++ b/src/Markdown.js @@ -91,7 +91,7 @@ export default class Markdown { return true; } - toHTML() { + toHTML({ externalLinks = false } = {}) { const renderer = new commonmark.HtmlRenderer({ safe: false, @@ -125,6 +125,24 @@ export default class Markdown { } }; + renderer.link = function(node, entering) { + const attrs = this.attrs(node); + if (entering) { + attrs.push(['href', this.esc(node.destination)]); + if (node.title) { + attrs.push(['title', this.esc(node.title)]); + } + // Modified link behaviour to treat them all as external and + // thus opening in a new tab. + if (externalLinks) { + attrs.push(['target', '_blank']); + attrs.push(['rel', 'noopener']); + } + this.tag('a', attrs); + } else { + this.tag('/a'); + } + }; renderer.html_inline = html_if_tag_allowed; diff --git a/src/components/views/dialogs/ReportEventDialog.js b/src/components/views/dialogs/ReportEventDialog.js index 2442bc2f95..99853582dd 100644 --- a/src/components/views/dialogs/ReportEventDialog.js +++ b/src/components/views/dialogs/ReportEventDialog.js @@ -102,7 +102,7 @@ export default class ReportEventDialog extends PureComponent { SdkConfig.get().reportEvent.adminMessageMD; let adminMessage; if (adminMessageMD) { - const html = new Markdown(adminMessageMD).toHTML(); + const html = new Markdown(adminMessageMD).toHTML({ externalLinks: true }); adminMessage =

; }