/** * Lightweight markdown → HTML for offline 小红书小工具. * No eval / network / Worker. */ (function (global) { function themeColors(theme) { if (theme === "light") { return { text: "#1e1630", muted: "#6d6286", accent: "#db2777", accentSoft: "rgba(219,39,119,.1)", code: "#7c3aed", strong: "#120f1d", }; } if (theme === "sepia") { return { text: "#3d2f1f", muted: "#8a7358", accent: "#c2410c", accentSoft: "rgba(194,65,12,.12)", code: "#c2410c", strong: "#2a1f14", }; } return { text: "#f3eefc", muted: "#b4a9c8", accent: "#ff7ab6", accentSoft: "rgba(255,122,182,.14)", code: "#c4b5fd", strong: "#ffffff", }; } function escapeHtml(s) { return String(s) .replace(/&/g, "&") .replace(//g, ">"); } function inline(s, c) { return escapeHtml(s) .replace(/\*\*(.+?)\*\*/g, '$1') .replace(/\*(.+?)\*/g, '$1') .replace(/`([^`]+)`/g, '$1'); } function markdownToHtml(src, opts) { opts = opts || {}; var dropCap = opts.dropCap !== false; var c = themeColors(opts.theme || "dark"); var text = String(src || "").replace(/\r\n/g, "\n"); text = text.replace(//g, ""); text = text.replace(//gi, "> 🎬 视频请在异度世界公众号查看"); text = text.replace(//gi, "> 🖼 配图已省略(离线版)"); text = text.replace(/!\[[^\]]*\]\([^)]+\)/g, "> 🖼 配图已省略(离线版)"); var lines = text.split("\n"); var out = []; var inCode = false; var codeBuf = []; var inUl = false; var inOl = false; var olIndex = 0; var inQuote = false; var quoteBuf = []; var paraCount = 0; var skippedChapterTitle = false; function closeLists() { if (inUl) { out.push(""); inUl = false; } if (inOl) { out.push(""); inOl = false; olIndex = 0; } } function flushQuote() { if (!inQuote) return; var body = quoteBuf.map(function (q) { return inline(q, c); }).join("
"); out.push('
' + body + "
"); inQuote = false; quoteBuf = []; } function isTableSep(line) { return /^\s*\|?[\s:-]+\|[\s|:-]*\|?\s*$/.test(line); } function splitRow(line) { var s = line.trim(); if (s.charAt(0) === "|") s = s.slice(1); if (s.charAt(s.length - 1) === "|") s = s.slice(0, -1); return s.split("|").map(function (cell) { return cell.trim(); }); } function renderTable(rows) { if (!rows.length) return; var html = ['
']; rows[0].forEach(function (cell) { html.push(""); }); html.push(""); for (var r = 1; r < rows.length; r++) { html.push(""); rows[r].forEach(function (cell) { html.push(""); }); html.push(""); } html.push("
" + inline(cell, c) + "
" + inline(cell, c) + "
"); out.push(html.join("")); } for (var i = 0; i < lines.length; i++) { var raw = lines[i]; var line = raw; if (/^```/.test(line)) { flushQuote(); closeLists(); if (inCode) { out.push('
' + escapeHtml(codeBuf.join("\n")) + "
"); codeBuf = []; inCode = false; } else { inCode = true; } continue; } if (inCode) { codeBuf.push(line); continue; } // GFM table if ( line.indexOf("|") !== -1 && i + 1 < lines.length && isTableSep(lines[i + 1]) ) { flushQuote(); closeLists(); var rows = [splitRow(line)]; i += 1; // skip separator while (i + 1 < lines.length && lines[i + 1].indexOf("|") !== -1 && lines[i + 1].trim()) { i += 1; if (isTableSep(lines[i])) continue; rows.push(splitRow(lines[i])); } renderTable(rows); continue; } if (/^\s*>\s?/.test(line)) { closeLists(); inQuote = true; quoteBuf.push(line.replace(/^\s*>\s?/, "")); continue; } if (inQuote) flushQuote(); if (/^\s*---+\s*$/.test(line) || /^\s*\*\*\*+\s*$/.test(line)) { closeLists(); out.push('
'); continue; } var h1 = /^#\s+(.+)$/.exec(line); if (h1) { closeLists(); if (!skippedChapterTitle) { skippedChapterTitle = true; continue; } out.push("

" + inline(h1[1], c) + "

"); continue; } var h2 = /^##\s+(.+)$/.exec(line); if (h2) { closeLists(); // Chapter title already shown in reader chrome — skip first ## if (!skippedChapterTitle) { skippedChapterTitle = true; continue; } out.push("

" + inline(h2[1], c) + "

"); continue; } var h3 = /^###\s+(.+)$/.exec(line); if (h3) { closeLists(); out.push("

" + inline(h3[1], c) + "

"); continue; } var task = /^\s*[-*+]\s+\[([ xX])\]\s+(.+)$/.exec(line); if (task) { if (inOl) { out.push(""); inOl = false; olIndex = 0; } if (!inUl) { out.push('
'); inUl = true; } var mark = /x/i.test(task[1]) ? "☑" : "☐"; out.push("
" + mark + " " + inline(task[2], c) + "
"); continue; } var ul = /^\s*[-*+]\s+(.+)$/.exec(line); if (ul) { if (inOl) { out.push("
"); inOl = false; olIndex = 0; } if (!inUl) { out.push('
'); inUl = true; } out.push("
• " + inline(ul[1], c) + "
"); continue; } var ol = /^\s*(\d+)\.\s+(.+)$/.exec(line); if (ol) { if (inUl) { out.push("
"); inUl = false; } if (!inOl) { out.push('
'); inOl = true; olIndex = 0; } olIndex += 1; out.push("
" + olIndex + ". " + inline(ol[2], c) + "
"); continue; } if (!line.trim()) { closeLists(); continue; } closeLists(); paraCount += 1; var cls = dropCap && paraCount === 1 ? ' class="dropcap"' : ""; out.push("" + inline(line.trim(), c) + "

"); } flushQuote(); closeLists(); if (inCode) { out.push('
' + escapeHtml(codeBuf.join("\n")) + "
"); } return out.join(""); } global.MarkdownLite = { markdownToHtml: markdownToHtml, themeColors: themeColors }; })(window);