Readme

Usage:

Create valimport blobEditor from "https://esm.town/v/pomdtr/blob_editor" export default blobEditor("article.md")

You can easily protect your val behind @pomdtr/passwordAuth or @pomdtr/emailAuth

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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/** @jsxImportSource npm:hono/jsx **/
import { extractValInfo } from "https://esm.town/v/pomdtr/extractValInfo";
import { html } from "https://esm.town/v/pomdtr/gfm";
import { blob } from "https://esm.town/v/std/blob?v=11";
import { Hono } from "npm:hono";
import { HTTPException } from "npm:hono/http-exception";
import { jsxRenderer, useRequestContext } from "npm:hono/jsx-renderer";
export function blobEditor(key: string, options?: { title?: string }) {
const router = new Hono();
router.use(jsxRenderer(({ children }) => {
const c = useRequestContext();
const { pathname } = new URL(c.req.url);
return (
<html>
<head>
<link rel="icon" href="https://fav.farm/๐Ÿ“ƒ" />
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css"
/>
</head>
<body>
<header class="container">
<nav>
<ul>
<li>
<a href={"/"}>
<strong>{options?.title || "Blob Editor"}</strong>
</a>
</li>
</ul>
{pathname == "/"
? (
<ul>
<li>
<a href={"/edit"} role="button" class="outline">Edit Blob</a>
</li>
</ul>
)
: undefined}
</nav>
</header>
<main class="container">
{children}
</main>
</body>
</html>
);
}));
router.get("/", async (c) => {
if (key.endsWith(".md")) {
const markdown = await readBlob(key);
return c.render(
<article dangerouslySetInnerHTML={{ __html: await html(markdown) }}>
</article>,
);
}
const text = await readBlob(key);
return c.text(text);
});
router.get("/edit", async (c) => {
const text = readBlob(key);
return c.render(
<main class="container">
<form method="POST" action="/">
<article>
<textarea name="content" id="editor" style={{ height: "60vh" }}>{text}</textarea>
<footer>
<input type="submit" value="Save" />
</footer>
</article>
</form>
</main>,
);
});
router.post("/", async (c) => {
const body = await c.req.parseBody();
if (typeof body.content != "string") {
throw new HTTPException(400);
}
await blob.set(key, body.content);
return c.redirect("/");
});
return router.fetch;
}
async function readBlob(key: string) {
const resp = await blob.get(key);
if (resp.status == 200) {
return resp.text();
} else if (resp.status == 404) {
return "";
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
v1
March 14, 2024