(function () { function esc(value) { return String(value == null ? '' : value) .replace(/&/g, '&') .replace(//g, '>') .replace(/"/g, '"'); } function safeUrl(value) { const url = String(value || '').trim(); if (/^\/uploads\/[A-Za-z0-9._-]+$/.test(url)) return url; if (/^https?:\/\/[^\s]+$/i.test(url)) return url; return ''; } function thumbUrl(path) { if (typeof window.thumbSrc === 'function') return window.thumbSrc(path); if (path.startsWith('/uploads/')) return '/uploads/thumb/' + path.slice('/uploads/'.length); return path; } function inline(raw) { const codes = []; let text = String(raw == null ? '' : raw).replace(/`([^`]+)`/g, (m, code) => { codes.push(code); return `\u0000${codes.length - 1}\u0000`; }); text = esc(text); text = text.replace(/!\[([^\]]*)\]\(([^)\s]+)\)/g, (m, alt, src) => { const url = safeUrl(src.replace(/&/g, '&')); if (!url) return alt; const img = `${esc(alt)}`; if (alt) return `
${img}
${esc(alt)}
`; return img; }); text = text.replace(/\[([^\]]+)\]\(([^)\s]+)\)/g, (m, label, href) => { const url = safeUrl(href.replace(/&/g, '&')); if (!url) return label; const external = /^https?:\/\//i.test(url); return `${label}`; }); text = text.replace(/\*\*([^*]+)\*\*/g, '$1'); text = text.replace(/__([^_]+)__/g, '$1'); text = text.replace(/~~([^~]+)~~/g, '$1'); text = text.replace(/(^|[^*])\*([^*\n]+)\*/g, '$1$2'); text = text.replace(/\u0000(\d+)\u0000/g, (m, index) => `${esc(codes[Number(index)])}`); return text; } function isBlockStart(line) { return /^\s*$/.test(line) || /^```/.test(line) || /^#{1,4}\s+/.test(line) || /^\s*>\s?/.test(line) || /^\s*([-*+]|\d+\.)\s+/.test(line) || /^\s*([-*_])\1{2,}\s*$/.test(line); } function parseList(lines, start) { const baseIndent = lines[start].match(/^(\s*)/)[1].length; const ordered = /^\s*\d+\.\s+/.test(lines[start]); let html = ordered ? '
    ' : '
' : ''; return { html, next: i }; } function splitRow(line) { let row = line.trim(); if (row.startsWith('|')) row = row.slice(1); if (row.endsWith('|')) row = row.slice(0, -1); return row.split('|').map(cell => cell.trim()); } function renderTable(lines, start) { const head = splitRow(lines[start]); let i = start + 2; const rows = []; while (i < lines.length && lines[i].includes('|') && lines[i].trim()) { rows.push(splitRow(lines[i])); i++; } const thead = head.map(cell => `${inline(cell)}`).join(''); const tbody = rows.map(row => `${row.map(cell => `${inline(cell)}`).join('')}`).join(''); return { html: `
${thead}${tbody}
`, next: i, }; } function render(markdown) { const lines = String(markdown == null ? '' : markdown).replace(/\r\n?/g, '\n').split('\n'); const out = []; let i = 0; while (i < lines.length) { const line = lines[i]; if (/^\s*$/.test(line)) { i++; continue; } const fence = line.match(/^```\s*([\w-]*)\s*$/); if (fence) { const code = []; i++; while (i < lines.length && !/^```\s*$/.test(lines[i])) { code.push(lines[i]); i++; } if (i < lines.length) i++; out.push(`
${esc(code.join('\n'))}
`); continue; } const heading = line.match(/^(#{1,4})\s+(.*)$/); if (heading) { const level = heading[1].length; out.push(`${inline(heading[2])}`); i++; continue; } if (/^\s*([-*_])\1{2,}\s*$/.test(line)) { out.push('
'); i++; continue; } if (/^\s*>\s?/.test(line)) { const quote = []; while (i < lines.length && /^\s*>\s?/.test(lines[i])) { quote.push(lines[i].replace(/^\s*>\s?/, '')); i++; } out.push(`
${render(quote.join('\n'))}
`); continue; } if (line.includes('|') && i + 1 < lines.length && /^\s*\|?[\s:|-]+\|[\s:|-]*$/.test(lines[i + 1]) && lines[i + 1].includes('-')) { const table = renderTable(lines, i); out.push(table.html); i = table.next; continue; } if (/^\s*([-*+]|\d+\.)\s+/.test(line)) { const list = parseList(lines, i); out.push(list.html); i = list.next; continue; } const paragraph = []; while (i < lines.length && !isBlockStart(lines[i])) { paragraph.push(lines[i]); i++; } if (!paragraph.length) { i++; continue; } out.push(`

${paragraph.map(inline).join('
')}

`); } return out.join('\n'); } let fallbackBound = false; function bindImageFallbacks() { if (fallbackBound || typeof document === 'undefined') return; fallbackBound = true; document.addEventListener('error', (e) => { const img = e.target; if (!(img instanceof HTMLImageElement) || !img.hasAttribute('data-img')) return; const full = img.getAttribute('data-img'); if (img.getAttribute('src') !== full) img.setAttribute('src', full); }, true); } bindImageFallbacks(); window.WhatIDoMD = { render, inline, esc, thumbUrl, safeUrl }; })();