Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resources/page: Allow section&taxonomy pages to have a permalink configuration #10847

Merged

Conversation

Mai-Lapyst
Copy link
Contributor

Allows using permalink configuration for sections (branch bundles) and also for taxonomy pages. Extends the current permalink configuration to be able to specified per page kind while also staying backward compatible: all permalink patterns not dedicated to a certain kind, get automatically added for both normal pages and term pages.

Fixes #8523

Example config:

[permalinks]
  docs = "/docs/1.0/:sections[1:]/:filename"  # Will get added to both page & term permalinks
[permalinks.section]
  docs = "/docs/1.0/:sections[1:]"
[permalinks.page]
[permalinks.term]
[permalinks.taxonomy]

@jmooring
Copy link
Member

jmooring commented Mar 18, 2023

@Mai-Lapyst I did some quick testing and the first thing I found was...

If an _index.md does not exist for a top level section, the permalink setting is not applied. It is somewhat common to not create an _index.md for top-level content sections, and very common to not create an _index.md file for taxonomy or term pages.

content/
├── posts/
│   └── post-1.md
└── _index.md
[permalinks]
posts = '/my-posts/:slug'
[permalinks.section]
posts = '/my-posts'
public/
├── my-posts/
│   └── post-1/
│       └── index.html
├── posts/            
│   └── index.html  <-- unexpected
└── index.html

But with this I get the desired result:

content/
├── posts/
│   ├── _index.md
│   └── post-1.md
└── _index.md

@Mai-Lapyst Mai-Lapyst force-pushed the allow-permalinks-conf-for-branchbundles branch from 1f25526 to fb35e7f Compare March 18, 2023 18:02
@Mai-Lapyst
Copy link
Contributor Author

Mai-Lapyst commented Mar 18, 2023

@jmooring Thanks for the feedback.

Turns out that the if around the expand is completly redundant now. Removed it and it works like intended.

Also added some debug logging to verify that the permalink for any given file/section is set and to what value exactly. (--debug)

@jmooring
Copy link
Member

WARN 2023/03/18 17:12:36 Permalinks configuration overwrites pattern in kind 'page' for key 'books' with '/my-books2/:slug' (which was '/my-books/:slug')

Nice!

I spent a couple of hours testing this, and functionally it looks great. It behaves as expected:

  • When url is set in front matter (trumps everything)
  • Monolingual
  • Multilingual
  • Multilingual with permalinks per language
  • etc.

FYI, when I run v0.111.3 (no patch) with the new permalink settings in site config, it panics, but that's much better than creating new keys. I don't see that as a problem.

@Mai-Lapyst
Copy link
Contributor Author

Thanks!

FYI, when I run v0.111.3 (no patch) with the new permalink settings in site config, it panics, but that's much better than creating new keys. I don't see that as a problem.

I noticed that too; digged a bit into it and you can get the same crash even in the patched version, by simply using an empty string:

[permalinks]
    posts = ""

It seems that an unchecked string slice access seems to be the problem:

func (l PermalinkExpander) validate(pp string) bool {
fragments := strings.Split(pp[1:], "/")

Will add a patch for it as well.

@jmooring
Copy link
Member

@bep Functionally this is great, and scratches a long-term itch. Would appreciate a review if/when you have the chance.

@bep bep added this to the v0.112.0 milestone May 19, 2023
@bep
Copy link
Member

bep commented May 19, 2023

OK, I guess this is what lots of people would want, and I like the config.

I will merge this if we could

  1. Get this rebased against master and get this integrated into my new allconfig.Config struct.
  2. Get a basic integration test up and running.

@bep bep modified the milestones: v0.112.0, v0.113.0 May 23, 2023
…iguration

Allows using permalink configuration for sections (branch bundles) and
also for taxonomy pages. Extends the current permalink configuration to
be able to specified per page kind while also staying backward compatible:
all permalink patterns not dedicated to a certain kind, get automatically
added for both normal pages and term pages.

Fixes gohugoio#8523
@Mai-Lapyst Mai-Lapyst force-pushed the allow-permalinks-conf-for-branchbundles branch from 2306a9f to 9caaf86 Compare May 26, 2023 09:03
@Mai-Lapyst
Copy link
Contributor Author

@bep Thanks for looking over it. Have rebased it onto master and reworked it to work with the new allconfig. Also added a test for it; let me know if more tests are needed.

@bep bep modified the milestones: v0.113.0, v0.115.0 Jun 13, 2023
@@ -93,6 +93,7 @@ const (
ContentClassBranch ContentClass = "branch"
ContentClassFile ContentClass = "zfile" // Sort below
ContentClassContent ContentClass = "zcontent"
ContentClassZero ContentClass = "zero" // Special value for zeroFile
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need/want this. A zero file would have an empty ContentClass string, which is the natural zero value, no need to make one up.

desc.ExpandedPermalink = opath

if !p.File().IsZero() {
s.Log.Debugf("Set expanded permalink path for %s %s to %#v", p.Kind(), p.File().Path(), opath)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove these debug statement, it makes the debug log very busy,.

// [permalinks.key]
// xyz = ???

if (k == "page") || (k == "section") || (k == "taxonomy") || (k == "term") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can use the constants in pagekinds.Page etc.

p.c.Permalinks = maps.CleanConfigStringMapString(p.p.GetStringMapString(d.key))
p.c.Permalinks = make(map[string]map[string]string)

p.c.Permalinks["page"] = make(map[string]string)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pagekinds.Page etc.

@@ -69,6 +69,7 @@ func TestPermalinkExpansion(t *testing.T) {
page.date = d
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This all looks good, but I think we need a test that shows the new setup working. I suggest you create a new permalink_integration_test.go and follow the the cue of similar tests (using the NewIntegrationTest...) that tests the new permalink setup in a site build.

@bep
Copy link
Member

bep commented Jun 26, 2023

Never mind the comments above. I suspect we need to test this out on some real projects and then do some adjustments/add some tests, so I might as well merge this early.

@bep bep merged commit cc14c6a into gohugoio:master Jun 26, 2023
jmooring added a commit to jmooring/hugo that referenced this pull request Jun 26, 2023
bep pushed a commit that referenced this pull request Jun 27, 2023
@Mai-Lapyst Mai-Lapyst deleted the allow-permalinks-conf-for-branchbundles branch June 27, 2023 19:47
Copy link

This pull request 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 github-actions bot locked as resolved and limited conversation to collaborators Jun 27, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Permalink option for section pages
3 participants