Merge branch 'mention-link-fix' into 'main'

Fix rendering mentions inside of URLs

See merge request soapbox-pub/ditto!479
This commit is contained in:
Alex Gleason 2024-09-07 14:44:56 +00:00
commit 85000cd00e
2 changed files with 33 additions and 13 deletions

View file

@ -123,21 +123,25 @@ const createStatusController: AppController = async (c) => {
const pubkeys = new Set<string>(); const pubkeys = new Set<string>();
const content = await asyncReplaceAll(data.status ?? '', /@([\w@+._]+)/g, async (match, username) => { const content = await asyncReplaceAll(
const pubkey = await lookupPubkey(username); data.status ?? '',
if (!pubkey) return match; /(?<![\w/])@([\w@+._]+)(?![\w/\.])/g,
async (match, username) => {
const pubkey = await lookupPubkey(username);
if (!pubkey) return match;
// Content addressing (default) // Content addressing (default)
if (!data.to) { if (!data.to) {
pubkeys.add(pubkey); pubkeys.add(pubkey);
} }
try { try {
return `nostr:${nip19.npubEncode(pubkey)}`; return `nostr:${nip19.npubEncode(pubkey)}`;
} catch { } catch {
return match; return match;
} }
}); },
);
// Explicit addressing // Explicit addressing
for (const to of data.to ?? []) { for (const to of data.to ?? []) {

View file

@ -49,6 +49,22 @@ Deno.test('parseNoteContent renders empty for non-profile nostr URIs', () => {
assertEquals(html, ''); assertEquals(html, '');
}); });
Deno.test("parseNoteContent doesn't fuck up links to my own post", () => {
const { html } = parseNoteContent(
'Check this post: https://gleasonator.dev/@alex@gleasonator.dev/posts/a8badb480d88f9e7b6a090342279ef47ed0e0a3989ed85f898dfedc6be94225f',
[{
id: '0461fcbecc4c3374439932d6b8f11269ccdb7cc973ad7a50ae362db135a474dd',
username: 'alex',
acct: 'alex@gleasonator.dev',
url: 'https://gleasonator.dev/@alex',
}],
);
assertEquals(
html,
'Check this post: <a href="https://gleasonator.dev/@alex@gleasonator.dev/posts/a8badb480d88f9e7b6a090342279ef47ed0e0a3989ed85f898dfedc6be94225f">https://gleasonator.dev/@alex@gleasonator.dev/posts/a8badb480d88f9e7b6a090342279ef47ed0e0a3989ed85f898dfedc6be94225f</a>',
);
});
Deno.test('getMediaLinks', () => { Deno.test('getMediaLinks', () => {
const links = [ const links = [
{ href: 'https://example.com/image.png' }, { href: 'https://example.com/image.png' },