mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-22 21:42:50 +02:00
media: Allow multiple file suffixes per media type
Before this commit, `Suffix` on `MediaType` was used both to set a custom file suffix and as a way to augment the mediatype definition (what you see after the "+", e.g. "image/svg+xml"). This had its limitations. For one, it was only possible with one file extension per MIME type. Now you can specify multiple file suffixes using "suffixes", but you need to specify the full MIME type identifier: [mediaTypes] [mediaTypes."image/svg+xml"] suffixes = ["svg", "abc ] In most cases, it will be enough to just change: [mediaTypes] [mediaTypes."my/custom-mediatype"] suffix = "txt" To: [mediaTypes] [mediaTypes."my/custom-mediatype"] suffixes = ["txt"] Hugo will still respect values set in "suffix" if no value for "suffixes" is provided, but this will be removed in a future release. Note that you can still get the Media Type's suffix from a template: {{ $mediaType.Suffix }}. But this will now map to the MIME type filename. Fixes #4920
This commit is contained in:
@@ -72,7 +72,7 @@ func createLayoutExamples() interface{} {
|
||||
Example: example.name,
|
||||
Kind: example.d.Kind,
|
||||
OutputFormat: example.f.Name,
|
||||
Suffix: example.f.MediaType.Suffix,
|
||||
Suffix: example.f.MediaType.Suffix(),
|
||||
Layouts: makeLayoutsPresentable(layouts)})
|
||||
}
|
||||
|
||||
|
@@ -47,7 +47,7 @@ type LayoutHandler struct {
|
||||
|
||||
type layoutCacheKey struct {
|
||||
d LayoutDescriptor
|
||||
f Format
|
||||
f string
|
||||
}
|
||||
|
||||
// NewLayoutHandler creates a new LayoutHandler.
|
||||
@@ -60,7 +60,7 @@ func NewLayoutHandler() *LayoutHandler {
|
||||
func (l *LayoutHandler) For(d LayoutDescriptor, f Format) ([]string, error) {
|
||||
|
||||
// We will get lots of requests for the same layouts, so avoid recalculations.
|
||||
key := layoutCacheKey{d, f}
|
||||
key := layoutCacheKey{d, f.Name}
|
||||
l.mu.RLock()
|
||||
if cacheVal, found := l.cache[key]; found {
|
||||
l.mu.RUnlock()
|
||||
@@ -209,7 +209,7 @@ func (l *layoutBuilder) resolveVariations() []string {
|
||||
"TYPE", typeVar,
|
||||
"LAYOUT", layoutVar,
|
||||
"VARIATIONS", variation,
|
||||
"EXTENSION", l.f.MediaType.Suffix,
|
||||
"EXTENSION", l.f.MediaType.Suffix(),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
@@ -27,11 +27,11 @@ import (
|
||||
func TestLayout(t *testing.T) {
|
||||
|
||||
noExtNoDelimMediaType := media.TextType
|
||||
noExtNoDelimMediaType.Suffix = ""
|
||||
noExtNoDelimMediaType.Suffixes = nil
|
||||
noExtNoDelimMediaType.Delimiter = ""
|
||||
|
||||
noExtMediaType := media.TextType
|
||||
noExtMediaType.Suffix = ""
|
||||
noExtMediaType.Suffixes = nil
|
||||
|
||||
var (
|
||||
ampType = Format{
|
||||
@@ -47,6 +47,7 @@ func TestLayout(t *testing.T) {
|
||||
MediaType: noExtNoDelimMediaType,
|
||||
BaseName: "_redirects",
|
||||
}
|
||||
|
||||
noExt = Format{
|
||||
Name: "NEX",
|
||||
MediaType: noExtMediaType,
|
||||
|
@@ -178,7 +178,7 @@ func (formats Formats) Less(i, j int) bool { return formats[i].Name < formats[j]
|
||||
// The lookup is case insensitive.
|
||||
func (formats Formats) GetBySuffix(suffix string) (f Format, found bool) {
|
||||
for _, ff := range formats {
|
||||
if strings.EqualFold(suffix, ff.MediaType.Suffix) {
|
||||
if strings.EqualFold(suffix, ff.MediaType.Suffix()) {
|
||||
if found {
|
||||
// ambiguous
|
||||
found = false
|
||||
@@ -331,7 +331,7 @@ func decode(mediaTypes media.Types, input, output interface{}) error {
|
||||
}
|
||||
|
||||
func (formats Format) BaseFilename() string {
|
||||
return formats.BaseName + "." + formats.MediaType.Suffix
|
||||
return formats.BaseName + formats.MediaType.FullSuffix()
|
||||
}
|
||||
|
||||
func (formats Format) MarshalJSON() ([]byte, error) {
|
||||
|
@@ -93,11 +93,11 @@ func TestGetFormatByExt(t *testing.T) {
|
||||
|
||||
func TestGetFormatByFilename(t *testing.T) {
|
||||
noExtNoDelimMediaType := media.TextType
|
||||
noExtNoDelimMediaType.Suffix = ""
|
||||
noExtNoDelimMediaType.OldSuffix = ""
|
||||
noExtNoDelimMediaType.Delimiter = ""
|
||||
|
||||
noExtMediaType := media.TextType
|
||||
noExtMediaType.Suffix = ""
|
||||
noExtMediaType.OldSuffix = ""
|
||||
|
||||
var (
|
||||
noExtDelimFormat = Format{
|
||||
|
Reference in New Issue
Block a user