package page import ( "bytes" "errors" "io/fs" "net/http" ) // RenderMarkdown is analogous to Render, except it spits out rendered markdown // as text/plain. It also sets .Vars.RenderingMarkdownOnly so templates can // vary on whether or not they're plain markdown. For instance, not including // some HTML tags. func RenderMarkdown(w http.ResponseWriter, r *http.Request, path string, vars map[string]interface{}, statusCode int) { // Sepcifically use the specified path for the page p := NewPage(path) if vars != nil { p.Vars = vars } if p.Vars != nil { p.Vars["RenderingMarkdownOnly"] = true } else { p.Vars = map[string]interface{}{ "RenderingMarkdownOnly": true, } } buf := &bytes.Buffer{} err := p.Render(buf) if err != nil { if errors.Is(err, fs.ErrNotExist) { plainResp(w, http.StatusNotFound, "Not found") return } logErr(r, "while rendering", err) plainResp(w, http.StatusInternalServerError, "Internal server error") return } // Error was handled above md, _ := p.GetMarkdown() w.Header().Set("Content-type", "text/plain") w.WriteHeader(statusCode) _, err = w.Write([]byte(md)) if err != nil { logErr(r, "while writing buf", err) return } logReq(r, statusCode) }