mirror of
https://github.com/gohugoio/hugo.git
synced 2025-02-17 03:02:51 +00:00
Avoid calling strings.Fields multiple times with same content
This should be a relief for big sites.
This commit is contained in:
parent
11a19e0760
commit
f8704c1bf2
2 changed files with 22 additions and 9 deletions
|
@ -275,8 +275,7 @@ func TruncateWords(s string, max int) string {
|
|||
|
||||
// TruncateWordsToWholeSentence takes content and an int
|
||||
// and returns entire sentences from content, delimited by the int.
|
||||
func TruncateWordsToWholeSentence(s string, max int) string {
|
||||
words := strings.Fields(s)
|
||||
func TruncateWordsToWholeSentence(words []string, max int) string {
|
||||
if max > len(words) {
|
||||
return strings.Join(words, " ")
|
||||
}
|
||||
|
|
|
@ -65,6 +65,8 @@ type Page struct {
|
|||
rawContent []byte
|
||||
contentShortCodes map[string]string
|
||||
plain string // TODO should be []byte
|
||||
plainWords []string
|
||||
plainInit sync.Once
|
||||
renderingConfig *helpers.Blackfriday
|
||||
renderingConfigInit sync.Once
|
||||
PageMeta
|
||||
|
@ -97,12 +99,22 @@ type Position struct {
|
|||
type Pages []*Page
|
||||
|
||||
func (p *Page) Plain() string {
|
||||
if len(p.plain) == 0 {
|
||||
p.plain = helpers.StripHTML(string(p.Content))
|
||||
}
|
||||
p.initPlain()
|
||||
return p.plain
|
||||
}
|
||||
|
||||
func (p *Page) PlainWords() []string {
|
||||
p.initPlain()
|
||||
return p.plainWords
|
||||
}
|
||||
|
||||
func (p *Page) initPlain() {
|
||||
p.plainInit.Do(func() {
|
||||
p.plain = helpers.StripHTML(string(p.Content))
|
||||
p.plainWords = strings.Fields(p.plain)
|
||||
})
|
||||
}
|
||||
|
||||
func (p *Page) IsNode() bool {
|
||||
return false
|
||||
}
|
||||
|
@ -178,9 +190,11 @@ func (p *Page) setSummary() {
|
|||
} else {
|
||||
// If hugo defines split:
|
||||
// render, strip html, then split
|
||||
plain := strings.Join(strings.Fields(p.Plain()), " ")
|
||||
p.Summary = helpers.BytesToHTML([]byte(helpers.TruncateWordsToWholeSentence(plain, helpers.SummaryLength)))
|
||||
p.Truncated = len(p.Summary) != len(plain)
|
||||
p.Summary = helpers.BytesToHTML([]byte(helpers.TruncateWordsToWholeSentence(p.PlainWords(), helpers.SummaryLength)))
|
||||
|
||||
// todo bep - check if the Plain() can be trimmed earlier
|
||||
p.Truncated = len(p.Summary) != len(strings.Trim(p.Plain(), "\n\r "))
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -322,7 +336,7 @@ func (p *Page) ReadFrom(buf io.Reader) (err error) {
|
|||
}
|
||||
|
||||
func (p *Page) analyzePage() {
|
||||
p.WordCount = helpers.TotalWords(p.Plain())
|
||||
p.WordCount = len(p.PlainWords())
|
||||
p.FuzzyWordCount = int((p.WordCount+100)/100) * 100
|
||||
p.ReadingTime = int((p.WordCount + 212) / 213)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue