mirror of
https://github.com/danog/gojekyll.git
synced 2025-01-22 21:01:18 +01:00
Add more permalink template variables
This commit is contained in:
parent
8958fb695e
commit
1c0cb4eb8d
16
page.go
16
page.go
@ -17,7 +17,7 @@ import (
|
||||
|
||||
var (
|
||||
frontmatterMatcher = regexp.MustCompile(`(?s)^---\n(.+?\n)---\n`)
|
||||
templateVariableMatcher = regexp.MustCompile(`:(?:collection|file_ext|name|path|title)\b`)
|
||||
templateVariableMatcher = regexp.MustCompile(`:\w+\b`)
|
||||
nonAlphanumericSequenceMatcher = regexp.MustCompile(`[^[:alnum:]]+`)
|
||||
)
|
||||
|
||||
@ -168,11 +168,12 @@ func expandPermalinkPattern(pattern string, data map[interface{}]interface{}, pa
|
||||
|
||||
var (
|
||||
collectionName string
|
||||
ext = filepath.Ext(path)
|
||||
localPath = path
|
||||
ext = filepath.Ext(path)
|
||||
root = path[:len(path)-len(ext)]
|
||||
outputExt = ext
|
||||
name = filepath.Base(localPath)
|
||||
title = getString(data, "title", name[:len(name)-len(ext)])
|
||||
name = filepath.Base(root)
|
||||
title = getString(data, "title", name)
|
||||
)
|
||||
|
||||
if ext == ".md" {
|
||||
@ -182,8 +183,8 @@ func expandPermalinkPattern(pattern string, data map[interface{}]interface{}, pa
|
||||
|
||||
if val, found := data["collection"]; found {
|
||||
collectionName = val.(string)
|
||||
collectionPath := "_" + collectionName + "/"
|
||||
localPath = localPath[len(collectionPath):]
|
||||
prefix := "_" + collectionName + "/"
|
||||
localPath = localPath[len(prefix):]
|
||||
}
|
||||
|
||||
replaceNonalphumericsByHyphens := func(s string) string {
|
||||
@ -192,8 +193,9 @@ func expandPermalinkPattern(pattern string, data map[interface{}]interface{}, pa
|
||||
|
||||
templateVariables := map[string]string{
|
||||
"collection": collectionName,
|
||||
"ext": strings.TrimLeft(ext, "."),
|
||||
"name": replaceNonalphumericsByHyphens(name),
|
||||
"output_ext": outputExt,
|
||||
"output_ext": strings.TrimLeft(outputExt, "."),
|
||||
"path": localPath,
|
||||
"title": replaceNonalphumericsByHyphens(title),
|
||||
// TODO year month imonth day i_day short_year hour minute second slug categories
|
||||
|
@ -7,11 +7,31 @@ import (
|
||||
)
|
||||
|
||||
func TestExpandPermalinkPattern(t *testing.T) {
|
||||
d := map[interface{}]interface{}{}
|
||||
path := "/a/b/c.d"
|
||||
var (
|
||||
d = map[interface{}]interface{}{}
|
||||
path = "/a/b/c.d"
|
||||
mdPath = "/a/b/c.md"
|
||||
)
|
||||
|
||||
t.Run(":ext", func(t *testing.T) {
|
||||
p := expandPermalinkPattern("/ext/:ext", d, path)
|
||||
assert.Equal(t, "/ext/d", p)
|
||||
})
|
||||
t.Run(":ext", func(t *testing.T) {
|
||||
p := expandPermalinkPattern("/ext/:ext", d, mdPath)
|
||||
assert.Equal(t, "/ext/md", p)
|
||||
})
|
||||
t.Run(":output_ext", func(t *testing.T) {
|
||||
p := expandPermalinkPattern("/ext/:output_ext", d, path)
|
||||
assert.Equal(t, "/ext/d", p)
|
||||
})
|
||||
t.Run(":output_ext", func(t *testing.T) {
|
||||
p := expandPermalinkPattern("/ext/:output_ext", d, mdPath)
|
||||
assert.Equal(t, "/ext/html", p)
|
||||
})
|
||||
t.Run(":name", func(t *testing.T) {
|
||||
p := expandPermalinkPattern("/name/:name", d, path)
|
||||
assert.Equal(t, "/name/c-d", p)
|
||||
assert.Equal(t, "/name/c", p)
|
||||
})
|
||||
t.Run(":path", func(t *testing.T) {
|
||||
p := expandPermalinkPattern("/prefix:path/post", d, path)
|
||||
|
Loading…
x
Reference in New Issue
Block a user