Merge pull request 'add markdown support plus example' (#2) from markdown into main
Reviewed-on: #2
This commit was merged in pull request #2.
This commit is contained in:
5
examples/markdown/Fes.toml
Normal file
5
examples/markdown/Fes.toml
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
[app]
|
||||||
|
|
||||||
|
name = "markdown"
|
||||||
|
version = "0.0.1"
|
||||||
|
authors = ["vx-clutch"]
|
||||||
1
examples/markdown/www/index.md
Normal file
1
examples/markdown/www/index.md
Normal file
@@ -0,0 +1 @@
|
|||||||
|
# Hello, World!
|
||||||
@@ -50,6 +50,14 @@ func handleDir(entries []os.DirEntry, dir string, routes map[string]string, base
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
route = joinBase(base, name)
|
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
|
routes[route] = path
|
||||||
}
|
}
|
||||||
@@ -64,7 +72,7 @@ func joinBase(base, name string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func basePath(base string) string {
|
func basePath(base string) string {
|
||||||
if base == "" {
|
if base == "" || base == "." {
|
||||||
return "/"
|
return "/"
|
||||||
}
|
}
|
||||||
return base
|
return base
|
||||||
@@ -401,7 +409,8 @@ func Start(dir string) error {
|
|||||||
|
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
path := r.URL.Path
|
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") {
|
if !ok && strings.HasPrefix(path, "/archive") {
|
||||||
fsPath := "." + path
|
fsPath := "." + path
|
||||||
@@ -417,7 +426,6 @@ func Start(dir string) error {
|
|||||||
if !ok {
|
if !ok {
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
w.Write([]byte(notFoundData))
|
w.Write([]byte(notFoundData))
|
||||||
fmt.Printf("> %s ", path)
|
|
||||||
color.Yellow("not found")
|
color.Yellow("not found")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -434,17 +442,18 @@ func Start(dir string) error {
|
|||||||
params: params,
|
params: params,
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("> %s ", filepath.Base(lp))
|
|
||||||
|
|
||||||
var data []byte
|
var data []byte
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
if strings.HasSuffix(lp, ".lua") {
|
if strings.HasSuffix(p, ".lua") {
|
||||||
var b string
|
var b string
|
||||||
b, err = loadLua(dir, lp, &cfg, req)
|
b, err = loadLua(dir, p, &cfg, req)
|
||||||
data = []byte(b)
|
data = []byte(b)
|
||||||
|
} else if strings.HasSuffix(p, ".md") {
|
||||||
|
data, err = os.ReadFile(p)
|
||||||
|
data = []byte(markdownToHTML(string(data)))
|
||||||
} else {
|
} else {
|
||||||
data, err = os.ReadFile(lp)
|
data, err = os.ReadFile(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -453,8 +462,8 @@ func Start(dir string) error {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
color.Green("ok")
|
|
||||||
w.Write(data)
|
w.Write(data)
|
||||||
|
color.Green("ok")
|
||||||
})
|
})
|
||||||
|
|
||||||
fmt.Printf("Server is running on http://localhost:%d\n", *config.Port)
|
fmt.Printf("Server is running on http://localhost:%d\n", *config.Port)
|
||||||
|
|||||||
Reference in New Issue
Block a user