add markdown support plus example

This commit is contained in:
2025-12-12 21:37:46 -05:00
parent 8dca9ab5da
commit 253c96c534
3 changed files with 24 additions and 9 deletions

View File

@@ -0,0 +1,5 @@
[app]
name = "markdown"
version = "0.0.1"
authors = ["vx-clutch"]

View File

@@ -0,0 +1 @@
# Hello, World!

View File

@@ -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)