-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
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
Redefine slug function #10875
Comments
Maybe giving the end user the ability to redefine slugs is overkill. Following the Discourse discussion, I realized a similar feature to what I want is already implemented. At least for now, I'm changing this issue to include support for filename prefix formats other than
|
The following gives the desired slug in all mentioned cases. @@ -126,18 +127,19 @@ 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
}
@@ -391,17 +393,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
}
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have file names:
Adding a rule to
config.yaml
:will give them
0101-firstpost
permalinks. Changing that rule to/:slug
gives a hyphenated "title
" from the front matter. I suggest enabling redefinition of:slug
placeholder or maybe creating a function that:filename
can be piped into.Related discussion: 43740
The text was updated successfully, but these errors were encountered: