pomdtr-mdx.web.val.run
Readme

export const title = "mdx"

{title}

Usage

import { extractValInfo } from "https://esm.town/v/pomdtr/extractValInfo"; import { mdx } from "https://esm.town/v/pomdtr/mdx"; const { author, name } = extractValInfo(import.meta.url); export default mdx(author, name);
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/** @jsxImportSource https://esm.sh/preact **/
import { modifyFetchHandler } from "https://esm.town/v/andreterron/codeOnValTown?v=50";
import { api } from "https://esm.town/v/pomdtr/api";
import { extractValInfo } from "https://esm.town/v/pomdtr/extractValInfo";
import { gfm } from "https://esm.town/v/pomdtr/gfm";
import { compile } from "npm:@mdx-js/mdx";
import { render } from "npm:preact-render-to-string";
const css = `
.markdown-body {
box-sizing: border-box;
min-width: 200px;
max-width: 980px;
margin: 0 auto;
padding: 45px;
}
@media (max-width: 767px) {
.markdown-body {
padding: 15px;
}
}
`;
export function mdx(author: string, name: string) {
return async (req: Request) => {
const { readme } = await api(`/v1/alias/${author}/${name}`);
const vfile = await compile(readme, {
jsxImportSource: "https://esm.sh/preact",
});
const url = new URL(req.url);
if (url.pathname == "/mod.js") {
return new Response(vfile.toString(), {
headers: {
"Content-Type": "text/javascript",
},
});
}
const blob = new Blob([vfile.toString()], {
type: "text/tsx",
});
const importURL = URL.createObjectURL(blob);
const { default: MDXContent } = await import(importURL);
return new Response(
render(
<html>
<head>
<title>{`${author}/${name}`}</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="📝" rel="icon" />
<link
href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.4.0/github-markdown.min.css"
rel="stylesheet"
/>
<link href="https://cdn.jsdelivr.net/npm/prismjs@v1.29.0/themes/prism.css" rel="stylesheet" />
<style dangerouslySetInnerHTML={{ __html: css }}></style>
</head>
<body class="markdown-body">
<MDXContent />
<script src="https://cdn.jsdelivr.net/npm/prismjs@v1.29.0/components/prism-core.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/prismjs@v1.29.0/plugins/autoloader/prism-autoloader.min.js">
</script>
</body>
</html>,
),
{
headers: {
"Content-Type": "text/html",
},
},
);
};
}
const { author, name } = extractValInfo(import.meta.url);
export default mdx(author, name);
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!
v58
March 28, 2024