From 1b7415a81b5fc946344ff736d367cc2e0c6f139f Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Fri, 20 Mar 2015 00:01:39 -0600 Subject: [PATCH] Markdown handles titles a little better --- middleware/markdown/markdown.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/middleware/markdown/markdown.go b/middleware/markdown/markdown.go index 56361cfb5..30504c882 100644 --- a/middleware/markdown/markdown.go +++ b/middleware/markdown/markdown.go @@ -3,6 +3,7 @@ package markdown import ( + "bytes" "io/ioutil" "net/http" "path" @@ -75,18 +76,35 @@ func (m Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) { 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 + } + } + } + html := htmlTemplate - html = strings.Replace(html, "{{title}}", path.Base(fpath), 1) + html = strings.Replace(html, "{{title}}", title, 1) 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 } } } + // Didn't qualify to serve as markdown; pass-thru m.Next(w, r) }