jxnblk-valtownbadge02.web.val.run
Readme

Render a Val Town badge

Fork it on Val Town

import valTownBadge from "https://esm.town/v/jxnblk/valTownBadge02"; export default async function(req: Request): Promise<Response> { const badge = valTownBadge(import.meta.url); const html = ` <h1>Hello, world</h1> ${badge} `; return new Response(html, { headers: { "Content-Type": "text/html; charset=utf-8", }, }); }
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
40
41
42
43
44
45
46
47
// fork it on val town badge v0.2
const left = 32;
const width = 160;
const height = 48; // width * 0.3;
const radius = 24;
const logotype = `
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 442 79" x="${left}" y="6" width="96" fill="#fff">
<path d="M92.182 67.387c3.89 0 7.064-1.082 9.524-3.248 2.459-2.164 3.689-5.046 3.689-8.644v-1.101H91.411c-2.57 0-4.589.551-6.056 1.652-1.47 1.101-2.202 2.642-2.202 4.624 0 1.982.771 3.598 2.312 4.845 1.542 1.249 3.78 1.872 6.717 1.872M89.76 78.618c-3.893
<path d="M13.903 22.458H0V33.91h13.903z" />
</svg>
`;
export const badge = `
<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}" viewBox="0 0 ${width} ${height}">
<rect width="${width}" height="${height}" fill="#111" rx="${radius}" ry="${radius}" />
<text x="32" y="16" textAnchor="start" fill="#fff" style="font-family:system-ui,sans-serif;font-weight:bold;font-size:10px;">
Fork it on
</text>
${logotype}
</svg>
`;
function getHREF(mod) {
const url = new URL(mod);
return `https://val.town${url.pathname}`;
}
export function getBadgeLink(url: string): string {
const href = getHREF(url);
return `<a href="${href}" target="_blank" style="text-decoration:none;color:inherit">${badge}</a>`;
}
function handler(req: Request): Response {
return new Response(badge, {
headers: {
"Content-Type": "image/svg+xml",
},
});
}
export default function htmlOrResponse(args: Request | string): string | Response {
if (typeof args === "string") return getBadgeLink(args);
if (args instanceof Request) return handler(args);
throw new Error("bad args");
}
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!
v6
May 24, 2024