(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;
return ``;
});
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 ? '
${esc(code.join('\n'))}`);
continue;
}
const heading = line.match(/^(#{1,4})\s+(.*)$/);
if (heading) {
const level = heading[1].length;
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('
')}