diff --git a/internal/highlight/highlight.go b/internal/highlight/highlight.go index e0f26f5572b7..640c0aee0209 100644 --- a/internal/highlight/highlight.go +++ b/internal/highlight/highlight.go @@ -6,6 +6,7 @@ import ( "fmt" "html/template" "net/http" + "path" "strings" "time" "unicode/utf8" @@ -165,6 +166,8 @@ func Code(ctx context.Context, p Params) (h template.HTML, aborted bool, err err stabilizeTimeout = 30 * time.Second } + p.Filepath = normalizeFilepath(p.Filepath) + resp, err := client.Highlight(ctx, &gosyntect.Query{ Code: code, Filepath: p.Filepath, @@ -510,3 +513,23 @@ func splitHighlightedLines(input template.HTML) ([]template.HTML, error) { return lines, nil } + +// normalizeFilepath ensures that the filepath p has a lowercase extension, i.e. it applies the +// following transformations: +// +// a/b/c/FOO.TXT → a/b/c/FOO.txt +// FOO.Sh → FOO.sh +// +// The following are left unmodified, as they already have lowercase extensions: +// +// a/b/c/FOO.txt +// a/b/c/Makefile +// Makefile.am +// FOO.txt +// +// It expects the filepath uses forward slashes always. +func normalizeFilepath(p string) string { + ext := path.Ext(p) + ext = strings.ToLower(ext) + return p[:len(p)-len(ext)] + ext +} diff --git a/internal/highlight/highlight_test.go b/internal/highlight/highlight_test.go index 7d263480e8fa..746634f123d5 100644 --- a/internal/highlight/highlight_test.go +++ b/internal/highlight/highlight_test.go @@ -218,3 +218,50 @@ line3` t.Fatalf("wrong highlighted lines: %s", diff) } } + +func Test_normalizeFilepath(t *testing.T) { + tests := []struct { + name string + input string + want string + }{ + { + name: "normalize_path", + input: "a/b/c/FOO.TXT", + want: "a/b/c/FOO.txt", + }, + { + name: "normalize_partial_path", + input: "FOO.Sh", + want: "FOO.sh", + }, + { + name: "unmodified_path", + input: "a/b/c/FOO.txt", + want: "a/b/c/FOO.txt", + }, + { + name: "unmodified_path_no_extension", + input: "a/b/c/Makefile", + want: "a/b/c/Makefile", + }, + { + name: "unmodified_partial_path_no_extension", + input: "Makefile", + want: "Makefile", + }, + { + name: "unmodified_partial_path_extension", + input: "Makefile.am", + want: "Makefile.am", + }, + } + for _, tst := range tests { + t.Run(tst.name, func(t *testing.T) { + got := normalizeFilepath(tst.input) + if diff := cmp.Diff(got, tst.want); diff != "" { + t.Fatalf("mismatch (-want +got):\n%s", diff) + } + }) + } +}