Skip to content

Commit 5e70992

Browse files
jmooringbep
authored andcommitted
hugolib: Fix ReadingTime and FuzzyWordCount calculations
Closes #15206
1 parent 0805c73 commit 5e70992

2 files changed

Lines changed: 109 additions & 19 deletions

File tree

‎hugolib/page__content.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -807,13 +807,13 @@ func (c *cachedContentScope) contentPlain(ctx context.Context) (contentPlainPlai
807807

808808
// TODO(bep) is set in a test. Fix that.
809809
if result.fuzzyWordCount == 0 {
810-
result.fuzzyWordCount = (result.wordCount + 100) / 100 * 100
810+
result.fuzzyWordCount = (result.wordCount + 99) / 100 * 100
811811
}
812812

813813
if isCJKLanguage {
814-
result.readingTime = (result.wordCount + 500) / 501
814+
result.readingTime = (result.wordCount + 499) / 500
815815
} else {
816-
result.readingTime = (result.wordCount + 212) / 213
816+
result.readingTime = (result.wordCount + 211) / 212
817817
}
818818

819819
rs.Value = result

‎hugolib/page_test.go‎

Lines changed: 106 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,8 @@ baseURL = "http://example.com/"
412412
p := b.H.Sites[0].RegularPages()[0]
413413

414414
b.Assert(p.Summary(context.Background()), qt.Equals, template.HTML(
415-
"<p>The <a href=\"http://gohugo.io/\">best static site generator</a>.<sup id=\"fnref:1\"><a href=\"#fn:1\" class=\"footnote-ref\" role=\"doc-noteref\">1</a></sup></p>"))
415+
"<p>The <a href=\"http://gohugo.io/\">best static site generator</a>.<sup id=\"fnref:1\"><a href=\"#fn:1\" class=\"footnote-ref\" role=\"doc-noteref\">1</a></sup></p>",
416+
))
416417

417418
cnt := content(p)
418419
b.Assert(cnt, qt.Equals, "<p>The <a href=\"http://gohugo.io/\">best static site generator</a>.<sup id=\"fnref:1\"><a href=\"#fn:1\" class=\"footnote-ref\" role=\"doc-noteref\">1</a></sup></p>\n<div class=\"footnotes\" role=\"doc-endnotes\">\n<hr>\n<ol>\n<li id=\"fn:1\">\n<p>Many people say so.&#160;<a href=\"#fnref:1\" class=\"footnote-backref\" role=\"doc-backlink\">&#x21a9;&#xfe0e;</a></p>\n</li>\n</ol>\n</div>")
@@ -730,7 +731,8 @@ This is **content**.
730731
Summary: {{ .Summary }}|Truncated: {{ .Truncated }}|
731732
Content: {{ .Content }}|
732733
733-
`).AssertFileContent("public/simple/index.html",
734+
`).AssertFileContent(
735+
"public/simple/index.html",
734736
"Summary: <p>This is <strong>summary</strong>.</p>|",
735737
"Truncated: true|",
736738
"Content: <p>This is <strong>summary</strong>.</p>\n<p>This is <strong>content</strong>.</p>|",
@@ -1286,13 +1288,15 @@ AllTranslations: {{ range .AllTranslations }}{{ .Language.Lang }}|{{ end }}|
12861288
12871289
`
12881290
b := Test(t, files)
1289-
b.AssertFileContent("public/en/sect/p1/index.html",
1291+
b.AssertFileContent(
1292+
"public/en/sect/p1/index.html",
12901293
"TranslationKey: adfasdf|",
12911294
"AllTranslations: en|nn||",
12921295
"Translations: nn||",
12931296
)
12941297

1295-
b.AssertFileContent("public/nn/sect/p1/index.html",
1298+
b.AssertFileContent(
1299+
"public/nn/sect/p1/index.html",
12961300
"TranslationKey: adfasdf|",
12971301
"Translations: en||",
12981302
"AllTranslations: en|nn||",
@@ -1378,12 +1382,14 @@ Resources: {{ range .Resources }}{{ .RelPermalink }}|{{ .Content }}|{{ end }}|
13781382
13791383
`
13801384
b := Test(t, files)
1381-
b.AssertFileContent("public/en/sect/mybundle_en/index.html",
1385+
b.AssertFileContent(
1386+
"public/en/sect/mybundle_en/index.html",
13821387
"TranslationKey: adfasdf|",
13831388
"Resources: /en/sect/mybundle_en/f1.txt|f1.en|/en/sect/mybundle_en/f2.txt|f2.en||",
13841389
)
13851390

1386-
b.AssertFileContent("public/nn/sect/mybundle_nn/index.html",
1391+
b.AssertFileContent(
1392+
"public/nn/sect/mybundle_nn/index.html",
13871393
"TranslationKey: adfasdf|",
13881394
"Title: mybundle nn|TranslationKey: adfasdf|\nResources: /en/sect/mybundle_en/f1.txt|f1.en|/nn/sect/mybundle_nn/f2.nn.txt|f2.nn||",
13891395
)
@@ -1497,31 +1503,37 @@ CONTENT:{{ .Content }}
14971503
`
14981504
b := Test(t, files)
14991505

1500-
b.AssertFileContent("public/page-md-shortcode/index.html",
1506+
b.AssertFileContent(
1507+
"public/page-md-shortcode/index.html",
15011508
"SUMMARY:<p>This is a a shortcode.</p>:END",
15021509
"CONTENT:<p>This is a a shortcode.</p>\n\n<p>Content.</p>\n",
15031510
)
15041511

1505-
b.AssertFileContent("public/page-md-shortcode-same-line/index.html",
1512+
b.AssertFileContent(
1513+
"public/page-md-shortcode-same-line/index.html",
15061514
"SUMMARY:<p>This is a a shortcode</p>:END",
15071515
"CONTENT:<p>This is a a shortcode</p>\n\n<p>Same line.</p>\n",
15081516
)
15091517

1510-
b.AssertFileContent("public/page-md-shortcode-same-line-after/index.html",
1518+
b.AssertFileContent(
1519+
"public/page-md-shortcode-same-line-after/index.html",
15111520
"SUMMARY:<p>Summary</p>:END",
15121521
"CONTENT:<p>Summary</p>\n\na shortcode",
15131522
)
15141523

1515-
b.AssertFileContent("public/page-org-shortcode/index.html",
1524+
b.AssertFileContent(
1525+
"public/page-org-shortcode/index.html",
15161526
"SUMMARY:<p>\nThis is a a shortcode.\n</p>:END",
15171527
"CONTENT:<p>\nThis is a a shortcode.\n</p>\n<p>\nContent.\t\n</p>\n",
15181528
)
1519-
b.AssertFileContent("public/page-org-variant1/index.html",
1529+
b.AssertFileContent(
1530+
"public/page-org-variant1/index.html",
15201531
"SUMMARY:<p>\nSummary.\n</p>:END",
15211532
"CONTENT:<p>\nSummary.\n</p>\n<p>\nContent.\t\n</p>\n",
15221533
)
15231534

1524-
b.AssertFileContent("public/page-md-only-shortcode/index.html",
1535+
b.AssertFileContent(
1536+
"public/page-md-only-shortcode/index.html",
15251537
"SUMMARY:a shortcode:END",
15261538
"CONTENT:a shortcode\n\na shortcode\n",
15271539
)
@@ -1731,7 +1743,8 @@ c: {{ .Scratch.Get "c" }}
17311743

17321744
b := Test(t, files)
17331745

1734-
b.AssertFileContent("public/index.html",
1746+
b.AssertFileContent(
1747+
"public/index.html",
17351748
".Scratch eq .Store: true",
17361749
"a: b",
17371750
"c: d",
@@ -1928,7 +1941,8 @@ Site: {{ site.Store.Get "Site" }}|
19281941

19291942
b := TestRunning(t, files)
19301943

1931-
b.AssertFileContent("public/index.html",
1944+
b.AssertFileContent(
1945+
"public/index.html",
19321946
`
19331947
Shortcode: sh-Home|
19341948
Page: p-Home|
@@ -1939,7 +1953,8 @@ Hugo: h-Home|
19391953

19401954
b.EditFileReplaceAll("content/_index.md", "Home", "Homer").Build()
19411955

1942-
b.AssertFileContent("public/index.html",
1956+
b.AssertFileContent(
1957+
"public/index.html",
19431958
`
19441959
Shortcode: sh-Homer|
19451960
Page: p-Homer|
@@ -2149,7 +2164,8 @@ EF
21492164

21502165
b := Test(t, files)
21512166

2152-
b.AssertFileContent("public/s1/p1/index.html",
2167+
b.AssertFileContent(
2168+
"public/s1/p1/index.html",
21532169
"ab|ab|ab",
21542170
"cD|cD|cD",
21552171
"EF|EF|EF",
@@ -2368,3 +2384,77 @@ All.
23682384
myothersection, _ := s.GetPage("myothersection") // backed by a content file.
23692385
check(myothersection, true)
23702386
}
2387+
2388+
// See issue 15206.
2389+
func TestReadingTimeAndFuzzyWordCountBoundaries(t *testing.T) {
2390+
t.Parallel()
2391+
2392+
files := `
2393+
-- hugo.toml --
2394+
disableKinds = ["home", "section", "taxonomy", "term", "rss", "sitemap"]
2395+
-- content/p99.md --
2396+
---
2397+
title: p99
2398+
---
2399+
` + strings.Repeat("word ", 99) + `
2400+
-- content/p100.md --
2401+
---
2402+
title: p100
2403+
---
2404+
` + strings.Repeat("word ", 100) + `
2405+
-- content/p101.md --
2406+
---
2407+
title: p101
2408+
---
2409+
` + strings.Repeat("word ", 101) + `
2410+
-- content/p211.md --
2411+
---
2412+
title: p211
2413+
---
2414+
` + strings.Repeat("word ", 211) + `
2415+
-- content/p212.md --
2416+
---
2417+
title: p212
2418+
---
2419+
` + strings.Repeat("word ", 212) + `
2420+
-- content/p213.md --
2421+
---
2422+
title: p213
2423+
---
2424+
` + strings.Repeat("word ", 213) + `
2425+
-- content/p499.md --
2426+
---
2427+
title: p499
2428+
isCJKLanguage: true
2429+
---
2430+
` + strings.Repeat("你", 499) + `
2431+
-- content/p500.md --
2432+
---
2433+
title: p500
2434+
isCJKLanguage: true
2435+
---
2436+
` + strings.Repeat("你", 500) + `
2437+
-- content/p501.md --
2438+
---
2439+
title: p501
2440+
isCJKLanguage: true
2441+
---
2442+
` + strings.Repeat("你", 501) + `
2443+
-- layouts/page.html --
2444+
{{ .WordCount }}|{{ .FuzzyWordCount }}|{{ .ReadingTime }}
2445+
`
2446+
2447+
b := Test(t, files)
2448+
2449+
b.AssertFileContent("public/p99/index.html", "99|100|1")
2450+
b.AssertFileContent("public/p100/index.html", "100|100|1")
2451+
b.AssertFileContent("public/p101/index.html", "101|200|1")
2452+
2453+
b.AssertFileContent("public/p211/index.html", "211|300|1")
2454+
b.AssertFileContent("public/p212/index.html", "212|300|1")
2455+
b.AssertFileContent("public/p213/index.html", "213|300|2")
2456+
2457+
b.AssertFileContent("public/p499/index.html", "499|500|1")
2458+
b.AssertFileContent("public/p500/index.html", "500|500|1")
2459+
b.AssertFileContent("public/p501/index.html", "501|600|2")
2460+
}

0 commit comments

Comments
 (0)