Spaces:
Running
Running
Download js/markdown.js from ereniko/LocalWebAgent: direct link, hf CLI and curl.
- Browser
- Download file 3.94 kB
-
https://huggingface.co/spaces/ereniko/LocalWebAgent/resolve/main/js/markdown.js
- Command line
-
hf download hf://spaces/ereniko/LocalWebAgent/js/markdown.js
-
curl -L -o markdown.js https://huggingface.co/spaces/ereniko/LocalWebAgent/resolve/main/js/markdown.js
3.94 kB
| // Tiny, safe Markdown subset for agent replies: fenced code, inline code, bold, | |
| // italics, headings, lists, GFM tables, links (text only, never navigates), paragraphs. | |
| const esc = (s) => s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """); | |
| function inline(s) { | |
| const codes = []; | |
| s = s.replace(/`([^`\n]+)`/g, (_, c) => { codes.push(c); return `\u0000${codes.length - 1}\u0000`; }); | |
| s = esc(s) | |
| .replace(/\*\*([^*\n]+)\*\*/g, "<strong>$1</strong>") | |
| .replace(/(^|[^*\w])\*([^*\n]+)\*(?!\w)/g, "$1<em>$2</em>") | |
| .replace(/\[([^\]\n]+)\]\(([^)\s]+)\)/g, '<span class="md-link" title="$2">$1</span>'); | |
| return s.replace(/\u0000(\d+)\u0000/g, (_, i) => `<code>${esc(codes[Number(i)])}</code>`); | |
| } | |
| // ---------- GFM tables ---------- | |
| const TABLE_SEP = /^\s*\|?\s*:?-+:?\s*(\|\s*:?-+:?\s*)*\|?\s*$/; | |
| /** Splits a table row on pipes that are not escaped (\|) or inside `code`. */ | |
| function splitRow(line) { | |
| let s = line.trim(); | |
| if (s.startsWith("|")) s = s.slice(1); | |
| if (s.endsWith("|") && !s.endsWith("\\|")) s = s.slice(0, -1); | |
| const cells = []; | |
| let cur = ""; | |
| let inCode = false; | |
| for (let k = 0; k < s.length; k++) { | |
| const ch = s[k]; | |
| if (ch === "\\" && s[k + 1] === "|") { cur += "|"; k++; continue; } | |
| if (ch === "`") inCode = !inCode; | |
| if (ch === "|" && !inCode) { cells.push(cur.trim()); cur = ""; continue; } | |
| cur += ch; | |
| } | |
| cells.push(cur.trim()); | |
| return cells; | |
| } | |
| const isTableStart = (lines, i) => lines[i].includes("|") && i + 1 < lines.length && TABLE_SEP.test(lines[i + 1]) && lines[i + 1].includes("-"); | |
| function renderTable(lines, i) { | |
| const head = splitRow(lines[i]); | |
| const aligns = splitRow(lines[i + 1]).map((c) => (c.startsWith(":") && c.endsWith(":") ? "center" : c.endsWith(":") ? "right" : c.startsWith(":") ? "left" : "")); | |
| const cell = (tag, text, k) => `<${tag}${aligns[k] ? ` style="text-align:${aligns[k]}"` : ""}>${inline(text ?? "")}</${tag}>`; | |
| let j = i + 2; | |
| const rows = []; | |
| while (j < lines.length && lines[j].trim() && lines[j].includes("|")) { | |
| const cells = splitRow(lines[j]); | |
| rows.push(`<tr>${head.map((_, k) => cell("td", cells[k], k)).join("")}</tr>`); | |
| j++; | |
| } | |
| const html = `<div class="md-table"><table><thead><tr>${head.map((h, k) => cell("th", h, k)).join("")}</tr></thead>` + | |
| `<tbody>${rows.join("")}</tbody></table></div>`; | |
| return { html, next: j }; | |
| } | |
| export function renderMarkdown(src) { | |
| const out = []; | |
| const lines = src.replace(/\r/g, "").split("\n"); | |
| let i = 0; | |
| let para = []; | |
| const flush = () => { if (para.length) { out.push(`<p>${inline(para.join("\n")).replace(/\n/g, "<br>")}</p>`); para = []; } }; | |
| while (i < lines.length) { | |
| const line = lines[i]; | |
| const fence = /^\s*```(\S*)/.exec(line); | |
| if (fence) { | |
| flush(); | |
| const body = []; | |
| i++; | |
| while (i < lines.length && !/^\s*```/.test(lines[i])) body.push(lines[i++]); | |
| i++; | |
| out.push(`<pre class="md-code"><code data-lang="${esc(fence[1])}">${esc(body.join("\n"))}</code></pre>`); | |
| continue; | |
| } | |
| if (isTableStart(lines, i)) { | |
| flush(); | |
| const t = renderTable(lines, i); | |
| out.push(t.html); | |
| i = t.next; | |
| continue; | |
| } | |
| const h = /^(#{1,4})\s+(.*)$/.exec(line); | |
| if (h) { flush(); out.push(`<h${h[1].length + 2}>${inline(h[2])}</h${h[1].length + 2}>`); i++; continue; } | |
| if (/^\s*([-*+]|\d+[.)])\s+/.test(line)) { | |
| flush(); | |
| const ordered = /^\s*\d+[.)]/.test(line); | |
| const items = []; | |
| while (i < lines.length && /^\s*([-*+]|\d+[.)])\s+/.test(lines[i])) { | |
| items.push(`<li>${inline(lines[i].replace(/^\s*([-*+]|\d+[.)])\s+/, ""))}</li>`); | |
| i++; | |
| } | |
| out.push(ordered ? `<ol>${items.join("")}</ol>` : `<ul>${items.join("")}</ul>`); | |
| continue; | |
| } | |
| if (!line.trim()) { flush(); i++; continue; } | |
| para.push(line); | |
| i++; | |
| } | |
| flush(); | |
| return out.join(""); | |
| } | |