From ec68f4e4b35def946517aca97b2af501ee6caf31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauris=20Buk=C5=A1is-Haberkorns?= Date: Thu, 4 May 2017 23:46:02 +0300 Subject: [PATCH 1/3] Fix commit sha1 URL rendering in markdown --- modules/markdown/markdown.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/markdown/markdown.go b/modules/markdown/markdown.go index 85be5d61de91e..b67a03b5594c0 100644 --- a/modules/markdown/markdown.go +++ b/modules/markdown/markdown.go @@ -56,7 +56,7 @@ var ( // Sha1CurrentPattern matches string that represents a commit SHA, e.g. d8a994ef243349f321568f9e36d5c3f444b99cae // FIXME: this pattern matches pure numbers as well, right now we do a hack to check in renderSha1CurrentPattern // by converting string to a number. - Sha1CurrentPattern = regexp.MustCompile(`(?:^|\s|\()[0-9a-f]{40}\b`) + Sha1CurrentPattern = regexp.MustCompile(`(?:^|\s|\()([0-9a-f]{40})\b`) // ShortLinkPattern matches short but difficult to parse [[name|link|arg=test]] syntax ShortLinkPattern = regexp.MustCompile(`(\[\[.*\]\]\w*)`) @@ -542,7 +542,7 @@ func RenderCrossReferenceIssueIndexPattern(rawBytes []byte, urlPrefix string, me func renderSha1CurrentPattern(rawBytes []byte, urlPrefix string) []byte { ms := Sha1CurrentPattern.FindAllSubmatch(rawBytes, -1) for _, m := range ms { - all := m[0] + all := m[1] if com.StrTo(all).MustInt() > 0 { continue } From 88812b25350ecffc2c34f7816920083f4139d8a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauris=20Buk=C5=A1is-Haberkorns?= Date: Fri, 5 May 2017 00:14:17 +0300 Subject: [PATCH 2/3] Add unit test for commit sha1 markdown rendering when sha1 has space before it --- modules/markdown/markdown_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/markdown/markdown_test.go b/modules/markdown/markdown_test.go index 8364146573f51..e6bc3683c6194 100644 --- a/modules/markdown/markdown_test.go +++ b/modules/markdown/markdown_test.go @@ -296,6 +296,7 @@ func TestRender_Commits(t *testing.T) { test(sha, `

b6dd6210ea

`) test(commit, `

b6dd6210ea

`) test(tree, `

b6dd6210ea/src

`) + test("commit "+sha, `

commit b6dd6210ea

`) } func TestRender_Images(t *testing.T) { From d19ca624dc515a3652abe51ad41e2333080f1d86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauris=20Buk=C5=A1is-Haberkorns?= Date: Fri, 5 May 2017 05:41:47 +0300 Subject: [PATCH 3/3] Change to better variable name --- modules/markdown/markdown.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/markdown/markdown.go b/modules/markdown/markdown.go index b67a03b5594c0..8f68c5516cc8b 100644 --- a/modules/markdown/markdown.go +++ b/modules/markdown/markdown.go @@ -542,12 +542,12 @@ func RenderCrossReferenceIssueIndexPattern(rawBytes []byte, urlPrefix string, me func renderSha1CurrentPattern(rawBytes []byte, urlPrefix string) []byte { ms := Sha1CurrentPattern.FindAllSubmatch(rawBytes, -1) for _, m := range ms { - all := m[1] - if com.StrTo(all).MustInt() > 0 { + hash := m[1] + if com.StrTo(hash).MustInt() > 0 { continue } - rawBytes = bytes.Replace(rawBytes, all, []byte(fmt.Sprintf( - `%s`, URLJoin(urlPrefix, "commit", string(all)), base.ShortSha(string(all)))), -1) + rawBytes = bytes.Replace(rawBytes, hash, []byte(fmt.Sprintf( + `%s`, URLJoin(urlPrefix, "commit", string(hash)), base.ShortSha(string(hash)))), -1) } return rawBytes }