always open links in new tab

This commit is contained in:
7w1 committed 2026-04-30 00:43:12 -05:00
1 parent c59c81ed39
commit 7aa82bb9d4
3 files changed
+18 -4

No files matched your search

+2 -2
View File
@@ -98,7 +98,7 @@ const elementToCustomHtml = (
}
const matrixTo = `https://matrix.to/#/${fragment}`;
return `<a href="${encodeURI(matrixTo)}">${sanitizeText(node.name)}</a>`;
return `<a href="${encodeURI(matrixTo)}" target="_blank" rel="noreferrer noopener">${sanitizeText(node.name)}</a>`;
}
case BlockType.Emoticon:
return node.key.startsWith('mxc://')
@@ -107,7 +107,7 @@ const elementToCustomHtml = (
)}" title="${sanitizeText(node.shortcode)}" height="32" />`
: sanitizeText(node.key);
case BlockType.Link:
return `<a href="${encodeURI(node.href)}">${children}</a>`;
return `<a href="${encodeURI(node.href)}" target="_blank" rel="noreferrer noopener">${children}</a>`;
case BlockType.Command:
return `/${sanitizeText(node.command)}`;
default:
+10 -1
View File
@@ -61,7 +61,14 @@ export function markdownToHtml(markdown: string): string {
// Unescape inline sequences (e.g., \*, \_) after parsing
const unescapedInline = unescapeMarkdownInlineSequences(html);
// Sanitize using DOMPurify, restricting to Matrix-spec allowed HTML tags/attributes
// Force all links to open in a new tab
DOMPurify.addHook('afterSanitizeAttributes', (node) => {
if (node.tagName === 'A' && node.getAttribute('href')) {
node.setAttribute('target', '_blank');
node.setAttribute('rel', 'noreferrer noopener');
}
});
const sanitized = DOMPurify.sanitize(unescapedInline, {
ALLOWED_TAGS: [
'h1',
@@ -125,5 +132,7 @@ export function markdownToHtml(markdown: string): string {
ALLOWED_URI_REGEXP: /^(?:https?|ftp|mailto|magnet|mxc):/i,
});
DOMPurify.removeHook('afterSanitizeAttributes');
return sanitized;
}
+6 -1
View File
@@ -250,7 +250,11 @@ export const factoryRenderLinkifyWithMention = (
}
}
return <a {...attributes}>{content}</a>;
return (
<a {...attributes} target="_blank" rel="noreferrer noopener">
{content}
</a>
);
};
return renderLink;
@@ -629,6 +633,7 @@ export const getReactCustomHtmlParser = (
const renderedChildren = renderChildren();
const anchorProps = {
...props,
target: '_blank',
rel: ensureNoopenerRel(props.rel),
};