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: Escaping colon in permalinks erroneously throws error in some circumstances #12948

Closed
Tracked by #2737
jmooring opened this issue Oct 16, 2024 · 5 comments · Fixed by #12951
Closed
Tracked by #2737
Assignees
Milestone

Comments

@jmooring
Copy link
Member

jmooring commented Oct 16, 2024

I found this while attempting to document how to escape a colon.

With this front matter:

---
title: p1
url: "/foo\\:bar/:slug/"
---

Hugo throws an error:

Error: error building site: render: error expanding "/foo\\:bar/:slug/": permalink ill-formed

Although not related to this bug, if I had taken the time to weigh in on the original proposal I would have expressed my concerns about OS compatibility. You cannot use this feature on Windows because the path cannot contain a colon (restricted character). We will have to note in the documentation that you can't use this feature on Windows, but that won't really help for projects with multiple contributors where some use Windows.

The existing tests pass on Windows because they don't write to disk.

I think we should consider removing this feature.

@jmooring
Copy link
Member Author

jmooring commented Oct 16, 2024

If we keep the feature I'll move this comment to a separate issue.

This currently works in front matter:

---
title: p1
url: "/special\\::slug/"
---

This currently fails in the site configuration (colon omitted from file path):

[permalinks]
s1 = "/special\\::slug/"

This test case covers both front matter and site configuration:

func TestPermalinksWithEscapedColons(t *testing.T) {
	t.Parallel()

	files := `
-- hugo.toml --
disableKinds = ['home','rss','section','sitemap','taxonomy','term']
[permalinks.page]
s2 = "/c\\:d/:slug/"
-- content/s1/p1.md --
---
title: p1
url: "/a\\:b/:slug/"
---
-- content/s2/p2.md --
---
title: p2
---
-- layouts/_default/single.html --
{{ .Title }}
`

	b := hugolib.Test(t, files)

	b.AssertFileExists("public/a:b/p1/index.html", true)
	b.AssertFileExists("public/c:d/p2/index.html", true)
}

@bep
Copy link
Member

bep commented Oct 16, 2024

@jmooring I have fixed this in #12951 -- but I needed to adjust the test case a little. I have added a comment. The test case looks a little odd, but opening up for colons in Hugo generated paths would create mayhem. I think this falls into the category of "the url set in front matter is the user's responsibility", but I'm kind of already regretting going down this rabbit hole.

@jmooring
Copy link
Member Author

Understood. Use with url front matter only, and don't use it with Windows.

I'm not fond of this rabbit hole either, and would support removing it.

@bep
Copy link
Member

bep commented Oct 16, 2024

I'm not fond of this rabbit hole either, and would support removing it.

Yea, well, maybe. The thing is, this was already possible before we added permalink tokens to the front matter url -- which goes to say that we're now fixing something we accidentally broke. I think it helps to call this a bug fix and not a feature.

There's many characters that's allowed in url front matter, but not in paths created by Hugo, so I think it helps to mentally add ... colon to that list.

func isAllowedPathCharacter(s string, i int, r rune) bool {
	if r == ' ' {
		return false
	}
	// Check for the most likely first (faster).
	isAllowed := unicode.IsLetter(r) || unicode.IsDigit(r)
	isAllowed = isAllowed || r == '.' || r == '/' || r == '\\' || r == '_' || r == '#' || r == '+' || r == '~' || r == '-' || r == '@'
	isAllowed = isAllowed || unicode.IsMark(r)
	isAllowed = isAllowed || (r == '%' && i+2 < len(s) && ishex(s[i+1]) && ishex(s[i+2]))
	return isAllowed
}

bep added a commit to bep/hugo that referenced this issue Oct 16, 2024
@bep bep closed this as completed in a2f666b Oct 16, 2024
Copy link

github-actions bot commented Nov 7, 2024

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

Successfully merging a pull request may close this issue.

2 participants