mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-05 18:44:58 +03:00
markdown: Change .Url -> .URL, increase summary length
Also, summary truncated at nearest space instead of middle of word, and code spans become part of summary.
This commit is contained in:
parent
0b01489f7d
commit
ff28bc8b0a
2 changed files with 17 additions and 5 deletions
|
@ -1,6 +1,7 @@
|
||||||
package markdown
|
package markdown
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -16,8 +17,8 @@ const (
|
||||||
// Date format YYYY-MM-DD HH:MM:SS
|
// Date format YYYY-MM-DD HH:MM:SS
|
||||||
timeLayout = `2006-01-02 15:04:05`
|
timeLayout = `2006-01-02 15:04:05`
|
||||||
|
|
||||||
// Length of page summary.
|
// Maximum length of page summary.
|
||||||
summaryLen = 150
|
summaryLen = 500
|
||||||
)
|
)
|
||||||
|
|
||||||
// PageLink represents a statically generated markdown page.
|
// PageLink represents a statically generated markdown page.
|
||||||
|
@ -25,7 +26,7 @@ type PageLink struct {
|
||||||
Title string
|
Title string
|
||||||
Summary string
|
Summary string
|
||||||
Date time.Time
|
Date time.Time
|
||||||
Url string
|
URL string
|
||||||
}
|
}
|
||||||
|
|
||||||
// byDate sorts PageLink by newest date to oldest.
|
// byDate sorts PageLink by newest date to oldest.
|
||||||
|
@ -99,15 +100,22 @@ func (l *linkGen) generateLinks(md Markdown, cfg *Config) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// truncate summary to maximum length
|
||||||
if len(summary) > summaryLen {
|
if len(summary) > summaryLen {
|
||||||
summary = summary[:summaryLen]
|
summary = summary[:summaryLen]
|
||||||
|
|
||||||
|
// trim to nearest word
|
||||||
|
lastSpace := bytes.LastIndex(summary, []byte(" "))
|
||||||
|
if lastSpace != -1 {
|
||||||
|
summary = summary[:lastSpace]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
metadata := parser.Metadata()
|
metadata := parser.Metadata()
|
||||||
|
|
||||||
cfg.Links = append(cfg.Links, PageLink{
|
cfg.Links = append(cfg.Links, PageLink{
|
||||||
Title: metadata.Title,
|
Title: metadata.Title,
|
||||||
Url: reqPath,
|
URL: reqPath,
|
||||||
Date: metadata.Date,
|
Date: metadata.Date,
|
||||||
Summary: string(blackfriday.Markdown(summary, PlaintextRenderer{}, 0)),
|
Summary: string(blackfriday.Markdown(summary, PlaintextRenderer{}, 0)),
|
||||||
})
|
})
|
||||||
|
|
|
@ -48,7 +48,11 @@ func (r PlaintextRenderer) TitleBlock(out *bytes.Buffer, text []byte) {}
|
||||||
|
|
||||||
func (r PlaintextRenderer) AutoLink(out *bytes.Buffer, link []byte, kind int) {}
|
func (r PlaintextRenderer) AutoLink(out *bytes.Buffer, link []byte, kind int) {}
|
||||||
|
|
||||||
func (r PlaintextRenderer) CodeSpan(out *bytes.Buffer, text []byte) {}
|
func (r PlaintextRenderer) CodeSpan(out *bytes.Buffer, text []byte) {
|
||||||
|
out.Write([]byte("`"))
|
||||||
|
out.Write(text)
|
||||||
|
out.Write([]byte("`"))
|
||||||
|
}
|
||||||
|
|
||||||
func (r PlaintextRenderer) DoubleEmphasis(out *bytes.Buffer, text []byte) {
|
func (r PlaintextRenderer) DoubleEmphasis(out *bytes.Buffer, text []byte) {
|
||||||
out.Write(text)
|
out.Write(text)
|
||||||
|
|
Loading…
Reference in a new issue