Files
pages/internal/server/compile_handler.go
T
Anya Tokugawa b90aef79d0
Build and publish container images / images (push) Successful in 11m37s
Release container images / release (push) Successful in 22s
feat: add browser upload queue and trusted sessions with bounded projections
2026-11-08 20:27:04 +09:00

97 lines
1.4 KiB
Go

package server
import (
"net/http"
"strings"
"pageswiki/internal/grammaratur"
)
func (s *Server) compileMarkdownFromPath(
r *http.Request,
path string,
) (string, error) {
return s.transpileMarkdownFromPath(r, path, markdownAuthoring)
}
type markdownTranspileMode string
const (
markdownAuthoring markdownTranspileMode = "authoring"
markdownProjection markdownTranspileMode = "projection"
)
func (s *Server) transpileMarkdownFromPath(r *http.Request, path string, mode markdownTranspileMode) (string, error) {
objectUUID, err :=
s.store.ResolvePathInDomain(
s.domainUUIDForRequest(r),
path,
)
if err != nil {
return "", err
}
latest, err :=
s.latestRevisionForRequest(
r,
objectUUID,
)
if err != nil {
return "", err
}
html := latest.Body
if mode == markdownProjection {
html = s.safeHTMLProjection(r, html, path, true)
} else {
return grammaratur.HTMLToMarkdownAuthoring(html)
}
return grammaratur.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(
r,
path,
)
if err != nil {
http.NotFound(w, r)
return
}
w.Header().Set(
"Content-Type",
"text/markdown; charset=utf-8",
)
_, _ = w.Write(
[]byte(out),
)
}