Skip to content

Commit 1bce189

Browse files
mrsdizziezeripath
authored andcommitted
Use ctx.metas for SHA hash links (#6645)
Since #6273 was merged, we now have access to proper context metas always. Update SHA generated links to use these instead of urlPrefix. Update tests as well. Fixes #4536.
1 parent e98565d commit 1bce189

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

modules/markup/html.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,9 @@ func crossReferenceIssueIndexPatternProcessor(ctx *postProcessCtx, node *html.No
625625

626626
// fullSha1PatternProcessor renders SHA containing URLs
627627
func fullSha1PatternProcessor(ctx *postProcessCtx, node *html.Node) {
628+
if ctx.metas == nil {
629+
return
630+
}
628631
m := anySHA1Pattern.FindStringSubmatchIndex(node.Data)
629632
if m == nil {
630633
return
@@ -686,7 +689,7 @@ func sha1CurrentPatternProcessor(ctx *postProcessCtx, node *html.Node) {
686689
// Although unlikely, deadbeef and 1234567 are valid short forms of SHA1 hash
687690
// as used by git and github for linking and thus we have to do similar.
688691
replaceContent(node, m[2], m[3],
689-
createCodeLink(util.URLJoin(ctx.urlPrefix, "commit", hash), base.ShortSha(hash)))
692+
createCodeLink(util.URLJoin(setting.AppURL, ctx.metas["user"], ctx.metas["repo"], "commit", hash), base.ShortSha(hash)))
690693
}
691694

692695
// emailAddressProcessor replaces raw email addresses with a mailto: link.

modules/markup/html_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,17 @@ import (
1616
"github.com/stretchr/testify/assert"
1717
)
1818

19+
var localMetas = map[string]string{
20+
"user": "gogits",
21+
"repo": "gogs",
22+
}
23+
1924
func TestRender_Commits(t *testing.T) {
2025
setting.AppURL = AppURL
2126
setting.AppSubURL = AppSubURL
2227

2328
test := func(input, expected string) {
24-
buffer := RenderString(".md", input, setting.AppSubURL, nil)
29+
buffer := RenderString(".md", input, setting.AppSubURL, localMetas)
2530
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
2631
}
2732

@@ -45,7 +50,7 @@ func TestRender_CrossReferences(t *testing.T) {
4550
setting.AppSubURL = AppSubURL
4651

4752
test := func(input, expected string) {
48-
buffer := RenderString("a.md", input, setting.AppSubURL, nil)
53+
buffer := RenderString("a.md", input, setting.AppSubURL, localMetas)
4954
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
5055
}
5156

@@ -195,7 +200,7 @@ func TestRender_ShortLinks(t *testing.T) {
195200
test := func(input, expected, expectedWiki string) {
196201
buffer := markdown.RenderString(input, tree, nil)
197202
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
198-
buffer = markdown.RenderWiki([]byte(input), setting.AppSubURL, nil)
203+
buffer = markdown.RenderWiki([]byte(input), setting.AppSubURL, localMetas)
199204
assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(string(buffer)))
200205
}
201206

modules/markup/markdown/markdown_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ func testAnswers(baseURLContent, baseURLImages string) []string {
103103
<li><a href="` + baseURLContent + `/Tips" rel="nofollow">Tips</a></li>
104104
</ul>
105105
106+
<p>See commit <a href="http://localhost:3000/gogits/gogs/commit/fc7f44dadf" rel="nofollow"><code>fc7f44dadf</code></a></p>
107+
106108
<p>Ideas and codes</p>
107109
108110
<ul>
@@ -192,6 +194,8 @@ var sameCases = []string{
192194
- [[Links, Language bindings, Engine bindings|Links]]
193195
- [[Tips]]
194196
197+
See commit fc7f44dadf
198+
195199
Ideas and codes
196200
197201
- Bezier widget (by @r-lyeh) ` + AppURL + `ocornut/imgui/issues/786

0 commit comments

Comments
 (0)