story-research-zapwall/lib/presentationParsing.ts
2026-01-06 00:26:31 +01:00

63 lines
2.2 KiB
TypeScript

import type { Article } from '@/types/nostr'
/**
* Extract presentation data from article content
* Supports two formats:
* 1. Old format: "${presentation}\n\n---\n\nDescription du contenu :\n${contentDescription}"
* 2. New format: "Nouveau profil publié sur zapwall.fr\n<url>\n<photo>\nPrésentation personnelle : <presentation>\nDescription de votre contenu : <description>\nAdresse Bitcoin mainnet (pour le sponsoring) : <adresse>\n\n---\n\n[Metadata JSON]\n<json>"
* The profile JSON is stored in the [Metadata JSON] section of the content, not in tags
*/
export function extractPresentationData(presentation: Article): {
presentation: string
contentDescription: string
} {
const content = presentation.content
// Try new format first
const newFormatMatch = content.match(/Présentation personnelle : (.+?)(?:\nDescription de votre contenu :|$)/s)
const descriptionMatch = content.match(/Description de votre contenu : (.+?)(?:\nAdresse Bitcoin mainnet|$)/s)
if (newFormatMatch && descriptionMatch) {
return {
presentation: newFormatMatch[1].trim(),
contentDescription: descriptionMatch[1].trim(),
}
}
// Try to extract from JSON metadata section
const jsonMatch = content.match(/\[Metadata JSON\]\n(.+)$/s)
if (jsonMatch) {
try {
const profileJson = JSON.parse(jsonMatch[1].trim())
if (profileJson.presentation && profileJson.contentDescription) {
return {
presentation: profileJson.presentation,
contentDescription: profileJson.contentDescription,
}
}
} catch (e) {
// JSON parsing failed, continue with old format
}
}
// Fallback to old format
const separator = '\n\n---\n\nDescription du contenu :\n'
const separatorIndex = content.indexOf(separator)
if (separatorIndex === -1) {
// Fallback: return content as presentation if separator not found
return {
presentation: content,
contentDescription: '',
}
}
const presentationText = content.substring(0, separatorIndex)
const contentDescription = content.substring(separatorIndex + separator.length)
return {
presentation: presentationText,
contentDescription,
}
}