1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
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)
}
|