From 253c96c5344bc7d9c263660cbddc3f1d77351d52 Mon Sep 17 00:00:00 2001 From: vx-clutch Date: Fri, 12 Dec 2025 21:37:46 -0500 Subject: [PATCH] add markdown support plus example --- examples/markdown/Fes.toml | 5 +++++ examples/markdown/www/index.md | 1 + src/server/server.go | 27 ++++++++++++++++++--------- 3 files changed, 24 insertions(+), 9 deletions(-) create mode 100644 examples/markdown/Fes.toml create mode 100644 examples/markdown/www/index.md diff --git a/examples/markdown/Fes.toml b/examples/markdown/Fes.toml new file mode 100644 index 0000000..f800d4d --- /dev/null +++ b/examples/markdown/Fes.toml @@ -0,0 +1,5 @@ +[app] + +name = "markdown" +version = "0.0.1" +authors = ["vx-clutch"] \ No newline at end of file diff --git a/examples/markdown/www/index.md b/examples/markdown/www/index.md new file mode 100644 index 0000000..ba4fcdf --- /dev/null +++ b/examples/markdown/www/index.md @@ -0,0 +1 @@ +# Hello, World! diff --git a/src/server/server.go b/src/server/server.go index bb2701a..0eeb4a8 100644 --- a/src/server/server.go +++ b/src/server/server.go @@ -50,6 +50,14 @@ func handleDir(entries []os.DirEntry, dir string, routes map[string]string, base continue } route = joinBase(base, name) + } else if !isStatic && strings.HasSuffix(entry.Name(), ".md") { + name := strings.TrimSuffix(entry.Name(), ".md") + if name == "index" { + routes[basePath(base)] = path + routes[route] = path + continue + } + route = joinBase(base, name) } routes[route] = path } @@ -64,7 +72,7 @@ func joinBase(base, name string) string { } func basePath(base string) string { - if base == "" { + if base == "" || base == "." { return "/" } return base @@ -401,7 +409,8 @@ func Start(dir string) error { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { path := r.URL.Path - lp, ok := routes[path] + p, ok := routes[path] + fmt.Printf("> %s ", basePath(filepath.Base(p))) if !ok && strings.HasPrefix(path, "/archive") { fsPath := "." + path @@ -417,7 +426,6 @@ func Start(dir string) error { if !ok { w.WriteHeader(http.StatusNotFound) w.Write([]byte(notFoundData)) - fmt.Printf("> %s ", path) color.Yellow("not found") return } @@ -434,17 +442,18 @@ func Start(dir string) error { params: params, } - fmt.Printf("> %s ", filepath.Base(lp)) - var data []byte var err error - if strings.HasSuffix(lp, ".lua") { + if strings.HasSuffix(p, ".lua") { var b string - b, err = loadLua(dir, lp, &cfg, req) + b, err = loadLua(dir, p, &cfg, req) data = []byte(b) + } else if strings.HasSuffix(p, ".md") { + data, err = os.ReadFile(p) + data = []byte(markdownToHTML(string(data))) } else { - data, err = os.ReadFile(lp) + data, err = os.ReadFile(p) } if err != nil { @@ -453,8 +462,8 @@ func Start(dir string) error { return } - color.Green("ok") w.Write(data) + color.Green("ok") }) fmt.Printf("Server is running on http://localhost:%d\n", *config.Port)