mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-05 18:44:58 +03:00
Support multiple occurrences of markdown directive
This commit is contained in:
parent
2fa6129c3a
commit
6d869ef55b
1 changed files with 49 additions and 31 deletions
|
@ -13,24 +13,8 @@ import (
|
||||||
"github.com/russross/blackfriday"
|
"github.com/russross/blackfriday"
|
||||||
)
|
)
|
||||||
|
|
||||||
// New creates a new instance of Markdown middleware that
|
// Markdown implements a layer of middleware that serves
|
||||||
// renders markdown to HTML on-the-fly.
|
// markdown as HTML.
|
||||||
func New(c middleware.Controller) (middleware.Middleware, error) {
|
|
||||||
md, err := parse(c)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
md.Root = c.Root()
|
|
||||||
md.Renderer = blackfriday.HtmlRenderer(0, "", "")
|
|
||||||
|
|
||||||
return func(next http.HandlerFunc) http.HandlerFunc {
|
|
||||||
md.Next = next
|
|
||||||
return md.ServeHTTP
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Markdown stores the configuration necessary to serve the Markdown middleware.
|
|
||||||
type Markdown struct {
|
type Markdown struct {
|
||||||
// Server root
|
// Server root
|
||||||
Root string
|
Root string
|
||||||
|
@ -38,6 +22,12 @@ type Markdown struct {
|
||||||
// Next HTTP handler in the chain
|
// Next HTTP handler in the chain
|
||||||
Next http.HandlerFunc
|
Next http.HandlerFunc
|
||||||
|
|
||||||
|
// The list of markdown configurations
|
||||||
|
Configs []MarkdownConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarkdownConfig stores markdown middleware configurations.
|
||||||
|
type MarkdownConfig struct {
|
||||||
// Markdown renderer
|
// Markdown renderer
|
||||||
Renderer blackfriday.Renderer
|
Renderer blackfriday.Renderer
|
||||||
|
|
||||||
|
@ -54,12 +44,35 @@ type Markdown struct {
|
||||||
Scripts []string
|
Scripts []string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// New creates a new instance of Markdown middleware that
|
||||||
|
// renders markdown to HTML on-the-fly.
|
||||||
|
func New(c middleware.Controller) (middleware.Middleware, error) {
|
||||||
|
mdconfigs, err := parse(c)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
md := Markdown{
|
||||||
|
Root: c.Root(),
|
||||||
|
Configs: mdconfigs,
|
||||||
|
}
|
||||||
|
|
||||||
|
return func(next http.HandlerFunc) http.HandlerFunc {
|
||||||
|
md.Next = next
|
||||||
|
return md.ServeHTTP
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
// ServeHTTP implements the http.Handler interface.
|
// ServeHTTP implements the http.Handler interface.
|
||||||
func (m Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
if middleware.Path(r.URL.Path).Matches(m.PathScope) {
|
for _, m := range md.Configs {
|
||||||
|
if !middleware.Path(r.URL.Path).Matches(m.PathScope) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
for _, ext := range m.Extensions {
|
for _, ext := range m.Extensions {
|
||||||
if strings.HasSuffix(r.URL.Path, ext) {
|
if strings.HasSuffix(r.URL.Path, ext) {
|
||||||
fpath := m.Root + r.URL.Path
|
fpath := md.Root + r.URL.Path
|
||||||
|
|
||||||
body, err := ioutil.ReadFile(fpath)
|
body, err := ioutil.ReadFile(fpath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -105,17 +118,21 @@ func (m Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Didn't qualify to serve as markdown; pass-thru
|
// Didn't qualify to serve as markdown; pass-thru
|
||||||
m.Next(w, r)
|
md.Next(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse fills up a new instance of Markdown middleware.
|
// parse creates new instances of Markdown middleware.
|
||||||
func parse(c middleware.Controller) (Markdown, error) {
|
func parse(c middleware.Controller) ([]MarkdownConfig, error) {
|
||||||
var md Markdown
|
var mdconfigs []MarkdownConfig
|
||||||
|
|
||||||
for c.Next() {
|
for c.Next() {
|
||||||
|
md := MarkdownConfig{
|
||||||
|
Renderer: blackfriday.HtmlRenderer(0, "", ""),
|
||||||
|
}
|
||||||
|
|
||||||
// Get the path scope
|
// Get the path scope
|
||||||
if !c.NextArg() {
|
if !c.NextArg() {
|
||||||
return md, c.ArgErr()
|
return mdconfigs, c.ArgErr()
|
||||||
}
|
}
|
||||||
md.PathScope = c.Val()
|
md.PathScope = c.Val()
|
||||||
|
|
||||||
|
@ -125,27 +142,28 @@ func parse(c middleware.Controller) (Markdown, error) {
|
||||||
case "ext":
|
case "ext":
|
||||||
exts := c.RemainingArgs()
|
exts := c.RemainingArgs()
|
||||||
if len(exts) == 0 {
|
if len(exts) == 0 {
|
||||||
return md, c.ArgErr()
|
return mdconfigs, c.ArgErr()
|
||||||
}
|
}
|
||||||
md.Extensions = append(md.Extensions, exts...)
|
md.Extensions = append(md.Extensions, exts...)
|
||||||
case "css":
|
case "css":
|
||||||
if !c.NextArg() {
|
if !c.NextArg() {
|
||||||
return md, c.ArgErr()
|
return mdconfigs, c.ArgErr()
|
||||||
}
|
}
|
||||||
md.Styles = append(md.Styles, c.Val())
|
md.Styles = append(md.Styles, c.Val())
|
||||||
case "js":
|
case "js":
|
||||||
if !c.NextArg() {
|
if !c.NextArg() {
|
||||||
return md, c.ArgErr()
|
return mdconfigs, c.ArgErr()
|
||||||
}
|
}
|
||||||
md.Scripts = append(md.Scripts, c.Val())
|
md.Scripts = append(md.Scripts, c.Val())
|
||||||
default:
|
default:
|
||||||
return md, c.Err("Expected valid markdown configuration property")
|
return mdconfigs, c.Err("Expected valid markdown configuration property")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mdconfigs = append(mdconfigs, md)
|
||||||
}
|
}
|
||||||
|
|
||||||
return md, nil
|
return mdconfigs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
Loading…
Reference in a new issue