Skip to content

Commit 4be59eb

Browse files
authored
Render links for commit hashes followed by comma (#14224) (#14227)
Regex test cases: https://regex101.com/r/mVbPxM/2/ fixes #14223
1 parent 450b32c commit 4be59eb

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

modules/markup/html.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ var (
4343
// sha1CurrentPattern matches string that represents a commit SHA, e.g. d8a994ef243349f321568f9e36d5c3f444b99cae
4444
// Although SHA1 hashes are 40 chars long, the regex matches the hash from 7 to 40 chars in length
4545
// so that abbreviated hash links can be used as well. This matches git and github useability.
46-
sha1CurrentPattern = regexp.MustCompile(`(?:\s|^|\(|\[)([0-9a-f]{7,40})(?:\s|$|\)|\]|\.(\s|$))`)
46+
sha1CurrentPattern = regexp.MustCompile(`(?:\s|^|\(|\[)([0-9a-f]{7,40})(?:\s|$|\)|\]|[.,](\s|$))`)
4747

4848
// shortLinkPattern matches short but difficult to parse [[name|link|arg=test]] syntax
4949
shortLinkPattern = regexp.MustCompile(`\[\[(.*?)\]\](\w*)`)

modules/markup/html_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ func TestRender_Commits(t *testing.T) {
4646
test("/home/gitea/"+sha, "<p>/home/gitea/"+sha+"</p>")
4747
test("deadbeef", `<p>deadbeef</p>`)
4848
test("d27ace93", `<p>d27ace93</p>`)
49+
test(sha[:14]+".x", `<p>`+sha[:14]+`.x</p>`)
50+
51+
expected14 := `<a href="` + commit[:len(commit)-(40-14)] + `" rel="nofollow"><code>` + sha[:10] + `</code></a>`
52+
test(sha[:14]+".", `<p>`+expected14+`.</p>`)
53+
test(sha[:14]+",", `<p>`+expected14+`,</p>`)
54+
test("["+sha[:14]+"]", `<p>[`+expected14+`]</p>`)
4955
}
5056

5157
func TestRender_CrossReferences(t *testing.T) {

0 commit comments

Comments
 (0)