Browse Source

fix export as asciidoc

imwald
Silberengel 3 months ago
parent
commit
b62cf25ae1
  1. 37
      src/components/Note/PublicationIndex/PublicationIndex.tsx
  2. 50
      src/components/NoteOptions/useMenuActions.tsx

37
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 // Add current event to visited set
const currentCoordinate = useMemo(() => { const currentCoordinate = useMemo(() => {

50
src/components/NoteOptions/useMenuActions.tsx

@ -5,6 +5,7 @@ import { toAlexandria } from '@/lib/link'
import logger from '@/lib/logger' import logger from '@/lib/logger'
import { pubkeyToNpub } from '@/lib/pubkey' import { pubkeyToNpub } from '@/lib/pubkey'
import { normalizeUrl, simplifyUrl } from '@/lib/url' import { normalizeUrl, simplifyUrl } from '@/lib/url'
import { generateBech32IdFromATag } from '@/lib/tag'
import { useCurrentRelays } from '@/providers/CurrentRelaysProvider' import { useCurrentRelays } from '@/providers/CurrentRelaysProvider'
import { useFavoriteRelays } from '@/providers/FavoriteRelaysProvider' import { useFavoriteRelays } from '@/providers/FavoriteRelaysProvider'
import { useMuteList } from '@/providers/MuteListProvider' import { useMuteList } from '@/providers/MuteListProvider'
@ -394,13 +395,56 @@ export function useMenuActions({
} }
} }
const exportAsAsciidoc = () => { const exportAsAsciidoc = async () => {
if (!isArticleType) return if (!isArticleType) return
try { try {
const title = articleMetadata?.title || 'Article' const title = articleMetadata?.title || 'Article'
const content = event.content let content = event.content
const filename = `${title}.adoc` 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 blob = new Blob([content], { type: 'text/plain' })
const url = URL.createObjectURL(blob) const url = URL.createObjectURL(blob)

Loading…
Cancel
Save