Global markdown Template

Is there a better way to open markdown as if it was straight HTML? Currently i have to add this into my HTML template in order to render markdown at all. and then i have to navigate to that html file specifically to open that specific html document

{{$markdownFilePath := printf "/content.md"}}
{{$markdownFile := (include $markdownFilePath | splitFrontMatter)}}
{{$title := default $markdownFile.Meta.title}}

Is there a better way so that i can just navigate to https://example.com/content/content.md and have it render the md file as html?

I am using caddyfile configs

www.example.com, example.com {
        import  cloudflare # custom config for my ssl
        root * /opt/caddy/www/example.com
        templates
        file_server {
                index index.html
        }
}

You could do that in v1, but it wasn’t useful because there’s no way to apply styling that way: in practice, Markdown files almost always need to be rendered within a template. (So, there’s no reason a Caddy module couldn’t be written to do what you want, but… shouldn’t the rendered HTML at least have an <html> tag and <head> and stuff?)

Sorry, realized i wasnt as clear as i could be, my bad
I am perfectly fine with using a template html, but the only way i have found, is if i hadrcode the html to point to a sepecific folder of markdown or a specific markdown file (like a README.md)

So basically the user has to directly go to that template.html file, however i am also using a filebrowser config (which is where this will be rendered) so i cant have that conflict with it.
I want to beable to click on one of these

and have it render as html for the person opening them, instead of being downloaded or displayed as plaintext

Any ideas? i tried rewrite but it never worked
rewrite /*.md /path/to/template.html

{"level":"error","ts":1597232634.7344894,"logger":"http.log.error","msg":"template: /_Releases/cdn/markdown.html:51:27: executing \"/_Releases/cdn/markdown.html\" at <$markdownFile.Body>: can't evaluate field Body in type string","request":{"method":"GET","uri":"/test.md","proto":"HTTP/1.1","remote_addr":"108.162.246.138:39352","host":"cdn.merith.tk","headers":{"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"],"Sec-Fetch-User":["?1"],"Cf-Request-Id":["0485956a2c000002a24b946200000001"],"Accept-Encoding":["gzip"],"Cf-Visitor":["{\"scheme\":\"https\"}"],"Sec-Fetch-Dest":["document"],"Sec-Fetch-Site":["same-origin"],"Sec-Fetch-Mode":["navigate"],"Cookie":["__cfduid=d8c55d4000c6014a93d06c0967202fd551595633270"],"Connection":["Keep-Alive"],"Cf-Ipcountry":["US"],"X-Forwarded-For":["98.142.44.233"],"Cf-Ray":["5c1c58237fc402a2-SEA"],"Accept-Language":["en-US,en;q=0.9"],"Cf-Connecting-Ip":["98.142.44.233"],"Cdn-Loop":["cloudflare"],"X-Forwarded-Proto":["https"],"Upgrade-Insecure-Requests":["1"],"User-Agent":["Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36"],"Referer":["https://cdn.merith.tk/"]},"tls":{"resumed":false,"version":772,"ciphersuite":4867,"proto":"","proto_mutual":true,"server_name":"cdn.merith.tk"}},"duration":0.024487803,"status":500,"err_id":"8bdy36xv3","err_trace":"templates.(*Templates).executeTemplate (templates.go:305)"}
{{$original := .OriginalReq.URL.Path}}

{{$markdownFile := $original}}

I think try_files may work if there was a way to check to see if the content ended in .md or not…

Okay so it did work, however it attempts to work on EVERYTHING, even folders… And i am pretty sure its because i goofed the matcher part

        route *.md {
                try_files *.md /_Releases/cdn/markdown.html
        }

{"level":"error","ts":1597233536.1007915,"logger":"http.log.error","msg":"template: /_Releases/cdn/markdown.html:2:20: executing \"/_Releases/cdn/markdown.html\" at <include $markdownFilePath>: error calling include: read /opt/FileServer/Workspace/_misc/DND: is a directory","request":{"method":"GET","uri":"/Workspace/_misc/DND/","proto":"HTTP/1.1","remote_addr":"108.162.246.138:49830","host":"cdn.merith.tk","headers":{"Cf-Ipcountry":["US"],"X-Forwarded-For":["98.142.44.233"],"X-Forwarded-Proto":["https"],"User-Agent":["Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36"],"Sec-Fetch-Mode":["navigate"],"Cf-Request-Id":["0485a32b44000002a24baa1200000001"],"Cf-Ray":["5c1c6e2539f202a2-SEA"],"Upgrade-Insecure-Requests":["1"],"Cf-Connecting-Ip":["98.142.44.233"],"Cf-Visitor":["{\"scheme\":\"https\"}"],"Sec-Fetch-Site":["same-origin"],"Sec-Fetch-Dest":["document"],"Referer":["https://cdn.merith.tk/Workspace/_misc/"],"Accept-Language":["en-US,en;q=0.9"],"Cookie":["__cfduid=d8c55d4000c6014a93d06c0967202fd551595633270"],"Cdn-Loop":["cloudflare"],"Connection":["Keep-Alive"],"Accept-Encoding":["gzip"],"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"]},"tls":{"resumed":false,"version":772,"ciphersuite":4867,"proto":"","proto_mutual":true,"server_name":"cdn.merith.tk"}},"duration":0.001364812,"status":500,"err_id":"udzqi2xhi","err_trace":"templates.(*Templates).executeTemplate (templates.go:305)"}

I was on the right track, and got it to work

        @mdcheck {
                path *.md
        }
        route @mdcheck {
                try_files *.md /_Releases/cdn/markdown.html
        }

Yep - inline matchers can only be exactly *, a path starting with /, or a named matcher starting with @.

You can shorten that a bit by using the single-line named matcher syntax (since Caddy v2.1):

        @mdcheck path *.md
        route @mdcheck {
                try_files *.md /_Releases/cdn/markdown.html
        }

I’m not sure wildcards work in try_files… I think you might just need a simple rewrite here, not try_files.

@mdcheck path *.md
rewrite @mdcheck /_Releases/cdn/markdown.html

try_files will check in sequence whether each part is a file that exists on disk, and if so, will rewrite to it. This means Caddy will look for a file called *.md on the disk, but that won’t exist, so it will move on and try /_Releases/cdn/markdown.html which will exist, so it’ll rewrite to that. This is pointless, you can just directly do the rewrite instead.

Note that try_files doesn’t support matchers, because it itself is a shortcut for a matcher plus a rewrite. See the syntax and expanded form:

wild cards appearntly work with try_files, goahed and look at my CDN, i have it working without error now, thanks for that first edit suggestion, makes it so it doesnt error on directories

https://cdn.merith.tk {
        import cloudflare
        root * /opt/FileServer

        basicauth /games/ {        }
        basicauth /cdn/PasswordProtected/ {        }

        templates
        file_server /* browse

        @mdcheck {
                path *.md
        }
        route @mdcheck {
                try_files *.md /_Releases/cdn/markdown.html
        }
}```

Are you sure? I think it’s just Caddy wasting cycles. Try the below instead, I bet it’ll work just the same, but waste less time doing filesystem lookups.

This topic was automatically closed after 30 days. New replies are no longer allowed.