Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/gohugoio/hugo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulien Midedji <Julien.Midedji@gmail.com>2021-05-03 10:10:06 +0300
committerGitHub <noreply@github.com>2021-05-03 10:10:06 +0300
commit7a2c10ae60f096dacee4b44e0c8ae0a1b66ae033 (patch)
tree26bd66b90657a90d1398e7de977132f60e6d7077 /tpl/strings
parente1c328df2590164becc150de842f69292abe557a (diff)
tpl: Fix countwords to handle special chars
Fixes #8479
Diffstat (limited to 'tpl/strings')
-rw-r--r--tpl/strings/strings.go10
-rw-r--r--tpl/strings/strings_test.go3
2 files changed, 13 insertions, 0 deletions
diff --git a/tpl/strings/strings.go b/tpl/strings/strings.go
index b2f02d8f5..ac2defed5 100644
--- a/tpl/strings/strings.go
+++ b/tpl/strings/strings.go
@@ -17,6 +17,7 @@ package strings
import (
"errors"
"html/template"
+ "regexp"
"strings"
"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")
}
+ 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
for _, word := range strings.Fields(helpers.StripHTML(ss)) {
runeCount := utf8.RuneCountInString(word)
diff --git a/tpl/strings/strings_test.go b/tpl/strings/strings_test.go
index f3bb82c63..6e14a408c 100644
--- a/tpl/strings/strings_test.go
+++ b/tpl/strings/strings_test.go
@@ -210,6 +210,9 @@ func TestCountWords(t *testing.T) {
{"Do Be Do Be Do", 5},
{"旁边", 2},
{`<div class="test">旁边</div>`, 2},
+ {"Here's to you...", 3},
+ {"Here’s to you...", 3},
+ {"Here’s to you…", 3},
// errors
{tstNoStringer{}, false},
} {