@@ -36,9 +36,6 @@ import (
3636 "strings"
3737)
3838
39- // SummaryLength is the length of the summary that Hugo extracts from a content.
40- var SummaryLength = 70
41-
4239// SummaryDivider denotes where content summarization should end. The default is "<!--more-->".
4340var SummaryDivider = []byte ("<!--more-->" )
4441
@@ -47,6 +44,8 @@ type ContentSpec struct {
4744 blackfriday map [string ]interface {}
4845 footnoteAnchorPrefix string
4946 footnoteReturnLinkContents string
47+ // SummaryLength is the length of the summary that Hugo extracts from a content.
48+ summaryLength int
5049
5150 Highlight func (code , lang , optsStr string ) (string , error )
5251 defatultPygmentsOpts map [string ]string
@@ -61,6 +60,7 @@ func NewContentSpec(cfg config.Provider) (*ContentSpec, error) {
6160 blackfriday : cfg .GetStringMap ("blackfriday" ),
6261 footnoteAnchorPrefix : cfg .GetString ("footnoteAnchorPrefix" ),
6362 footnoteReturnLinkContents : cfg .GetString ("footnoteReturnLinkContents" ),
63+ summaryLength : cfg .GetInt ("summaryLength" ),
6464
6565 cfg : cfg ,
6666 }
@@ -480,20 +480,20 @@ func totalWordsOld(s string) int {
480480}
481481
482482// TruncateWordsByRune truncates words by runes.
483- func TruncateWordsByRune (words []string , max int ) (string , bool ) {
483+ func ( c * ContentSpec ) TruncateWordsByRune (words []string ) (string , bool ) {
484484 count := 0
485485 for index , word := range words {
486- if count >= max {
486+ if count >= c . summaryLength {
487487 return strings .Join (words [:index ], " " ), true
488488 }
489489 runeCount := utf8 .RuneCountInString (word )
490490 if len (word ) == runeCount {
491491 count ++
492- } else if count + runeCount < max {
492+ } else if count + runeCount < c . summaryLength {
493493 count += runeCount
494494 } else {
495495 for ri := range word {
496- if count >= max {
496+ if count >= c . summaryLength {
497497 truncatedWords := append (words [:index ], word [:ri ])
498498 return strings .Join (truncatedWords , " " ), true
499499 }
@@ -507,8 +507,7 @@ func TruncateWordsByRune(words []string, max int) (string, bool) {
507507
508508// TruncateWordsToWholeSentence takes content and truncates to whole sentence
509509// limited by max number of words. It also returns whether it is truncated.
510- func TruncateWordsToWholeSentence (s string , max int ) (string , bool ) {
511-
510+ func (c * ContentSpec ) TruncateWordsToWholeSentence (s string ) (string , bool ) {
512511 var (
513512 wordCount = 0
514513 lastWordIndex = - 1
@@ -519,7 +518,7 @@ func TruncateWordsToWholeSentence(s string, max int) (string, bool) {
519518 wordCount ++
520519 lastWordIndex = i
521520
522- if wordCount >= max {
521+ if wordCount >= c . summaryLength {
523522 break
524523 }
525524
@@ -551,24 +550,24 @@ func isEndOfSentence(r rune) bool {
551550}
552551
553552// Kept only for benchmark.
554- func truncateWordsToWholeSentenceOld (content string , max int ) (string , bool ) {
553+ func ( c * ContentSpec ) truncateWordsToWholeSentenceOld (content string ) (string , bool ) {
555554 words := strings .Fields (content )
556555
557- if max >= len (words ) {
556+ if c . summaryLength >= len (words ) {
558557 return strings .Join (words , " " ), false
559558 }
560559
561- for counter , word := range words [max :] {
560+ for counter , word := range words [c . summaryLength :] {
562561 if strings .HasSuffix (word , "." ) ||
563562 strings .HasSuffix (word , "?" ) ||
564563 strings .HasSuffix (word , ".\" " ) ||
565564 strings .HasSuffix (word , "!" ) {
566- upper := max + counter + 1
565+ upper := c . summaryLength + counter + 1
567566 return strings .Join (words [:upper ], " " ), (upper < len (words ))
568567 }
569568 }
570569
571- return strings .Join (words [:max ], " " ), true
570+ return strings .Join (words [:c . summaryLength ], " " ), true
572571}
573572
574573func getAsciidocExecPath () string {
0 commit comments