mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
Fix parsing of regular URLs
This commit is contained in:
parent
cde70e4c8f
commit
a63b838195
2 changed files with 32 additions and 22 deletions
|
|
@ -10,7 +10,12 @@ Deno.test('parseNoteContent', () => {
|
||||||
assertEquals(firstUrl, undefined);
|
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: <a href="https://alexgleason.me">https://alexgleason.me</a>');
|
||||||
|
});
|
||||||
|
|
||||||
|
Deno.test('parseNoteContent parses mentions with apostrophes', () => {
|
||||||
const { html } = parseNoteContent(
|
const { html } = parseNoteContent(
|
||||||
`did you see nostr:nprofile1qqsqgc0uhmxycvm5gwvn944c7yfxnnxm0nyh8tt62zhrvtd3xkj8fhgprdmhxue69uhkwmr9v9ek7mnpw3hhytnyv4mz7un9d3shjqgcwaehxw309ahx7umywf5hvefwv9c8qtmjv4kxz7gpzemhxue69uhhyetvv9ujumt0wd68ytnsw43z7s3al0v's speech?`,
|
`did you see nostr:nprofile1qqsqgc0uhmxycvm5gwvn944c7yfxnnxm0nyh8tt62zhrvtd3xkj8fhgprdmhxue69uhkwmr9v9ek7mnpw3hhytnyv4mz7un9d3shjqgcwaehxw309ahx7umywf5hvefwv9c8qtmjv4kxz7gpzemhxue69uhhyetvv9ujumt0wd68ytnsw43z7s3al0v's speech?`,
|
||||||
[{
|
[{
|
||||||
|
|
|
||||||
|
|
@ -33,31 +33,36 @@ function parseNoteContent(content: string, mentions: MastodonMention[]): ParsedN
|
||||||
return html`<a class="mention hashtag" href="${href}" rel="tag"><span>#</span>${tag}</a>`;
|
return html`<a class="mention hashtag" href="${href}" rel="tag"><span>#</span>${tag}</a>`;
|
||||||
},
|
},
|
||||||
url: ({ attributes, content }) => {
|
url: ({ attributes, content }) => {
|
||||||
try {
|
const { protocol, pathname } = new URL(content);
|
||||||
const { pathname } = new URL(content);
|
|
||||||
const match = pathname.match(new RegExp(`^${nip19.BECH32_REGEX.source}`));
|
if (protocol === 'nostr:') {
|
||||||
if (match) {
|
try {
|
||||||
const bech32 = match[0];
|
const match = pathname.match(new RegExp(`^${nip19.BECH32_REGEX.source}`));
|
||||||
const extra = pathname.slice(bech32.length);
|
if (match) {
|
||||||
const decoded = nip19.decode(bech32);
|
const bech32 = match[0];
|
||||||
const pubkey = getDecodedPubkey(decoded);
|
const extra = pathname.slice(bech32.length);
|
||||||
if (pubkey) {
|
const decoded = nip19.decode(bech32);
|
||||||
const mention = mentions.find((m) => m.id === pubkey);
|
const pubkey = getDecodedPubkey(decoded);
|
||||||
const npub = nip19.npubEncode(pubkey);
|
if (pubkey) {
|
||||||
const acct = mention?.acct ?? npub;
|
const mention = mentions.find((m) => m.id === pubkey);
|
||||||
const name = mention?.acct ?? npub.substring(0, 8);
|
const npub = nip19.npubEncode(pubkey);
|
||||||
const href = mention?.url ?? Conf.local(`/@${acct}`);
|
const acct = mention?.acct ?? npub;
|
||||||
return html`<span class="h-card"><a class="u-url mention" href="${href}" rel="ugc">@<span>${name}</span></a></span>${extra}`;
|
const name = mention?.acct ?? npub.substring(0, 8);
|
||||||
|
const href = mention?.url ?? Conf.local(`/@${acct}`);
|
||||||
|
return html`<span class="h-card"><a class="u-url mention" href="${href}" rel="ugc">@<span>${name}</span></a></span>${extra}`;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} catch {
|
||||||
|
// fallthrough
|
||||||
}
|
}
|
||||||
return content;
|
return content;
|
||||||
} catch {
|
|
||||||
const attr = Object.entries(attributes)
|
|
||||||
.map(([name, value]) => `${name}="${value}"`)
|
|
||||||
.join(' ');
|
|
||||||
|
|
||||||
return `<a ${attr}>${content}</a>`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const attr = Object.entries(attributes)
|
||||||
|
.map(([name, value]) => `${name}="${value}"`)
|
||||||
|
.join(' ');
|
||||||
|
|
||||||
|
return `<a ${attr}>${content}</a>`;
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}).replace(/\n+$/, '');
|
}).replace(/\n+$/, '');
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue