mirror of
https://github.com/gohugoio/hugo.git
synced 2025-04-20 12:13:05 +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
|
// TruncateWordsToWholeSentence takes content and an int
|
||||||
// and returns entire sentences from content, delimited by the int.
|
// and returns entire sentences from content, delimited by the int.
|
||||||
func TruncateWordsToWholeSentence(s string, max int) string {
|
func TruncateWordsToWholeSentence(words []string, max int) string {
|
||||||
words := strings.Fields(s)
|
|
||||||
if max > len(words) {
|
if max > len(words) {
|
||||||
return strings.Join(words, " ")
|
return strings.Join(words, " ")
|
||||||
}
|
}
|
||||||
|
|
|
@ -65,6 +65,8 @@ type Page struct {
|
||||||
rawContent []byte
|
rawContent []byte
|
||||||
contentShortCodes map[string]string
|
contentShortCodes map[string]string
|
||||||
plain string // TODO should be []byte
|
plain string // TODO should be []byte
|
||||||
|
plainWords []string
|
||||||
|
plainInit sync.Once
|
||||||
renderingConfig *helpers.Blackfriday
|
renderingConfig *helpers.Blackfriday
|
||||||
renderingConfigInit sync.Once
|
renderingConfigInit sync.Once
|
||||||
PageMeta
|
PageMeta
|
||||||
|
@ -97,12 +99,22 @@ type Position struct {
|
||||||
type Pages []*Page
|
type Pages []*Page
|
||||||
|
|
||||||
func (p *Page) Plain() string {
|
func (p *Page) Plain() string {
|
||||||
if len(p.plain) == 0 {
|
p.initPlain()
|
||||||
p.plain = helpers.StripHTML(string(p.Content))
|
|
||||||
}
|
|
||||||
return p.plain
|
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 {
|
func (p *Page) IsNode() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -178,9 +190,11 @@ func (p *Page) setSummary() {
|
||||||
} else {
|
} else {
|
||||||
// If hugo defines split:
|
// If hugo defines split:
|
||||||
// render, strip html, then split
|
// render, strip html, then split
|
||||||
plain := strings.Join(strings.Fields(p.Plain()), " ")
|
p.Summary = helpers.BytesToHTML([]byte(helpers.TruncateWordsToWholeSentence(p.PlainWords(), helpers.SummaryLength)))
|
||||||
p.Summary = helpers.BytesToHTML([]byte(helpers.TruncateWordsToWholeSentence(plain, helpers.SummaryLength)))
|
|
||||||
p.Truncated = len(p.Summary) != len(plain)
|
// 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() {
|
func (p *Page) analyzePage() {
|
||||||
p.WordCount = helpers.TotalWords(p.Plain())
|
p.WordCount = len(p.PlainWords())
|
||||||
p.FuzzyWordCount = int((p.WordCount+100)/100) * 100
|
p.FuzzyWordCount = int((p.WordCount+100)/100) * 100
|
||||||
p.ReadingTime = int((p.WordCount + 212) / 213)
|
p.ReadingTime = int((p.WordCount + 212) / 213)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue