Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: wider support for filename date formats #12670

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions resources/page/pagemeta/page_frontmatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,19 +400,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 @@ -752,17 +751,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.PageConfig.Params["slug"]; !found {
// Use slug from filename
d.PageConfig.Slug = slug
}

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