From 50b32a66deb0c5795d80bcf885c80e36a5404f28 Mon Sep 17 00:00:00 2001 From: Gigi Date: Thu, 2 Oct 2025 22:34:06 +0200 Subject: [PATCH] feat(bookmarks): extract URLs from content into urlReferences for each item --- src/services/bookmarkHelpers.ts | 10 ++++++++++ src/services/bookmarkService.ts | 12 ++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/services/bookmarkHelpers.ts b/src/services/bookmarkHelpers.ts index 187ea46f..41a78a89 100644 --- a/src/services/bookmarkHelpers.ts +++ b/src/services/bookmarkHelpers.ts @@ -131,4 +131,14 @@ export function dedupeBookmarksById(bookmarks: IndividualBookmark[]): Individual return result } +export function extractUrlsFromContent(content: string): string[] { + if (!content) return [] + // Basic URL regex covering http(s) schemes + const urlRegex = /https?:\/\/[\w.-]+(?:\/[\w\-._~:/?#[\]@!$&'()*+,;=%]*)?/gi + const matches = content.match(urlRegex) + if (!matches) return [] + // Normalize by trimming trailing punctuation + return Array.from(new Set(matches.map(u => u.replace(/[),.;]+$/, '')))) +} + diff --git a/src/services/bookmarkService.ts b/src/services/bookmarkService.ts index b6a09f87..c16f5e47 100644 --- a/src/services/bookmarkService.ts +++ b/src/services/bookmarkService.ts @@ -9,7 +9,8 @@ import { isHexId, hasNip04Decrypt, hasNip44Decrypt, - dedupeBookmarksById + dedupeBookmarksById, + extractUrlsFromContent } from './bookmarkHelpers' import { Bookmark } from '../types/bookmarks' import { collectBookmarksFromEvents } from './bookmarkProcessing.ts' @@ -113,7 +114,14 @@ export const fetchBookmarks = async ( ]) // Sort individual bookmarks by timestamp (newest first) - const sortedBookmarks = allBookmarks.sort((a, b) => (b.created_at || 0) - (a.created_at || 0)) + const enriched = allBookmarks.map(b => ({ + ...b, + tags: b.tags || [], + content: b.content || '' + })) + const sortedBookmarks = enriched + .map(b => ({ ...b, urlReferences: extractUrlsFromContent(b.content) })) + .sort((a, b) => (b.created_at || 0) - (a.created_at || 0)) const bookmark: Bookmark = { id: `${activeAccount.pubkey}-bookmarks`,