From fb35b0b3c3305f5f37cc22449094bf8675b84b1d Mon Sep 17 00:00:00 2001 From: Vinoth Kannan Date: Wed, 29 Aug 2018 07:26:25 +0000 Subject: [PATCH] FIX: Keep emojis and remove clicks count --- .../discourse/lib/to-markdown.js.es6 | 26 ++++++++++++++++++- .../discourse/lib/utilities.js.es6 | 7 ----- test/javascripts/lib/to-markdown-test.js.es6 | 13 ++++++++++ 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/app/assets/javascripts/discourse/lib/to-markdown.js.es6 b/app/assets/javascripts/discourse/lib/to-markdown.js.es6 index 5d3fb6657de..560c0657300 100644 --- a/app/assets/javascripts/discourse/lib/to-markdown.js.es6 +++ b/app/assets/javascripts/discourse/lib/to-markdown.js.es6 @@ -162,6 +162,24 @@ export class Tag { }; } + static span() { + return class extends Tag { + constructor() { + super("span"); + } + + decorate(text) { + const attr = this.element.attributes; + + if (attr.class === "badge badge-notification clicks") { + return ""; + } + + return super.decorate(text); + } + }; + } + static link() { return class extends Tag { constructor() { @@ -200,6 +218,11 @@ export class Tag { const attr = e.attributes; const pAttr = (e.parent && e.parent.attributes) || {}; const src = attr.src || pAttr.src; + const cssClass = attr.class || pAttr.class; + + if (cssClass === "emoji") { + return attr.title || pAttr.title; + } if (src) { let alt = attr.alt || pAttr.alt || ""; @@ -443,7 +466,8 @@ function tags() { Tag.table(), Tag.tr(), Tag.ol(), - Tag.list("ul") + Tag.list("ul"), + Tag.span() ]; } diff --git a/app/assets/javascripts/discourse/lib/utilities.js.es6 b/app/assets/javascripts/discourse/lib/utilities.js.es6 index 1829e36dfe9..8a29ac6d944 100644 --- a/app/assets/javascripts/discourse/lib/utilities.js.es6 +++ b/app/assets/javascripts/discourse/lib/utilities.js.es6 @@ -140,13 +140,6 @@ export function selectedText() { $div.append(range.cloneContents()); } - // strip click counters - $div.find(".clicks").remove(); - // replace emojis - $div.find("img.emoji").replaceWith(function() { - return this.title; - }); - return toMarkdown($div.html()); } diff --git a/test/javascripts/lib/to-markdown-test.js.es6 b/test/javascripts/lib/to-markdown-test.js.es6 index ec59e164a6f..0ab96fa43ab 100644 --- a/test/javascripts/lib/to-markdown-test.js.es6 +++ b/test/javascripts/lib/to-markdown-test.js.es6 @@ -314,3 +314,16 @@ QUnit.test("keeps mention/hash class", assert => { assert.equal(toMarkdown(html), markdown); }); + +QUnit.test("keeps emoji and removes click count", assert => { + const html = ` +

+ A link1 with click count + and :boom: emoji. +

+ `; + + const markdown = `A [link](http://example.com) with click count and :boom: emoji.`; + + assert.equal(toMarkdown(html), markdown); +});