2019-05-23 15:41:43 -05:00
|
|
|
package markdown
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"gopkg.in/russross/blackfriday.v2"
|
|
|
|
|
2019-06-14 12:58:28 -05:00
|
|
|
"github.com/caddyserver/caddy"
|
|
|
|
"github.com/caddyserver/caddy/modules/caddyhttp"
|
2019-05-23 15:41:43 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-06-14 12:58:28 -05:00
|
|
|
caddy.RegisterModule(caddy.Module{
|
2019-05-23 15:41:43 -05:00
|
|
|
Name: "http.middleware.markdown",
|
|
|
|
New: func() interface{} { return new(Markdown) },
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Markdown is a middleware for rendering a Markdown response body.
|
|
|
|
type Markdown struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
|
|
|
|
mrw := &markdownResponseWriter{
|
|
|
|
ResponseWriterWrapper: &caddyhttp.ResponseWriterWrapper{ResponseWriter: w},
|
|
|
|
}
|
|
|
|
return next.ServeHTTP(mrw, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
type markdownResponseWriter struct {
|
|
|
|
*caddyhttp.ResponseWriterWrapper
|
|
|
|
statusCode int
|
|
|
|
wroteHeader bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mrw *markdownResponseWriter) WriteHeader(code int) {
|
|
|
|
mrw.statusCode = code
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mrw *markdownResponseWriter) Write(d []byte) (int, error) {
|
|
|
|
output := blackfriday.Run(d)
|
|
|
|
if !mrw.wroteHeader {
|
|
|
|
mrw.Header().Set("Content-Length", strconv.Itoa(len(output)))
|
|
|
|
mrw.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
|
mrw.WriteHeader(mrw.statusCode)
|
|
|
|
mrw.wroteHeader = true
|
|
|
|
}
|
|
|
|
return mrw.ResponseWriter.Write(output)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interface guard
|
|
|
|
var _ caddyhttp.MiddlewareHandler = (*Markdown)(nil)
|