diff options
Diffstat (limited to 'output')
-rw-r--r-- | output/outputFormat.go | 7 | ||||
-rw-r--r-- | output/outputFormat_test.go | 41 |
2 files changed, 47 insertions, 1 deletions
diff --git a/output/outputFormat.go b/output/outputFormat.go index 6e5f42930..2b75120f5 100644 --- a/output/outputFormat.go +++ b/output/outputFormat.go @@ -219,7 +219,12 @@ func (formats Formats) FromFilename(filename string) (f Format, found bool) { } if ext != "" { - return formats.GetBySuffix(ext) + f, found = formats.GetBySuffix(ext) + if !found && len(parts) == 2 { + // For extensionless output formats (e.g. Netlify's _redirects) + // we must fall back to using the extension as format lookup. + f, found = formats.GetByName(ext) + } } return } diff --git a/output/outputFormat_test.go b/output/outputFormat_test.go index 0540eac08..18b84a0fa 100644 --- a/output/outputFormat_test.go +++ b/output/outputFormat_test.go @@ -91,6 +91,47 @@ func TestGetFormatByExt(t *testing.T) { require.False(t, found) } +func TestGetFormatByFilename(t *testing.T) { + noExtNoDelimMediaType := media.TextType + noExtNoDelimMediaType.Suffix = "" + noExtNoDelimMediaType.Delimiter = "" + + noExtMediaType := media.TextType + noExtMediaType.Suffix = "" + + var ( + noExtDelimFormat = Format{ + Name: "NEM", + MediaType: noExtNoDelimMediaType, + BaseName: "_redirects", + } + noExt = Format{ + Name: "NEX", + MediaType: noExtMediaType, + BaseName: "next", + } + ) + + formats := Formats{AMPFormat, HTMLFormat, noExtDelimFormat, noExt, CalendarFormat} + f, found := formats.FromFilename("my.amp.html") + require.True(t, found) + require.Equal(t, AMPFormat, f) + f, found = formats.FromFilename("my.ics") + require.True(t, found) + f, found = formats.FromFilename("my.html") + require.True(t, found) + require.Equal(t, HTMLFormat, f) + f, found = formats.FromFilename("my.nem") + require.True(t, found) + require.Equal(t, noExtDelimFormat, f) + f, found = formats.FromFilename("my.nex") + require.True(t, found) + require.Equal(t, noExt, f) + f, found = formats.FromFilename("my.css") + require.False(t, found) + +} + func TestDecodeFormats(t *testing.T) { mediaTypes := media.Types{media.JSONType, media.XMLType} |