Files
pages/internal/server/semantic_handlers.go
T

200 lines
2.3 KiB
Go

package server
import (
"html/template"
"log"
"net/http"
"strings"
)
func (s *Server) handleSemanticRoute(
w http.ResponseWriter,
r *http.Request,
) {
path := r.URL.Path
objectUUID, err :=
s.store.ResolvePath(path)
if err != nil {
http.NotFound(w, r)
return
}
latest, err :=
s.store.GetLatestRevision(
objectUUID,
)
if err != nil {
http.Error(
w,
err.Error(),
500,
)
return
}
bs := buildBreadcrumbs(path)
log.Printf("breadcrumb debug path=%s bs=%#v", path, bs)
rendered :=
s.expandMacros(
latest.Body,
path,
)
s.render(
w,
"page.gohtml",
map[string]any{
"Title": path,
"Path": path,
"Breadcrumbs": buildBreadcrumbs(path),
"ObjectUUID": objectUUID,
"NavItems": resolveNavItems(
s.getSystemNavItems(),
NavContext{
Path: path,
ObjectUUID: objectUUID,
ObjectURI: "uuid:" + objectUUID,
},
),
"RenderedHTML": template.HTML(
rendered,
),
},
)
}
func (s *Server) handleSemanticEditor(
w http.ResponseWriter,
r *http.Request,
) {
path := strings.TrimPrefix(
r.URL.Path,
"/e",
)
if path == "" {
path = "/"
}
objectUUID, err :=
s.store.ResolvePath(path)
if err != nil {
objectUUID, err =
s.store.CreateObject(path)
if err != nil {
http.Error(
w,
err.Error(),
500,
)
return
}
}
latest, err :=
s.store.GetLatestRevision(
objectUUID,
)
body := ""
if err == nil {
body = latest.Body
}
editType :=
r.URL.Query().Get(
"edittype",
)
if editType == "" {
editType = "html"
}
if editType == "markdown" {
body =
htmlToMarkdown(
body,
)
}
s.render(
w,
"editor.gohtml",
map[string]any{
"Title": path,
"Path": path,
"ObjectUUID": objectUUID,
"Body": body,
"RenderedHTML": template.HTML(
s.expandMacros(
body,
path,
),
),
"EditType": editType,
},
)
}
func htmlToMarkdown(
in string,
) string {
out := in
out = strings.ReplaceAll(
out,
"<h1>",
"# ",
)
out = strings.ReplaceAll(
out,
"</h1>",
"",
)
out = strings.ReplaceAll(
out,
"<ul>",
"",
)
out = strings.ReplaceAll(
out,
"</ul>",
"",
)
out = strings.ReplaceAll(
out,
"<li>",
"- ",
)
out = strings.ReplaceAll(
out,
"</li>",
"",
)
return out
}