Public
HTTP (deprecated)
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Readme

mdConvert

Inspired by markdown.rest. Converts markdown (plain text or base64 encoded) to HTML (plain text or base64 encoded)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { html } from "https://esm.town/v/stevekrouse/html";
import { micromark } from "npm:micromark";
import { z } from "npm:zod";
// a
const reqBodySchema = z.object({
isBase64: z.boolean().optional(),
returnBase64: z.boolean().optional(),
content: z.string(),
});
export default async function handler(request: Request) {
if (request.method !== "POST") {
return html(`
<pre>
curl https://nbbaier-mdConvert.web.val.run \\
-X POST \\
-d '{"content":"# Hello","returnBase64":true}'
</pre>
`);
}
try {
const body = await request.json();
const { isBase64, returnBase64, content } = reqBodySchema.parse(body);
const md = isBase64 ? atob(content) : content;
const html = returnBase64 ? btoa(micromark(md)) : micromark(md);
return new Response(html, { headers: { "Content-Type": "text/plain" } });
} catch (e) {
return Response.json({ message: e.message }, {
status: 400,
});
}
}
nbbaier-mdconvert.web.val.run
May 26, 2024