Fix some code issues

This commit is contained in:
Mai-Lapyst 2024-03-15 23:52:38 +01:00
parent 781a37fbe1
commit 8309f008c2
No known key found for this signature in database
GPG key ID: F88D929C09E239F8

View file

@ -10,6 +10,7 @@ import (
"path" "path"
"path/filepath" "path/filepath"
"regexp" "regexp"
"slices"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -64,7 +65,7 @@ var (
validLinksPattern = regexp.MustCompile(`^[a-z][\w-]+://`) validLinksPattern = regexp.MustCompile(`^[a-z][\w-]+://`)
// filePreviewPattern matches "http://domain/org/repo/src/commit/COMMIT/filepath#L1-L2" // filePreviewPattern matches "http://domain/org/repo/src/commit/COMMIT/filepath#L1-L2"
filePreviewPattern = regexp.MustCompile(`https?://((?:\S+/){3})src/commit/([0-9a-f]{7,64})/(\S+)#(L\d+(?:-L\d+)?)`) filePreviewPattern = regexp.MustCompile(`https?://((?:\S+/){4,5})src/commit/([0-9a-f]{7,64})/(\S+)#(L\d+(?:-L\d+)?)`)
// While this email regex is definitely not perfect and I'm sure you can come up // While this email regex is definitely not perfect and I'm sure you can come up
// with edge cases, it is still accepted by the CommonMark specification, as // with edge cases, it is still accepted by the CommonMark specification, as
@ -1075,11 +1076,9 @@ func filePreviewPatternProcessor(ctx *RenderContext, node *html.Node) {
return return
} }
// Ensure that every group (m[0]...m[9]) has a match // Ensure that every group has a match
for i := 0; i < 10; i++ { if slices.Contains(m, -1) {
if m[i] == -1 { return
return
}
} }
urlFull := node.Data[m[0]:m[1]] urlFull := node.Data[m[0]:m[1]]
@ -1089,8 +1088,7 @@ func filePreviewPatternProcessor(ctx *RenderContext, node *html.Node) {
return return
} }
projPath := node.Data[m[2]:m[3]] projPath := strings.TrimSuffix(node.Data[m[2]:m[3]], "/")
projPath = strings.TrimSuffix(projPath, "/")
commitSha := node.Data[m[4]:m[5]] commitSha := node.Data[m[4]:m[5]]
filePath := node.Data[m[6]:m[7]] filePath := node.Data[m[6]:m[7]]