Skip to content

Commit 77b5402

Browse files
committed
Refactor link rendering.
1 parent 1432d4e commit 77b5402

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+948
-360
lines changed

models/issues/comment_code.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,11 @@ func findCodeComments(ctx context.Context, opts FindCommentsOptions, issue *Issu
109109

110110
var err error
111111
if comment.RenderedContent, err = markdown.RenderString(&markup.RenderContext{
112-
Ctx: ctx,
113-
URLPrefix: issue.Repo.Link(),
114-
Metas: issue.Repo.ComposeMetas(),
112+
Ctx: ctx,
113+
Links: markup.Links{
114+
Base: issue.Repo.Link(),
115+
},
116+
Metas: issue.Repo.ComposeMetas(),
115117
}, comment.Content); err != nil {
116118
return nil, err
117119
}

models/repo/repo.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,8 +574,7 @@ func (repo *Repository) CanEnableEditor() bool {
574574
// DescriptionHTML does special handles to description and return HTML string.
575575
func (repo *Repository) DescriptionHTML(ctx context.Context) template.HTML {
576576
desc, err := markup.RenderDescriptionHTML(&markup.RenderContext{
577-
Ctx: ctx,
578-
URLPrefix: repo.HTMLURL(),
577+
Ctx: ctx,
579578
// Don't use Metas to speedup requests
580579
}, repo.Description)
581580
if err != nil {

modules/markup/external/external.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,10 @@ func envMark(envName string) string {
7979
// Render renders the data of the document to HTML via the external tool.
8080
func (p *Renderer) Render(ctx *markup.RenderContext, input io.Reader, output io.Writer) error {
8181
var (
82-
urlRawPrefix = strings.Replace(ctx.URLPrefix, "/src/", "/raw/", 1)
83-
command = strings.NewReplacer(envMark("GITEA_PREFIX_SRC"), ctx.URLPrefix,
84-
envMark("GITEA_PREFIX_RAW"), urlRawPrefix).Replace(p.Command)
82+
command = strings.NewReplacer(
83+
envMark("GITEA_PREFIX_SRC"), ctx.Links.SrcLink(),
84+
envMark("GITEA_PREFIX_RAW"), ctx.Links.RawLink(),
85+
).Replace(p.Command)
8586
commands = strings.Fields(command)
8687
args = commands[1:]
8788
)
@@ -121,14 +122,14 @@ func (p *Renderer) Render(ctx *markup.RenderContext, input io.Reader, output io.
121122
ctx.Ctx = graceful.GetManager().ShutdownContext()
122123
}
123124

124-
processCtx, _, finished := process.GetManager().AddContext(ctx.Ctx, fmt.Sprintf("Render [%s] for %s", commands[0], ctx.URLPrefix))
125+
processCtx, _, finished := process.GetManager().AddContext(ctx.Ctx, fmt.Sprintf("Render [%s] for %s", commands[0], ctx.Links.SrcLink()))
125126
defer finished()
126127

127128
cmd := exec.CommandContext(processCtx, commands[0], args...)
128129
cmd.Env = append(
129130
os.Environ(),
130-
"GITEA_PREFIX_SRC="+ctx.URLPrefix,
131-
"GITEA_PREFIX_RAW="+urlRawPrefix,
131+
"GITEA_PREFIX_SRC="+ctx.Links.SrcLink(),
132+
"GITEA_PREFIX_RAW="+ctx.Links.RawLink(),
132133
)
133134
if !p.IsInputFile {
134135
cmd.Stdin = input

modules/markup/html.go

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,10 @@ const keywordClass = "issue-keyword"
8080

8181
// IsLink reports whether link fits valid format.
8282
func IsLink(link []byte) bool {
83-
return isLink(link)
84-
}
85-
86-
// isLink reports whether link fits valid format.
87-
func isLink(link []byte) bool {
8883
return validLinksPattern.Match(link)
8984
}
9085

91-
func isLinkStr(link string) bool {
86+
func IsLinkStr(link string) bool {
9287
return validLinksPattern.MatchString(link)
9388
}
9489

@@ -344,7 +339,7 @@ func postProcess(ctx *RenderContext, procs []processor, input io.Reader, output
344339
node = node.FirstChild
345340
}
346341

347-
visitNode(ctx, procs, procs, node)
342+
visitNode(ctx, procs, node)
348343

349344
newNodes := make([]*html.Node, 0, 5)
350345

@@ -375,7 +370,7 @@ func postProcess(ctx *RenderContext, procs []processor, input io.Reader, output
375370
return nil
376371
}
377372

378-
func visitNode(ctx *RenderContext, procs, textProcs []processor, node *html.Node) {
373+
func visitNode(ctx *RenderContext, procs []processor, node *html.Node) {
379374
// Add user-content- to IDs and "#" links if they don't already have them
380375
for idx, attr := range node.Attr {
381376
val := strings.TrimPrefix(attr.Val, "#")
@@ -390,35 +385,38 @@ func visitNode(ctx *RenderContext, procs, textProcs []processor, node *html.Node
390385
}
391386

392387
if attr.Key == "class" && attr.Val == "emoji" {
393-
textProcs = nil
388+
procs = nil
394389
}
395390
}
396391

397392
// We ignore code and pre.
398393
switch node.Type {
399394
case html.TextNode:
400-
textNode(ctx, textProcs, node)
395+
textNode(ctx, procs, node)
401396
case html.ElementNode:
402397
if node.Data == "img" {
403398
for i, attr := range node.Attr {
404399
if attr.Key != "src" {
405400
continue
406401
}
407-
if len(attr.Val) > 0 && !isLinkStr(attr.Val) && !strings.HasPrefix(attr.Val, "data:image/") {
408-
prefix := ctx.URLPrefix
402+
if len(attr.Val) > 0 && !IsLinkStr(attr.Val) && !strings.HasPrefix(attr.Val, "data:image/") {
403+
var base string
409404
if ctx.IsUncyclo {
410-
prefix = util.URLJoin(prefix, "wiki", "raw")
405+
base = ctx.Links.UncycloRawLink()
406+
} else if ctx.Links.HasBranchInfo() {
407+
base = ctx.Links.MediaLink()
408+
} else {
409+
base = ctx.Links.Base
411410
}
412-
prefix = strings.Replace(prefix, "/src/", "/media/", 1)
413411

414-
attr.Val = util.URLJoin(prefix, attr.Val)
412+
attr.Val = util.URLJoin(base, attr.Val)
415413
}
416414
attr.Val = camoHandleLink(attr.Val)
417415
node.Attr[i] = attr
418416
}
419417
} else if node.Data == "a" {
420418
// Restrict text in links to emojis
421-
textProcs = emojiProcessors
419+
procs = emojiProcessors
422420
} else if node.Data == "code" || node.Data == "pre" {
423421
return
424422
} else if node.Data == "i" {
@@ -444,7 +442,7 @@ func visitNode(ctx *RenderContext, procs, textProcs []processor, node *html.Node
444442
}
445443
}
446444
for n := node.FirstChild; n != nil; n = n.NextSibling {
447-
visitNode(ctx, procs, textProcs, n)
445+
visitNode(ctx, procs, n)
448446
}
449447
}
450448
// ignore everything else
@@ -641,10 +639,6 @@ func mentionProcessor(ctx *RenderContext, node *html.Node) {
641639
}
642640

643641
func shortLinkProcessor(ctx *RenderContext, node *html.Node) {
644-
shortLinkProcessorFull(ctx, node, false)
645-
}
646-
647-
func shortLinkProcessorFull(ctx *RenderContext, node *html.Node, noLink bool) {
648642
next := node.NextSibling
649643
for node != nil && node != next {
650644
m := shortLinkPattern.FindStringSubmatchIndex(node.Data)
@@ -665,7 +659,7 @@ func shortLinkProcessorFull(ctx *RenderContext, node *html.Node, noLink bool) {
665659
if equalPos := strings.IndexByte(v, '='); equalPos == -1 {
666660
// There is no equal in this argument; this is a mandatory arg
667661
if props["name"] == "" {
668-
if isLinkStr(v) {
662+
if IsLinkStr(v) {
669663
// If we clearly see it is a link, we save it so
670664

671665
// But first we need to ensure, that if both mandatory args provided
@@ -740,7 +734,7 @@ func shortLinkProcessorFull(ctx *RenderContext, node *html.Node, noLink bool) {
740734
DataAtom: atom.A,
741735
}
742736
childNode.Parent = linkNode
743-
absoluteLink := isLinkStr(link)
737+
absoluteLink := IsLinkStr(link)
744738
if !absoluteLink {
745739
if image {
746740
link = strings.ReplaceAll(link, " ", "+")
@@ -751,16 +745,18 @@ func shortLinkProcessorFull(ctx *RenderContext, node *html.Node, noLink bool) {
751745
link = url.PathEscape(link)
752746
}
753747
}
754-
urlPrefix := ctx.URLPrefix
755748
if image {
749+
var base string
750+
if ctx.IsUncyclo {
751+
base = ctx.Links.UncycloRawLink()
752+
} else if ctx.Links.HasBranchInfo() {
753+
base = ctx.Links.RawLink()
754+
} else {
755+
base = ctx.Links.Base
756+
}
757+
756758
if !absoluteLink {
757-
if IsSameDomain(urlPrefix) {
758-
urlPrefix = strings.Replace(urlPrefix, "/src/", "/raw/", 1)
759-
}
760-
if ctx.IsUncyclo {
761-
link = util.URLJoin("wiki", "raw", link)
762-
}
763-
link = util.URLJoin(urlPrefix, link)
759+
link = util.URLJoin(base, link)
764760
}
765761
title := props["title"]
766762
if title == "" {
@@ -789,18 +785,15 @@ func shortLinkProcessorFull(ctx *RenderContext, node *html.Node, noLink bool) {
789785
} else {
790786
if !absoluteLink {
791787
if ctx.IsUncyclo {
792-
link = util.URLJoin("wiki", link)
788+
link = util.URLJoin(ctx.Links.UncycloLink(), link)
789+
} else {
790+
link = util.URLJoin(ctx.Links.SrcLink(), link)
793791
}
794-
link = util.URLJoin(urlPrefix, link)
795792
}
796793
childNode.Type = html.TextNode
797794
childNode.Data = name
798795
}
799-
if noLink {
800-
linkNode = childNode
801-
} else {
802-
linkNode.Attr = []html.Attribute{{Key: "href", Val: link}}
803-
}
796+
linkNode.Attr = []html.Attribute{{Key: "href", Val: link}}
804797
replaceContent(node, m[0], m[1], linkNode)
805798
node = node.NextSibling.NextSibling
806799
}

modules/markup/html_internal_test.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,8 @@ func TestRender_IssueIndexPattern_Document(t *testing.T) {
287287
}
288288

289289
func testRenderIssueIndexPattern(t *testing.T, input, expected string, ctx *RenderContext) {
290-
if ctx.URLPrefix == "" {
291-
ctx.URLPrefix = TestAppURL
290+
if ctx.Links.Base == "" {
291+
ctx.Links.Base = TestRepoURL
292292
}
293293

294294
var buf strings.Builder
@@ -303,19 +303,23 @@ func TestRender_AutoLink(t *testing.T) {
303303
test := func(input, expected string) {
304304
var buffer strings.Builder
305305
err := PostProcess(&RenderContext{
306-
Ctx: git.DefaultContext,
307-
URLPrefix: TestRepoURL,
308-
Metas: localMetas,
306+
Ctx: git.DefaultContext,
307+
Links: Links{
308+
Base: TestRepoURL,
309+
},
310+
Metas: localMetas,
309311
}, strings.NewReader(input), &buffer)
310312
assert.Equal(t, err, nil)
311313
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer.String()))
312314

313315
buffer.Reset()
314316
err = PostProcess(&RenderContext{
315-
Ctx: git.DefaultContext,
316-
URLPrefix: TestRepoURL,
317-
Metas: localMetas,
318-
IsUncyclo: true,
317+
Ctx: git.DefaultContext,
318+
Links: Links{
319+
Base: TestRepoURL,
320+
},
321+
Metas: localMetas,
322+
IsUncyclo: true,
319323
}, strings.NewReader(input), &buffer)
320324
assert.Equal(t, err, nil)
321325
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer.String()))
@@ -342,9 +346,11 @@ func TestRender_FullIssueURLs(t *testing.T) {
342346
test := func(input, expected string) {
343347
var result strings.Builder
344348
err := postProcess(&RenderContext{
345-
Ctx: git.DefaultContext,
346-
URLPrefix: TestRepoURL,
347-
Metas: localMetas,
349+
Ctx: git.DefaultContext,
350+
Links: Links{
351+
Base: TestRepoURL,
352+
},
353+
Metas: localMetas,
348354
}, []processor{fullIssuePatternProcessor}, strings.NewReader(input), &result)
349355
assert.NoError(t, err)
350356
assert.Equal(t, expected, result.String())

0 commit comments

Comments
 (0)