Repository: 11ty/eleventy (JavaScript)

Issue:
page.fileSlug and directory index files: date doesn't get stripped out
**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](https://github.com/AleksandrHovhannisyan/aleksandrhovhannisyan.com/blob/master/config/utils/index.js#L6) using 11ty's collection API:

```js
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`:

```json
{
  "layout": "post",
  "permalink": "/blog/{{ page.fileSlug }}/",
  "isBlogPage": true,
  "isPost": true
}
```

[As expected](https://www.11ty.dev/docs/data-eleventy-supplied/#fileslug), `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](https://user-images.githubusercontent.com/19352442/130438909-f6075379-6745-4f0f-8be1-301a3d601c30.png)

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](https://user-images.githubusercontent.com/19352442/130439252-e73f6c53-c05e-4149-bcdd-a5c673e0dfd9.png)

**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



Discussion:
Just a gentle nudge to see if this is a known issue or if I'm trying to do something dumb 😄 
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`.
+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: https://github.com/11ty/eleventy/issues/2132
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? 
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)



Patch:
diff --git a/src/TemplateFileSlug.js b/src/TemplateFileSlug.js
index ed95edeb8..a98f3db1f 100644
--- a/src/TemplateFileSlug.js
+++ b/src/TemplateFileSlug.js
@@ -23,7 +23,12 @@ class TemplateFileSlug {
   }
 
   _getRawSlug() {
-    let slug = this.filenameNoExt;
+    const slug = this.filenameNoExt;
+    return this._stripDateFromSlug(slug);
+  }
+
+  /** Removes dates in the format of YYYY-MM-DD from a given slug string candidate. */
+  _stripDateFromSlug(slug) {
     let reg = slug.match(/\d{4}-\d{2}-\d{2}-(.*)/);
     if (reg) {
       return reg[1];
@@ -35,7 +40,11 @@ class TemplateFileSlug {
     let rawSlug = this._getRawSlug();
 
     if (rawSlug === "index") {
-      return this.dirs.length ? this.dirs[this.dirs.length - 1] : "";
+      if (!this.dirs.length) {
+        return "";
+      }
+      const lastDir = this.dirs[this.dirs.length - 1];
+      return this._stripDateFromSlug(lastDir);
     }
 
     return rawSlug;


Test patch:
diff --git a/test/TemplateFileSlugTest.js b/test/TemplateFileSlugTest.js
index 972cab52a..4cfa46fa5 100644
--- a/test/TemplateFileSlugTest.js
+++ b/test/TemplateFileSlugTest.js
@@ -96,6 +96,12 @@ test("Easy slug with date, index with dir", (t) => {
   t.is(fs.getFullPathWithoutExtension(), "/test/index");
 });
 
+test("Strips date from dir name", (t) => {
+  let fs = getNewSlugInstance("./2021-11-20-my-awesome-post/index.md");
+  t.is(fs.getSlug(), "my-awesome-post");
+  t.is(fs.getFullPathWithoutExtension(), "/2021-11-20-my-awesome-post/index");
+});
+
 /* Pass Input dir */
 test("Easy slug, input dir", (t) => {
   let fs = getNewSlugInstance("./file.html", ".");