From b62cf25ae17a56f3024e84232d9477dee8940fa9 Mon Sep 17 00:00:00 2001 From: Silberengel Date: Mon, 8 Dec 2025 20:48:58 +0100 Subject: [PATCH] fix export as asciidoc --- .../PublicationIndex/PublicationIndex.tsx | 37 -------------- src/components/NoteOptions/useMenuActions.tsx | 50 +++++++++++++++++-- 2 files changed, 47 insertions(+), 40 deletions(-) diff --git a/src/components/Note/PublicationIndex/PublicationIndex.tsx b/src/components/Note/PublicationIndex/PublicationIndex.tsx index 492760c..2790b85 100644 --- a/src/components/Note/PublicationIndex/PublicationIndex.tsx +++ b/src/components/Note/PublicationIndex/PublicationIndex.tsx @@ -403,43 +403,6 @@ export default function PublicationIndex({ } } - // Export publication as AsciiDoc - const exportPublication = async () => { - try { - // Collect all content from references - const contentParts: string[] = [] - - for (const ref of references) { - if (!ref.event) continue - - // Extract title - const title = ref.event.tags.find(tag => tag[0] === 'title')?.[1] || 'Untitled' - - // For AsciiDoc, output the raw content with title - contentParts.push(`= ${title}\n\n${ref.event.content}\n\n`) - } - - const fullContent = contentParts.join('\n') - const filename = `${metadata.title || 'publication'}.adoc` - - // Export as AsciiDoc - const blob = new Blob([fullContent], { type: 'text/plain' }) - - const url = URL.createObjectURL(blob) - const a = document.createElement('a') - a.href = url - a.download = filename - document.body.appendChild(a) - a.click() - document.body.removeChild(a) - URL.revokeObjectURL(url) - - logger.info('[PublicationIndex] Exported publication as .adoc') - } catch (error) { - logger.error('[PublicationIndex] Error exporting publication:', error) - alert('Failed to export publication. Please try again.') - } - } // Add current event to visited set const currentCoordinate = useMemo(() => { diff --git a/src/components/NoteOptions/useMenuActions.tsx b/src/components/NoteOptions/useMenuActions.tsx index e479f21..06c8c70 100644 --- a/src/components/NoteOptions/useMenuActions.tsx +++ b/src/components/NoteOptions/useMenuActions.tsx @@ -5,6 +5,7 @@ import { toAlexandria } from '@/lib/link' import logger from '@/lib/logger' import { pubkeyToNpub } from '@/lib/pubkey' import { normalizeUrl, simplifyUrl } from '@/lib/url' +import { generateBech32IdFromATag } from '@/lib/tag' import { useCurrentRelays } from '@/providers/CurrentRelaysProvider' import { useFavoriteRelays } from '@/providers/FavoriteRelaysProvider' import { useMuteList } from '@/providers/MuteListProvider' @@ -394,13 +395,56 @@ export function useMenuActions({ } } - const exportAsAsciidoc = () => { + const exportAsAsciidoc = async () => { if (!isArticleType) return try { const title = articleMetadata?.title || 'Article' - const content = event.content - const filename = `${title}.adoc` + let content = event.content + let filename = `${title}.adoc` + + // For publications (30040), export all referenced sections + if (event.kind === ExtendedKind.PUBLICATION) { + const contentParts: string[] = [] + + // Extract all 'a' tag references + const aTags = event.tags.filter(tag => tag[0] === 'a' && tag[1]) + + // Fetch all referenced events + const fetchPromises = aTags.map(async (tag) => { + try { + const coordinate = tag[1] + const [kindStr] = coordinate.split(':') + const kind = parseInt(kindStr) + + if (isNaN(kind)) return null + + // Try to fetch the event + const aTag = ['a', coordinate, tag[2] || '', tag[3] || ''] + const bech32Id = generateBech32IdFromATag(aTag) + if (bech32Id) { + const fetchedEvent = await client.fetchEvent(bech32Id) + return fetchedEvent + } + return null + } catch (error) { + logger.warn('[NoteOptions] Error fetching referenced event for export:', error) + return null + } + }) + + const referencedEvents = (await Promise.all(fetchPromises)).filter((e): e is Event => e !== null) + + // Combine all events into one AsciiDoc document + for (const refEvent of referencedEvents) { + const refTitle = refEvent.tags.find(tag => tag[0] === 'title')?.[1] || 'Untitled' + contentParts.push(`= ${refTitle}\n\n${refEvent.content}\n\n`) + } + + if (contentParts.length > 0) { + content = contentParts.join('\n') + } + } const blob = new Blob([content], { type: 'text/plain' }) const url = URL.createObjectURL(blob)