import { apiFetch } from "./api.js"; import { dom } from "./dom.js"; import { state } from "./state.js"; function escapeHtml(text) { return text .replaceAll("&", "&") .replaceAll("<", "<") .replaceAll(">", ">"); } function slugify(text) { return text .toLowerCase() .trim() .replace(/[^\w\u4e00-\u9fa5]+/g, "-") .replace(/^-+|-+$/g, ""); } function parseMarkdown(text) { const lines = text.split(/\r?\n/); const blocks = []; const headings = []; let inCode = false; let codeBuffer = []; let paragraph = []; const flushParagraph = () => { if (!paragraph.length) { return; } blocks.push(`
${escapeHtml(paragraph.join(" "))}
`); paragraph = []; }; const flushCode = () => { if (!codeBuffer.length) { return; } blocks.push(`${escapeHtml(codeBuffer.join("\n"))}`);
codeBuffer = [];
};
lines.forEach((line) => {
if (line.startsWith("```")) {
if (inCode) {
flushCode();
} else {
flushParagraph();
}
inCode = !inCode;
return;
}
if (inCode) {
codeBuffer.push(line);
return;
}
const heading = line.match(/^(#{1,4})\s+(.*)$/);
if (heading) {
flushParagraph();
const level = heading[1].length;
const textValue = heading[2].trim();
const id = slugify(textValue);
headings.push({ level, text: textValue, id });
blocks.push(`API.md 为空
"; dom.apiDocToc.innerHTML = headings.length ? headings .map( (item) => `${escapeHtml(item.text)}`, ) .join("") : "