mirror of
https://github.com/caddyserver/caddy.git
synced 2025-02-25 01:09:04 +01:00
Markdown handles titles a little better
This commit is contained in:
parent
abdadf1ee1
commit
1b7415a81b
1 changed files with 19 additions and 1 deletions
|
@ -3,6 +3,7 @@
|
||||||
package markdown
|
package markdown
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
"path"
|
||||||
|
@ -75,18 +76,35 @@ func (m Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
scripts += strings.Replace(jsTemplate, "{{url}}", script, 1) + "\r\n"
|
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 := 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, "{{css}}", styles, 1)
|
||||||
html = strings.Replace(html, "{{js}}", scripts, 1)
|
html = strings.Replace(html, "{{js}}", scripts, 1)
|
||||||
html = strings.Replace(html, "{{body}}", string(content), 1)
|
html = strings.Replace(html, "{{body}}", string(content), 1)
|
||||||
|
|
||||||
w.Write([]byte(html))
|
w.Write([]byte(html))
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Didn't qualify to serve as markdown; pass-thru
|
||||||
m.Next(w, r)
|
m.Next(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue