From 86496f3e06824d16cc5bc9c52040f1d1cd21cf8a Mon Sep 17 00:00:00 2001 From: Clark Winkelmann Date: Thu, 29 Jun 2017 00:56:15 +0200 Subject: [PATCH] Keep images and links when quoting (#25) * Keep images and links when quoting * Fix bower dependencies --- .../mentions/js/forum/dist/extension.js | 19 ++++++++++++++++--- .../js/forum/src/utils/selectedText.js | 13 +++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/extensions/mentions/js/forum/dist/extension.js b/extensions/mentions/js/forum/dist/extension.js index 3a9972387..5a089eaa7 100644 --- a/extensions/mentions/js/forum/dist/extension.js +++ b/extensions/mentions/js/forum/dist/extension.js @@ -1218,9 +1218,9 @@ System.register('flarum/mentions/utils/reply', ['flarum/utils/DiscussionControls execute: function () {} }; });; -"use strict"; +'use strict'; -System.register("flarum/mentions/utils/selectedText", [], function (_export, _context) { +System.register('flarum/mentions/utils/selectedText', [], function (_export, _context) { "use strict"; function selectedText(body) { @@ -1230,16 +1230,29 @@ System.register("flarum/mentions/utils/selectedText", [], function (_export, _co var parent = range.commonAncestorContainer; if (body[0] === parent || $.contains(body[0], parent)) { var clone = $("
").append(range.cloneContents()); + + // Replace emoji images with their shortcode (found in alt attribute) clone.find('img.emoji').replaceWith(function () { return this.alt; }); + + // Replace all other images with a Markdown image + clone.find('img').replaceWith(function () { + return '![](' + this.src + ')'; + }); + + // Replace all links with a Markdown link + clone.find('a').replaceWith(function () { + return '[' + this.innerText + '](' + this.href + ')'; + }); + return clone.text(); } } return ""; } - _export("default", selectedText); + _export('default', selectedText); return { setters: [], diff --git a/extensions/mentions/js/forum/src/utils/selectedText.js b/extensions/mentions/js/forum/src/utils/selectedText.js index 4c81d44b3..477a0b4af 100644 --- a/extensions/mentions/js/forum/src/utils/selectedText.js +++ b/extensions/mentions/js/forum/src/utils/selectedText.js @@ -5,9 +5,22 @@ export default function selectedText(body) { const parent = range.commonAncestorContainer; if (body[0] === parent || $.contains(body[0], parent)) { const clone = $("
").append(range.cloneContents()); + + // Replace emoji images with their shortcode (found in alt attribute) clone.find('img.emoji').replaceWith(function() { return this.alt; }); + + // Replace all other images with a Markdown image + clone.find('img').replaceWith(function() { + return '![](' + this.src + ')'; + }); + + // Replace all links with a Markdown link + clone.find('a').replaceWith(function() { + return '[' + this.innerText + '](' + this.href + ')'; + }); + return clone.text(); } }