/** * Markdown cleanup utility for leftover markdown syntax after Asciidoc rendering */ export function cleanupMarkdown(html: string): string { let cleaned = html // Clean up markdown image syntax: ![alt](url) cleaned = cleaned.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, (_match, alt, url) => { const altText = alt || '' return `${altText}` }) // Clean up markdown link syntax: [text](url) cleaned = cleaned.replace(/\[([^\]]+)\]\(([^)]+)\)/g, (_match, text, url) => { // Check if it's already an HTML link if (cleaned.includes(`href="${url}"`)) { return _match } return `${text} ` }) // Clean up markdown table syntax cleaned = cleanupMarkdownTables(cleaned) return cleaned } function cleanupMarkdownTables(html: string): string { // Simple markdown table detection and conversion const tableRegex = /(\|.*\|[\r\n]+\|[\s\-\|]*[\r\n]+(\|.*\|[\r\n]+)*)/g return html.replace(tableRegex, (match) => { const lines = match.trim().split('\n').filter(line => line.trim()) if (lines.length < 2) return match const headerRow = lines[0] const separatorRow = lines[1] const dataRows = lines.slice(2) // Check if it's actually a table (has separator row with dashes) if (!separatorRow.includes('-')) return match const headers = headerRow.split('|').map(cell => cell.trim()).filter(cell => cell) const rows = dataRows.map(row => row.split('|').map(cell => cell.trim()).filter(cell => cell) ) let tableHtml = '\n' // Header tableHtml += ' \n \n' headers.forEach(header => { tableHtml += ` \n` }) tableHtml += ' \n \n' // Body tableHtml += ' \n' rows.forEach(row => { tableHtml += ' \n' row.forEach((cell, index) => { const tag = index < headers.length ? 'td' : 'td' tableHtml += ` <${tag} class="border border-gray-300 px-4 py-2">${cell}\n` }) tableHtml += ' \n' }) tableHtml += ' \n' tableHtml += '
${header}
' return tableHtml }) }