0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-23 22:27:38 -05:00

markdown: Match index file for each extension; fix #1418 (#1559)

* Create list of index files based on extensions and check on a per config
basis

* remove log lines

* fixed tests

* made gofmt suggested change

* Changes made to simplify
This commit is contained in:
Toby Allen 2017-04-08 07:47:33 +01:00 committed by Matt Holt
parent e26a855d8b
commit 7d15435361
3 changed files with 29 additions and 21 deletions

View file

@ -29,9 +29,6 @@ type Markdown struct {
// The list of markdown configurations // The list of markdown configurations
Configs []*Config Configs []*Config
// The list of index files to try
IndexFiles []string
} }
// Config stores markdown middleware configurations. // Config stores markdown middleware configurations.
@ -51,6 +48,9 @@ type Config struct {
// List of JavaScript files to load for each markdown file // List of JavaScript files to load for each markdown file
Scripts []string Scripts []string
// The list of index files to try
IndexFiles []string
// Template(s) to render with // Template(s) to render with
Template *template.Template Template *template.Template
} }
@ -78,7 +78,7 @@ func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
var dirents []os.FileInfo var dirents []os.FileInfo
var lastModTime time.Time var lastModTime time.Time
fpath := r.URL.Path fpath := r.URL.Path
if idx, ok := httpserver.IndexFile(md.FileSys, fpath, md.IndexFiles); ok { if idx, ok := httpserver.IndexFile(md.FileSys, fpath, cfg.IndexFiles); ok {
// We're serving a directory index file, which may be a markdown // We're serving a directory index file, which may be a markdown
// file with a template. Let's grab a list of files this directory // file with a template. Let's grab a list of files this directory
// URL points to, and pass that in to any possible template invocations, // URL points to, and pass that in to any possible template invocations,

View file

@ -33,9 +33,10 @@ func TestMarkdown(t *testing.T) {
Extensions: map[string]struct{}{ Extensions: map[string]struct{}{
".md": {}, ".md": {},
}, },
Styles: []string{}, IndexFiles: []string{"index.md"},
Scripts: []string{}, Styles: []string{},
Template: setDefaultTemplate(f("markdown_tpl.html")), Scripts: []string{},
Template: setDefaultTemplate(f("markdown_tpl.html")),
}, },
{ {
Renderer: blackfriday.HtmlRenderer(0, "", ""), Renderer: blackfriday.HtmlRenderer(0, "", ""),
@ -43,9 +44,10 @@ func TestMarkdown(t *testing.T) {
Extensions: map[string]struct{}{ Extensions: map[string]struct{}{
".md": {}, ".md": {},
}, },
Styles: []string{}, IndexFiles: []string{"index.md"},
Scripts: []string{}, Styles: []string{},
Template: setDefaultTemplate(f("docflags/template.txt")), Scripts: []string{},
Template: setDefaultTemplate(f("docflags/template.txt")),
}, },
{ {
Renderer: blackfriday.HtmlRenderer(0, "", ""), Renderer: blackfriday.HtmlRenderer(0, "", ""),
@ -53,9 +55,10 @@ func TestMarkdown(t *testing.T) {
Extensions: map[string]struct{}{ Extensions: map[string]struct{}{
".md": {}, ".md": {},
}, },
Styles: []string{"/resources/css/log.css", "/resources/css/default.css"}, IndexFiles: []string{"index.md"},
Scripts: []string{"/resources/js/log.js", "/resources/js/default.js"}, Styles: []string{"/resources/css/log.css", "/resources/css/default.css"},
Template: GetDefaultTemplate(), Scripts: []string{"/resources/js/log.js", "/resources/js/default.js"},
Template: GetDefaultTemplate(),
}, },
{ {
Renderer: blackfriday.HtmlRenderer(0, "", ""), Renderer: blackfriday.HtmlRenderer(0, "", ""),
@ -63,12 +66,13 @@ func TestMarkdown(t *testing.T) {
Extensions: map[string]struct{}{ Extensions: map[string]struct{}{
".md": {}, ".md": {},
}, },
Styles: []string{}, IndexFiles: []string{"index.md"},
Scripts: []string{}, Styles: []string{},
Template: setDefaultTemplate(f("markdown_tpl.html")), Scripts: []string{},
Template: setDefaultTemplate(f("markdown_tpl.html")),
}, },
}, },
IndexFiles: []string{"index.html"},
Next: httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { Next: httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
t.Fatalf("Next shouldn't be called") t.Fatalf("Next shouldn't be called")
return 0, nil return 0, nil

View file

@ -26,10 +26,9 @@ func setup(c *caddy.Controller) error {
cfg := httpserver.GetConfig(c) cfg := httpserver.GetConfig(c)
md := Markdown{ md := Markdown{
Root: cfg.Root, Root: cfg.Root,
FileSys: http.Dir(cfg.Root), FileSys: http.Dir(cfg.Root),
Configs: mdconfigs, Configs: mdconfigs,
IndexFiles: []string{"index.md"},
} }
cfg.AddMiddleware(func(next httpserver.Handler) httpserver.Handler { cfg.AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
@ -48,6 +47,7 @@ func markdownParse(c *caddy.Controller) ([]*Config, error) {
Renderer: blackfriday.HtmlRenderer(0, "", ""), Renderer: blackfriday.HtmlRenderer(0, "", ""),
Extensions: make(map[string]struct{}), Extensions: make(map[string]struct{}),
Template: GetDefaultTemplate(), Template: GetDefaultTemplate(),
IndexFiles: []string{},
} }
// Get the path scope // Get the path scope
@ -75,6 +75,10 @@ func markdownParse(c *caddy.Controller) ([]*Config, error) {
md.Extensions[".mdown"] = struct{}{} md.Extensions[".mdown"] = struct{}{}
} }
// Make a list of index files to match extensions
for ext := range md.Extensions {
md.IndexFiles = append(md.IndexFiles, "index"+ext)
}
mdconfigs = append(mdconfigs, md) mdconfigs = append(mdconfigs, md)
} }