Eleventy: Add automatic `fileSlug` key from file name

Created on 6 Mar 2018  ·  10Comments  ·  Source: 11ty/eleventy

Match {slug-name}.md or YYYY-MM-DD-{slug-name}.md

enhancement high-priority

Most helpful comment

Any progress on this one? I tried to patch it by creating a filter and using this in permalink creation, but turns out that page.url returns an incorrect value when using this approach — another bug?

Given the filter I created, while the desired files are created (/path/to/slug.html), page.url returns /path/to/.html (slug is missing). 🤔

All 10 comments

Ha, just run into this! This would be very helpful in being able to port my Jekyll site over to 11ty and maintain the same URLs. Not sure there’s a way to maintain current slugs currently without adding them to each document manually.

Correct me if I’m wrong, but with this, I could create an isolated directory with my content like this:

File structure:

content/
├─ content.json
├─ index.html
└─ nice-piece-of-content.md

content.json:

{
  "layout": "layouts/content.njk",
  "permalink": "/{{ slug }}/index.html"
}

Which would change the input/output mapping like this:

BEFORE: content/nice-piece-of-content.md → _site/content/nice-piece-of-content/index.html
AFTER:  content/nice-piece-of-content.md → _site/nice-piece-of-content/index.html
BEFORE: content/index.html → _site/content/index.html
AFTER:  content/index.html → _site/index.html

Yes @kleinfreund that is what I would expect to happen too so I will not correct you 👍

Hmm @kleinfreund looking at this again I think that second example _may_ be a point of consistency contention.

I see three options there for index templates:

  • slug evaluates to empty if the file name is index.* (outputs to _site/index.html)
  • slug evaluates to the folder name (outputs to _site/content/index.html)
  • slug evaluates to index (outputs to _site/index/index.html) (not really a fan of this but it may be the most consistent/obvious thing)

At this point in the world of static site generators, index.* is a special breed. Its main use is creating extension-less permalinks (e.g. thing/index.html can be reached at thing/ if the web server is set up that way).

Now inferring the slug from a file’s name would generate index from index.html and thing from thing.md. That’s very consistent. However, it’s not useful. In this case, it would mean trading consistency for the capability of writing meta data-less content.

As shown above, in a directory-specific data file one can specifiy a dynamic permalink based on {{ slug }}. However, one would not be able to generate an index.html at the root of that directory as the consistent solution always creates a slug; thus, a sub-directory will be created.

Another thought: thing.md producing thing/index.html is already inconsistent. Is index the title of that document? thing seems to be the slug. Then index can’t be. If index is not the slug, why is index.html resulting in index/indext.html?

Also, let me just clarify that I see why this rings your alarm bells. Breaking consistency always does. Let me know if I should clarify some bits. I’ve got the feeling that this already sounds rather abstract.

Any progress on this one? I tried to patch it by creating a filter and using this in permalink creation, but turns out that page.url returns an incorrect value when using this approach — another bug?

Given the filter I created, while the desired files are created (/path/to/slug.html), page.url returns /path/to/.html (slug is missing). 🤔

This is checked in and will be released with 0.3.4. If you’re still running off of master @paulrobertlloyd I’d love if you’d give it a test.

Oh, nice one @zachleat! Been doing some early testing, and seems to be working as expected. Great stuff. Thanks again 👍🏻

Oh nice, can’t wait for this to get out and see what it can do. I’m gonna wait for the release, though. :)

For my future searching ability, this feature is available as fileSlug.

Was this page helpful?
0 / 5 - 0 ratings