mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-11-03 19:17:29 -05:00 
			
		
		
		
	Add ability to markdown a directory with a template.
This commit is contained in:
		
							parent
							
								
									e652d12cfc
								
							
						
					
					
						commit
						b541c717ca
					
				@ -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
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
 | 
			
		||||
@ -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)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user