From 8e949db3b540486a8b5f085ebf2a7ff2d8cea646 Mon Sep 17 00:00:00 2001 From: silverwind Date: Tue, 9 Apr 2019 05:18:48 +0200 Subject: [PATCH] Render SHA1 links as code blocks (#6546) --- modules/markup/html.go | 45 +++++++++++++++++++--------- modules/markup/html_internal_test.go | 6 ++-- modules/markup/html_test.go | 12 ++++---- 3 files changed, 40 insertions(+), 23 deletions(-) diff --git a/modules/markup/html.go b/modules/markup/html.go index a3bf15fe25..136f76d301 100644 --- a/modules/markup/html.go +++ b/modules/markup/html.go @@ -327,22 +327,39 @@ func (ctx *postProcessCtx) textNode(node *html.Node) { } func createLink(href, content string) *html.Node { - textNode := &html.Node{ + a := &html.Node{ + Type: html.ElementNode, + Data: atom.A.String(), + Attr: []html.Attribute{{Key: "href", Val: href}}, + } + text := &html.Node{ Type: html.TextNode, Data: content, } - linkNode := &html.Node{ - FirstChild: textNode, - LastChild: textNode, - Type: html.ElementNode, - Data: "a", - DataAtom: atom.A, - Attr: []html.Attribute{ - {Key: "href", Val: href}, - }, + + a.AppendChild(text) + return a +} + +func createCodeLink(href, content string) *html.Node { + a := &html.Node{ + Type: html.ElementNode, + Data: atom.A.String(), + Attr: []html.Attribute{{Key: "href", Val: href}}, } - textNode.Parent = linkNode - return linkNode + text := &html.Node{ + Type: html.TextNode, + Data: content, + } + + code := &html.Node{ + Type: html.ElementNode, + Data: atom.Code.String(), + } + + code.AppendChild(text) + a.AppendChild(code) + return a } // replaceContent takes a text node, and in its content it replaces a section of @@ -633,7 +650,7 @@ func fullSha1PatternProcessor(ctx *postProcessCtx, node *html.Node) { text += " (" + hash + ")" } - replaceContent(node, start, end, createLink(urlFull, text)) + replaceContent(node, start, end, createCodeLink(urlFull, text)) } // sha1CurrentPatternProcessor renders SHA1 strings to corresponding links that @@ -651,7 +668,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], - createLink(util.URLJoin(ctx.urlPrefix, "commit", hash), base.ShortSha(hash))) + createCodeLink(util.URLJoin(ctx.urlPrefix, "commit", hash), base.ShortSha(hash))) } // emailAddressProcessor replaces raw email addresses with a mailto: link. diff --git a/modules/markup/html_internal_test.go b/modules/markup/html_internal_test.go index cc261318eb..c71948593d 100644 --- a/modules/markup/html_internal_test.go +++ b/modules/markup/html_internal_test.go @@ -195,13 +195,13 @@ func TestRender_AutoLink(t *testing.T) { // render valid commit URLs tmp := util.URLJoin(AppSubURL, "commit", "d8a994ef243349f321568f9e36d5c3f444b99cae") - test(tmp, "d8a994ef24") + test(tmp, "d8a994ef24") tmp += "#diff-2" - test(tmp, "d8a994ef24 (diff-2)") + test(tmp, "d8a994ef24 (diff-2)") // render other commit URLs tmp = "https://external-link.gogs.io/gogs/gogs/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2" - test(tmp, "d8a994ef24 (diff-2)") + test(tmp, "d8a994ef24 (diff-2)") } func TestRender_FullIssueURLs(t *testing.T) { diff --git a/modules/markup/html_test.go b/modules/markup/html_test.go index 6bd9a465b5..6d3da31934 100644 --- a/modules/markup/html_test.go +++ b/modules/markup/html_test.go @@ -30,12 +30,12 @@ func TestRender_Commits(t *testing.T) { var subtree = util.URLJoin(commit, "src") var tree = strings.Replace(subtree, "/commit/", "/tree/", -1) - test(sha, `

b6dd6210ea

`) - test(sha[:7], `

b6dd621

`) - test(sha[:39], `

b6dd6210ea

`) - test(commit, `

b6dd6210ea

`) - test(tree, `

b6dd6210ea/src

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

commit b6dd6210ea

`) + test(sha, `

b6dd6210ea

`) + test(sha[:7], `

b6dd621

`) + test(sha[:39], `

b6dd6210ea

`) + test(commit, `

b6dd6210ea

`) + test(tree, `

b6dd6210ea/src

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

commit b6dd6210ea

`) test("/home/gitea/"+sha, "

/home/gitea/"+sha+"

") }