From 7d15435361c95aa029be244f962b47950487a539 Mon Sep 17 00:00:00 2001
From: Toby Allen <tobyallen@toflidium.com>
Date: Sat, 8 Apr 2017 07:47:33 +0100
Subject: [PATCH] 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
---
 caddyhttp/markdown/markdown.go      |  8 ++++----
 caddyhttp/markdown/markdown_test.go | 30 ++++++++++++++++-------------
 caddyhttp/markdown/setup.go         | 12 ++++++++----
 3 files changed, 29 insertions(+), 21 deletions(-)

diff --git a/caddyhttp/markdown/markdown.go b/caddyhttp/markdown/markdown.go
index d48561a6d..efb94501b 100644
--- a/caddyhttp/markdown/markdown.go
+++ b/caddyhttp/markdown/markdown.go
@@ -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,
diff --git a/caddyhttp/markdown/markdown_test.go b/caddyhttp/markdown/markdown_test.go
index b4db6ead6..4da39a7f5 100644
--- a/caddyhttp/markdown/markdown_test.go
+++ b/caddyhttp/markdown/markdown_test.go
@@ -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
diff --git a/caddyhttp/markdown/setup.go b/caddyhttp/markdown/setup.go
index 24dc6fcf6..2424abf61 100644
--- a/caddyhttp/markdown/setup.go
+++ b/caddyhttp/markdown/setup.go
@@ -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)
 	}