removeTrailingUrls -> removeTrailingTokens

This commit is contained in:
Alex Gleason 2025-03-12 16:48:31 -05:00
parent bd71b45a8d
commit 69a9534463
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
3 changed files with 14 additions and 14 deletions

View file

@ -1,7 +1,7 @@
import { DittoConf } from '@ditto/conf'; import { DittoConf } from '@ditto/conf';
import { assertEquals } from '@std/assert'; import { assertEquals } from '@std/assert';
import { contentToHtml, getCardUrl, getMediaLinks, removeTrailingUrls } from '@/utils/note.ts'; import { contentToHtml, getCardUrl, getMediaLinks, removeTrailingTokens } from '@/utils/note.ts';
import { genEvent } from '@nostrify/nostrify/test'; import { genEvent } from '@nostrify/nostrify/test';
Deno.test('contentToHtml', () => { Deno.test('contentToHtml', () => {
@ -124,13 +124,13 @@ Deno.test('getMediaLinks', () => {
]]); ]]);
}); });
Deno.test('removeTrailingUrls with spaces', () => { Deno.test('removeTrailingTokens with spaces', () => {
const urls = new Set<string>([ const urls = new Set<string>([
'https://ditto.pub/a.png', 'https://ditto.pub/a.png',
'https://ditto.pub/b.jpg', 'https://ditto.pub/b.jpg',
]); ]);
const result = removeTrailingUrls( const result = removeTrailingTokens(
'hey!\n\nthis is cool https://ditto.pub/a.png https://ditto.pub/b.jpg', 'hey!\n\nthis is cool https://ditto.pub/a.png https://ditto.pub/b.jpg',
urls, urls,
); );
@ -138,13 +138,13 @@ Deno.test('removeTrailingUrls with spaces', () => {
assertEquals(result, 'hey!\n\nthis is cool'); assertEquals(result, 'hey!\n\nthis is cool');
}); });
Deno.test('removeTrailingUrls with newlines', () => { Deno.test('removeTrailingTokens with newlines', () => {
const urls = new Set<string>([ const urls = new Set<string>([
'https://ditto.pub/a.png', 'https://ditto.pub/a.png',
'https://ditto.pub/b.jpg', 'https://ditto.pub/b.jpg',
]); ]);
const result = removeTrailingUrls( const result = removeTrailingTokens(
'Hey!\n\nthis is cool \n\nhttps://ditto.pub/a.png\nhttps://ditto.pub/b.jpg', 'Hey!\n\nthis is cool \n\nhttps://ditto.pub/a.png\nhttps://ditto.pub/b.jpg',
urls, urls,
); );
@ -152,13 +152,13 @@ Deno.test('removeTrailingUrls with newlines', () => {
assertEquals(result, 'Hey!\n\nthis is cool'); assertEquals(result, 'Hey!\n\nthis is cool');
}); });
Deno.test('removeTrailingUrls with only URLs', () => { Deno.test('removeTrailingTokens with only URLs', () => {
const urls = new Set<string>([ const urls = new Set<string>([
'https://ditto.pub/a.png', 'https://ditto.pub/a.png',
'https://ditto.pub/b.jpg', 'https://ditto.pub/b.jpg',
]); ]);
const result = removeTrailingUrls( const result = removeTrailingTokens(
'https://ditto.pub/a.png https://ditto.pub/b.jpg', 'https://ditto.pub/a.png https://ditto.pub/b.jpg',
urls, urls,
); );
@ -166,9 +166,9 @@ Deno.test('removeTrailingUrls with only URLs', () => {
assertEquals(result, ''); assertEquals(result, '');
}); });
Deno.test('removeTrailingUrls with just one URL', () => { Deno.test('removeTrailingTokens with just one URL', () => {
const urls = new Set<string>(['https://ditto.pub/a.png']); const urls = new Set<string>(['https://ditto.pub/a.png']);
const result = removeTrailingUrls('https://ditto.pub/a.png', urls); const result = removeTrailingTokens('https://ditto.pub/a.png', urls);
assertEquals(result, ''); assertEquals(result, '');
}); });

View file

@ -69,13 +69,13 @@ export function contentToHtml(content: string, mentions: MastodonMention[], opts
}).replace(/\n+$/, ''); }).replace(/\n+$/, '');
} }
/** Remove the URLs from the _end_ of the content. */ /** Remove the tokens from the _end_ of the content. */
export function removeTrailingUrls(text: string, urls: Set<string>): string { export function removeTrailingTokens(text: string, tokens: Set<string>): string {
let trimmedText = text; let trimmedText = text;
while (true) { while (true) {
const match = trimmedText.match(/\s?([^\s]+)\s?$/); const match = trimmedText.match(/\s?([^\s]+)\s?$/);
if (match && urls.has(match[1])) { if (match && tokens.has(match[1])) {
trimmedText = trimmedText.slice(0, match.index).replace(/\s+$/, ''); trimmedText = trimmedText.slice(0, match.index).replace(/\s+$/, '');
} else { } else {
break; break;

View file

@ -4,7 +4,7 @@ import { nip19 } from 'nostr-tools';
import { Conf } from '@/config.ts'; import { Conf } from '@/config.ts';
import { type DittoEvent } from '@/interfaces/DittoEvent.ts'; import { type DittoEvent } from '@/interfaces/DittoEvent.ts';
import { nostrDate } from '@/utils.ts'; import { nostrDate } from '@/utils.ts';
import { contentToHtml, getLinks, getMediaLinks, removeTrailingUrls } from '@/utils/note.ts'; import { contentToHtml, getLinks, getMediaLinks, removeTrailingTokens } from '@/utils/note.ts';
import { findReplyTag } from '@/utils/tags.ts'; import { findReplyTag } from '@/utils/tags.ts';
import { accountFromPubkey, renderAccount } from '@/views/mastodon/accounts.ts'; import { accountFromPubkey, renderAccount } from '@/views/mastodon/accounts.ts';
import { renderAttachment } from '@/views/mastodon/attachments.ts'; import { renderAttachment } from '@/views/mastodon/attachments.ts';
@ -63,7 +63,7 @@ async function renderStatus(
} }
} }
const html = contentToHtml(removeTrailingUrls(event.content, mediaUrls), mentions, { conf: Conf }); const html = contentToHtml(removeTrailingTokens(event.content, mediaUrls), mentions, { conf: Conf });
const relatedEvents = viewerPubkey const relatedEvents = viewerPubkey
? await store.query([ ? await store.query([