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

page.fileSlug and directory index files: date doesn't get stripped out #1947

Closed
AleksandrHovhannisyan opened this issue Aug 23, 2021 · 5 comments · Fixed by #2111
Closed
Labels
breaking-change This will have to be included with a major version as it breaks backwards compatibility. bug pull-requested

Comments

@AleksandrHovhannisyan
Copy link
Contributor

Describe the bug

Currently, my post Markdown files follow the YYYY-MM-DD-file-slug.md convention and are located immediately under a _posts directory. I aggregate all of these files into a custom posts collection using 11ty's collection API:

const getAllPosts = (collection) => {
  return collection.getFilteredByGlob(`${dir.input}/_posts/*.md`);
};

I also have the following _posts.json at the root of my _posts directory to apply common front matter to each post; of note is the permalink value, which uses page.fileSlug:

{
  "layout": "post",
  "permalink": "/blog/{{ page.fileSlug }}/",
  "isBlogPage": true,
  "isPost": true
}

As expected, page.fileSlug strips out the dates from my file names. So YYYY-MM-DD-file-slug maps to the URL /blog/file-slug/. This is all working as intended.

image

Now, I want to restructure my blog to co-locate images and posts under the same directory using index files. So instead of containing the Markdown posts as immediate children, my _posts directory will now contain nested directories of the form YYYY-MM-DD-file-slug, and nested inside each directory will be a corresponding index.md for the post and an images directory for any images.

I've adjusted my config to create posts using collection.getFilteredByGlob('src/_posts/**/*.md'). This works. However, per the examples in the documentation linked above, I would expect the date to also get stripped out from the directory name, but it doesn't. So now, my post permalinks are /blog/YYYY-MM-DD-file-slug/ rather than /blog/file-slug/:

image

To Reproduce

  1. Clone my site: https://github.com/AleksandrHovhannisyan/aleksandrhovhannisyan.com
  2. Check out the branch co-located-images:
  3. Run yarn to install dependencies.
  4. Run yarn serve:eleventy to start up just the 11ty server.
  5. When it finishes, navigate to localhost:4001. Click any post, and observe that its URL contains the date in the slug.

Expected behavior
When using index files in directories, the date should get stripped out from the directory name in fileSlug.

Environment:

  • OS and Version: Windows 10
  • Eleventy Version: 0.12.1
@AleksandrHovhannisyan AleksandrHovhannisyan changed the title File slug and index files in directories for posts: date doesn't get stripped out page.fileSlug and directory index files: date doesn't get stripped out Aug 23, 2021
@AleksandrHovhannisyan
Copy link
Contributor Author

Just a gentle nudge to see if this is a known issue or if I'm trying to do something dumb 😄

AleksandrHovhannisyan added a commit to AleksandrHovhannisyan/eleventy that referenced this issue Nov 20, 2021
@AleksandrHovhannisyan
Copy link
Contributor Author

AleksandrHovhannisyan commented Nov 20, 2021

Hopefully the commit above makes it easier to debug this; my expectation would be that 2021-11-20-dir-name/index.md yields dir-name for the slug, consistent with how 2021-11-20-file-name.md yields file-name for the slug.

Test results:

 Difference:

  - '2021-11-20-my-awesome-post'
  + 'my-awesome-post'

Edit: Ah, so it seems to be related to this line: https://github.com/11ty/eleventy/blob/master/src/TemplateFileSlug.js#L38. The directory array contains the raw string 2021-11-20-my-awesome-post.

AleksandrHovhannisyan added a commit to AleksandrHovhannisyan/eleventy that referenced this issue Nov 20, 2021
AleksandrHovhannisyan added a commit to AleksandrHovhannisyan/eleventy that referenced this issue Nov 20, 2021
AleksandrHovhannisyan added a commit to AleksandrHovhannisyan/eleventy that referenced this issue Nov 20, 2021
@mahnunchik
Copy link

+1 for this feature. I've faced with the same issue.

Maybe there is sense to add ability to add custom function to extract any mete from file path: #2132

@AleksandrHovhannisyan
Copy link
Contributor Author

AleksandrHovhannisyan commented Dec 8, 2021

I put up a PR (#2111) to fix this a while back, but I assume it's low priority at the moment for the beta release (no reviews yet). Plus it would introduce a breaking change.

I'm guessing we want to prefer @mahnunchik's proposed workaround since it wouldn't introduce breaking changes?

@zachleat zachleat added pull-requested breaking-change This will have to be included with a major version as it breaks backwards compatibility. bug on-hold and removed needs-triage labels Feb 18, 2022
@zachleat
Copy link
Member

Added a few labels here, you’re absolutely right that this should be fixed but it is a breaking change so unfortunately I have to put it on hold for now (sorry)

@zachleat zachleat removed the on-hold label May 10, 2022
@zachleat zachleat added this to the Eleventy 2.0.0 milestone May 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change This will have to be included with a major version as it breaks backwards compatibility. bug pull-requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants