Skip to content

Commit

Permalink
Merge pull request #1 from dbohdan/master
Browse files Browse the repository at this point in the history
This improves several things:

* example.com/path/foo.md redirects to github;
* Indices work in subdirs;
* You can serve plain HTML pages, too;
* Missing files return error 404, not 500;
* You can have a favicon.ico.
  • Loading branch information
shawwn authored Jul 3, 2021
2 parents d0ee17f + 3bc0ebe commit 34d200c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 11 deletions.
51 changes: 44 additions & 7 deletions media/Caddyfile
Original file line number Diff line number Diff line change
@@ -1,23 +1,60 @@
# 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

#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 {
file
path_regexp \.md$
}
@markdown_exists {
file {path}.md
}

encode gzip
handle @media {
file_server
}
handle @markdown {
redir * https://github.com/shawwn/website/blob/master{path}
}
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
}
}
}
19 changes: 15 additions & 4 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
<!DOCTYPE html>

{{ $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 }}

<html>
<head>
Expand All @@ -15,9 +20,15 @@
</head>
<body>
{{ include "/templates/header.html" }}

<main>
<article>{{ markdown $markdownFile.Body }}</article>
</main>

<footer>
<a href="{{ $path | clean }}">Page source code</a>.
</footer>

{{ include "/templates/footer.html" }}
</body>
</html>

0 comments on commit 34d200c

Please sign in to comment.