Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166import { createServer } from "node:http";import { readFileSync } from "node:fs";import { convert } from "../src/index.js";
const PORT = Number(process.env.PORT) || 8080;const DEFAULT_SOURCE = readFileSync(new URL("./sample.lingo", import.meta.url), "utf8");
function escapeHtml(s: string): string { return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");}
// The sample source references image filenames (dog.jpg, cat.jpg, ...) that// don't exist as real files. Serve a labeled placeholder for any of them so// the preview pane doesn't just show a broken-image icon.function placeholderSvg(name: string): string { return `<svg xmlns="http://www.w3.org/2000/svg" width="320" height="200"> <rect width="100%" height="100%" fill="#dde3ea"/> <text x="50%" y="50%" dominant-baseline="middle" text-anchor="middle" font-family="sans-serif" font-size="16" fill="#5b6b7c">${escapeHtml(name)}</text></svg>`;}
// A small base stylesheet for the *preview* iframe only — gives block// elements visible margins so the structure is unambiguous at a glance.// The "Compiled HTML" pane shows convert()'s raw output untouched.const PREVIEW_STYLE = `<style> body { font-family: -apple-system, system-ui, sans-serif; padding: 1rem 140px 1rem 1rem; } p, ul, ol, blockquote, figure, h1, h2, h3, .math-block, section { margin: 0 0 1rem 0; } li { margin-bottom: 0.25rem; } figure { border: 1px solid #ddd; padding: 0.5rem; display: inline-block; } /* margin notes, Tufte-CSS style — float into the reserved right padding */ aside.sidenote, .sidenote-inline { float: right; clear: right; width: 120px; margin-right: -140px; font-size: 0.8em; line-height: 1.3; color: #555; border-left: 2px solid #ccc; padding-left: 0.5rem; }</style>`;
// KaTeX, loaded client-side from a CDN by the visitor's browser (not fetched// by this server). render.ts already emits math wrapped in \( \) / \[ \] —// auto-render's default delimiters match those exactly, so no config needed.const KATEX_HEAD = `<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.css"><script src="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.js"></script><script src="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/contrib/auto-render.min.js"></script>`;
const KATEX_RENDER_SCRIPT = `<script>renderMathInElement(document.body);</script>`;
function pageHtml(): string { return `<!doctype html><html><head><meta charset="utf-8"><title>Lingo demo</title><style> :root { font-family: -apple-system, system-ui, sans-serif; } body { margin: 0; height: 100vh; display: flex; flex-direction: column; } header { padding: 0.75rem 1rem; border-bottom: 1px solid #ddd; display: flex; align-items: center; gap: 1rem; } header h1 { font-size: 1rem; margin: 0; } main { flex: 1; display: flex; min-height: 0; } .pane { flex: 1; display: flex; flex-direction: column; min-width: 0; border-right: 1px solid #ddd; } .pane:last-child { border-right: none; } .pane h2 { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.05em; color: #666; margin: 0; padding: 0.5rem 0.75rem; border-bottom: 1px solid #eee; background: #fafafa; } textarea { flex: 1; border: none; resize: none; padding: 0.75rem; font: 13px/1.5 ui-monospace, monospace; outline: none; } iframe { flex: 1; border: none; background: #fff; } pre { flex: 1; margin: 0; padding: 0.75rem; overflow: auto; font: 12px/1.5 ui-monospace, monospace; background: #fafafa; } label { font-size: 0.85rem; color: #444; display: flex; align-items: center; gap: 0.3rem; }</style></head><body><header> <h1>Lingo → HTML</h1> <label><input type="checkbox" id="compat"> markdown compat mode</label></header><main> <div class="pane"> <h2>Lingo source</h2> <textarea id="source" spellcheck="false">${escapeHtml(DEFAULT_SOURCE)}</textarea> </div> <div class="pane"> <h2>Rendered preview</h2> <iframe id="preview"></iframe> </div> <div class="pane"> <h2>Compiled HTML</h2> <pre id="output"></pre> </div></main><script> const source = document.getElementById('source'); const compat = document.getElementById('compat'); const preview = document.getElementById('preview'); const output = document.getElementById('output'); let timer; async function runConvert() { const res = await fetch('/convert', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ source: source.value, markdownCompat: compat.checked }), }); const data = await res.json(); if (data.error) { output.textContent = data.error; return; } preview.srcdoc = data.previewHtml; output.textContent = data.html; } function scheduleConvert() { clearTimeout(timer); timer = setTimeout(runConvert, 200); } source.addEventListener('input', scheduleConvert); compat.addEventListener('change', runConvert); runConvert();</script></body></html>`;}
const server = createServer((req, res) => { if (req.method === "GET" && req.url === "/") { res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" }); res.end(pageHtml()); return; }
const imgMatch = req.method === "GET" ? req.url?.match(/^\/([^/]+\.(?:jpg|jpeg|png|gif|webp))$/i) : null; if (imgMatch) { res.writeHead(200, { "Content-Type": "image/svg+xml" }); res.end(placeholderSvg(imgMatch[1]!)); return; }
if (req.method === "POST" && req.url === "/convert") { let body = ""; req.on("data", (chunk) => { body += chunk; }); req.on("end", () => { try { const { source, markdownCompat } = JSON.parse(body || "{}"); const html = convert(String(source ?? ""), { markdownCompat: Boolean(markdownCompat) }); const previewHtml = `<!doctype html><html><head>${PREVIEW_STYLE}${KATEX_HEAD}</head><body>${html}${KATEX_RENDER_SCRIPT}</body></html>`; res.writeHead(200, { "Content-Type": "application/json" }); res.end(JSON.stringify({ html, previewHtml })); } catch (err) { res.writeHead(400, { "Content-Type": "application/json" }); res.end(JSON.stringify({ error: String(err) })); } }); return; }
res.writeHead(404); res.end("not found");});
server.listen(PORT, () => { console.log(`Lingo demo listening on http://localhost:${PORT}`);});