Apply different layout according to the file name pattern

I have two types of layout: home.html and post.html in _layouts directory.

For a post entity whose filename is ended with index.md, I want ‘home’ layout to be applied to it. Otherwise, the ‘post’ layout should be used.

I added the defaults->scope->path attribute in _config.yml, but it does not work properly. All articles are rendered with ‘post’ layout.

defaults:
  -
    scope:
      path: ""
      type: "pages"
    values:
      layout: "post"
  -
    scope:
      path: "*/index.md"
      type: "pages"
    values:
      layout: "home"

Is the regex */index.md wrong? I trutly have no idea how could I separate the scope.

maybe it applies the first one that works and moves on? so move up the more specific one and see if that works.

Are the posts actually posts? you don’t have any better paths to use? how many of each are there?

Sadly, changing scope order or even removal of first one (layout: "post") does not resolve the issue. The home layout is only applied to root URL (localhost:4000/), but not to other index.md in sub URL (ex., localhost:4000/docs/)

My site configuration is:

collections:
  pages:
    output: true
    permalink: /:path

defaults:
  -
    scope:
      path: "*/index.md"
      type: "pages"
    values:
      layout: "home"

I used collection name “pages”, so my posts are located as follow:

_pages/index.md  <-- home layout
_pages/docs1/index.md  <-- home layout
_pages/docs1/postA.md  <-- post layout
_pages/docs2/index.md  <-- home layout
_pages/docs2/postB.md  <-- post layout
_pages/docs2/postC.md  <-- post layout
...

It works only for files within a given folder or files with specific names in any folder. Files ending with specific words require regex but Jekyll doesn’t seem to support Regex in scope path.

A plugin does seem to provide this functionality, but it also no longer maintained.