regis
August 16, 2021, 2:44pm
1
For one project, I need an xml feed to live at /feed.rss
instead of /index.xml
. Now I tried adding the rss
suffix to the application/rss+xml
mediaType before using it on my custom output format.
mediaTypes:
application/rss+xml:
suffixes:
- xml
- rss
outputFormats:
rss_feed:
mediaType: application/rss+xml
baseName: feed
Now how can I have the file generated at /feed.rss
instead of /feed.xml
. I tried using a layout called index.rss_feed.rss
but it is not being picked up (the output format file is not created)
Any ideas?
Thanks a ton!
@regis
The following emits both feed.rss and index.xml for me:
config.toml
[mediaTypes."application/rss+xml"]
suffixes = ["rss","xml"]
[outputs]
home = ["HTML", "RSS", "rss_feed"]
[outputFormats]
[outputFormats.rss_feed]
mediaType = "application/rss+xml"
baseName = "feed"
notAlternatives = true
copied from internal Hugo template
layouts/index.rss_feed.rss
HTH
Previous attempts
Do you need both types of RSS files (index.xml and feed.rss)? I think what is happening is the default xml layout is getting picked up first.
EDIT: What happens if you reverse the order of suffixes?
EDIT: Sorry realized you looked at this already after the fact.
Looking at Custom output formats | Hugo
The key distinction for Hugo versions 0.20 and newer is that Hugo looks at an output format’s Name
and MediaType’s Suffixes
when choosing the template used to render a given Page
.
I think you will have to create a new template name something like:
layouts/_default/rss_feed.rss
bep
August 16, 2021, 5:30pm
4
I think that the only current way to create outputs for different suffix for a given media type is to create multiple media types. We could (and probably should) add a “suffix selector” to output format config …
1 Like
regis
August 16, 2021, 11:50pm
5
Thank you all for your answers. I created an issue to follow up this discussion:
opened 11:45PM - 16 Aug 21 UTC
Proposal
## The problem
While this is possible and recommended to define any mediaType… 's extensions as an array of suffices, it is not currently possible to choose which suffix to use when generating a file from an output format.
To clarify, if I...
1. add the `rss` suffix to the built-in `application/rss+xml` media
2. and use this media type on custom output format
```yaml
# 1
mediaTypes:
application/rss+xml:
suffixes:
- xml
- rss
# 2
outputFormats:
rss_feed:
mediaType: application/rss+xml
baseName: feed
```
The `rss_feed` output format will generate a `feed.xml` file, using the first suffix.
Even though several suffixes are declared on the media type, it is impossible to choose one when assigning it to an output format.
## proposed solution 1 (as suggested [here](https://discourse.gohugo.io/t/mediatypes-how-to-choose-which-suffix/34294/4?u=regis))
Adding a `suffix` key to the `outputFormat`.
**If set**, Hugo would use the given `suffix` when generating the file.
**If not set**, current behaviour prevails, it would use the first suffix.
```yaml
# 2
outputFormats:
rss_feed:
mediaType: application/rss+xml
baseName: feed
suffix: rss
```
Would generate `/feed.rss`
## proposed solution 2
Use the extension of the template file if matching any of the mediaType suffixes.
if `index.rss_feed.rss` is found, this means the file should be generated at `feed.rss`
if `index.rss_feed.xml` is found, this means the file should be generated at `feed.xml`
If both files are found, first suffix of the set output format should be used or both files could be created...