import NDK from "@nostr-dev-kit/ndk"; import { processAsciiDocAnchors, processImageWithReveal, processNostrIdentifiersInText, processWikilinks, } from "./markupUtils.ts"; /** * Processes AsciiDoc image blocks to add reveal/enlarge functionality */ function processImageBlocks(html: string): string { // Process image blocks with reveal functionality return html.replace( /
\s*
\s*]+)>\s*<\/div>\s*(?:
([^<]+)<\/div>)?\s*<\/div>/g, (match, imgAttributes, title) => { // Extract src and alt from img attributes const srcMatch = imgAttributes.match(/src="([^"]+)"/); const altMatch = imgAttributes.match(/alt="([^"]*)"/); const src = srcMatch ? srcMatch[1] : ""; const alt = altMatch ? altMatch[1] : ""; const titleHtml = title ? `
${title}
` : ""; return `
${processImageWithReveal(src, alt)}
${titleHtml}
`; }, ); } /** * Fixes AsciiDoctor stem blocks for MathJax rendering. * Joins split spans and wraps content in $$...$$ for block math. */ function fixStemBlocks(html: string): string { // Replace
$...$
// with
$$...$$
return html.replace( /
\s*
\s*\$<\/span>([\s\S]*?)\$<\/span>\s*<\/div>\s*<\/div>/g, (_match, mathContent) => { // Remove any extra tags inside mathContent const cleanMath = mathContent.replace(/<\/?span[^>]*>/g, "").trim(); return `
$$${cleanMath}$$
`; }, ); } /** * Post-processes asciidoctor HTML output to add wikilink and nostr address rendering. * This function should be called after asciidoctor.convert() to enhance the HTML output. */ export async function postProcessAsciidoctorHtml( html: string, ndk?: NDK, ): Promise { if (!html) return html; try { // First process AsciiDoctor-generated anchors let processedHtml = processAsciiDocAnchors(html); // Then process wikilinks in [[...]] format (if any remain) processedHtml = processWikilinks(processedHtml); // Then process nostr addresses (but not those already in links) processedHtml = await processNostrIdentifiersInText(processedHtml, ndk); processedHtml = fixStemBlocks(processedHtml); // Fix math blocks for MathJax // Process image blocks to add reveal/enlarge functionality processedHtml = processImageBlocks(processedHtml); return processedHtml; } catch (error) { console.error("Error in postProcessAsciidoctorHtml:", error); return html; // Return original HTML if processing fails } }