Skip to content

Render Markdown as HTML

Notes on the pad are Markdown. This converter gives them as HTML, in two forms: a plain page, and a "card" with a light or dark theme. You read either form with as and with. The converter is for reading only: the index keeps using inseam’s own Markdown text, because neither conversion goes to text/markdown or text/plain.

The calls use the mcp helper and the token from make a file type searchable.

Save this as markdown-html.js:

const escape = (s) => s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
// Inline marks, after escaping: code, bold, italic, links with http(s) targets only.
const inline = (s) =>
escape(s)
.replace(/`([^`]+)`/g, "<code>$1</code>")
.replace(/\*\*([^*]+)\*\*/g, "<strong>$1</strong>")
.replace(/\*([^*]+)\*/g, "<em>$1</em>")
.replace(/\[([^\]]+)\]\((https?:\/\/[^\s)]+)\)/g, '<a href="$2">$1</a>');
const render = (text) => {
const out = [];
let list = false;
let code = false;
let para = [];
const flush = () => {
if (para.length) out.push("<p>" + para.map(inline).join(" ") + "</p>");
para = [];
};
const endList = () => {
if (list) out.push("</ul>");
list = false;
};
for (const raw of text.split(/\r?\n/)) {
if (raw.startsWith("```")) {
flush();
endList();
out.push(code ? "</code></pre>" : "<pre><code>");
code = !code;
continue;
}
if (code) {
out.push(escape(raw));
continue;
}
const heading = /^(#{1,6})\s+(.*)$/.exec(raw);
const item = /^\s*[-*]\s+(.*)$/.exec(raw);
if (heading) {
flush();
endList();
const n = heading[1].length;
out.push("<h" + n + ">" + inline(heading[2]) + "</h" + n + ">");
} else if (item) {
flush();
if (!list) out.push("<ul>");
list = true;
out.push("<li>" + inline(item[1]) + "</li>");
} else if (raw.trim() === "") {
flush();
endList();
} else {
endList();
para.push(raw.trim());
}
}
flush();
endList();
if (code) out.push("</code></pre>");
return out.join("\n");
};
const titleOf = (text) => {
const h = /^#\s+(.+)$/m.exec(text);
return h ? h[1].trim().slice(0, 200) : undefined;
};
export default {
manifest: {
contract: 3,
name: "markdown-html",
title: "Markdown as HTML",
description: "Reads Markdown notes as an HTML page, or as a themed card.",
convert: [
{ from: "text/markdown", to: "text/html" },
{ from: "text/markdown", to: "text/html", name: "card", options: [{ name: "theme", label: "Theme", type: "string", values: ["light", "dark"], default: "light" }] },
],
},
convert({ text, name, options }) {
const body = render(text);
const title = titleOf(text);
if (name === "card") return { title, text: '<article class="card ' + options.theme + '">\n' + body + "\n</article>" };
return { title, text: body };
},
};

What makes it pass inseam’s checks:

  • It adds no words. Everything it writes is markup or the note’s own text, so metadata passes.
  • It escapes first. <script> in a note becomes text, not a tag. Links keep only http and https targets.
  • It is linear. One pass over the lines, and no regular expression that backtracks.
  • The title is the first heading, never a sentence from the body.
Terminal window
mcp trial_plugin "$(jq -n --rawfile code markdown-html.js '{
name: "Markdown as HTML",
code: $code,
examples: [
{ text: "# Plan\n\nShip **v2** on Friday.\n\n- tests\n- docs\n", expect: { contains: ["<h1>Plan</h1>", "<strong>v2</strong>", "<li>docs</li>"], title: "Plan" } },
{ conversion: 1, text: "# Plan\n\nShip it.\n", options: { theme: "dark" }, expect: { contains: ["<article class=\"card dark\">"] } }
]
}')"

Then follow next, as in make a file type searchable: trial_status, review_plugin with the checklist and 3 to 20 tests (for example a note with <script> in it, an unclosed code block, a javascript: link), and trial_status again until the state is installed.

The plain page:

Terminal window
curl -s "$INSEAM/v1/o/pad/notes/plan.md?as=text%2Fhtml" -H "Authorization: Bearer $INSEAM_TOKEN"
<h1>Plan</h1>
<p>Ship <strong>v2</strong> on Friday.</p>
<ul>
<li>tests</li>
<li>docs</li>
</ul>

The dark card, with the named conversion and its option:

Terminal window
curl -s "$INSEAM/v1/o/pad/notes/plan.md?as=text%2Fhtml&with=card&opt.theme=dark" -H "Authorization: Bearer $INSEAM_TOKEN" -D - -o card.html
HTTP/2 200
content-type: text/html; charset=utf-8
x-inseam-converter: plugin:5b0d…
x-inseam-title: Plan

An agent does the same with the read tool:

{ "address": "inseam://n7q2…/pad/notes/plan.md", "as": "text/html", "with": "card", "options": { "theme": "dark" } }

get lists the forms a note can be read as:

Terminal window
curl -s "$INSEAM/v1/get?address=pad/notes/plan.md" -H "Authorization: Bearer $INSEAM_TOKEN" | jq .readAs
[{ "to": "text/html", "name": null }, { "to": "text/html", "name": "card" }, { "to": "text/plain", "name": null }]

A theme that the conversion does not list is refused before the converter runs:

{ "error": { "code": "invalid", "message": "options.theme: got \"blue\"; give one of light, dark", … } }

The second read of the same note does not run the converter: the result is kept for a day (not for a sealed host).