Skip to content

Commit c43b512

Browse files
committed
output: Identify extension-less text types as text
See #3614
1 parent 19f2e72 commit c43b512

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

‎output/outputFormat.go‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,12 @@ func (formats Formats) FromFilename(filename string) (f Format, found bool) {
219219
}
220220

221221
if ext != "" {
222-
return formats.GetBySuffix(ext)
222+
f, found = formats.GetBySuffix(ext)
223+
if !found && len(parts) == 2 {
224+
// For extensionless output formats (e.g. Netlify's _redirects)
225+
// we must fall back to using the extension as format lookup.
226+
f, found = formats.GetByName(ext)
227+
}
223228
}
224229
return
225230
}

‎output/outputFormat_test.go‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,47 @@ func TestGetFormatByExt(t *testing.T) {
9191
require.False(t, found)
9292
}
9393

94+
func TestGetFormatByFilename(t *testing.T) {
95+
noExtNoDelimMediaType := media.TextType
96+
noExtNoDelimMediaType.Suffix = ""
97+
noExtNoDelimMediaType.Delimiter = ""
98+
99+
noExtMediaType := media.TextType
100+
noExtMediaType.Suffix = ""
101+
102+
var (
103+
noExtDelimFormat = Format{
104+
Name: "NEM",
105+
MediaType: noExtNoDelimMediaType,
106+
BaseName: "_redirects",
107+
}
108+
noExt = Format{
109+
Name: "NEX",
110+
MediaType: noExtMediaType,
111+
BaseName: "next",
112+
}
113+
)
114+
115+
formats := Formats{AMPFormat, HTMLFormat, noExtDelimFormat, noExt, CalendarFormat}
116+
f, found := formats.FromFilename("my.amp.html")
117+
require.True(t, found)
118+
require.Equal(t, AMPFormat, f)
119+
f, found = formats.FromFilename("my.ics")
120+
require.True(t, found)
121+
f, found = formats.FromFilename("my.html")
122+
require.True(t, found)
123+
require.Equal(t, HTMLFormat, f)
124+
f, found = formats.FromFilename("my.nem")
125+
require.True(t, found)
126+
require.Equal(t, noExtDelimFormat, f)
127+
f, found = formats.FromFilename("my.nex")
128+
require.True(t, found)
129+
require.Equal(t, noExt, f)
130+
f, found = formats.FromFilename("my.css")
131+
require.False(t, found)
132+
133+
}
134+
94135
func TestDecodeFormats(t *testing.T) {
95136

96137
mediaTypes := media.Types{media.JSONType, media.XMLType}

0 commit comments

Comments
 (0)