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

Redefine slug function #10875

Open
avidseeker opened this issue Apr 4, 2023 · 2 comments · May be fixed by #12670
Open

Redefine slug function #10875

avidseeker opened this issue Apr 4, 2023 · 2 comments · May be fixed by #12670
Labels
Milestone

Comments

@avidseeker
Copy link

I have file names:

0101-firstpost.md
0117-second.md
0217-third.md
...

Adding a rule to config.yaml:

permalinks:
  posts: "/:filename"

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

@avidseeker
Copy link
Author

avidseeker commented Apr 10, 2023

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 YYYY-mm-dd. That is:

  • If prefix of the form: "2006-01-02" then parse as "YYYY-mm-dd"
  • If prefix of the form: "01-02" then parse as "mm-dd"
  • If prefix of the form: "0102" then parse as "mmdd"
  • If prefix of the form: "01" then there's no date, but it needs to be trimmed. This is useful for book authors that need to order according to chapters, but don't need chapter numbers to be in page slug.

@avidseeker
Copy link
Author

avidseeker commented Apr 10, 2023

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
        }
 }

avidseeker added a commit to avidseeker/hugo that referenced this issue Apr 10, 2023
@bep bep added this to the v0.113.0 milestone Apr 15, 2023
@bep bep modified the milestones: v0.113.0, v0.114.0, v0.115.0 Jun 8, 2023
@bep bep modified the milestones: v0.115.0, v0.116.0 Jun 30, 2023
@bep bep modified the milestones: v0.116.0, v0.117.0 Aug 1, 2023
@bep bep modified the milestones: v0.117.0, v0.118.0 Aug 30, 2023
@bep bep modified the milestones: v0.118.0, v0.119.0 Sep 15, 2023
@bep bep modified the milestones: v0.119.0, v0.120.0 Oct 5, 2023
@bep bep modified the milestones: v0.120.0, v0.121.0 Oct 31, 2023
@bep bep modified the milestones: v0.121.0, v0.122.0 Dec 6, 2023
@bep bep modified the milestones: v0.122.0, v0.123.0, v0.124.0 Jan 27, 2024
@bep bep modified the milestones: v0.124.0, v0.125.0 Mar 4, 2024
@avidseeker avidseeker linked a pull request Jul 20, 2024 that will close this issue
@bep bep removed the NeedsTriage label Jul 22, 2024
@bep bep modified the milestones: v0.125.0, v0.137.0, v0.138.0, v0.139.0 Oct 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants