Skip to content

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

FIX: supports user/group mentions and category hashtags when quoting posts

Régis Hanol3483c7bc587c
diff --git a/app/assets/javascripts/discourse/lib/to-markdown.js.es6 b/app/assets/javascripts/discourse/lib/to-markdown.js.es6
index 692d4350ed2..6fb56e4db8f 100644
--- a/app/assets/javascripts/discourse/lib/to-markdown.js.es6
+++ b/app/assets/javascripts/discourse/lib/to-markdown.js.es6
@@ -126,6 +126,14 @@ class Tag {
decorate(text) {
const attr = this.element.attributes;
+ if (/^mention/.test(attr.class) && "@" === text[0]) {
+ return text;
+ }
+
+ if ("hashtag" === attr.class && "#" === text[0]) {
+ return text;
+ }
+
if (attr.href && text !== attr.href) {
text = text.replace(/\n{2,}/g, "\n");
return "[" + text + "](" + attr.href + ")";
diff --git a/test/javascripts/lib/to-markdown-test.js.es6 b/test/javascripts/lib/to-markdown-test.js.es6
index 4a99ab8138e..d310d97f20a 100644
--- a/test/javascripts/lib/to-markdown-test.js.es6
+++ b/test/javascripts/lib/to-markdown-test.js.es6
@@ -104,7 +104,7 @@ QUnit.test("converts table tags", assert => {
<tbody>
<tr><td>Lorem</td><td>ipsum</td></tr>
<tr><td><b>dolor</b></td> <td><i>sit amet</i></td> </tr>
-
+
</tbody>
</table>
`;
@@ -286,3 +286,16 @@ QUnit.test("converts list tag from word", assert => {
const markdown = `Sample\n\n* **Item 1**\n * *Item 2*\n * Item 3\n* Item 4\n\nList`;
assert.equal(toMarkdown(html), markdown);
});
+
+QUnit.test("keeps mention/hash class", assert => {
+ const html = `
+ <p>User mention: <a class="mention" href="/u/discourse">@discourse</a></p>
+ <p>Group mention: <a class="mention-group" href="/groups/discourse">@discourse-group</a></p>
+ <p>Category link: <a class="hashtag" href="/c/foo/1">#<span>foo</span></a></p>
+ <p>Sub-category link: <a class="hashtag" href="/c/foo/bar/2">#<span>foo:bar</span></a></p>
+ `;
+
+ const markdown = `User mention: @discourse\n\nGroup mention: @discourse-group\n\nCategory link: #foo\n\nSub-category link: #foo:bar`;
+
+ assert.equal(toMarkdown(html), markdown);
+});