diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2020-03-20 09:37:21 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2020-03-20 18:28:55 +0100 |
commit | 99958f90fedec11d749a1397300860aa8e8459c2 (patch) | |
tree | 902f114f6ec14694311fddb416375794d33c3a92 /resources/page | |
parent | 1d91d8e14b13bd135dc4d4a901fc936c9649b219 (diff) |
Allow headless bundles to list pages via $page.Pages and $page.RegularPages
Fixes #7075
Diffstat (limited to 'resources/page')
-rw-r--r-- | resources/page/pagemeta/pagemeta.go | 36 | ||||
-rw-r--r-- | resources/page/pagemeta/pagemeta_test.go | 64 |
2 files changed, 96 insertions, 4 deletions
diff --git a/resources/page/pagemeta/pagemeta.go b/resources/page/pagemeta/pagemeta.go index 1f8afdc18..632f46df7 100644 --- a/resources/page/pagemeta/pagemeta.go +++ b/resources/page/pagemeta/pagemeta.go @@ -24,8 +24,14 @@ type URLPath struct { Section string } +const ( + Never = "never" + Always = "always" + ListLocally = "local" +) + var defaultBuildConfig = BuildConfig{ - List: true, + List: Always, Render: true, PublishResources: true, set: true, @@ -35,8 +41,12 @@ var defaultBuildConfig = BuildConfig{ // build process. type BuildConfig struct { // Whether to add it to any of the page collections. - // Note that the page can still be found with .Site.GetPage. - List bool + // Note that the page can always be found with .Site.GetPage. + // Valid values: never, always, local. + // Setting it to 'local' means they will be available via the local + // page collections, e.g. $section.Pages. + // Note: before 0.57.2 this was a bool, so we accept those too. + List string // Whether to render it. Render bool @@ -51,7 +61,7 @@ type BuildConfig struct { // Disable sets all options to their off value. func (b *BuildConfig) Disable() { - b.List = false + b.List = Never b.Render = false b.PublishResources = false b.set = true @@ -61,11 +71,29 @@ func (b BuildConfig) IsZero() bool { return !b.set } +func (b *BuildConfig) ShouldList() bool { + return b.List == Always || b.List == ListLocally +} + func DecodeBuildConfig(m interface{}) (BuildConfig, error) { b := defaultBuildConfig if m == nil { return b, nil } + err := mapstructure.WeakDecode(m, &b) + + // In 0.67.1 we changed the list attribute from a bool to a string (enum). + // Bool values will become 0 or 1. + switch b.List { + case "0": + b.List = Never + case "1": + b.List = Always + case Always, Never, ListLocally: + default: + b.List = Always + } + return b, err } diff --git a/resources/page/pagemeta/pagemeta_test.go b/resources/page/pagemeta/pagemeta_test.go new file mode 100644 index 000000000..a66a1f432 --- /dev/null +++ b/resources/page/pagemeta/pagemeta_test.go @@ -0,0 +1,64 @@ +// Copyright 2020 The Hugo Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package pagemeta + +import ( + "fmt" + "testing" + + "github.com/gohugoio/hugo/htesting/hqt" + + "github.com/gohugoio/hugo/config" + + qt "github.com/frankban/quicktest" +) + +func TestDecodeBuildConfig(t *testing.T) { + t.Parallel() + + c := qt.New(t) + + configTempl := ` +[_build] +render = true +list = %s +publishResources = true` + + for _, test := range []struct { + list interface{} + expect string + }{ + {"true", Always}, + {"false", Never}, + {`"always"`, Always}, + {`"local"`, ListLocally}, + {`"asdfadf"`, Always}, + } { + cfg, err := config.FromConfigString(fmt.Sprintf(configTempl, test.list), "toml") + c.Assert(err, qt.IsNil) + bcfg, err := DecodeBuildConfig(cfg.Get("_build")) + c.Assert(err, qt.IsNil) + + eq := qt.CmpEquals(hqt.DeepAllowUnexported(BuildConfig{})) + + c.Assert(bcfg, eq, BuildConfig{ + Render: true, + List: test.expect, + PublishResources: true, + set: true, + }) + + } + +} |