From a63b8381956c7759d44b013792058cc9a76daf0e Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 28 Aug 2024 20:59:38 +0200 Subject: [PATCH] Fix parsing of regular URLs --- src/utils/note.test.ts | 7 ++++++- src/utils/note.ts | 47 +++++++++++++++++++++++------------------- 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/src/utils/note.test.ts b/src/utils/note.test.ts index 67f802c6..58c39359 100644 --- a/src/utils/note.test.ts +++ b/src/utils/note.test.ts @@ -10,7 +10,12 @@ Deno.test('parseNoteContent', () => { assertEquals(firstUrl, undefined); }); -Deno.test('parseNoteContent handles apostrophes', () => { +Deno.test('parseNoteContent parses URLs', () => { + const { html } = parseNoteContent(`check out my website: https://alexgleason.me`, []); + assertEquals(html, 'check out my website: https://alexgleason.me'); +}); + +Deno.test('parseNoteContent parses mentions with apostrophes', () => { const { html } = parseNoteContent( `did you see nostr:nprofile1qqsqgc0uhmxycvm5gwvn944c7yfxnnxm0nyh8tt62zhrvtd3xkj8fhgprdmhxue69uhkwmr9v9ek7mnpw3hhytnyv4mz7un9d3shjqgcwaehxw309ahx7umywf5hvefwv9c8qtmjv4kxz7gpzemhxue69uhhyetvv9ujumt0wd68ytnsw43z7s3al0v's speech?`, [{ diff --git a/src/utils/note.ts b/src/utils/note.ts index 8a47937a..1dfa08a1 100644 --- a/src/utils/note.ts +++ b/src/utils/note.ts @@ -33,31 +33,36 @@ function parseNoteContent(content: string, mentions: MastodonMention[]): ParsedN return html``; }, url: ({ attributes, content }) => { - try { - const { pathname } = new URL(content); - const match = pathname.match(new RegExp(`^${nip19.BECH32_REGEX.source}`)); - if (match) { - const bech32 = match[0]; - const extra = pathname.slice(bech32.length); - const decoded = nip19.decode(bech32); - const pubkey = getDecodedPubkey(decoded); - if (pubkey) { - const mention = mentions.find((m) => m.id === pubkey); - const npub = nip19.npubEncode(pubkey); - const acct = mention?.acct ?? npub; - const name = mention?.acct ?? npub.substring(0, 8); - const href = mention?.url ?? Conf.local(`/@${acct}`); - return html`@${name}${extra}`; + const { protocol, pathname } = new URL(content); + + if (protocol === 'nostr:') { + try { + const match = pathname.match(new RegExp(`^${nip19.BECH32_REGEX.source}`)); + if (match) { + const bech32 = match[0]; + const extra = pathname.slice(bech32.length); + const decoded = nip19.decode(bech32); + const pubkey = getDecodedPubkey(decoded); + if (pubkey) { + const mention = mentions.find((m) => m.id === pubkey); + const npub = nip19.npubEncode(pubkey); + const acct = mention?.acct ?? npub; + const name = mention?.acct ?? npub.substring(0, 8); + const href = mention?.url ?? Conf.local(`/@${acct}`); + return html`@${name}${extra}`; + } } + } catch { + // fallthrough } return content; - } catch { - const attr = Object.entries(attributes) - .map(([name, value]) => `${name}="${value}"`) - .join(' '); - - return `${content}`; } + + const attr = Object.entries(attributes) + .map(([name, value]) => `${name}="${value}"`) + .join(' '); + + return `${content}`; }, }, }).replace(/\n+$/, '');