Skip to content

A platform for community discussion. Free, open, simple.

FIX: Keep emojis and remove clicks count

Vinoth Kannanfb35b0b3c330
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 = `
+ <p>
+ A <a href="http://example.com">link</a><span class="badge badge-notification clicks" title="1 click">1</span> with click count
+ and <img class="emoji" title=":boom:" src="https://d11a6trkgmumsb.cloudfront.net/images/emoji/twitter/boom.png?v=5" alt=":boom:" /> emoji.
+ </p>
+ `;
+
+ const markdown = `A [link](http://example.com) with click count and :boom: emoji.`;
+
+ assert.equal(toMarkdown(html), markdown);
+});