Skip to content

Commit 9a0b0da

Browse files
lafrikslunny
authored andcommitted
Fix commit sha1 URL rendering in markdown (#1677)
* Fix commit sha1 URL rendering in markdown * Add unit test for commit sha1 markdown rendering when sha1 has space before it * Change to better variable name
1 parent 2e17dda commit 9a0b0da

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

modules/markdown/markdown.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ var (
5656
// Sha1CurrentPattern matches string that represents a commit SHA, e.g. d8a994ef243349f321568f9e36d5c3f444b99cae
5757
// FIXME: this pattern matches pure numbers as well, right now we do a hack to check in renderSha1CurrentPattern
5858
// by converting string to a number.
59-
Sha1CurrentPattern = regexp.MustCompile(`(?:^|\s|\()[0-9a-f]{40}\b`)
59+
Sha1CurrentPattern = regexp.MustCompile(`(?:^|\s|\()([0-9a-f]{40})\b`)
6060

6161
// ShortLinkPattern matches short but difficult to parse [[name|link|arg=test]] syntax
6262
ShortLinkPattern = regexp.MustCompile(`(\[\[.*\]\]\w*)`)
@@ -542,12 +542,12 @@ func RenderCrossReferenceIssueIndexPattern(rawBytes []byte, urlPrefix string, me
542542
func renderSha1CurrentPattern(rawBytes []byte, urlPrefix string) []byte {
543543
ms := Sha1CurrentPattern.FindAllSubmatch(rawBytes, -1)
544544
for _, m := range ms {
545-
all := m[0]
546-
if com.StrTo(all).MustInt() > 0 {
545+
hash := m[1]
546+
if com.StrTo(hash).MustInt() > 0 {
547547
continue
548548
}
549-
rawBytes = bytes.Replace(rawBytes, all, []byte(fmt.Sprintf(
550-
`<a href="%s">%s</a>`, URLJoin(urlPrefix, "commit", string(all)), base.ShortSha(string(all)))), -1)
549+
rawBytes = bytes.Replace(rawBytes, hash, []byte(fmt.Sprintf(
550+
`<a href="%s">%s</a>`, URLJoin(urlPrefix, "commit", string(hash)), base.ShortSha(string(hash)))), -1)
551551
}
552552
return rawBytes
553553
}

modules/markdown/markdown_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ func TestRender_Commits(t *testing.T) {
296296
test(sha, `<p><a href="`+commit+`" rel="nofollow">b6dd6210ea</a></p>`)
297297
test(commit, `<p><a href="`+commit+`" rel="nofollow">b6dd6210ea</a></p>`)
298298
test(tree, `<p><a href="`+src+`" rel="nofollow">b6dd6210ea/src</a></p>`)
299+
test("commit "+sha, `<p>commit <a href="`+commit+`" rel="nofollow">b6dd6210ea</a></p>`)
299300
}
300301

301302
func TestRender_Images(t *testing.T) {

0 commit comments

Comments
 (0)