@@ -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,29 @@ 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
409
- if ctx .IsWiki {
410
- prefix = util .URLJoin (prefix , "wiki" , "raw" )
411
- }
412
- prefix = strings .Replace (prefix , "/src/" , "/media/" , 1 )
413
-
414
- attr .Val = util .URLJoin (prefix , attr .Val )
402
+ if len (attr .Val ) > 0 && ! IsLinkStr (attr .Val ) && ! strings .HasPrefix (attr .Val , "data:image/" ) {
403
+ attr .Val = util .URLJoin (ctx .Links .ResolveMediaLink (ctx .IsWiki ), attr .Val )
415
404
}
416
405
attr .Val = camoHandleLink (attr .Val )
417
406
node .Attr [i ] = attr
418
407
}
419
408
} else if node .Data == "a" {
420
409
// Restrict text in links to emojis
421
- textProcs = emojiProcessors
410
+ procs = emojiProcessors
422
411
} else if node .Data == "code" || node .Data == "pre" {
423
412
return
424
413
} else if node .Data == "i" {
@@ -444,7 +433,7 @@ func visitNode(ctx *RenderContext, procs, textProcs []processor, node *html.Node
444
433
}
445
434
}
446
435
for n := node .FirstChild ; n != nil ; n = n .NextSibling {
447
- visitNode (ctx , procs , textProcs , n )
436
+ visitNode (ctx , procs , n )
448
437
}
449
438
}
450
439
// ignore everything else
@@ -641,10 +630,6 @@ func mentionProcessor(ctx *RenderContext, node *html.Node) {
641
630
}
642
631
643
632
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
633
next := node .NextSibling
649
634
for node != nil && node != next {
650
635
m := shortLinkPattern .FindStringSubmatchIndex (node .Data )
@@ -665,7 +650,7 @@ func shortLinkProcessorFull(ctx *RenderContext, node *html.Node, noLink bool) {
665
650
if equalPos := strings .IndexByte (v , '=' ); equalPos == - 1 {
666
651
// There is no equal in this argument; this is a mandatory arg
667
652
if props ["name" ] == "" {
668
- if isLinkStr (v ) {
653
+ if IsLinkStr (v ) {
669
654
// If we clearly see it is a link, we save it so
670
655
671
656
// But first we need to ensure, that if both mandatory args provided
@@ -740,7 +725,7 @@ func shortLinkProcessorFull(ctx *RenderContext, node *html.Node, noLink bool) {
740
725
DataAtom : atom .A ,
741
726
}
742
727
childNode .Parent = linkNode
743
- absoluteLink := isLinkStr (link )
728
+ absoluteLink := IsLinkStr (link )
744
729
if ! absoluteLink {
745
730
if image {
746
731
link = strings .ReplaceAll (link , " " , "+" )
@@ -751,16 +736,9 @@ func shortLinkProcessorFull(ctx *RenderContext, node *html.Node, noLink bool) {
751
736
link = url .PathEscape (link )
752
737
}
753
738
}
754
- urlPrefix := ctx .URLPrefix
755
739
if image {
756
740
if ! absoluteLink {
757
- if IsSameDomain (urlPrefix ) {
758
- urlPrefix = strings .Replace (urlPrefix , "/src/" , "/raw/" , 1 )
759
- }
760
- if ctx .IsWiki {
761
- link = util .URLJoin ("wiki" , "raw" , link )
762
- }
763
- link = util .URLJoin (urlPrefix , link )
741
+ link = util .URLJoin (ctx .Links .ResolveMediaLink (ctx .IsWiki ), link )
764
742
}
765
743
title := props ["title" ]
766
744
if title == "" {
@@ -789,18 +767,15 @@ func shortLinkProcessorFull(ctx *RenderContext, node *html.Node, noLink bool) {
789
767
} else {
790
768
if ! absoluteLink {
791
769
if ctx .IsWiki {
792
- link = util .URLJoin ("wiki" , link )
770
+ link = util .URLJoin (ctx .Links .WikiLink (), link )
771
+ } else {
772
+ link = util .URLJoin (ctx .Links .SrcLink (), link )
793
773
}
794
- link = util .URLJoin (urlPrefix , link )
795
774
}
796
775
childNode .Type = html .TextNode
797
776
childNode .Data = name
798
777
}
799
- if noLink {
800
- linkNode = childNode
801
- } else {
802
- linkNode .Attr = []html.Attribute {{Key : "href" , Val : link }}
803
- }
778
+ linkNode .Attr = []html.Attribute {{Key : "href" , Val : link }}
804
779
replaceContent (node , m [0 ], m [1 ], linkNode )
805
780
node = node .NextSibling .NextSibling
806
781
}
0 commit comments