summaryrefslogtreecommitdiff
path: root/modules/caddyhttp/markdown/markdown.go
diff options
context:
space:
mode:
Diffstat (limited to 'modules/caddyhttp/markdown/markdown.go')
-rw-r--r--modules/caddyhttp/markdown/markdown.go14
1 files changed, 11 insertions, 3 deletions
diff --git a/modules/caddyhttp/markdown/markdown.go b/modules/caddyhttp/markdown/markdown.go
index 574039e..b2292a4 100644
--- a/modules/caddyhttp/markdown/markdown.go
+++ b/modules/caddyhttp/markdown/markdown.go
@@ -4,6 +4,7 @@ import (
"bytes"
"net/http"
"strconv"
+ "strings"
"sync"
"gopkg.in/russross/blackfriday.v2"
@@ -28,12 +29,19 @@ func (m Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyht
buf.Reset()
defer bufPool.Put(buf)
- rr := caddyhttp.NewResponseRecorder(w, buf)
+ shouldBuf := func(status int) bool {
+ return strings.HasPrefix(w.Header().Get("Content-Type"), "text/")
+ }
+
+ rec := caddyhttp.NewResponseRecorder(w, buf, shouldBuf)
- err := next.ServeHTTP(rr, r)
+ err := next.ServeHTTP(rec, r)
if err != nil {
return err
}
+ if !rec.Buffered() {
+ return nil
+ }
output := blackfriday.Run(buf.Bytes())
@@ -43,7 +51,7 @@ func (m Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyht
w.Header().Del("Etag") // don't know a way to quickly generate etag for dynamic content
w.Header().Del("Last-Modified") // useless for dynamic content since it's always changing
- w.WriteHeader(rr.Status())
+ w.WriteHeader(rec.Status())
w.Write(output)
return nil