diff --git a/packages/ditto/utils/note.test.ts b/packages/ditto/utils/note.test.ts index 2ff47453..69e5bdbb 100644 --- a/packages/ditto/utils/note.test.ts +++ b/packages/ditto/utils/note.test.ts @@ -1,7 +1,7 @@ import { DittoConf } from '@ditto/conf'; 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'; Deno.test('contentToHtml', () => { @@ -124,13 +124,13 @@ Deno.test('getMediaLinks', () => { ]]); }); -Deno.test('removeTrailingUrls with spaces', () => { +Deno.test('removeTrailingTokens with spaces', () => { const urls = new Set([ 'https://ditto.pub/a.png', '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', urls, ); @@ -138,13 +138,13 @@ Deno.test('removeTrailingUrls with spaces', () => { assertEquals(result, 'hey!\n\nthis is cool'); }); -Deno.test('removeTrailingUrls with newlines', () => { +Deno.test('removeTrailingTokens with newlines', () => { const urls = new Set([ 'https://ditto.pub/a.png', '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', urls, ); @@ -152,13 +152,13 @@ Deno.test('removeTrailingUrls with newlines', () => { assertEquals(result, 'Hey!\n\nthis is cool'); }); -Deno.test('removeTrailingUrls with only URLs', () => { +Deno.test('removeTrailingTokens with only URLs', () => { const urls = new Set([ 'https://ditto.pub/a.png', 'https://ditto.pub/b.jpg', ]); - const result = removeTrailingUrls( + const result = removeTrailingTokens( 'https://ditto.pub/a.png https://ditto.pub/b.jpg', urls, ); @@ -166,9 +166,9 @@ Deno.test('removeTrailingUrls with only URLs', () => { assertEquals(result, ''); }); -Deno.test('removeTrailingUrls with just one URL', () => { +Deno.test('removeTrailingTokens with just one URL', () => { const urls = new Set(['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, ''); }); diff --git a/packages/ditto/utils/note.ts b/packages/ditto/utils/note.ts index 2bbf0ed6..9fe9439b 100644 --- a/packages/ditto/utils/note.ts +++ b/packages/ditto/utils/note.ts @@ -69,13 +69,13 @@ export function contentToHtml(content: string, mentions: MastodonMention[], opts }).replace(/\n+$/, ''); } -/** Remove the URLs from the _end_ of the content. */ -export function removeTrailingUrls(text: string, urls: Set): string { +/** Remove the tokens from the _end_ of the content. */ +export function removeTrailingTokens(text: string, tokens: Set): string { let trimmedText = text; while (true) { 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+$/, ''); } else { break; diff --git a/packages/ditto/views/mastodon/statuses.ts b/packages/ditto/views/mastodon/statuses.ts index 65839f7a..a2d4a740 100644 --- a/packages/ditto/views/mastodon/statuses.ts +++ b/packages/ditto/views/mastodon/statuses.ts @@ -4,7 +4,7 @@ import { nip19 } from 'nostr-tools'; import { Conf } from '@/config.ts'; import { type DittoEvent } from '@/interfaces/DittoEvent.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 { accountFromPubkey, renderAccount } from '@/views/mastodon/accounts.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 ? await store.query([