From 241004685b02246e827d8f58200986794346b953 Mon Sep 17 00:00:00 2001 From: Jake Lazaroff Date: Fri, 12 Jan 2024 23:05:33 -0500 Subject: [PATCH] Render Markdown as HTML --- caddy/serve-markdown-files-as-html.md | 51 +++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 caddy/serve-markdown-files-as-html.md diff --git a/caddy/serve-markdown-files-as-html.md b/caddy/serve-markdown-files-as-html.md new file mode 100644 index 0000000..01df749 --- /dev/null +++ b/caddy/serve-markdown-files-as-html.md @@ -0,0 +1,51 @@ +# Serve Markdown files as HTML + +[Caddy](https://caddyserver.com) is a simple web server that nonetheless has a bunch of cool features. One example is pre-processing responses using Go's [`text/template` package](https://pkg.go.dev/text/template). + +Here's a Caddyfile that uses a template to render `.md` files as HTML: + +```caddy +localhost:8080 + +file_server /* +templates + +@md { + file + path *.md +} + +rewrite @md /markdown.html +``` + +The first few lines set up the server: + +- `localhost:8080` tells Caddy to serve at `localhost` on port 8080 +- `file_server /*` tells it to run a file server for all paths below the current directory +- `templates` enables the [template middleware](https://caddyserver.com/docs/modules/http.handlers.templates), which treats the responses as Go templates + +The `@md` block is a [named matcher](https://caddyserver.com/docs/caddyfile/matchers#named-matchers). It captures a set of constraints that can be reused elsewhere. In this case, it matches all paths that end in `.md`. + +The final line `rewrite @md /markdown.html` serves the file `/markdown.html` whenever a request matches the `@md` block — i.e. whenever a Markdown file is requested. + +Here's a simple example of what `markdown.html` could look like: + +```html +{{$md := (include .OriginalReq.URL.Path | splitFrontMatter)}} + + + + {{$md.Meta.title}} + + +

{{$md.Meta.title}}

+ {{markdown $md.Body}} + + +``` + +At the top, we get the original request path, include the Markdown file, split the front matter and store it in the variable `$md`. + +The page title is set to the `title` property in the front matter. + +The body renders the `title` from the front matter in an `

` tag, then uses the [`markdown`](https://caddyserver.com/docs/modules/http.handlers.templates#markdown) template function to render the Markdown as HTML.