I’m new to Hugo but not to SSGs. I have been pulling out my hair trying to get Hugo to generate an index.html under each section in /public/ and I would really appreciate some feedback.
Scenario: I have a Hugo-based documentation website with several pages organized by section (/content/section1, /content/section2, etc.). I want users to be able to click the section name in a breadcrumb navigation and have it load an index page at /public/section/index.html which lists all of the pages under that particular section. I have a full ToC on the homepage, and the navbar is generating the breadcrumb as expected, but without the index.html files, the links are 404.
Thus far, I’ve created the following templates and files without effect:
Regardless of what I do, Hugo will not generate an index.html page under any of the section directories in /public.
This seems like such an easy thing to accomplish, but I just can’t figure it out. I do see that section indexes are generated in the root of /public (/public/section1.html, /public/section2.html, etc.), but that’s not what I want.
Thanks for the reply, but like I said this has already been identified as an existing issue (4428). Expected behaviour is that /content/section/_index.md should generate /public/section/index.html, but when uglyurls = true it generates /public/section.html instead. This leaves /public/section/ without a directory index, which is not the traditional expectation of directory indexes regardless of pretty URLs.
I think it finally clicked: you want most pages to resolve to filename.html, but not section list pages, which themselves take on the name of the the section.
This is why I asked for a demo repo, and why someone should put it together with real world examples and add it to that issue. I suspect hardly any of the regulars around here use uglyURLs, so how it is supposed to behave looks mostly correct from our POV. And when you ask, “how do I generate an index.html?” I wonder why you are using uglyURLs at all, since by default all of them are index pages…
No problem. If you review the bug thread I’ve linked to, you’ll see that the Hugo documentation was updated immediately after the bug was discovered, but the debate continued as to whether this was appropriate behaviour as it is unconventional. Personally, it does seem odd that Hugo will not allow developers to generate an index.html inside a sub-directory if uglyurls = true.
The ticket remains open to implement an option which supports the original (expected) behaviour that was present in Hugo until sometime between versions 0.21 and 0.54, as well as the behaviour that was inadvertently adopted sometime later (so as to not break existing websites).
For what it’s worth, I’m using ugly URLs because this website will be served from a local filesystem (unhosted) and pretty URLs won’t work in such a case (e.g. you must link to the HTML file itself).
So this appears to be a side effect of uglyurls = true. When uglyurls is true, section pages are generated in the root directory of /public instead of their relative section folders. There is an open ticket to resolve this here:
Workaround: As TjeuKayim mentions in the above bug thread, a workaround is to name the _index.md something else (e.g. dirlist.md) and add slug: index to the front matter of the file. This will generate index.md in the expected section’s directory. Depending on your theme’s design, you might need to add layout: section to the front matter as well to avoid it using the single.html template.
For example:
Rename /content/some-section/_index.md to /content/some-section/dirlist.md
Add slug: index and layout: section to the front matter of the file
No, I simply want oneindex.html rendered as a section list in every sub-directory containing content pages, regardless of whether uglyurls is true. This currently isn’t possible. For example, I want /public to render as follows: