summaryrefslogtreecommitdiffstats
path: root/source
diff options
context:
space:
mode:
authorJanus <janus@insignificancegalore.net>2018-10-16 23:51:48 +0200
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2018-10-16 23:51:48 +0200
commit1f42e47e475c0cd684426dd230de411d4c385a3c (patch)
tree9a16adcf24b856f38498abe63e9664e64153f8e2 /source
parent35fbfb19a173b01bc881f2bbc5d104136633a7ec (diff)
Allow date and slug from filename for leaf bundles
Fixes #4558
Diffstat (limited to 'source')
-rw-r--r--source/fileInfo.go20
-rw-r--r--source/fileInfo_test.go13
2 files changed, 31 insertions, 2 deletions
diff --git a/source/fileInfo.go b/source/fileInfo.go
index fc55706fe..ad302f470 100644
--- a/source/fileInfo.go
+++ b/source/fileInfo.go
@@ -70,6 +70,10 @@ type File interface {
// not even the optional language extension part.
TranslationBaseName() string
+ // ContentBaseName is a either TranslationBaseName or name of containing folder
+ // if file is a leaf bundle.
+ ContentBaseName() string
+
// UniqueID is the MD5 hash of the file's path and is for most practical applications,
// Hugo content files being one of them, considered to be unique.
UniqueID() string
@@ -106,6 +110,7 @@ type FileInfo struct {
relPath string
baseName string
translationBaseName string
+ contentBaseName string
section string
isLeafBundle bool
@@ -144,6 +149,13 @@ func (fi *FileInfo) BaseFileName() string { return fi.baseName }
// language segement (ie. "page").
func (fi *FileInfo) TranslationBaseName() string { return fi.translationBaseName }
+// ContentBaseName is a either TranslationBaseName or name of containing folder
+// if file is a leaf bundle.
+func (fi *FileInfo) ContentBaseName() string {
+ fi.init()
+ return fi.contentBaseName
+}
+
// Section returns a file's section.
func (fi *FileInfo) Section() string {
fi.init()
@@ -177,11 +189,15 @@ func (fi *FileInfo) init() {
if (!fi.isLeafBundle && len(parts) == 1) || len(parts) > 1 {
section = parts[0]
}
-
fi.section = section
- fi.uniqueID = helpers.MD5String(filepath.ToSlash(fi.relPath))
+ if fi.isLeafBundle && len(parts) > 0 {
+ fi.contentBaseName = parts[len(parts)-1]
+ } else {
+ fi.contentBaseName = fi.translationBaseName
+ }
+ fi.uniqueID = helpers.MD5String(filepath.ToSlash(fi.relPath))
})
}
diff --git a/source/fileInfo_test.go b/source/fileInfo_test.go
index 9d3566240..9390c6247 100644
--- a/source/fileInfo_test.go
+++ b/source/fileInfo_test.go
@@ -94,4 +94,17 @@ func TestFileInfoLanguage(t *testing.T) {
assert.Equal("sv", fiSv.Lang())
assert.Equal("en", fiEn.Lang())
+
+ // test contentBaseName implementation
+ fi := s.NewFileInfo("", "2018-10-01-contentbasename.md", false, nil)
+ assert.Equal("2018-10-01-contentbasename", fi.ContentBaseName())
+
+ fi = s.NewFileInfo("", "2018-10-01-contentbasename.en.md", false, nil)
+ assert.Equal("2018-10-01-contentbasename", fi.ContentBaseName())
+
+ fi = s.NewFileInfo("", filepath.Join("2018-10-01-contentbasename", "index.en.md"), true, nil)
+ assert.Equal("2018-10-01-contentbasename", fi.ContentBaseName())
+
+ fi = s.NewFileInfo("", filepath.Join("2018-10-01-contentbasename", "_index.en.md"), false, nil)
+ assert.Equal("_index", fi.ContentBaseName())
}