Shield external portal jumps (keep EXTERNAL_* for later), serve /nav from static build, and proxy /book + /community to vertical Next apps with Caddy notes. Co-authored-by: Cursor <cursoragent@cursor.com>
275 lines
7.2 KiB
JavaScript
275 lines
7.2 KiB
JavaScript
/**
|
|
* 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, "<")
|
|
.replace(/>/g, ">");
|
|
}
|
|
|
|
function inline(s, c) {
|
|
return escapeHtml(s)
|
|
.replace(/\*\*(.+?)\*\*/g, '<strong style="color:' + c.strong + ';font-weight:700;">$1</strong>')
|
|
.replace(/\*(.+?)\*/g, '<em style="color:' + c.muted + ';">$1</em>')
|
|
.replace(/`([^`]+)`/g, '<span class="md-code">$1</span>');
|
|
}
|
|
|
|
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(/<!--[\s\S]*?-->/g, "");
|
|
text = text.replace(/<video[\s\S]*?<\/video>/gi, "> 🎬 视频请在异度世界公众号查看");
|
|
text = text.replace(/<img\b[\s\S]*?>/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("</div>");
|
|
inUl = false;
|
|
}
|
|
if (inOl) {
|
|
out.push("</div>");
|
|
inOl = false;
|
|
olIndex = 0;
|
|
}
|
|
}
|
|
|
|
function flushQuote() {
|
|
if (!inQuote) return;
|
|
var body = quoteBuf.map(function (q) {
|
|
return inline(q, c);
|
|
}).join("<br/>");
|
|
out.push('<div class="md-quote">' + body + "</div>");
|
|
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 = ['<div class="md-table-wrap"><table class="md-table"><thead><tr>'];
|
|
rows[0].forEach(function (cell) {
|
|
html.push("<th>" + inline(cell, c) + "</th>");
|
|
});
|
|
html.push("</tr></thead><tbody>");
|
|
for (var r = 1; r < rows.length; r++) {
|
|
html.push("<tr>");
|
|
rows[r].forEach(function (cell) {
|
|
html.push("<td>" + inline(cell, c) + "</td>");
|
|
});
|
|
html.push("</tr>");
|
|
}
|
|
html.push("</tbody></table></div>");
|
|
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('<pre class="md-pre"><code>' + escapeHtml(codeBuf.join("\n")) + "</code></pre>");
|
|
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('<hr class="md-hr"/>');
|
|
continue;
|
|
}
|
|
|
|
var h1 = /^#\s+(.+)$/.exec(line);
|
|
if (h1) {
|
|
closeLists();
|
|
if (!skippedChapterTitle) {
|
|
skippedChapterTitle = true;
|
|
continue;
|
|
}
|
|
out.push("<h2>" + inline(h1[1], c) + "</h2>");
|
|
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("<h2>" + inline(h2[1], c) + "</h2>");
|
|
continue;
|
|
}
|
|
var h3 = /^###\s+(.+)$/.exec(line);
|
|
if (h3) {
|
|
closeLists();
|
|
out.push("<h3>" + inline(h3[1], c) + "</h3>");
|
|
continue;
|
|
}
|
|
|
|
var task = /^\s*[-*+]\s+\[([ xX])\]\s+(.+)$/.exec(line);
|
|
if (task) {
|
|
if (inOl) {
|
|
out.push("</div>");
|
|
inOl = false;
|
|
olIndex = 0;
|
|
}
|
|
if (!inUl) {
|
|
out.push('<div class="md-list">');
|
|
inUl = true;
|
|
}
|
|
var mark = /x/i.test(task[1]) ? "☑" : "☐";
|
|
out.push("<div>" + mark + " " + inline(task[2], c) + "</div>");
|
|
continue;
|
|
}
|
|
|
|
var ul = /^\s*[-*+]\s+(.+)$/.exec(line);
|
|
if (ul) {
|
|
if (inOl) {
|
|
out.push("</div>");
|
|
inOl = false;
|
|
olIndex = 0;
|
|
}
|
|
if (!inUl) {
|
|
out.push('<div class="md-list">');
|
|
inUl = true;
|
|
}
|
|
out.push("<div>• " + inline(ul[1], c) + "</div>");
|
|
continue;
|
|
}
|
|
var ol = /^\s*(\d+)\.\s+(.+)$/.exec(line);
|
|
if (ol) {
|
|
if (inUl) {
|
|
out.push("</div>");
|
|
inUl = false;
|
|
}
|
|
if (!inOl) {
|
|
out.push('<div class="md-list">');
|
|
inOl = true;
|
|
olIndex = 0;
|
|
}
|
|
olIndex += 1;
|
|
out.push("<div>" + olIndex + ". " + inline(ol[2], c) + "</div>");
|
|
continue;
|
|
}
|
|
|
|
if (!line.trim()) {
|
|
closeLists();
|
|
continue;
|
|
}
|
|
|
|
closeLists();
|
|
paraCount += 1;
|
|
var cls = dropCap && paraCount === 1 ? ' class="dropcap"' : "";
|
|
out.push("<p" + cls + ">" + inline(line.trim(), c) + "</p>");
|
|
}
|
|
|
|
flushQuote();
|
|
closeLists();
|
|
if (inCode) {
|
|
out.push('<pre class="md-pre"><code>' + escapeHtml(codeBuf.join("\n")) + "</code></pre>");
|
|
}
|
|
return out.join("");
|
|
}
|
|
|
|
global.MarkdownLite = { markdownToHtml: markdownToHtml, themeColors: themeColors };
|
|
})(window);
|