# https://github.com/dbohdan/caddy-markdown-site
# Copyright (c) 2021 D. Bohdan.  License: MIT.

h2jammy.yushei.net:43889 {
# http://192.168.48.229:43889 {
        root * demo

        encode gzip

        file_server browse
        templates

        log {
                output file access.43115.log
        }

        @media {
                path /favicon.ico
                path /media/*
        }
        @templates {
                path /templates/*
                not path /templates/*.css /templates/*.js
        }
        @markdown {
                path_regexp \.md$
        }
        @markdown_exists {
                file {path}.md
        }

        handle @media {
                file_server
        }
        handle @templates {
                error 403
        }
        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 {
                file_server
                templates

                @markdown_index_exists_404 {
                        file {path}/index.md
                        expression `{http.error.status_code} == 404`
                }

                handle @markdown_index_exists_404 {
                        map {path} {caddy_markdown_site.append_to_path} {
                                default index
                        }
                        file_server {
                                status 200
                        }
                        rewrite * /templates/index.html
                }
                handle {
                        rewrite * /templates/error.html
                }
        }
}