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.expandMacrosForRequest(r, html, path, s.domainUUIDForRequest(r)) } 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), ) }