diff options
author | Lauris BH <lauris@nix.lv> | 2017-05-05 11:49:13 +0300 |
---|---|---|
committer | Lunny Xiao <xiaolunwen@gmail.com> | 2017-05-05 16:49:13 +0800 |
commit | 9a0b0da1fe1a32b8ff5cc09f97687395fec2ed57 (patch) | |
tree | d68e7c45dacdca5add1d653c38d180d35e954633 | |
parent | 2e17dda8bb583d2a088589ca4d37ce2d4131b0e7 (diff) | |
download | gitea-9a0b0da1fe1a32b8ff5cc09f97687395fec2ed57.tar.gz gitea-9a0b0da1fe1a32b8ff5cc09f97687395fec2ed57.zip |
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
-rw-r--r-- | modules/markdown/markdown.go | 10 | ||||
-rw-r--r-- | modules/markdown/markdown_test.go | 1 |
2 files changed, 6 insertions, 5 deletions
diff --git a/modules/markdown/markdown.go b/modules/markdown/markdown.go index 85be5d61de..8f68c5516c 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,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[0] - if com.StrTo(all).MustInt() > 0 { + hash := m[1] + if com.StrTo(hash).MustInt() > 0 { continue } - rawBytes = bytes.Replace(rawBytes, all, []byte(fmt.Sprintf( - `<a href="%s">%s</a>`, URLJoin(urlPrefix, "commit", string(all)), base.ShortSha(string(all)))), -1) + rawBytes = bytes.Replace(rawBytes, hash, []byte(fmt.Sprintf( + `<a href="%s">%s</a>`, URLJoin(urlPrefix, "commit", string(hash)), base.ShortSha(string(hash)))), -1) } return rawBytes } diff --git a/modules/markdown/markdown_test.go b/modules/markdown/markdown_test.go index 8364146573..e6bc3683c6 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, `<p><a href="`+commit+`" rel="nofollow">b6dd6210ea</a></p>`) test(commit, `<p><a href="`+commit+`" rel="nofollow">b6dd6210ea</a></p>`) test(tree, `<p><a href="`+src+`" rel="nofollow">b6dd6210ea/src</a></p>`) + test("commit "+sha, `<p>commit <a href="`+commit+`" rel="nofollow">b6dd6210ea</a></p>`) } func TestRender_Images(t *testing.T) { |