Skip to content

Commit

Permalink
fix commit subject DefaultLink
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed Nov 16, 2024
1 parent 526286b commit ebd88a5
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 43 deletions.
25 changes: 10 additions & 15 deletions modules/markup/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package markup

import (
"bytes"
"fmt"
"html/template"
"io"
"regexp"
"strings"
Expand Down Expand Up @@ -196,13 +198,6 @@ func RenderCommitMessage(
content string,
) (string, error) {
procs := commitMessageProcessors
if ctx.DefaultLink != "" {
// we don't have to fear data races, because being
// commitMessageProcessors of fixed len and cap, every time we append
// something to it the slice is realloc+copied, so append always
// generates the slice ex-novo.
procs = append(procs, genDefaultLinkProcessor(ctx.DefaultLink))
}
return renderProcessString(ctx, procs, content)
}

Expand Down Expand Up @@ -230,17 +225,17 @@ var emojiProcessors = []processor{
// which changes every text node into a link to the passed default link.
func RenderCommitMessageSubject(
ctx *RenderContext,
content string,
defaultLink, content string,
) (string, error) {
procs := commitMessageSubjectProcessors
if ctx.DefaultLink != "" {
// we don't have to fear data races, because being
// commitMessageSubjectProcessors of fixed len and cap, every time we
// append something to it the slice is realloc+copied, so append always
// generates the slice ex-novo.
procs = append(procs, genDefaultLinkProcessor(ctx.DefaultLink))
rendered, err := renderProcessString(ctx, procs, content)
if err != nil {
return "", err
}
return renderProcessString(ctx, procs, content)
if defaultLink != "" {
rendered = fmt.Sprintf(`<a href="%s" class="muted">%s</a>`, template.HTMLEscapeString(defaultLink), rendered)
}
return rendered, nil
}

// RenderIssueTitle to process title on individual issue/pull page
Expand Down
19 changes: 0 additions & 19 deletions modules/markup/html_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,25 +200,6 @@ func linkProcessor(ctx *RenderContext, node *html.Node) {
}
}

func genDefaultLinkProcessor(defaultLink string) processor {
return func(ctx *RenderContext, node *html.Node) {
ch := &html.Node{
Parent: node,
Type: html.TextNode,
Data: node.Data,
}

node.Type = html.ElementNode
node.Data = "a"
node.DataAtom = atom.A
node.Attr = []html.Attribute{
{Key: "href", Val: defaultLink},
{Key: "class", Val: "default-link muted"},
}
node.FirstChild, node.LastChild = ch, ch
}
}

// descriptionLinkProcessor creates links for DescriptionHTML
func descriptionLinkProcessor(ctx *RenderContext, node *html.Node) {
next := node.NextSibling
Expand Down
1 change: 0 additions & 1 deletion modules/markup/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ type RenderContext struct {
// markdownLineBreakStyle (comment, document)
Metas map[string]string

DefaultLink string // TODO: need to figure out
GitRepo *git.Repository
Repo gitrepo.Repository
ShaExistCache map[string]bool
Expand Down
11 changes: 5 additions & 6 deletions modules/templates/util_render.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,18 @@ func (ut *RenderUtils) RenderCommitMessageLinkSubject(msg, urlDefault string, me
}
msgLine = strings.TrimRightFunc(msgLine, unicode.IsSpace)
if len(msgLine) == 0 {
return template.HTML("")
return ""
}

// we can safely assume that it will not return any error, since there
// shouldn't be any special HTML.
renderedMessage, err := markup.RenderCommitMessageSubject(&markup.RenderContext{
Ctx: ut.ctx,
DefaultLink: urlDefault,
Metas: metas,
}, template.HTMLEscapeString(msgLine))
Ctx: ut.ctx,
Metas: metas,
}, urlDefault, template.HTMLEscapeString(msgLine))
if err != nil {
log.Error("RenderCommitMessageSubject: %v", err)
return template.HTML("")
return ""
}
return renderCodeBlock(template.HTML(renderedMessage))
}
Expand Down
2 changes: 1 addition & 1 deletion modules/templates/util_render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func TestRenderCommitMessage(t *testing.T) {
}

func TestRenderCommitMessageLinkSubject(t *testing.T) {
expected := `<a href="https://example.com/link" class="default-link muted">space </a><a href="/mention-user" data-markdown-generated-content="" class="mention">@mention-user</a>`
expected := `<a href="https://example.com/link" class="muted">space </a><a href="/mention-user" data-markdown-generated-content="" class="mention">@mention-user</a>`
assert.EqualValues(t, expected, newTestRenderUtils().RenderCommitMessageLinkSubject(testInput(), "https://example.com/link", testMetas))
}

Expand Down
1 change: 1 addition & 0 deletions routers/common/markup.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func RenderMarkup(ctx *context.Base, repo *context.Repository, mode, text, urlPa
case "wiki":
renderCtx.Metas = map[string]string{"markdownLineBreakStyle": "document", "markupContentMode": "wiki"}
case "file":
// render the repo file content by its extension
renderCtx.Metas = map[string]string{"markdownLineBreakStyle": "document"}
renderCtx.MarkupType = ""
renderCtx.RelativePath = filePath
Expand Down
2 changes: 1 addition & 1 deletion templates/repo/view_file.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<div class="file-header-left tw-flex tw-items-center tw-py-2 tw-pr-4">
{{if .ReadmeInList}}
{{svg "octicon-book" 16 "tw-mr-2"}}
<strong><a class="default-link muted" href="#readme">{{.FileName}}</a></strong>
<strong><a class="muted" href="#readme">{{.FileName}}</a></strong>
{{else}}
{{template "repo/file_info" .}}
{{end}}
Expand Down

0 comments on commit ebd88a5

Please sign in to comment.