2019-06-30 17:07:58 -05:00
|
|
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-05-23 15:41:43 -05:00
|
|
|
package markdown
|
|
|
|
|
|
|
|
import (
|
2019-06-20 22:49:45 -05:00
|
|
|
"bytes"
|
2019-05-23 15:41:43 -05:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2019-06-21 15:36:26 -05:00
|
|
|
"strings"
|
2019-06-20 22:49:45 -05:00
|
|
|
"sync"
|
2019-05-23 15:41:43 -05:00
|
|
|
|
2019-07-15 18:33:08 -05:00
|
|
|
"github.com/russross/blackfriday/v2"
|
2019-05-23 15:41:43 -05:00
|
|
|
|
2019-07-02 13:37:06 -05:00
|
|
|
"github.com/caddyserver/caddy/v2"
|
|
|
|
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
2019-05-23 15:41:43 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-08-21 11:46:35 -05:00
|
|
|
caddy.RegisterModule(Markdown{})
|
2019-05-23 15:41:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Markdown is a middleware for rendering a Markdown response body.
|
|
|
|
type Markdown struct {
|
|
|
|
}
|
|
|
|
|
2019-08-21 11:46:35 -05:00
|
|
|
// CaddyModule returns the Caddy module information.
|
|
|
|
func (Markdown) CaddyModule() caddy.ModuleInfo {
|
|
|
|
return caddy.ModuleInfo{
|
|
|
|
Name: "http.handlers.markdown",
|
|
|
|
New: func() caddy.Module { return new(Markdown) },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-23 15:41:43 -05:00
|
|
|
func (m Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
|
2019-06-20 22:49:45 -05:00
|
|
|
buf := bufPool.Get().(*bytes.Buffer)
|
|
|
|
buf.Reset()
|
|
|
|
defer bufPool.Put(buf)
|
|
|
|
|
2019-06-21 15:36:26 -05:00
|
|
|
shouldBuf := func(status int) bool {
|
|
|
|
return strings.HasPrefix(w.Header().Get("Content-Type"), "text/")
|
|
|
|
}
|
|
|
|
|
|
|
|
rec := caddyhttp.NewResponseRecorder(w, buf, shouldBuf)
|
2019-06-20 22:49:45 -05:00
|
|
|
|
2019-06-21 15:36:26 -05:00
|
|
|
err := next.ServeHTTP(rec, r)
|
2019-06-20 22:49:45 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-05-23 15:41:43 -05:00
|
|
|
}
|
2019-06-21 15:36:26 -05:00
|
|
|
if !rec.Buffered() {
|
|
|
|
return nil
|
|
|
|
}
|
2019-05-23 15:41:43 -05:00
|
|
|
|
2019-06-20 22:49:45 -05:00
|
|
|
output := blackfriday.Run(buf.Bytes())
|
2019-05-23 15:41:43 -05:00
|
|
|
|
2019-06-20 22:49:45 -05:00
|
|
|
w.Header().Set("Content-Length", strconv.Itoa(len(output)))
|
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
|
w.Header().Del("Accept-Ranges") // we don't know ranges for dynamically-created content
|
|
|
|
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
|
|
|
|
|
2019-06-21 15:36:26 -05:00
|
|
|
w.WriteHeader(rec.Status())
|
2019-06-20 22:49:45 -05:00
|
|
|
w.Write(output)
|
|
|
|
|
|
|
|
return nil
|
2019-05-23 15:41:43 -05:00
|
|
|
}
|
|
|
|
|
2019-06-20 22:49:45 -05:00
|
|
|
var bufPool = sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return new(bytes.Buffer)
|
|
|
|
},
|
2019-05-23 15:41:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Interface guard
|
|
|
|
var _ caddyhttp.MiddlewareHandler = (*Markdown)(nil)
|