A platform for community discussion. Free, open, simple.
FIX: revert previously removed mentions transformation on the client (#23084)
Andrei Prigorshnev2d16446078f6
diff --git a/plugins/chat/assets/javascripts/discourse/lib/transform-mentions.js b/plugins/chat/assets/javascripts/discourse/lib/transform-mentions.jsnew file mode 100644index 00000000000..a1b92409922--- /dev/null+++ b/plugins/chat/assets/javascripts/discourse/lib/transform-mentions.js@@ -0,0 +1,20 @@+import getURL from "discourse-common/lib/get-url";++const domParser = new DOMParser();++export default function transformMentions(cooked) {+ const html = domParser.parseFromString(cooked, "text/html");+ transform(html);+ return html.body.innerHTML;+}++function transform(html) {+ (html.querySelectorAll("span.mention") || []).forEach((mentionSpan) => {+ let mentionLink = document.createElement("a");+ let mentionText = document.createTextNode(mentionSpan.innerText);+ mentionLink.classList.add("mention");+ mentionLink.appendChild(mentionText);+ mentionLink.href = getURL(`/u/${mentionSpan.innerText.substring(1)}`);+ mentionSpan.parentNode.replaceChild(mentionLink, mentionSpan);+ });+}diff --git a/plugins/chat/assets/javascripts/discourse/models/chat-message.js b/plugins/chat/assets/javascripts/discourse/models/chat-message.jsindex 5c913750c19..2c4a4f6d87f 100644--- a/plugins/chat/assets/javascripts/discourse/models/chat-message.js+++ b/plugins/chat/assets/javascripts/discourse/models/chat-message.js@@ -5,6 +5,7 @@ import ChatMessageReaction from "discourse/plugins/chat/discourse/models/chat-meimport Bookmark from "discourse/models/bookmark";import I18n from "I18n";import { generateCookFunction } from "discourse/lib/text";+import transformMentions from "discourse/plugins/chat/discourse/lib/transform-mentions";import { getOwner } from "discourse-common/lib/get-owner";import discourseLater from "discourse-common/lib/later";@@ -191,7 +192,7 @@ export default class ChatMessage {} else {const cookFunction = await generateCookFunction(markdownOptions);ChatMessage.cookFunction = (raw) => {- return cookFunction(raw);+ return transformMentions(cookFunction(raw));};this.cooked = ChatMessage.cookFunction(this.message);