diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2019-05-02 09:54:26 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2019-05-02 14:23:16 +0200 |
commit | 6b76841b052b97625b8995f326d758b89f5c2349 (patch) | |
tree | 0a29c666b6e11e6c73f64da4beb0ca86d3965da5 /output/outputFormat.go | |
parent | bcbed4ebdaf55b67abc521d69bba456c041a7e7d (diff) | |
download | hugo-6b76841b052b97625b8995f326d758b89f5c2349.tar.gz hugo-6b76841b052b97625b8995f326d758b89f5c2349.zip |
output: Fix permalink in sitemap etc. when multiple permalinkable output formats
In Hugo 0.55.0 we made AMP `permalinkable`. We also render the output formats in their natural sort order, meaning `AMP` will be rendered before `HTML`. References in the sitemap would then point to the AMP version, and this is normally not what you'd want.
This commit fixes that by making `HTML` by default sort before the others.
If this is not you want, you can set `weight` on the output format configuration.
Fixes #5910
Diffstat (limited to 'output/outputFormat.go')
-rw-r--r-- | output/outputFormat.go | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/output/outputFormat.go b/output/outputFormat.go index 9f371f856..c9c108ac5 100644 --- a/output/outputFormat.go +++ b/output/outputFormat.go @@ -78,6 +78,9 @@ type Format struct { // example. AMP would, however, be a good example of an output format where this // behaviour is wanted. Permalinkable bool `json:"permalinkable"` + + // Setting this to a non-zero value will be used as the first sort criteria. + Weight int `json:"weight"` } // An ordered list of built-in output formats. @@ -125,6 +128,10 @@ var ( Rel: "canonical", IsHTML: true, Permalinkable: true, + + // Weight will be used as first sort criteria. HTML will, by default, + // be rendered first, but set it to 10 so it's easy to put one above it. + Weight: 10, } JSONFormat = Format{ @@ -180,9 +187,21 @@ func init() { // Formats is a slice of Format. type Formats []Format -func (formats Formats) Len() int { return len(formats) } -func (formats Formats) Swap(i, j int) { formats[i], formats[j] = formats[j], formats[i] } -func (formats Formats) Less(i, j int) bool { return formats[i].Name < formats[j].Name } +func (formats Formats) Len() int { return len(formats) } +func (formats Formats) Swap(i, j int) { formats[i], formats[j] = formats[j], formats[i] } +func (formats Formats) Less(i, j int) bool { + fi, fj := formats[i], formats[j] + if fi.Weight == fj.Weight { + return fi.Name < fj.Name + } + + if fj.Weight == 0 { + return true + } + + return fi.Weight > 0 && fi.Weight < fj.Weight + +} // GetBySuffix gets a output format given as suffix, e.g. "html". // It will return false if no format could be found, or if the suffix given |