Markdown support in v2

You can do it a few ways. The easiest way is your first example: {{markdown "whole Markdown file here"}}

You can also bring in a file by name: {{include "file.md" | markdown}}

Here’s another example, it’s how we serve the docs on the Caddy site: website/index.html at master · caddyserver/website · GitHub

It’s a bit more advanced than the basic use case. The first 3 lines construct a filepath to the actual markdown file from the page’s URI:

{{$pathParts := splitList "/" .OriginalReq.URL.Path}}
{{$markdownFilename := default "index" (slice $pathParts 2 | join "/")}}
{{$markdownFilePath := printf "/docs/markdown/%s.md" $markdownFilename}}

(.OriginalReq is the request before rewriting, because all docs pages actually go to this index file with a directive in our Caddyfile: rewrite /docs/* /docs/index.html)

The next line splits the markdown content from its front matter (totally optional, but we wanted some data structure associated with each file for metadata purposes):

{{$markdownFile := (include $markdownFilePath | splitFrontMatter)}}

Then in the actual HTML document we can render the title, which we got from the front matter (again, this is optional):

{{$markdownFile.Meta.title}}

Then the actual rendering of the markdown file in the body of our HTML is easy:

{{markdown $markdownFile.Body}}
3 Likes