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
Configs []*Config
// The list of index files to try
IndexFiles []string
}
// Config stores markdown middleware configurations.
@ -51,6 +48,9 @@ type Config struct {
// List of JavaScript files to load for each markdown file
Scripts []string
// The list of index files to try
IndexFiles []string
// Template(s) to render with
Template *template.Template
}
@ -78,7 +78,7 @@ func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
var dirents []os.FileInfo
var lastModTime time.Time
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
// 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,

View File

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

View File

@ -26,10 +26,9 @@ func setup(c *caddy.Controller) error {
cfg := httpserver.GetConfig(c)
md := Markdown{
Root: cfg.Root,
FileSys: http.Dir(cfg.Root),
Configs: mdconfigs,
IndexFiles: []string{"index.md"},
Root: cfg.Root,
FileSys: http.Dir(cfg.Root),
Configs: mdconfigs,
}
cfg.AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
@ -48,6 +47,7 @@ func markdownParse(c *caddy.Controller) ([]*Config, error) {
Renderer: blackfriday.HtmlRenderer(0, "", ""),
Extensions: make(map[string]struct{}),
Template: GetDefaultTemplate(),
IndexFiles: []string{},
}
// Get the path scope
@ -75,6 +75,10 @@ func markdownParse(c *caddy.Controller) ([]*Config, error) {
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)
}