Readme

Serve Readme

Usage

import codeOnValTown from "https://esm.town/v/andreterron/codeOnValTown?v=50"; import { serveReadme } from "https://esm.town/v/pomdtr/serve_readme"; const val = extractValInfo(import.meta.url); export default serveReadme({ val, title: "Code Search is Easy", });

Example

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
33
34
35
36
37
38
39
import { api } from "https://esm.town/v/pomdtr/api";
import { gfm } from "https://esm.town/v/pomdtr/gfm";
import { callerRef } from "https://esm.town/v/pomdtr/refs";
import { html } from "https://esm.town/v/stevekrouse/html?v=5";
export async function valReadme({ val, title }: {
val: { author: string; name: string };
title?: string;
}) {
if (!title) {
title = `@${val.author}/${val.name}`;
}
const { readme = "" } = await api(`/v1/alias/${val.author}/${val.name}`);
return readme;
}
export function serveReadme({ val, title }: {
val: { author: string; name: string };
title?: string;
}) {
return async (req: Request) => {
const url = new URL(req.url);
if (url.pathname == "/edit") {
return Response.redirect(`https://www.val.town/v/${val.author}/${val.name}`);
}
const readme = await valReadme({ val, title });
if (url.pathname == "/raw") {
return new Response(readme, {
headers: {
"content-type": "text/markdown",
},
});
}
const body = await gfm(readme, { title, favicon: "📝" });
return html(body);
};
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
July 6, 2024