Skip to content

Commit

Permalink
FIX: revert previously removed mentions transformation on the client (#…
Browse files Browse the repository at this point in the history
…23084)

This partially reverts 2ecc829.

The problem is that if we don't transform mentions right away, 
there is a noticeable lag before a mention gets fully rendered, 
while with the transformation, everything is super smooth.

I'm reverting that change only for mentions. Another part was about 
category hashtags, but unfortunately they lag both with and without 
the transformation. We need to address them separately.
  • Loading branch information
AndrewPrigorshnev committed Aug 22, 2023
1 parent 0e00784 commit 2d16446
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
@@ -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);
});
}
Expand Up @@ -5,6 +5,7 @@ import ChatMessageReaction from "discourse/plugins/chat/discourse/models/chat-me
import 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";

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 2d16446

Please sign in to comment.