mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
parent
e1c328df25
commit
7a2c10ae60
2 changed files with 13 additions and 0 deletions
|
@ -17,6 +17,7 @@ package strings
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"html/template"
|
"html/template"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
|
||||||
|
@ -75,6 +76,15 @@ func (ns *Namespace) CountWords(s interface{}) (int, error) {
|
||||||
return 0, _errors.Wrap(err, "Failed to convert content to string")
|
return 0, _errors.Wrap(err, "Failed to convert content to string")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
isCJKLanguage, err := regexp.MatchString(`\p{Han}|\p{Hangul}|\p{Hiragana}|\p{Katakana}`, ss)
|
||||||
|
if err != nil {
|
||||||
|
return 0, _errors.Wrap(err, "Failed to match regex pattern against string")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !isCJKLanguage {
|
||||||
|
return len(strings.Fields(helpers.StripHTML((ss)))), nil
|
||||||
|
}
|
||||||
|
|
||||||
counter := 0
|
counter := 0
|
||||||
for _, word := range strings.Fields(helpers.StripHTML(ss)) {
|
for _, word := range strings.Fields(helpers.StripHTML(ss)) {
|
||||||
runeCount := utf8.RuneCountInString(word)
|
runeCount := utf8.RuneCountInString(word)
|
||||||
|
|
|
@ -210,6 +210,9 @@ func TestCountWords(t *testing.T) {
|
||||||
{"Do Be Do Be Do", 5},
|
{"Do Be Do Be Do", 5},
|
||||||
{"旁边", 2},
|
{"旁边", 2},
|
||||||
{`<div class="test">旁边</div>`, 2},
|
{`<div class="test">旁边</div>`, 2},
|
||||||
|
{"Here's to you...", 3},
|
||||||
|
{"Here’s to you...", 3},
|
||||||
|
{"Here’s to you…", 3},
|
||||||
// errors
|
// errors
|
||||||
{tstNoStringer{}, false},
|
{tstNoStringer{}, false},
|
||||||
} {
|
} {
|
||||||
|
|
Loading…
Reference in a new issue