2015-05-05 21:37:29 -05:00
|
|
|
package markdown
|
|
|
|
|
|
|
|
import (
|
2015-05-06 18:19:02 -05:00
|
|
|
"path/filepath"
|
|
|
|
|
2015-07-21 00:58:34 -05:00
|
|
|
"github.com/mholt/caddy/middleware"
|
2016-04-16 16:45:32 -05:00
|
|
|
"github.com/mholt/caddy/middleware/markdown/metadata"
|
2015-05-06 18:19:02 -05:00
|
|
|
"github.com/russross/blackfriday"
|
2015-05-05 21:37:29 -05:00
|
|
|
)
|
|
|
|
|
2016-04-16 16:45:32 -05:00
|
|
|
// Markdown processes the contents of a page in b. It parses the metadata
|
2015-05-09 22:12:28 -05:00
|
|
|
// (if any) and uses the template (if found).
|
2016-04-16 16:45:32 -05:00
|
|
|
func (c *Config) Markdown(requestPath string, b []byte, ctx middleware.Context) ([]byte, error) {
|
|
|
|
parser := metadata.GetParser(b)
|
|
|
|
markdown := parser.Markdown()
|
|
|
|
mdata := parser.Metadata()
|
2015-05-09 05:49:54 -05:00
|
|
|
|
2015-05-05 21:37:29 -05:00
|
|
|
// process markdown
|
2016-04-10 19:20:09 -05:00
|
|
|
extns := 0
|
|
|
|
extns |= blackfriday.EXTENSION_TABLES
|
|
|
|
extns |= blackfriday.EXTENSION_FENCED_CODE
|
|
|
|
extns |= blackfriday.EXTENSION_STRIKETHROUGH
|
|
|
|
extns |= blackfriday.EXTENSION_DEFINITION_LISTS
|
2015-10-29 10:59:14 -05:00
|
|
|
markdown = blackfriday.Markdown(markdown, c.Renderer, extns)
|
2015-05-08 10:20:07 -05:00
|
|
|
|
2015-05-06 18:19:02 -05:00
|
|
|
// set it as body for template
|
2016-04-16 16:45:32 -05:00
|
|
|
mdata.Variables["body"] = string(markdown)
|
|
|
|
title := mdata.Title
|
2015-07-25 15:39:13 -05:00
|
|
|
if title == "" {
|
|
|
|
title = filepath.Base(requestPath)
|
|
|
|
var extension = filepath.Ext(requestPath)
|
|
|
|
title = title[0 : len(title)-len(extension)]
|
|
|
|
}
|
2016-04-16 16:45:32 -05:00
|
|
|
mdata.Variables["title"] = title
|
2015-05-07 07:45:27 -05:00
|
|
|
|
2016-04-16 16:45:32 -05:00
|
|
|
return execTemplate(c, mdata, ctx)
|
2015-05-08 10:20:07 -05:00
|
|
|
}
|