hugo icon indicating copy to clipboard operation
hugo copied to clipboard

Add Permalink option for sections

Open writeonlycode opened this issue 3 years ago • 9 comments

I have something like the following option in the config.toml:

[permalinks]
posts = "/new-posts/:filename/"

If I visit http://localhost:1313/new-posts/post-name/ I get the intended page. But if I visit http://localhost:1313/new-posts/ I don't get the list page, as I would expect. Instead, I get nothing at all. I still have to go to http://localhost:1313/posts/ to get the list page.

The version I'm using:

hugo v0.83.1+extended linux/amd64 BuildDate=unknown

I'm not sure if this behavior is intended. But it would be a good idea to have the ability to change the URL of the list pages using the permalinks option as well. Right now, I have to add the url parameter in the front matter of the _index.md to make the list page have the different url.

writeonlycode avatar May 11 '21 21:05 writeonlycode

I'm not sure if this behavior is intended.

It is.

bep avatar May 11 '21 21:05 bep

I see. Maybe this should be a feature request, then? It would be quite helpful for me at least to be able to change the URL of the list pages using some option similar to the permalinks option.

writeonlycode avatar May 11 '21 21:05 writeonlycode

I see. Maybe this should be a feature request, then?

It behaves like it is for a reason -- I don't see how you could translate the permalink config to work for list pages, but I would be happy to be proven wrong.

bep avatar May 11 '21 21:05 bep

I don't really know how it works behind the scenes. But we already have the ability to change the URL of list pages. We just have to use the url parameter in their front matter. The "problem" I'm having is that I want to change the whole folder to a different URL, so, on top of adding the permalinks option, I also have to add the url in all _index.md files in that folder and subfolders. The proposal would be to have an option to add to the config.toml that does this job of "adding the url option to all list pages". Not a huge problem really, but it would be convenient.

writeonlycode avatar May 11 '21 21:05 writeonlycode

So, on the technical side this isn't a problem.

But it's a permalink config, so they should be ... permanent. And the only permanent part of a section is the ... section path.

Given 2 common examples (assuming no date is set in section front matter):

[permalinks]
  "posts" = "/:year/:month/:filename/"
[permalinks]
  posts = "/:06/:1/:2/:title/"

How stable would the above be? And would it be a good URL?

bep avatar May 11 '21 21:05 bep

Right. I suppose it would be necessary to have different options for single pages vs. list pages. Something like the following for list pages:

[permalinks.sections]
posts = "/some/new/path/"

It could support the :year, :month and such as well, even if these options would not be terribly useful in regular use cases. Then the current permalinks option could stay as it is, and this new option would change the url of the list pages.

writeonlycode avatar May 11 '21 21:05 writeonlycode

@writeonlycode yes, something like that could work, but we need to somehow rework the existing permalinks struct in a backwards compatible way (we're not adding something ... else)... I suggest we somehow use the page Kind. I'm not sure if this is even valid TOML, but:

[permalinks]
[permalinks.section]
posts = "/some/new/path/"
[permalinks.page]
posts = "/:06/:1/:2/:title/"

bep avatar May 12 '21 06:05 bep

That looks good! And it is valid TOML as far as I can see. I suppose the assumption should be that if no kind is specified, it defaults to single pages, so the following two should be equivalent:

[permalinks]
posts = "/:06/:1/:2/:title/"
[permalinks]
[permalinks.page]
posts = "/:06/:1/:2/:title/"

In this way, it will be backwards compatible. And in addition to those, we could have the section option, to accommodate the new behavior:

[permalinks]
[permalinks.section]
posts = "/some/new/path/"

This looks like a good way to go!

writeonlycode avatar May 12 '21 13:05 writeonlycode

Another way / syntax to handle the section case would be that the value of the keys in [permalinks] can be either a string "" or an array ["", ""].

If an array is specified :

  • the first string define the path of page (or term if it's a taxonomy).
  • The second define the section path.

Current version:

[permalinks]
posts = "/:06/:1/:2/:title/"

Proposition:

[permalinks]
# Classic way
categories = "/projects/:slug/"
# Advance way to define also the section path
posts = [ "/:06/:1/:2/:title/", "/articles/" ]

BenSouchet avatar Jul 22 '22 20:07 BenSouchet

This would be very helpful, indeed. I am facing the same situation in a multilingual setup. At least there would be only one place to define the translated section name. Currently I have to define the translation both in the configuration for the pages and in the list page in the url frontmatter entry.

config.yml

languages:
  de:
    permalinks:
      offer: /angebot/:slug/

offer/_index.md

---
weight: 30
title: Angebot
url: /angebot/
---

This is doable, but shifts parts of the translation into the configuration, which is not optimal.

For multilingual content management it is way easier to keep the path and filenames untranslated and translate the target only. One of the reasons for that is that you need lots of translationKey entries if you simply translate the section path in the file system. The second reason is that most tools are capable of translating file contents only, but not file paths. In these scenarios the limitation that the target of a section cannot be changed at build time in hugo makes content maintenance much more complicated.

The translation tools I am aware of are capable of translating frontmatter and content (file content), but not the configuration. And they cannot translate path segments in the file tree of the generated output (file path).

Having that said, it would be even better to have an option where the url of list pages is simply inherited for all children, so effectively a means to set permalink from frontmatter for list pages and the children. But this would be a different Enhancement (obviously of the sort that would change a fundamental design decision as per https://gohugo.io/content-management/sections/) and I absolutely understand that these are not accepted these days.

+1 for @BenSouchet 's proposal, because this structure keeps the multiple options in one place per section.

istr avatar Dec 09 '22 21:12 istr

That looks good! And it is valid TOML as far as I can see. I suppose the assumption should be that if no kind is specified, it defaults to single pages, so the following two should be equivalent:

[permalinks]
posts = "/:06/:1/:2/:title/"
[permalinks]
[permalinks.page]
posts = "/:06/:1/:2/:title/"

In this way, it will be backwards compatible. And in addition to those, we could have the section option, to accommodate the new behavior:

[permalinks]
[permalinks.section]
posts = "/some/new/path/"

This looks like a good way to go!

It makes a lot of sense that way. It would be great if this problem is fixed with a new update.

ardacarofficial avatar Jan 09 '23 17:01 ardacarofficial

any update?

omerfdmrl avatar Jan 09 '23 19:01 omerfdmrl

I would love this feature. I like this solution: https://github.com/gohugoio/hugo/issues/8523#issuecomment-1192894337.

jhvanderschee avatar Feb 18 '23 01:02 jhvanderschee

I'll add another use case for this FWIW:

I'm currently working with the mechanism espoused in this discussion. After jumping through a few hoops (mostly media related), it works fairly well -- except that I've also set up non-default permalinks, and none of my multi-page posts appear in the correct location unless I deliberately configure their front matter, which is very inconvenient.

I can see other potential uses, but this is the concrete one I'm fighting with literally right now. 😀

macdweller avatar Feb 23 '23 04:02 macdweller

It is a daily fight for me, as most of my Hugo websites are multilingual and my front matter is accessible to my clients/content editors. Therefore I would love to have a way to configure this properly.

jhvanderschee avatar Feb 25 '23 13:02 jhvanderschee

@writeonlycode yes, something like that could work, but we need to somehow rework the existing permalinks struct in a backwards compatible way (we're not adding something ... else)... I suggest we somehow use the page Kind. I'm not sure if this is even valid TOML, but:

[permalinks]
[permalinks.section]
posts = "/some/new/path/"
[permalinks.page]
posts = "/:06/:1/:2/:title/"

has this been implemented yet?

mproyyan avatar Feb 27 '23 02:02 mproyyan

@writeonlycode yes, something like that could work, but we need to somehow rework the existing permalinks struct in a backwards compatible way (we're not adding something ... else)... I suggest we somehow use the page Kind. I'm not sure if this is even valid TOML, but:

[permalinks]
[permalinks.section]
posts = "/some/new/path/"
[permalinks.page]
posts = "/:06/:1/:2/:title/"

has this been implemented yet?

No, just a suggestion. If developers want to make this feature, they will be inspired by the ideas here. Ultimately, if the update comes, it's up to the developer to code this solution.

ardacarofficial avatar Feb 27 '23 15:02 ardacarofficial

Since this was a major deal-breaker I worked on a patch for this; here's the branch in my fork for anyone interested: https://github.com/Mai-Lapyst/hugo/tree/allow-permalinks-conf-for-branchbundles Can also open a PR if wanted.

Works currently with permalinks configs like

[permalinks]
  docs = "/docs/1.0/:sections[1:]/:filename"

And even when using :slug, :slugorfilename or :title.

Mai-Lapyst avatar Mar 16 '23 13:03 Mai-Lapyst

@Mai-Lapyst I think that a PR would be highly welcome.

istr avatar Mar 16 '23 21:03 istr

@Mai-Lapyst I think that a PR would be highly welcome.

@istr Have created one; you can test it out if you want.

Mai-Lapyst avatar Mar 17 '23 23:03 Mai-Lapyst

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Jul 18 '23 02:07 github-actions[bot]