Fix some links have same class names from the app itself

Srsly need to sanitize the HTML one day
pull/228/head
Lim Chee Aun 2023-09-16 14:47:35 +08:00
rodzic 679fba4f66
commit 7d28744234
1 zmienionych plików z 13 dodań i 2 usunięć

Wyświetl plik

@ -10,15 +10,26 @@ function enhanceContent(content, opts = {}) {
const hasLink = /<a/i.test(enhancedContent);
const hasCodeBlock = enhancedContent.indexOf('```') !== -1;
// Add target="_blank" to all links with no target="_blank"
// E.g. `note` in `account`
if (hasLink) {
// Add target="_blank" to all links with no target="_blank"
// E.g. `note` in `account`
const noTargetBlankLinks = Array.from(
dom.querySelectorAll('a:not([target="_blank"])'),
);
noTargetBlankLinks.forEach((link) => {
link.setAttribute('target', '_blank');
});
// Remove all classes except `u-url`, `mention`, `hashtag`
const links = Array.from(dom.querySelectorAll('a[class]'));
const whitelistClasses = ['u-url', 'mention', 'hashtag'];
links.forEach((link) => {
link.classList.forEach((c) => {
if (!whitelistClasses.includes(c)) {
link.classList.remove(c);
}
});
});
}
// Add 'has-url-text' to all links that contains a url