avoid search engines following 404ing internal links from descriptions

This commit is contained in:
localhost 2026-01-24 16:35:46 +01:00
parent 906acde9bd
commit 478b760af1
1 changed files with 28 additions and 0 deletions

View File

@ -55,6 +55,34 @@ app.get('/watch', async ({ query: { v }, set, redirect, error }) => {
.execute()
}
DOMPurify.addHook('afterSanitizeAttributes', function (node) {
if (node.tagName === 'A') {
const disallowedPatterns: RegExp[] = [
/\/playlist/i,
/\/hashtag\//i,
/\/live\//i,
/\/user\//i,
/\/shorts\//i,
/\/c\//i,
/\/@[^\/]/i
];
const href = node.getAttribute('href') || '';
const shouldConvertToSpan = disallowedPatterns.some(pattern =>
pattern.test(href)
);
if (shouldConvertToSpan) {
const span = node.ownerDocument.createElement('span');
span.innerHTML = node.innerHTML;
if (node.className) span.className = node.className;
node.parentNode?.replaceChild(span, node);
} else {
node.setAttribute('rel', 'nofollow noopener noreferrer');
}
}
})
const html = await m(eta.render('./watch', {
transparency,
...json,