Skip to content

Commit

Permalink
feat: Render other markdown files (#52)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tedroden committed May 11, 2024
1 parent e541afb commit 36fa47c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
20 changes: 16 additions & 4 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net"
"net/http"
"path/filepath"
"strings"
"text/template"
)

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
})
}

Expand Down
3 changes: 2 additions & 1 deletion cmd/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@
></script>
<script type="text/javascript">
function loadmd() {

$.ajax({
url: "/__/md",
url: "/__/md?path=" + window.location.pathname.slice(1),
success: function (result) {
$("#markdown-body").html(result).promise().done(function(){
var count = $("#markdown-body a").length;
Expand Down

0 comments on commit 36fa47c

Please sign in to comment.