(function (root, factory) { const renderMarkdown = factory(); if (typeof module === 'object' && module.exports) module.exports = renderMarkdown; root.renderMarkdown = renderMarkdown; })(typeof globalThis !== 'undefined' ? globalThis : this, function () { function escapeHtml(value) { return String(value || '').replace(/[&<>"']/g, (character) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''', })[character]); } function renderInline(value) { const code = []; let rendered = escapeHtml(value).replace(/`([^`\n]+)`/g, (_match, body) => { const marker = '\u0000CODE' + code.length + '\u0000'; code.push('' + body + ''); return marker; }); rendered = rendered .replace(/\[([^\]\n]+)\]\(([^\s)]+)(?:\s+"[^&]*?")?\)/g, (_match, label, escapedUrl) => { const url = escapedUrl.replace(/&/g, '&'); const safe = /^(https?:|mailto:)/i.test(url) || /^(\/|#|\.\/|\.\.\/)/.test(url); if (!safe) return label; return '' + label + ''; }) .replace(/\*\*(.+?)\*\*/g, '$1') .replace(/(^|[^*])\*([^*\n]+)\*/g, '$1$2'); return rendered.replace(/\u0000CODE(\d+)\u0000/g, (_match, index) => code[Number(index)]); } function renderList(lines) { const taskList = lines.every(line => /^[-*+]\s+\[[ xX]\]\s+/.test(line)); const items = lines.map(line => { let body = line.replace(/^[-*+]\s+/, ''); if (!taskList) return '
  • ' + renderInline(body) + '
  • '; const checked = /^\[[xX]\]\s+/.test(body); body = body.replace(/^\[[ xX]\]\s+/, ''); return '
  • ' + renderInline(body) + '
  • '; }).join(''); return '' + items + ''; } return function renderMarkdown(raw) { const lines = String(raw || '').replace(/\r\n?/g, '\n').split('\n'); const output = []; let index = 0; while (index < lines.length) { const line = lines[index]; if (!line.trim()) { index += 1; continue; } const fence = line.match(/^```([A-Za-z0-9_-]*)\s*$/); if (fence) { const body = []; index += 1; while (index < lines.length && !/^```\s*$/.test(lines[index])) { body.push(lines[index]); index += 1; } if (index < lines.length) index += 1; const language = fence[1] ? ' class="language-' + fence[1] + '"' : ''; output.push('
    ' + escapeHtml(body.join('\n')) + '
    '); continue; } const heading = line.match(/^(#{1,6})\s+(.+)$/); if (heading) { const level = heading[1].length; output.push('' + renderInline(heading[2]) + ''); index += 1; continue; } if (/^>\s?/.test(line)) { const quote = []; while (index < lines.length && /^>\s?/.test(lines[index])) { quote.push(lines[index].replace(/^>\s?/, '')); index += 1; } output.push('

    ' + renderInline(quote.join('\n')).replace(/\n/g, '
    ') + '

    '); continue; } if (/^[-*+]\s+/.test(line)) { const list = []; while (index < lines.length && /^[-*+]\s+/.test(lines[index])) { list.push(lines[index]); index += 1; } output.push(renderList(list)); continue; } const paragraph = []; while (index < lines.length && lines[index].trim() && !/^```/.test(lines[index]) && !/^(#{1,6})\s+/.test(lines[index]) && !/^>\s?/.test(lines[index]) && !/^[-*+]\s+/.test(lines[index])) { paragraph.push(lines[index]); index += 1; } output.push('

    ' + renderInline(paragraph.join('\n')).replace(/\n/g, '
    ') + '

    '); } return output.join(''); }; });