how-gists-kejadlen-dev-works
change qyuyrntlsvqpuuxwyklqlqqltmvzsrrx
commit a18dbe3a23738dc2fa812b95fa6cc0155d89e989
author Alpha Chen <alpha@kejadlen.dev>
date
parent trvskwzz
diff --git a/how-gists-kejadlen-dev-works.md b/how-gists-kejadlen-dev-works.md
new file mode 100644
index 0000000..e0bc9eb
--- /dev/null
+++ b/how-gists-kejadlen-dev-works.md
@@ -0,0 +1,54 @@
+# How gists.kejadlen.dev works
+
+When I realized I wanted a place to post random snippets, I explored
+self-hosted pastebin options before stumbling across a far simpler alternative
+using Caddy's [`template` directive][template-directive].
+
+[template-directive]: https://caddyserver.com/docs/caddyfile/directives/templates
+
+Abridged Caddyfile (with my [gists repo][gists-repo] mounted at `/data/gists`):
+
+[gists-repo]: https://quire.kejadlen.dev/gists/tree
+
+```
+gists.kejadlen.dev {
+	root * /data/gists
+	encode zstd gzip
+
+	# rendered views; templates stays scoped here so raw .md is never evaluated
+	handle {
+		rewrite * /_render.html
+		templates
+		file_server
+	}
+}
+```
+
+And the abridged `_render.html` in the `gists` repo:
+
+```
+{{$path := .OriginalReq.URL.Path -}}
+{{$file := printf "%s.md" (trimSuffix "/" $path) -}}
+{{if not (fileExists $file)}}{{httpError 404}}{{end -}}
+{{$doc := include $file | splitFrontMatter -}}
+<!doctype html>
+<html lang="en">
+<head>
+<meta charset="utf-8">
+<meta name="viewport" content="width=device-width, initial-scale=1">
+<meta name="color-scheme" content="light dark">
+<title>{{default (trimSuffix ".md" $file) $doc.Meta.title}} — kejadlen.dev</title>
+{{with $doc.Meta.description}}<meta name="description" content="{{.}}">
+{{end}}<script>try{var t=localStorage.getItem("theme");if(t)document.documentElement.dataset.theme=t}catch(e){}</script>
+<style>
+(elided)
+</style>
+</head>
+<body>
+<button id="theme" hidden></button>
+<article>
+{{$doc.Body | markdown}}
+</article>
+</body>
+</html>
+```