Extract Grammaratur, Dregarnuhr, Geduldh, Mestionora, Ordoschnellic, Schutzaria, Kunstzeal and Seheweit behind compatible application contracts. Include accumulated editor resilience, navigation and theme UX, remote CLI, storage/schema and DR groundwork, tests and design/operations documentation. Verified: uncached full Go suite; go vet ./...; race tests for all eight components; 11 targeted Chromium tests; Node suites; 19 Python CLI tests; reproducible editor build. No push, release tag or deployment in this checkpoint.
97 lines
3.1 KiB
Go
97 lines
3.1 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"pageswiki/internal/grammaratur"
|
|
)
|
|
|
|
// Converts authoring input only. It never expands macros or writes a Revision.
|
|
func (s *Server) handleEditorConvert(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
w.Header().Set("Cache-Control", "private, no-store")
|
|
if r.Method != http.MethodPost {
|
|
w.Header().Set("Allow", "POST")
|
|
http.Error(w, "method not allowed", 405)
|
|
return
|
|
}
|
|
var input struct {
|
|
HTML string `json:"html"`
|
|
}
|
|
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 2<<20)).Decode(&input); err != nil {
|
|
http.Error(w, "invalid input", 400)
|
|
return
|
|
}
|
|
markdown, err := grammaratur.HTMLToMarkdownAuthoring(input.HTML)
|
|
if err != nil {
|
|
http.Error(w, "conversion failed", 422)
|
|
return
|
|
}
|
|
_ = json.NewEncoder(w).Encode(map[string]string{"markdown": markdown})
|
|
}
|
|
|
|
// Restore loads a working buffer only; it never mutates an old Revision.
|
|
func (s *Server) handleEditorState(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
w.Header().Set("Cache-Control", "private, no-store")
|
|
w.Header().Set("Vary", "Authorization, Cookie")
|
|
if r.Method != http.MethodGet {
|
|
w.Header().Set("Allow", "GET")
|
|
http.Error(w, "method not allowed", 405)
|
|
return
|
|
}
|
|
domain := s.domainUUIDForRequest(r)
|
|
principal, authErr := s.store.PrincipalForSession(domain, s.authTokenFromRequest(r))
|
|
if mode, _ := s.store.GetConfigInDomain(domain, "authorization_mode"); mode == "enforcing" && authErr != nil {
|
|
http.Error(w, "authentication required", 401)
|
|
return
|
|
}
|
|
object := r.URL.Query().Get("uuid")
|
|
if object == "" || !s.store.ObjectBoundInDomain(domain, object) || s.authorizeObjectForRequest(r, "read", object) != nil {
|
|
http.Error(w, "forbidden", 403)
|
|
return
|
|
}
|
|
rev, err := s.store.GetRevisionMetadataForExactLanguage(object, r.URL.Query().Get("lang"))
|
|
if err != nil {
|
|
http.Error(w, "invalid language or unavailable revision", 400)
|
|
return
|
|
}
|
|
head := rev.UUID
|
|
if id := r.URL.Query().Get("revision"); id != "" {
|
|
metadata, e := s.store.GetRevisionMetadata(id)
|
|
if e != nil || metadata.ObjectUUID != object {
|
|
http.Error(w, "revision not found", 404)
|
|
return
|
|
}
|
|
rev = metadata
|
|
}
|
|
owner := ""
|
|
if principal != nil {
|
|
owner = principal.UUID
|
|
}
|
|
out := map[string]any{"revision_uuid": rev.UUID, "head_revision_uuid": head, "lang": rev.Language, "content_type": rev.ContentType, "filename": rev.Filename, "principal_uuid": owner}
|
|
if r.URL.Query().Get("metadata") != "1" {
|
|
if rev.UUID != "" {
|
|
rev, err = s.store.GetRevision(rev.UUID)
|
|
}
|
|
if err != nil {
|
|
http.Error(w, "revision unavailable", 503)
|
|
return
|
|
}
|
|
markdown := rev.Body
|
|
if strings.HasPrefix(rev.ContentType, "text/html") {
|
|
markdown, err = grammaratur.HTMLToMarkdownAuthoring(rev.Body)
|
|
}
|
|
if err != nil {
|
|
http.Error(w, "conversion unavailable", 503)
|
|
return
|
|
}
|
|
out["body"], out["markdown"] = rev.Body, markdown
|
|
kind := revisionContentView(rev, "").Kind
|
|
out["editable"] = rev.UUID == "" || kind == "html" || kind == "code"
|
|
}
|
|
_ = json.NewEncoder(w).Encode(out)
|
|
}
|