Skip to content

Use ctx.metas for SHA hash links #6645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion modules/markup/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,9 @@ func crossReferenceIssueIndexPatternProcessor(ctx *postProcessCtx, node *html.No

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

// emailAddressProcessor replaces raw email addresses with a mailto: link.
Expand Down
11 changes: 8 additions & 3 deletions modules/markup/html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ import (
"github.com/stretchr/testify/assert"
)

var localMetas = map[string]string{
"user": "gogits",
"repo": "gogs",
}

func TestRender_Commits(t *testing.T) {
setting.AppURL = AppURL
setting.AppSubURL = AppSubURL

test := func(input, expected string) {
buffer := RenderString(".md", input, setting.AppSubURL, nil)
buffer := RenderString(".md", input, setting.AppSubURL, localMetas)
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
}

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

test := func(input, expected string) {
buffer := RenderString("a.md", input, setting.AppSubURL, nil)
buffer := RenderString("a.md", input, setting.AppSubURL, localMetas)
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
}

Expand Down Expand Up @@ -195,7 +200,7 @@ func TestRender_ShortLinks(t *testing.T) {
test := func(input, expected, expectedWiki string) {
buffer := markdown.RenderString(input, tree, nil)
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
buffer = markdown.RenderWiki([]byte(input), setting.AppSubURL, nil)
buffer = markdown.RenderWiki([]byte(input), setting.AppSubURL, localMetas)
assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(string(buffer)))
}

Expand Down
4 changes: 4 additions & 0 deletions modules/markup/markdown/markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ func testAnswers(baseURLContent, baseURLImages string) []string {
<li><a href="` + baseURLContent + `/Tips" rel="nofollow">Tips</a></li>
</ul>

<p>See commit <a href="http://localhost:3000/gogits/gogs/commit/fc7f44dadf" rel="nofollow"><code>fc7f44dadf</code></a></p>

<p>Ideas and codes</p>

<ul>
Expand Down Expand Up @@ -192,6 +194,8 @@ var sameCases = []string{
- [[Links, Language bindings, Engine bindings|Links]]
- [[Tips]]

See commit fc7f44dadf

Ideas and codes

- Bezier widget (by @r-lyeh) ` + AppURL + `ocornut/imgui/issues/786
Expand Down