@@ -80,15 +80,10 @@ const keywordClass = "issue-keyword"
80
80
81
81
// IsLink reports whether link fits valid format.
82
82
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 {
88
83
return validLinksPattern .Match (link )
89
84
}
90
85
91
- func isLinkStr (link string ) bool {
86
+ func IsLinkStr (link string ) bool {
92
87
return validLinksPattern .MatchString (link )
93
88
}
94
89
@@ -344,7 +339,7 @@ func postProcess(ctx *RenderContext, procs []processor, input io.Reader, output
344
339
node = node .FirstChild
345
340
}
346
341
347
- visitNode (ctx , procs , procs , node )
342
+ visitNode (ctx , procs , node )
348
343
349
344
newNodes := make ([]* html.Node , 0 , 5 )
350
345
@@ -375,7 +370,7 @@ func postProcess(ctx *RenderContext, procs []processor, input io.Reader, output
375
370
return nil
376
371
}
377
372
378
- func visitNode (ctx * RenderContext , procs , textProcs []processor , node * html.Node ) {
373
+ func visitNode (ctx * RenderContext , procs []processor , node * html.Node ) {
379
374
// Add user-content- to IDs and "#" links if they don't already have them
380
375
for idx , attr := range node .Attr {
381
376
val := strings .TrimPrefix (attr .Val , "#" )
@@ -390,35 +385,38 @@ func visitNode(ctx *RenderContext, procs, textProcs []processor, node *html.Node
390
385
}
391
386
392
387
if attr .Key == "class" && attr .Val == "emoji" {
393
- textProcs = nil
388
+ procs = nil
394
389
}
395
390
}
396
391
397
392
// We ignore code and pre.
398
393
switch node .Type {
399
394
case html .TextNode :
400
- textNode (ctx , textProcs , node )
395
+ textNode (ctx , procs , node )
401
396
case html .ElementNode :
402
397
if node .Data == "img" {
403
398
for i , attr := range node .Attr {
404
399
if attr .Key != "src" {
405
400
continue
406
401
}
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
409
404
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
411
410
}
412
- prefix = strings .Replace (prefix , "/src/" , "/media/" , 1 )
413
411
414
- attr .Val = util .URLJoin (prefix , attr .Val )
412
+ attr .Val = util .URLJoin (base , attr .Val )
415
413
}
416
414
attr .Val = camoHandleLink (attr .Val )
417
415
node .Attr [i ] = attr
418
416
}
419
417
} else if node .Data == "a" {
420
418
// Restrict text in links to emojis
421
- textProcs = emojiProcessors
419
+ procs = emojiProcessors
422
420
} else if node .Data == "code" || node .Data == "pre" {
423
421
return
424
422
} else if node .Data == "i" {
@@ -444,7 +442,7 @@ func visitNode(ctx *RenderContext, procs, textProcs []processor, node *html.Node
444
442
}
445
443
}
446
444
for n := node .FirstChild ; n != nil ; n = n .NextSibling {
447
- visitNode (ctx , procs , textProcs , n )
445
+ visitNode (ctx , procs , n )
448
446
}
449
447
}
450
448
// ignore everything else
@@ -641,10 +639,6 @@ func mentionProcessor(ctx *RenderContext, node *html.Node) {
641
639
}
642
640
643
641
func shortLinkProcessor (ctx * RenderContext , node * html.Node ) {
644
- shortLinkProcessorFull (ctx , node , false )
645
- }
646
-
647
- func shortLinkProcessorFull (ctx * RenderContext , node * html.Node , noLink bool ) {
648
642
next := node .NextSibling
649
643
for node != nil && node != next {
650
644
m := shortLinkPattern .FindStringSubmatchIndex (node .Data )
@@ -665,7 +659,7 @@ func shortLinkProcessorFull(ctx *RenderContext, node *html.Node, noLink bool) {
665
659
if equalPos := strings .IndexByte (v , '=' ); equalPos == - 1 {
666
660
// There is no equal in this argument; this is a mandatory arg
667
661
if props ["name" ] == "" {
668
- if isLinkStr (v ) {
662
+ if IsLinkStr (v ) {
669
663
// If we clearly see it is a link, we save it so
670
664
671
665
// 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) {
740
734
DataAtom : atom .A ,
741
735
}
742
736
childNode .Parent = linkNode
743
- absoluteLink := isLinkStr (link )
737
+ absoluteLink := IsLinkStr (link )
744
738
if ! absoluteLink {
745
739
if image {
746
740
link = strings .ReplaceAll (link , " " , "+" )
@@ -751,16 +745,18 @@ func shortLinkProcessorFull(ctx *RenderContext, node *html.Node, noLink bool) {
751
745
link = url .PathEscape (link )
752
746
}
753
747
}
754
- urlPrefix := ctx .URLPrefix
755
748
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
+
756
758
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 )
764
760
}
765
761
title := props ["title" ]
766
762
if title == "" {
@@ -789,18 +785,15 @@ func shortLinkProcessorFull(ctx *RenderContext, node *html.Node, noLink bool) {
789
785
} else {
790
786
if ! absoluteLink {
791
787
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 )
793
791
}
794
- link = util .URLJoin (urlPrefix , link )
795
792
}
796
793
childNode .Type = html .TextNode
797
794
childNode .Data = name
798
795
}
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 }}
804
797
replaceContent (node , m [0 ], m [1 ], linkNode )
805
798
node = node .NextSibling .NextSibling
806
799
}
0 commit comments