Skip to content

Commit b80cd5d

Browse files
committed
fix
1 parent 8c21bc0 commit b80cd5d

File tree

3 files changed

+23
-32
lines changed

3 files changed

+23
-32
lines changed

modules/markup/html.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,38 +53,38 @@ var (
5353
// shortLinkPattern matches short but difficult to parse [[name|link|arg=test]] syntax
5454
shortLinkPattern = regexp.MustCompile(`\[\[(.*?)\]\](\w*)`)
5555

56-
// anySHA1Pattern splits url containing SHA into parts
56+
// anyHashPattern splits url containing SHA into parts
5757
anyHashPattern = regexp.MustCompile(`https?://(?:\S+/){4,5}([0-9a-f]{40,64})(/[-+~_%.a-zA-Z0-9/]+)?(#[-+~_%.a-zA-Z0-9]+)?`)
5858

5959
// comparePattern matches "http://domain/org/repo/compare/COMMIT1...COMMIT2#hash"
6060
comparePattern = regexp.MustCompile(`https?://(?:\S+/){4,5}([0-9a-f]{7,64})(\.\.\.?)([0-9a-f]{7,64})?(#[-+~_%.a-zA-Z0-9]+)?`)
6161

62-
validLinksPattern = regexp.MustCompile(`^[a-z][\w-]+://`)
62+
// fullURLPattern matches full URL like "mailto:...", "https://..." and "ssh+git://..."
63+
fullURLPattern = regexp.MustCompile(`^[a-z][-+\w]+:`)
6364

64-
// While this email regex is definitely not perfect and I'm sure you can come up
65-
// with edge cases, it is still accepted by the CommonMark specification, as
66-
// well as the HTML5 spec:
65+
// emailRegex is definitely not perfect with edge cases,
66+
// it is still accepted by the CommonMark specification, as well as the HTML5 spec:
6767
// http://spec.commonmark.org/0.28/#email-address
6868
// https://html.spec.whatwg.org/multipage/input.html#e-mail-state-(type%3Demail)
6969
emailRegex = regexp.MustCompile("(?:\\s|^|\\(|\\[)([a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9]{2,}(?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+)(?:\\s|$|\\)|\\]|;|,|\\?|!|\\.(\\s|$))")
7070

71-
// blackfriday extensions create IDs like fn:user-content-footnote
71+
// blackfridayExtRegex is for blackfriday extensions create IDs like fn:user-content-footnote
7272
blackfridayExtRegex = regexp.MustCompile(`[^:]*:user-content-`)
7373

74-
// EmojiShortCodeRegex find emoji by alias like :smile:
75-
EmojiShortCodeRegex = regexp.MustCompile(`:[-+\w]+:`)
74+
// emojiShortCodeRegex find emoji by alias like :smile:
75+
emojiShortCodeRegex = regexp.MustCompile(`:[-+\w]+:`)
7676
)
7777

7878
// CSS class for action keywords (e.g. "closes: #1")
7979
const keywordClass = "issue-keyword"
8080

81-
// IsLink reports whether link fits valid format.
82-
func IsLink(link []byte) bool {
83-
return validLinksPattern.Match(link)
81+
// IsFullURLBytes reports whether link fits valid format.
82+
func IsFullURLBytes(link []byte) bool {
83+
return fullURLPattern.Match(link)
8484
}
8585

86-
func IsLinkStr(link string) bool {
87-
return validLinksPattern.MatchString(link)
86+
func IsFullURLString(link string) bool {
87+
return fullURLPattern.MatchString(link)
8888
}
8989

9090
// regexp for full links to issues/pulls
@@ -399,7 +399,7 @@ func visitNode(ctx *RenderContext, procs []processor, node *html.Node) {
399399
if attr.Key != "src" {
400400
continue
401401
}
402-
if len(attr.Val) > 0 && !IsLinkStr(attr.Val) && !strings.HasPrefix(attr.Val, "data:image/") {
402+
if !IsFullURLString(attr.Val) && !strings.HasPrefix(attr.Val, "data:image/") {
403403
attr.Val = util.URLJoin(ctx.Links.ResolveMediaLink(ctx.IsWiki), attr.Val)
404404
}
405405
attr.Val = camoHandleLink(attr.Val)
@@ -650,7 +650,7 @@ func shortLinkProcessor(ctx *RenderContext, node *html.Node) {
650650
if equalPos := strings.IndexByte(v, '='); equalPos == -1 {
651651
// There is no equal in this argument; this is a mandatory arg
652652
if props["name"] == "" {
653-
if IsLinkStr(v) {
653+
if IsFullURLString(v) {
654654
// If we clearly see it is a link, we save it so
655655

656656
// But first we need to ensure, that if both mandatory args provided
@@ -725,7 +725,7 @@ func shortLinkProcessor(ctx *RenderContext, node *html.Node) {
725725
DataAtom: atom.A,
726726
}
727727
childNode.Parent = linkNode
728-
absoluteLink := IsLinkStr(link)
728+
absoluteLink := IsFullURLString(link)
729729
if !absoluteLink {
730730
if image {
731731
link = strings.ReplaceAll(link, " ", "+")
@@ -1059,7 +1059,7 @@ func emojiShortCodeProcessor(ctx *RenderContext, node *html.Node) {
10591059
start := 0
10601060
next := node.NextSibling
10611061
for node != nil && node != next && start < len(node.Data) {
1062-
m := EmojiShortCodeRegex.FindStringSubmatchIndex(node.Data[start:])
1062+
m := emojiShortCodeRegex.FindStringSubmatchIndex(node.Data[start:])
10631063
if m == nil {
10641064
return
10651065
}

modules/markup/markdown/goldmark.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ import (
2626
"github.com/yuin/goldmark/util"
2727
)
2828

29-
var byteMailto = []byte("mailto:")
30-
3129
// ASTTransformer is a default transformer of the goldmark tree.
3230
type ASTTransformer struct{}
3331

@@ -84,7 +82,7 @@ func (g *ASTTransformer) Transform(node *ast.Document, reader text.Reader, pc pa
8482
// 2. If they're not wrapped with a link they need a link wrapper
8583

8684
// Check if the destination is a real link
87-
if len(v.Destination) > 0 && !markup.IsLink(v.Destination) {
85+
if len(v.Destination) > 0 && !markup.IsFullURLBytes(v.Destination) {
8886
v.Destination = []byte(giteautil.URLJoin(
8987
ctx.Links.ResolveMediaLink(ctx.IsWiki),
9088
strings.TrimLeft(string(v.Destination), "/"),
@@ -130,23 +128,17 @@ func (g *ASTTransformer) Transform(node *ast.Document, reader text.Reader, pc pa
130128
case *ast.Link:
131129
// Links need their href to munged to be a real value
132130
link := v.Destination
133-
if len(link) > 0 && !markup.IsLink(link) &&
134-
link[0] != '#' && !bytes.HasPrefix(link, byteMailto) {
135-
// special case: this is not a link, a hash link or a mailto:, so it's a
136-
// relative URL
137-
138-
var base string
131+
isAnchorFragment := len(link) > 0 && link[0] == '#'
132+
if !isAnchorFragment && !markup.IsFullURLBytes(link) {
133+
base := ctx.Links.Base
139134
if ctx.IsWiki {
140135
base = ctx.Links.WikiLink()
141136
} else if ctx.Links.HasBranchInfo() {
142137
base = ctx.Links.SrcLink()
143-
} else {
144-
base = ctx.Links.Base
145138
}
146-
147139
link = []byte(giteautil.URLJoin(base, string(link)))
148140
}
149-
if len(link) > 0 && link[0] == '#' {
141+
if isAnchorFragment {
150142
link = []byte("#user-content-" + string(link)[1:])
151143
}
152144
v.Destination = link

modules/markup/orgmode/orgmode.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ type Writer struct {
136136
func (r *Writer) resolveLink(kind, link string) string {
137137
link = strings.TrimPrefix(link, "file:")
138138
if !strings.HasPrefix(link, "#") && // not a URL fragment
139-
!markup.IsLinkStr(link) && // not an absolute URL
140-
!strings.HasPrefix(link, "mailto:") {
139+
!markup.IsFullURLString(link) {
141140
if kind == "regular" {
142141
// orgmode reports the link kind as "regular" for "[[ImageLink.svg][The Image Desc]]"
143142
// so we need to try to guess the link kind again here

0 commit comments

Comments
 (0)