// A tiny Markdown renderer for the rules. It supports exactly what the rules use: #, ##, ### headings,
// paragraphs, - and 1. lists (no nesting), **bold**, *italic*, `code`, pipe tables, --- and > quotes.
// Everything is escaped first, so a rules file can never inject markup.
import { esc } from "./desk.js";
function inline(text) {
return esc(text)
.replace(/`([^`]+)`/g, "$1")
.replace(/\*\*([^*]+)\*\*/g, "$1")
.replace(/(^|[^*\w])\*([^*\s][^*]*?)\*(?![*\w])/g, "$1$2");
}
const cells = (line) => line.trim().replace(/^\||\|$/g, "").split("|").map((c) => c.trim());
export function renderMarkdown(src) {
const lines = src.replace(/\r\n?/g, "\n").split("\n");
const out = [];
let i = 0;
while (i < lines.length) {
const line = lines[i];
if (!line.trim()) { i++; continue; }
const h = /^(#{1,3})\s+(.*)$/.exec(line);
if (h) {
out.push(`
| ${inline(c)} | `).join("")}
|---|
| ${inline(c)} | `).join("")}
${quote.map(inline).join(" ")}`); continue; } const list = /^(-|\d+\.)\s+/.exec(line); if (list) { const tag = list[1] === "-" ? "ul" : "ol"; const re = tag === "ul" ? /^-\s+(.*)$/ : /^\d+\.\s+(.*)$/; const items = []; while (i < lines.length && re.test(lines[i])) items.push(re.exec(lines[i++])[1]); out.push(`<${tag}>${items.map((x) => `
${inline(para.join(" "))}
`); } return out.join("\n"); }