fix(util/markdown): preserve at signs in URLs (#45555)

This commit is contained in:
mithil
2026-09-01 09:41:53 +00:00
committed by GitHub
parent ea17651002
commit cfb2a76354
2 changed files with 15 additions and 3 deletions
+10
View File
@@ -110,5 +110,15 @@ describe('util/markdown', () => {
'mention @​user here',
);
});
it('should not add zero-width space to @ inside URLs', async () => {
const input =
'Read more at https://medium.com/@fastifyjs/fastify-v4-ga-59f2103b5f0e by @user';
const linkified = await linkify(input, { repository: 'some/repo' });
expect(sanitizeMarkdown(linkified)).toEqual(
'Read more at <https://medium.com/@fastifyjs/fastify-v4-ga-59f2103b5f0e> by [@&#8203;user](https://github.com/user)\n',
);
});
});
});
+5 -3
View File
@@ -10,11 +10,13 @@ export function sanitizeMarkdown(markdown: string): string {
// Put a zero width space after every # followed by a digit
res = res.replace(regEx(/(\W)#(\d)/gi), '$1#&#8203;$2');
// Put a zero width space after every @ symbol to prevent unintended hyperlinking,
// but leave code blocks (triple backticks) and inline code spans untouched
// but leave URLs, code blocks (triple backticks), and inline code spans untouched
res = res
.split(regEx(/(```[\s\S]*?```|`[^`\n]*?`)/g))
.split(regEx(/(```[\s\S]*?```|`[^`\n]*?`|https?:\/\/[^\s<]+)/gi))
.map((part) =>
part.startsWith('`') ? part : part.replace(regEx(/@/g), '@&#8203;'),
part.startsWith('`') || regEx(/^https?:\/\//i).test(part)
? part
: part.replace(regEx(/@/g), '@&#8203;'),
)
.join('');
res = res.replace(regEx(/([a-z]@)&#8203;/gi), '$1');