0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-23 22:27:38 -05:00
caddy/middleware/markdown/markdown.go

139 lines
3.1 KiB
Go
Raw Normal View History

2015-03-16 12:45:51 -05:00
// Package markdown is middleware to render markdown files as HTML
// on-the-fly.
package markdown
import (
"bytes"
2015-03-16 12:45:51 -05:00
"io/ioutil"
"net/http"
"os"
2015-03-16 12:45:51 -05:00
"path"
"strings"
"github.com/mholt/caddy/middleware"
"github.com/russross/blackfriday"
)
// Markdown implements a layer of middleware that serves
// markdown as HTML.
2015-03-16 12:45:51 -05:00
type Markdown struct {
// Server root
Root string
// Jail the requests to site root with a mock file system
FileSys http.FileSystem
2015-03-16 12:45:51 -05:00
// Next HTTP handler in the chain
Next middleware.Handler
2015-03-16 12:45:51 -05:00
// The list of markdown configurations
2015-05-04 12:49:49 -05:00
Configs []Config
// The list of index files to try
IndexFiles []string
}
2015-05-04 12:49:49 -05:00
// Config stores markdown middleware configurations.
type Config struct {
2015-03-16 12:45:51 -05:00
// Markdown renderer
Renderer blackfriday.Renderer
// Base path to match
PathScope string
// List of extensions to consider as markdown files
Extensions []string
// List of style sheets to load for each markdown file
Styles []string
// List of JavaScript files to load for each markdown file
Scripts []string
}
// ServeHTTP implements the http.Handler interface.
func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
for _, m := range md.Configs {
if !middleware.Path(r.URL.Path).Matches(m.PathScope) {
continue
}
fpath := r.URL.Path
if idx, ok := middleware.IndexFile(md.FileSys, fpath, md.IndexFiles); ok {
fpath = idx
}
2015-03-16 12:45:51 -05:00
for _, ext := range m.Extensions {
if strings.HasSuffix(fpath, ext) {
f, err := md.FileSys.Open(fpath)
2015-03-16 12:45:51 -05:00
if err != nil {
if os.IsPermission(err) {
return http.StatusForbidden, err
}
return http.StatusNotFound, nil
2015-03-16 12:45:51 -05:00
}
body, err := ioutil.ReadAll(f)
if err != nil {
return http.StatusInternalServerError, err
}
2015-03-16 12:45:51 -05:00
content := blackfriday.Markdown(body, m.Renderer, 0)
var scripts, styles string
for _, style := range m.Styles {
styles += strings.Replace(cssTemplate, "{{url}}", style, 1) + "\r\n"
}
for _, script := range m.Scripts {
scripts += strings.Replace(jsTemplate, "{{url}}", script, 1) + "\r\n"
}
// Title is first line (length-limited), otherwise filename
title := path.Base(fpath)
newline := bytes.Index(body, []byte("\n"))
if newline > -1 {
firstline := body[:newline]
newTitle := strings.TrimSpace(string(firstline))
if len(newTitle) > 1 {
if len(newTitle) > 128 {
title = newTitle[:128]
} else {
title = newTitle
}
}
}
2015-03-16 12:45:51 -05:00
html := htmlTemplate
html = strings.Replace(html, "{{title}}", title, 1)
2015-03-16 12:45:51 -05:00
html = strings.Replace(html, "{{css}}", styles, 1)
html = strings.Replace(html, "{{js}}", scripts, 1)
html = strings.Replace(html, "{{body}}", string(content), 1)
w.Write([]byte(html))
return http.StatusOK, nil
2015-03-16 12:45:51 -05:00
}
}
}
// Didn't qualify to serve as markdown; pass-thru
return md.Next.ServeHTTP(w, r)
2015-03-16 12:45:51 -05:00
}
const (
htmlTemplate = `<!DOCTYPE html>
<html>
<head>
<title>{{title}}</title>
<meta charset="utf-8">
{{css}}
{{js}}
</head>
<body>
{{body}}
</body>
</html>`
cssTemplate = `<link rel="stylesheet" href="{{url}}">`
jsTemplate = `<script src="{{url}}"></script>`
)