aboutsummaryrefslogtreecommitdiffhomepage
path: root/resources/page/page_markup.go
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <[email protected]>2024-09-10 08:54:03 +0200
committerBjørn Erik Pedersen <[email protected]>2024-09-10 11:03:47 +0200
commit3d6baedaec306300f2c6f7ed471e774dca0f112a (patch)
tree4a7b7f62c337aceb9983f8a0490b7e153a7b3d23 /resources/page/page_markup.go
parent84ee00bbc24328295237695a39e6e876ed186312 (diff)
downloadhugo-3d6baedaec306300f2c6f7ed471e774dca0f112a.tar.gz
hugo-3d6baedaec306300f2c6f7ed471e774dca0f112a.zip
Don't count HTML markup in auto summaries
This commit also fixes a bug where a `</picture>` end tag was wrongly used to detect a end paragraph. This should be very rare, though. Closes #12837
Diffstat (limited to 'resources/page/page_markup.go')
-rw-r--r--resources/page/page_markup.go20
1 files changed, 19 insertions, 1 deletions
diff --git a/resources/page/page_markup.go b/resources/page/page_markup.go
index ef4a56e3a..44980e8b0 100644
--- a/resources/page/page_markup.go
+++ b/resources/page/page_markup.go
@@ -161,6 +161,16 @@ func (s *HtmlSummary) resolveParagraphTagAndSetWrapper(mt media.Type) tagReStart
return ptag
}
+// Avoid counting words that are most likely HTML tokens.
+var (
+ isProbablyHTMLTag = regexp.MustCompile(`^<\/?[A-Za-z]+>?$`)
+ isProablyHTMLAttribute = regexp.MustCompile(`^[A-Za-z]+=["']`)
+)
+
+func isProbablyHTMLToken(s string) bool {
+ return s == ">" || isProbablyHTMLTag.MatchString(s) || isProablyHTMLAttribute.MatchString(s)
+}
+
// ExtractSummaryFromHTML extracts a summary from the given HTML content.
func ExtractSummaryFromHTML(mt media.Type, input string, numWords int, isCJK bool) (result HtmlSummary) {
result.source = input
@@ -173,6 +183,14 @@ func ExtractSummaryFromHTML(mt media.Type, input string, numWords int, isCJK boo
var count int
countWord := func(word string) int {
+ word = strings.TrimSpace(word)
+ if len(word) == 0 {
+ return 0
+ }
+ if isProbablyHTMLToken(word) {
+ return 0
+ }
+
if isCJK {
word = tpl.StripHTML(word)
runeCount := utf8.RuneCountInString(word)
@@ -193,7 +211,7 @@ func ExtractSummaryFromHTML(mt media.Type, input string, numWords int, isCJK boo
for j := result.WrapperStart.High; j < high; {
s := input[j:]
- closingIndex := strings.Index(s, "</"+ptag.tagName)
+ closingIndex := strings.Index(s, "</"+ptag.tagName+">")
if closingIndex == -1 {
break