mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
removeTrailingUrls -> removeTrailingTokens
This commit is contained in:
parent
bd71b45a8d
commit
69a9534463
3 changed files with 14 additions and 14 deletions
|
|
@ -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<string>([
|
||||
'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<string>([
|
||||
'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<string>([
|
||||
'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<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, '');
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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>): string {
|
||||
/** Remove the tokens from the _end_ of the content. */
|
||||
export function removeTrailingTokens(text: string, tokens: Set<string>): 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;
|
||||
|
|
|
|||
|
|
@ -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([
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue