import type { Event } from 'nostr-tools' import type { Article } from '@/types/nostr' /** * Parse article metadata from Nostr event */ export function parseArticleFromEvent(event: Event): Article | null { try { const tags = extractTags(event) const { previewContent } = getPreviewContent(event.content, tags.preview) return buildArticle(event, tags, previewContent) } catch (e) { console.error('Error parsing article:', e) return null } } function extractTags(event: Event) { const findTag = (key: string) => event.tags.find((tag) => tag[0] === key)?.[1] return { title: findTag('title') ?? 'Untitled', preview: findTag('preview'), zapAmount: parseInt(findTag('zap') ?? '800', 10), invoice: findTag('invoice'), paymentHash: findTag('payment_hash'), category: findTag('category') as import('@/types/nostr').ArticleCategory | undefined, isPresentation: findTag('presentation') === 'true', mainnetAddress: findTag('mainnet_address'), totalSponsoring: parseInt(findTag('total_sponsoring') ?? '0', 10), authorPresentationId: findTag('author_presentation_id'), } } function getPreviewContent(content: string, previewTag?: string) { const lines = content.split('\n') const previewContent = previewTag ?? lines[0] ?? content.substring(0, 200) return { previewContent } } function buildArticle(event: Event, tags: ReturnType, preview: string): Article { return { id: event.id, pubkey: event.pubkey, title: tags.title, preview, content: '', createdAt: event.created_at, zapAmount: tags.zapAmount, paid: false, invoice: tags.invoice, paymentHash: tags.paymentHash, category: tags.category, isPresentation: tags.isPresentation, mainnetAddress: tags.mainnetAddress, totalSponsoring: tags.totalSponsoring, authorPresentationId: tags.authorPresentationId, } }