Files
David TaylorandGitHub 666b5dddde DEV: Move discourse-common/(utils|lib) to discourse/lib (#30733)
`discourse-common` was created in the past to share logic between the
'wizard' app and the main 'discourse' app. Since then, the wizard has
been consolidated into the main app, so the separation of
`discourse-common` is no longer useful.

This commit moves `discourse-common/(lib|utils)/*` into
`discourse/lib/*`, adds shims for the imports, and updates existing
uses in core.
2025-01-13 13:02:49 +00:00

37 lines
1.2 KiB
JavaScript

import getURL from "discourse/lib/get-url";
import { generatePlaceholderHashtagHTML } from "discourse/lib/hashtag-decorator";
const domParser = new DOMParser();
export default function transformAutolinks(cooked) {
const html = domParser.parseFromString(cooked, "text/html");
transformMentions(html);
transformHashtags(html);
return html.body.innerHTML;
}
function transformMentions(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.replaceWith(mentionLink);
});
}
function transformHashtags(html) {
(html.querySelectorAll("span.hashtag-raw") || []).forEach((hashtagSpan) => {
// Doesn't matter what "type" of hashtag we use here, it will get replaced anyway,
// this is just for the placeholder HTML.
generatePlaceholderHashtagHTML("category", hashtagSpan, {
id: -1,
text: "...",
relative_url: "/",
slug: "",
icon: "square-full",
});
});
}