Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Feat: wider support for filename date formats
Solves #10875
  • Loading branch information
avidseeker committed Apr 10, 2023
commit e2faa1fc332d4fb043be7c2a6e168d5f9508f5a5
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@

*.test
public
hugo
.hugo_build.lock
*.test
27 changes: 12 additions & 15 deletions resources/page/pagemeta/page_frontmatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,18 @@ func (f FrontMatterHandler) IsDateKey(key string) bool {
func dateAndSlugFromBaseFilename(location *time.Location, name string) (time.Time, string) {
withoutExt, _ := paths.FileAndExt(name)

if len(withoutExt) < 10 {
// This can not be a date.
return time.Time{}, ""
slug := strings.TrimLeft(withoutExt, "0123456789-_")
lastIdx := len(withoutExt) - len(slug)
if lastIdx == -1 {
lastIdx = 0
}
prefix := withoutExt[:lastIdx]

d, err := htime.ToTimeInDefaultLocationE(withoutExt[:10], location)
d, err := htime.ToTimeInDefaultLocationE(prefix, location)
if err != nil {
return time.Time{}, ""
d = time.Time{}
}

// Be a little lenient with the format here.
slug := strings.Trim(withoutExt[10:], " -_")


return d, slug
}

Expand Down Expand Up @@ -391,17 +390,15 @@ func (f *frontmatterFieldHandlers) newDateFieldHandler(key string, setter func(d
func (f *frontmatterFieldHandlers) newDateFilenameHandler(setter func(d *FrontMatterDescriptor, t time.Time)) frontMatterFieldHandler {
return func(d *FrontMatterDescriptor) (bool, error) {
date, slug := dateAndSlugFromBaseFilename(d.Location, d.BaseFilename)
if date.IsZero() {
return false, nil
}

setter(d, date)

if _, found := d.Frontmatter["slug"]; !found {
// Use slug from filename
d.PageURLs.Slug = slug
}

if date.IsZero() {
return false, nil
}
setter(d, date)
return true, nil
}
}
Expand Down