The URL path elements in a published Hugo site typically mirror the directory and file names of the content hierarchy within the content/
directory, however, this is not always desirable. There are legitimate reasons for wanting these structures to differ.
It is easy to modify the final filename element of the URL using the slug
front matter parameter but it is not possible to modify the naming of individual sections in a similar way.
We propose adding a :sectionslug
permalink token (similar in priniciple to the existing :sections
token) that modifies a section’s URL path element by using the slug
or title
parameters as defined in a section’s front matter (in _index.md
file) iteratively to create the URL path, resulting in a slugified path
NOTE: The
url
field in the front matter can currently be used to override the entire path. This approach becomes error prone when handling nested content section hierarchies as care must be taken to propagate any changes to a section’surl
to its subsections.
Example
Given the content structure:
content/
└── 01-parent-section/
├── _index.md # slug: my-parent-section-slug ; title: "My Parent Section Title"
├── 01-article.md
└── 02-child-section/
├── _index.md # title: "My Child Section"
└── 01-nested-article.md # title: "My Nested Article"
And the following permalink configuration using the proposed :sectionslug
token:
# config.yaml
...
permalinks:
page:
01-parent-section: /:sectionslug/:slug
section:
01-parent-section: /:sectionslug/:slug
The published site structure would be:
public/
└── my-parent-section-slug/
├── index.html
└── my-child-section/
├── index.html
└── my-nested-article/
└── index.html
We have already written and tested the code for this on multiple Hugo sites, and can make it available on request.