diff --git a/network-poc/frontend/src/pages/index.astro b/network-poc/frontend/src/pages/index.astro index 039cd68..4751da0 100644 --- a/network-poc/frontend/src/pages/index.astro +++ b/network-poc/frontend/src/pages/index.astro @@ -906,18 +906,71 @@ OUTPUT FORMAT: })(); function renderMd(md) { + const lines = md.split('\n'); let html = '', inCode = false, lang = '', buf = ''; - for (const line of md.split('\n')) { - if (line.startsWith('```')) { if (inCode) { html += `
${buf.replace(/
`; inCode=false; buf=''; } else { inCode=true; lang=line.slice(3).trim()||'plaintext'; } continue; } - if (inCode) { buf += (buf?'\n':'') + line; continue; } - if (!line.trim()) { html += '
'; continue; } - if (line.startsWith('# ')) html += `

${line.slice(2)}

`; - else if (line.startsWith('## ')) html += `

${line.slice(3)}

`; - else if (line.startsWith('### ')) html += `

${line.slice(4)}

`; - else if (line.startsWith('---')) html += '
'; - else if (line.match(/^[\-\*] /)) html += `
${line.replace(/^[\-\*] /,'• ').replace(/\*\*(.+?)\*\*/g,'$1').replace(/`(.+?)`/g,'$1')}
`; - else html += `

${line.replace(/\*\*(.+?)\*\*/g,'$1').replace(/`(.+?)`/g,'$1')}

`; + let inTable = false, tableRows = []; + + function inline(text) { + return text + .replace(/\*\*(.+?)\*\*/g, '$1') + .replace(/\*(.+?)\*/g, '$1') + .replace(/`([^`]+)`/g, '$1'); } + + function flushTable() { + if (!inTable || tableRows.length < 2) { inTable = false; tableRows = []; return; } + const hdr = tableRows[0].split('|').filter(c => c.trim()); + const body = tableRows.slice(2); + html += '
'; + html += '' + hdr.map(c => ``).join('') + ''; + for (const row of body) { + const cells = row.split('|').filter(c => c.trim()); + if (cells.length) html += '' + cells.map(c => ``).join('') + ''; + } + html += '
${inline(c.trim())}
${inline(c.trim())}
'; + inTable = false; tableRows = []; + } + + for (const line of lines) { + // Koodiblokit + if (line.startsWith('```')) { + if (inCode) { + html += `
${buf.replace(/
`; + inCode = false; buf = ''; + } else { + flushTable(); + inCode = true; lang = line.slice(3).trim() || 'plaintext'; + } + continue; + } + if (inCode) { buf += (buf ? '\n' : '') + line; continue; } + + // Taulukot + if (line.includes('|') && line.trim().startsWith('|')) { + if (!inTable) inTable = true; + tableRows.push(line); + continue; + } else { flushTable(); } + + // Tyhjä rivi + if (!line.trim()) { html += '
'; continue; } + + // Otsikot + if (line.startsWith('# ')) { html += `

${inline(line.slice(2))}

`; continue; } + if (line.startsWith('## ')) { html += `

${inline(line.slice(3))}

`; continue; } + if (line.startsWith('### ')) { html += `

${inline(line.slice(4))}

`; continue; } + + // Viiva + if (line.match(/^-{3,}$/)) { html += '
'; continue; } + + // Listat + if (line.match(/^[\-\*] /)) { html += `
${inline(line.replace(/^[\-\*] /, '• '))}
`; continue; } + if (line.match(/^\d+\. /)) { html += `
${inline(line)}
`; continue; } + + // Normaali teksti + html += `

${inline(line)}

`; + } + flushTable(); return html; } // === Settings panel ===