1
0
mirror of https://github.com/danog/gojekyll.git synced 2025-01-22 21:11:53 +01:00

Default permalink honors md, scss output extensions

This commit is contained in:
Oliver Steele 2017-06-15 22:32:10 -04:00
parent 15cff8988a
commit 6a4b048a09
3 changed files with 18 additions and 9 deletions

View File

@ -54,6 +54,8 @@ func (p *pageFields) Published() bool {
return p.frontMatter.Bool("published", true) return p.frontMatter.Bool("published", true)
} }
// The permalink is computed once instead of on demand, so that subsequent
// access needn't check for an error.
func (p *pageFields) setPermalink(permalink string) { func (p *pageFields) setPermalink(permalink string) {
p.permalink = permalink p.permalink = permalink
} }
@ -77,13 +79,12 @@ func ReadPage(path string, defaults VariableMap) (p Page, err error) {
} }
if p != nil { if p != nil {
// Compute this after creating the page, to pick up the the front matter. // Compute this after creating the page, to pick up the the front matter.
// The permalink is computed once instead of on demand, so that subsequent pattern := data.frontMatter.String("permalink", ":path:output_ext")
// access needn't check for an error.
pattern := data.frontMatter.String("permalink", ":path")
permalink, err := expandPermalinkPattern(pattern, data.path, data.frontMatter) permalink, err := expandPermalinkPattern(pattern, data.path, data.frontMatter)
if err != nil { if err != nil {
return nil, err return nil, err
} }
println(path, pattern, permalink)
p.setPermalink(permalink) p.setPermalink(permalink)
} }
return return

View File

@ -5,6 +5,7 @@ import (
"path" "path"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings"
"time" "time"
) )
@ -43,18 +44,25 @@ func permalinkTemplateVariables(path string, frontMatter VariableMap) map[string
name = filepath.Base(root) name = filepath.Base(root)
title = frontMatter.String("title", name) title = frontMatter.String("title", name)
) )
if isMarkdown(path) { switch {
case isMarkdown(path):
outputExt = ".html"
case ext == ".scss":
outputExt = ".html" outputExt = ".html"
} }
if val, found := frontMatter["collection"]; found { if val, found := frontMatter["collection"]; found {
collectionName = val.(string) collectionName = val.(string)
prefix := "_" + collectionName + "/" prefix := "_" + collectionName + "/"
if !strings.HasPrefix(localPath, prefix) {
panic(fmt.Errorf("Expected %s to start with %s", localPath, prefix))
}
localPath = localPath[len(prefix):] localPath = localPath[len(prefix):]
root = root[len(prefix):]
} }
vs := map[string]string{ vs := map[string]string{
"collection": collectionName, "collection": collectionName,
"name": name, "name": Slugify(name),
"path": "/" + localPath, "path": "/" + root,
"title": title, "title": title,
"slug": Slugify(name), "slug": Slugify(name),
// TODO categories // TODO categories

View File

@ -28,7 +28,7 @@ func TestExpandPermalinkPattern(t *testing.T) {
}) })
t.Run(":path", func(t *testing.T) { t.Run(":path", func(t *testing.T) {
p, _ := expandPermalinkPattern("/prefix:path/post", path, d) p, _ := expandPermalinkPattern("/prefix:path/post", path, d)
assert.Equal(t, "/prefix/a/b/base.html/post", p) assert.Equal(t, "/prefix/a/b/base/post", p)
}) })
t.Run(":title", func(t *testing.T) { t.Run(":title", func(t *testing.T) {
p, _ := expandPermalinkPattern("/title/:title.html", path, d) p, _ := expandPermalinkPattern("/title/:title.html", path, d)
@ -40,9 +40,9 @@ func TestExpandPermalinkPattern(t *testing.T) {
}) })
d["collection"] = "c" d["collection"] = "c"
path = "/_c/a/b/c.d" path = "_c/a/b/c.d"
t.Run(":path", func(t *testing.T) { t.Run(":path", func(t *testing.T) {
p, _ := expandPermalinkPattern("/prefix:path/post", path, d) p, _ := expandPermalinkPattern("/prefix:path/post", path, d)
assert.Equal(t, "/prefix/a/b/c.d/post", p) assert.Equal(t, "/prefix/a/b/c/post", p)
}) })
} }