Files
pages/internal/server/compile_handler.go
T

81 lines
938 B
Go

package server
import (
"net/http"
"strings"
"pageswiki/internal/compiler"
)
func (s *Server) compileMarkdownFromPath(
path string,
) (string, error) {
objectUUID, err :=
s.store.ResolvePath(
path,
)
if err != nil {
return "", err
}
latest, err :=
s.store.GetLatestRevision(
objectUUID,
)
if err != nil {
return "", err
}
html :=
s.expandMacros(
latest.Body,
path,
)
return compiler.HTMLToMarkdown(
html,
)
}
func (s *Server) handleCompileMarkdown(
w http.ResponseWriter,
r *http.Request,
) {
path :=
strings.TrimPrefix(
r.URL.Path,
"/:/compile/markdown",
)
if path == "" {
path = "/"
}
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
out, err :=
s.compileMarkdownFromPath(
path,
)
if err != nil {
http.NotFound(w, r)
return
}
w.Header().Set(
"Content-Type",
"text/markdown; charset=utf-8",
)
_, _ = w.Write(
[]byte(out),
)
}