diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2022-02-22 14:42:33 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2022-02-23 10:02:16 +0100 |
commit | e97d3c640fc5e845135c5f8f49ff11e23cffddeb (patch) | |
tree | 3aba294bd72363dcafd9ef6bad8ce99ad97289ec /hugolib | |
parent | 7732da9f93503c1a723d6ac5bb77da206cb0fa0e (diff) |
Add page.Store
Fixes #9546
Diffstat (limited to 'hugolib')
-rw-r--r-- | hugolib/page__common.go | 7 | ||||
-rw-r--r-- | hugolib/page__new.go | 1 | ||||
-rw-r--r-- | hugolib/page_test.go | 46 |
3 files changed, 53 insertions, 1 deletions
diff --git a/hugolib/page__common.go b/hugolib/page__common.go index bf11ae7d6..0294393e4 100644 --- a/hugolib/page__common.go +++ b/hugolib/page__common.go @@ -60,6 +60,9 @@ type pageCommon struct { // Lazily initialized dependencies. init *lazy.Init + // Store holds state that survives server rebuilds. + store *maps.Scratch + // All of these represents the common parts of a page.Page maps.Scratcher navigation.PageMenusProvider @@ -134,6 +137,10 @@ type pageCommon struct { forceRender bool } +func (p *pageCommon) Store() *maps.Scratch { + return p.store +} + type pagePages struct { pagesInit sync.Once pages page.Pages diff --git a/hugolib/page__new.go b/hugolib/page__new.go index 5efebe4f5..918477843 100644 --- a/hugolib/page__new.go +++ b/hugolib/page__new.go @@ -41,6 +41,7 @@ func newPageBase(metaProvider *pageMeta) (*pageState, error) { FileProvider: metaProvider, AuthorProvider: metaProvider, Scratcher: maps.NewScratcher(), + store: maps.NewScratch(), Positioner: page.NopPage, InSectionPositioner: page.NopPage, ResourceMetaProvider: metaProvider, diff --git a/hugolib/page_test.go b/hugolib/page_test.go index 6b35e4814..1edef622b 100644 --- a/hugolib/page_test.go +++ b/hugolib/page_test.go @@ -1769,7 +1769,7 @@ Summary: In Chinese, 好 means good. b.AssertFileContent("public/p6/index.html", "WordCount: 7\nFuzzyWordCount: 100\nReadingTime: 1\nLen Plain: 638\nLen PlainWords: 7\nTruncated: false\nLen Summary: 637\nLen Content: 652") } -func TestScratchSite(t *testing.T) { +func TestScratch(t *testing.T) { t.Parallel() b := newTestSitesBuilder(t) @@ -1796,6 +1796,50 @@ title: Scratch Me! b.AssertFileContent("public/scratchme/index.html", "C: cv") } +func TestScratchRebuild(t *testing.T) { + t.Parallel() + + files := ` +-- config.toml -- +-- content/p1.md -- +--- +title: "p1" +--- +{{< scratchme >}} +-- layouts/shortcodes/foo.html -- +notused +-- layouts/shortcodes/scratchme.html -- +{{ .Page.Scratch.Set "scratch" "foo" }} +{{ .Page.Store.Set "scratch" "bar" }} +-- layouts/_default/single.html -- +{{ .Content }} +Scratch: {{ .Scratch.Get "scratch" }}| +Store: {{ .Store.Get "scratch" }}| +` + + b := NewIntegrationTestBuilder( + IntegrationTestConfig{ + T: t, + TxtarString: files, + Running: true, + }, + ).Build() + + b.AssertFileContent("public/p1/index.html", ` +Scratch: foo| +Store: bar| + `) + + b.EditFiles("layouts/shortcodes/foo.html", "edit") + + b.Build() + + b.AssertFileContent("public/p1/index.html", ` +Scratch: | +Store: bar| + `) +} + func TestPageParam(t *testing.T) { t.Parallel() |