From a719e60e5cb6e17760a4f335881a3d236ab7bd38 Mon Sep 17 00:00:00 2001 From: "D. Bohdan" Date: Tue, 29 Jun 2021 10:29:21 +0000 Subject: [PATCH 1/5] build: upd Caddyfile, index.html from "upstream" This improves several things: * Both example.com/path/foo and example.com/path/foo.md returns the page; * Indices work in subdirs; * You can serve plain HTML pages, too; * Missing files return error 404, not 500; * You can have a /favicon.ico. --- media/Caddyfile | 47 +++++++++++++++++++++++++++++++++++++------- templates/index.html | 13 ++++++++---- 2 files changed, 49 insertions(+), 11 deletions(-) diff --git a/media/Caddyfile b/media/Caddyfile index 0c72db6..f99bb37 100644 --- a/media/Caddyfile +++ b/media/Caddyfile @@ -2,22 +2,55 @@ # 2. overwrite /etc/caddy/Caddyfile with this file # 3. sudo systemctl restart caddy -#http://blog.gpt4.org { :80 { root * /home/shawn/website + encode gzip + file_server + templates - @page not path /media/* - templates @page - rewrite @page /templates/index.html + @media { + path /favicon.ico + path /media/* + } + @markdown { + path_regexp \.md$ + } + @markdown_exists { + file {path}.md + } - encode gzip + handle @media { + file_server + } + handle @markdown { + rewrite * /templates/index.html + } + handle @markdown_exists { + map {path} {caddy_markdown_site.append_to_path} { + default extension + } + rewrite * /templates/index.html + } handle_errors { - rewrite * /templates/error.html - file_server templates + + @markdown_index_exists { + file {path}/index.md + expression `{http.error.status_code} == 404` + } + + handle @markdown_index_exists { + map {path} {caddy_markdown_site.append_to_path} { + default index + } + rewrite @markdown_index_exists /templates/index.html + } + handle { + rewrite * /templates/error.html + } } } diff --git a/templates/index.html b/templates/index.html index c34ec13..e04e1d4 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,9 +1,14 @@ -{{ $pathParts := splitList "/" .OriginalReq.URL.Path }} -{{ $markdownFilePath := printf "%s.md" (default "index" (slice $pathParts 1 | join "/")) }} -{{ $markdownFile := (include $markdownFilePath | splitFrontMatter) }} -{{ $title := default .OriginalReq.URL.Path $markdownFile.Meta.title }} +{{ $path := .OriginalReq.URL.Path }} +{{ $append := placeholder "caddy_markdown_site.append_to_path" }} +{{ if eq $append "extension" }} + {{ $path = printf "%s.md" $path }} +{{ else if eq $append "index" }} + {{ $path = printf "%s/index.md" $path }} +{{ end }} +{{ $markdownFile := (include $path | splitFrontMatter) }} +{{ $title := default $path $markdownFile.Meta.title }} From 937587e711f7bb6ac36adb523cbd862602f6a51c Mon Sep 17 00:00:00 2001 From: "D. Bohdan" Date: Wed, 30 Jun 2021 05:48:56 +0000 Subject: [PATCH 2/5] build(caddyfile): link caddy-markdown-site; step 4 --- media/Caddyfile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/media/Caddyfile b/media/Caddyfile index f99bb37..8a6eb97 100644 --- a/media/Caddyfile +++ b/media/Caddyfile @@ -1,6 +1,9 @@ +# Based on https://github.com/dbohdan/caddy-markdown-site + # 1. install caddy_2.4.3_linux_amd64.deb from https://github.com/caddyserver/caddy/releases/tag/v2.4.3 # 2. overwrite /etc/caddy/Caddyfile with this file # 3. sudo systemctl restart caddy +# 4. make sure /home/shawn/website is readable by anyone :80 { root * /home/shawn/website From b196aab976c86d60083299d9119f0d68a57a0cba Mon Sep 17 00:00:00 2001 From: "D. Bohdan" Date: Sat, 3 Jul 2021 07:05:47 +0000 Subject: [PATCH 3/5] feat: redirect .md to website source on GitHub --- media/Caddyfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/media/Caddyfile b/media/Caddyfile index 8a6eb97..0fd5cac 100644 --- a/media/Caddyfile +++ b/media/Caddyfile @@ -28,7 +28,7 @@ file_server } handle @markdown { - rewrite * /templates/index.html + redir * https://github.com/shawwn/website/blob/master{path} } handle @markdown_exists { map {path} {caddy_markdown_site.append_to_path} { From 0ff31a34fb8dede0877f1ad39d85b6af6801dfaf Mon Sep 17 00:00:00 2001 From: "D. Bohdan" Date: Sat, 3 Jul 2021 07:09:52 +0000 Subject: [PATCH 4/5] fix: require file to exist for GitHub redirect Otherwise serve a 404. --- media/Caddyfile | 1 + 1 file changed, 1 insertion(+) diff --git a/media/Caddyfile b/media/Caddyfile index 0fd5cac..d9bd85d 100644 --- a/media/Caddyfile +++ b/media/Caddyfile @@ -18,6 +18,7 @@ path /media/* } @markdown { + file path_regexp \.md$ } @markdown_exists { From 3bc0ebe625fb5a3cb76a4c41230dfe5e8c18cfe6 Mon Sep 17 00:00:00 2001 From: "D. Bohdan" Date: Sat, 3 Jul 2021 07:27:46 +0000 Subject: [PATCH 5/5] feat(index.html): add footer link to page source --- templates/index.html | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/templates/index.html b/templates/index.html index e04e1d4..5f28d41 100644 --- a/templates/index.html +++ b/templates/index.html @@ -20,9 +20,15 @@ {{ include "/templates/header.html" }} +
{{ markdown $markdownFile.Body }}
+ + + {{ include "/templates/footer.html" }}