mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-24 02:51:17 -08:00
always open links in new tab
This commit is contained in:
3 files changed
+18
-4
No files matched your search
@@ -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:
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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),
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user