0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-23 22:27:38 -05:00
caddy/middleware/markdown/process.go
2016-04-16 16:50:45 -07:00

45 lines
1.2 KiB
Go

package markdown
import (
"os"
"path/filepath"
"github.com/mholt/caddy/middleware"
"github.com/mholt/caddy/middleware/markdown/metadata"
"github.com/russross/blackfriday"
)
// Markdown processes the contents of a page in b. It parses the metadata
// (if any) and uses the template (if found).
func (c *Config) Markdown(requestPath string, b []byte, dirents []os.FileInfo, ctx middleware.Context) ([]byte, error) {
parser := metadata.GetParser(b)
markdown := parser.Markdown()
mdata := parser.Metadata()
// process markdown
extns := 0
extns |= blackfriday.EXTENSION_TABLES
extns |= blackfriday.EXTENSION_FENCED_CODE
extns |= blackfriday.EXTENSION_STRIKETHROUGH
extns |= blackfriday.EXTENSION_DEFINITION_LISTS
markdown = blackfriday.Markdown(markdown, c.Renderer, extns)
// set it as body for template
mdata.Variables["body"] = string(markdown)
title := mdata.Title
if title == "" {
title = filepath.Base(requestPath)
var extension = filepath.Ext(requestPath)
title = title[0 : len(title)-len(extension)]
}
mdata.Variables["title"] = title
if len(dirents) > 0 {
mdata.Flags["dirents"] = true
mdata.Dirents = dirents
} else {
mdata.Flags["dirents"] = false
}
return execTemplate(c, mdata, ctx)
}