diff --git a/middleware/markdown/markdown.go b/middleware/markdown/markdown.go index 17083668..5d6da36f 100644 --- a/middleware/markdown/markdown.go +++ b/middleware/markdown/markdown.go @@ -89,7 +89,6 @@ func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error // Set path to found index file fpath = idx - _ = dirents } // If supported extension, process it @@ -117,7 +116,7 @@ func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error Req: r, URL: r.URL, } - html, err := cfg.Markdown(fpath, body, ctx) + html, err := cfg.Markdown(fpath, body, dirents, ctx) if err != nil { return http.StatusInternalServerError, err } diff --git a/middleware/markdown/metadata/metadata.go b/middleware/markdown/metadata/metadata.go index 24cdb391..19b50b44 100644 --- a/middleware/markdown/metadata/metadata.go +++ b/middleware/markdown/metadata/metadata.go @@ -3,6 +3,7 @@ package metadata import ( "bufio" "bytes" + "os" "time" ) @@ -30,6 +31,9 @@ type Metadata struct { // Flags to be used with Template Flags map[string]bool + + // Directory entries present, if a directory + Dirents []os.FileInfo } // NewMetadata() returns a new Metadata struct, loaded with the given map diff --git a/middleware/markdown/process.go b/middleware/markdown/process.go index 27f337dd..bae5cf81 100644 --- a/middleware/markdown/process.go +++ b/middleware/markdown/process.go @@ -1,6 +1,7 @@ package markdown import ( + "os" "path/filepath" "github.com/mholt/caddy/middleware" @@ -10,7 +11,7 @@ import ( // Markdown processes the contents of a page in b. It parses the metadata // (if any) and uses the template (if found). -func (c *Config) Markdown(requestPath string, b []byte, ctx middleware.Context) ([]byte, error) { +func (c *Config) Markdown(requestPath string, b []byte, dirents []os.FileInfo, ctx middleware.Context) ([]byte, error) { parser := metadata.GetParser(b) markdown := parser.Markdown() mdata := parser.Metadata() @@ -33,5 +34,12 @@ func (c *Config) Markdown(requestPath string, b []byte, ctx middleware.Context) } mdata.Variables["title"] = title + if len(dirents) > 0 { + mdata.Flags["dirents"] = true + mdata.Dirents = dirents + } else { + mdata.Flags["dirents"] = false + } + return execTemplate(c, mdata, ctx) }