Automatically add _index.md to new subsections

I’ve been reading through the content types and archetypes documentation and wonder if it’s possible to have Hugo create a new _index.md file for new subsections? An example may help:

In my content directory, I have a blog section. Blogs are organised in subsections of year and again of month. This gives a tree that looks a bit like this:

./content/blog/
├── 2017
│   ├── 10
│   │   └── test-3.md
│   ├── 11
│   │   ├── test-1.md
│   │   └── test-2.md
│   ├── 12
│   │   ├── _index.md
│   │   └── automating-hugo-deployment-to-digitalocean-with-codeship.md
│   ├── 6
│   │   └── test-10.md
│   ├── 7
│   │   └── test-9.md
│   ├── 8
│   │   ├── test-7.md
│   │   └── test-8.md
│   └── 9
│       ├── test-4.md
│       ├── test-5.md
│       └── test-6.md
└── _index.md

Given that each month, and each new year, a new directory is created and should have an _index.md with pre-populated front matter, is it possible for Hugo to generate this?

Using the above tree as an example, suppose now we are in 2018 I create a new blog hugo new blog/2018/1/some-post.md, can Hugo be configured to create _index.md in both the 2018 and 1 directories? And then in February, when I do hugo new blog/2018/2/some_other-post.md, create _index.md in 2? And so on?

If so, how would I do this?

To your concrete question the answer is: No.

But I would recommend you look into Hugo’s permalink settings instead of manually creating that “monthly file structure”.

Thanks, @bep. Useful to know both of those.

Having implemented the latter, I have 2 questions:

  1. Is it possible to generate a list of items that fall under localhost:1313/2017/10, or /2017, or /2018, etc? Or is the answer still no?

  2. Previously, when I had the link as /blog/2017/12, I had a blog menu item that was highlighted whenever anything under /blog was the current page. This no longer works. How should I adapt the following snippet to provide this same functionality?

{{ $currentPage := . }}
{{ range .Site.Menus.main }}
   <li class='menu-item{{if or ($currentPage.IsMenuCurrent "main" .) ($currentPage.HasMenuCurrent "main" .) }} current-menu-item{{end}}'>
   <a title="{{ .Name }}" href="{{ .URL }}">{{ .Name }}</a>
  </li>
{{ end }}

Second point slightly off-topic, I know - happy to move to another conversation if need be.

  1. Yes, but you need to manually (or create a script yourself) that creates “_index.md” in the bottom-most folders.

So if I script that, I’m trying to understand the steps involved. As I see it, it would be:

  1. Loop through all posts and read the date of each.
  2. For each distinct year, create a folder for that year.
  3. For each folder created, create an _index.md file.
  4. Run hugo, which will merge the created folders with the generated folder structure, which will then additionally include list pages for each of the years.

Is that correct?

You will have to come up the script yourself (this is not some corporate support), but as I said: You need to create the file at the lowest level (which I guess is the month); the content of that file does not matter too much for this (you would probably want a date) as long as it is a valid content file with front matter.

And here I was hoping I’d found some free corporate support! It is a support forum :wink:

Just trying to make sure I understand the problem and approach to solving it in the context of Hugo.