Skip to content

Commit

Permalink
Fix surprise OutputFormat.Rel overwriting
Browse files Browse the repository at this point in the history
In page.NewOutputFormat, we take an output.Format f and use it to
create a page.OutputFormat. If the format is canonical, we assign
the final OutputFormat's Rel to "canonical" rather than using
f.Rel. However, this leads to unexpected behavior for custom
output formats, where a user can define a "rel" for a format
via the config file.

For example, the standard for "humans.txt" files requires using
rel="author" in HTML "link" elements. Meanwhile, humans.txt is
usually the only format used for its content. As a result, for
Hugo configurations that define a humans.txt custom output format,
Hugo will render "link" elements to content in this format with
rel="canonical," rather than "author" as required by the standard.

This commit changes page.NewOutputFormat to check whether a given
format is user defined and, if so, skips assigning Rel to
"canonical," even if isCanonical is true.

Fixes #8030
  • Loading branch information
ptgott authored and bep committed Jan 4, 2022
1 parent d632dd7 commit d3c4fdb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
30 changes: 30 additions & 0 deletions hugolib/site_output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,36 @@ baseName = "customdelimbase"
c.Assert(outputs.Get("CUS").RelPermalink(), qt.Equals, "/blog/customdelimbase_del")
}

// Issue 8030
func TestGetOutputFormatRel(t *testing.T) {
b := newTestSitesBuilder(t).
WithSimpleConfigFileAndSettings(map[string]interface{}{
"outputFormats": map[string]interface{}{
"humansTXT": map[string]interface{}{
"name": "HUMANS",
"mediaType": "text/plain",
"baseName": "humans",
"isPlainText": true,
"rel": "author",
},
},
}).WithTemplates("index.html", `
{{- with ($.Site.GetPage "humans").OutputFormats.Get "humans" -}}
<link rel="{{ .Rel }}" type="{{ .MediaType.String }}" href="{{ .Permalink }}">
{{- end -}}
`).WithContent("humans.md", `---
outputs:
- HUMANS
---
This is my content.
`)

b.Build(BuildCfg{})
b.AssertFileContent("public/index.html", `
<link rel="author" type="text/plain" href="/humans.txt">
`)
}

func TestCreateSiteOutputFormats(t *testing.T) {
t.Run("Basic", func(t *testing.T) {
c := qt.New(t)
Expand Down
12 changes: 11 additions & 1 deletion resources/page/page_outputformat.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,18 @@ func (o OutputFormat) RelPermalink() string {
}

func NewOutputFormat(relPermalink, permalink string, isCanonical bool, f output.Format) OutputFormat {
isUserConfigured := true
for _, d := range output.DefaultFormats {
if strings.EqualFold(d.Name, f.Name) {
isUserConfigured = false
}
}
rel := f.Rel
if isCanonical {
// If the output format is the canonical format for the content, we want
// to specify this in the "rel" attribute of an HTML "link" element.
// However, for custom output formats, we don't want to surprise users by
// overwriting "rel"
if isCanonical && !isUserConfigured {
rel = "canonical"
}
return OutputFormat{Rel: rel, Format: f, relPermalink: relPermalink, permalink: permalink}
Expand Down

0 comments on commit d3c4fdb

Please sign in to comment.