From 36fa47c287aa71012bca0edd17a72b8e83aeceaa Mon Sep 17 00:00:00 2001 From: Ted Roden Date: Sat, 11 May 2024 01:55:45 -0400 Subject: [PATCH] feat: Render other markdown files (#52) This adds the ability to render other markdown files in the project - template.html now supplies the `path=window.location.pathname` - mdHandler uses the path if it's supplied This should fix #40 --- cmd/server.go | 20 ++++++++++++++++---- cmd/template.html | 3 ++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/cmd/server.go b/cmd/server.go index 0fba048..d3aa9e1 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -6,6 +6,7 @@ import ( "net" "net/http" "path/filepath" + "strings" "text/template" ) @@ -78,8 +79,9 @@ func (server *Server) Serve(param *Param) error { } func handler(filename string, param *Param, h http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.URL.Path != "/" { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + + if !strings.HasSuffix(r.URL.Path, ".md") && r.URL.Path != "/" { h.ServeHTTP(w, r) return } @@ -112,8 +114,7 @@ func handler(filename string, param *Param, h http.Handler) http.Handler { }) } -func mdHandler(filename string) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { +func mdResponse(w http.ResponseWriter, filename string) { w.Header().Set("Content-Type", "text/html; charset=utf-8") markdown, err := slurp(filename) @@ -128,6 +129,17 @@ func mdHandler(filename string) http.Handler { return } fmt.Fprintf(w, "%s", html) + +} + +func mdHandler(filename string) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + pathParam := r.URL.Query().Get("path") + if pathParam != "" { + mdResponse(w, pathParam) + } else { + mdResponse(w, filename) + } }) } diff --git a/cmd/template.html b/cmd/template.html index 9c79150..6d1f994 100644 --- a/cmd/template.html +++ b/cmd/template.html @@ -61,8 +61,9 @@ >