From 64511397565c518a653fc6aa8d475b95410f24e1 Mon Sep 17 00:00:00 2001 From: Oliver Steele Date: Sun, 27 Aug 2017 14:18:57 -0400 Subject: [PATCH] Posts obey config.permalink --- collection/collection.go | 2 +- collection/collection_test.go | 2 +- collection/read.go | 11 +++++------ collection/strategies.go | 10 +++++++--- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/collection/collection.go b/collection/collection.go index 7ba2c1a..4c6ca75 100644 --- a/collection/collection.go +++ b/collection/collection.go @@ -93,6 +93,6 @@ func (c *Collection) ToLiquid() interface{} { // PermalinkPattern returns the default permalink pattern for this collection. func (c *Collection) PermalinkPattern() string { - pattern := c.strategy().defaultPermalinkPattern() + pattern := c.strategy().defaultPermalinkPattern(c.cfg) return templates.VariableMap(c.Metadata).String("permalink", pattern) } diff --git a/collection/collection_test.go b/collection/collection_test.go index 4b22799..4259c2c 100644 --- a/collection/collection_test.go +++ b/collection/collection_test.go @@ -36,7 +36,7 @@ func TestPermalinkPattern(t *testing.T) { require.Equal(t, "out", c2.PermalinkPattern()) c3 := New(site, "posts", map[string]interface{}{}) - require.Contains(t, c3.PermalinkPattern(), "/:year/:month/:day/:title") + require.Contains(t, c3.PermalinkPattern(), "date") } func Test_ReadPages(t *testing.T) { diff --git a/collection/read.go b/collection/read.go index edbfa0d..737f714 100644 --- a/collection/read.go +++ b/collection/read.go @@ -68,8 +68,8 @@ func (c *Collection) scanDirectory(dirname string) error { }) } -func (c *Collection) readPost(abs string, rel string) error { - siteRel := utils.MustRel(c.cfg.Source, abs) +func (c *Collection) readPost(path string, rel string) error { + siteRel := utils.MustRel(c.cfg.Source, path) strategy := c.strategy() switch { case !strategy.isCollectible(rel): @@ -77,13 +77,12 @@ func (c *Collection) readPost(abs string, rel string) error { case strategy.isFuture(rel) && !c.cfg.Future: return nil } - pageDefaults := frontmatter.FrontMatter{ + fm := frontmatter.FrontMatter{ "collection": c.Name, "permalink": c.PermalinkPattern(), - } - fm := pageDefaults.Merged(c.cfg.GetFrontMatterDefaults(c.Name, siteRel)) + }.Merged(c.cfg.GetFrontMatterDefaults(c.Name, siteRel)) strategy.parseFilename(rel, fm) - f, err := pages.NewFile(c.site, abs, filepath.ToSlash(rel), fm) + f, err := pages.NewFile(c.site, path, filepath.ToSlash(rel), fm) switch { case err != nil: return err diff --git a/collection/strategies.go b/collection/strategies.go index 3b44a55..f8b14ad 100644 --- a/collection/strategies.go +++ b/collection/strategies.go @@ -3,13 +3,14 @@ package collection import ( "time" + "github.com/osteele/gojekyll/config" "github.com/osteele/gojekyll/utils" ) // A collectionStrategy encapsulates behavior differences between the `_post` // collection and other collections. type collectionStrategy interface { - defaultPermalinkPattern() string + defaultPermalinkPattern(*config.Config) string isCollectible(filename string) bool isFuture(filename string) bool parseFilename(string, map[string]interface{}) @@ -58,10 +59,13 @@ const DefaultCollectionPermalinkPattern = "/:collection/:path:output_ext" // DefaultPostsCollectionPermalinkPattern is the default collection permalink pattern const DefaultPostsCollectionPermalinkPattern = "/:categories/:year/:month/:day/:title.html" -func (s defaultStrategy) defaultPermalinkPattern() string { +func (s defaultStrategy) defaultPermalinkPattern(*config.Config) string { return DefaultCollectionPermalinkPattern } -func (s postsStrategy) defaultPermalinkPattern() string { +func (s postsStrategy) defaultPermalinkPattern(cfg *config.Config) string { + if s, ok := cfg.String("permalink"); ok { + return s + } return DefaultPostsCollectionPermalinkPattern }