Fix various bugs (#36446)
* Fix #36409 * Fix #36322 * Fix #30101 * Fix #36317 --------- Signed-off-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
@@ -62,7 +62,7 @@ var globalVars = sync.OnceValue(func() *globalVarsType {
|
||||
v.shortLinkPattern = regexp.MustCompile(`\[\[(.*?)\]\](\w*)`)
|
||||
|
||||
// anyHashPattern splits url containing SHA into parts
|
||||
v.anyHashPattern = regexp.MustCompile(`https?://(?:\S+/){4,5}([0-9a-f]{40,64})(/[-+~%./\w]+)?(\?[-+~%.\w&=]+)?(#[-+~%.\w]+)?`)
|
||||
v.anyHashPattern = regexp.MustCompile(`https?://(?:\S+/){4,5}([0-9a-f]{40,64})((\.\w+)*)(/[-+~%./\w]+)?(\?[-+~%.\w&=]+)?(#[-+~%.\w]+)?`)
|
||||
|
||||
// comparePattern matches "http://domain/org/repo/compare/COMMIT1...COMMIT2#hash"
|
||||
v.comparePattern = regexp.MustCompile(`https?://(?:\S+/){4,5}([0-9a-f]{7,64})(\.\.\.?)([0-9a-f]{7,64})?(#[-+~_%.a-zA-Z0-9]+)?`)
|
||||
|
||||
@@ -16,12 +16,14 @@ import (
|
||||
)
|
||||
|
||||
type anyHashPatternResult struct {
|
||||
PosStart int
|
||||
PosEnd int
|
||||
FullURL string
|
||||
CommitID string
|
||||
SubPath string
|
||||
QueryHash string
|
||||
PosStart int
|
||||
PosEnd int
|
||||
FullURL string
|
||||
CommitID string
|
||||
CommitExt string
|
||||
SubPath string
|
||||
QueryParams string
|
||||
QueryHash string
|
||||
}
|
||||
|
||||
func createCodeLink(href, content, class string) *html.Node {
|
||||
@@ -56,7 +58,11 @@ func anyHashPatternExtract(s string) (ret anyHashPatternResult, ok bool) {
|
||||
return ret, false
|
||||
}
|
||||
|
||||
ret.PosStart, ret.PosEnd = m[0], m[1]
|
||||
pos := 0
|
||||
|
||||
ret.PosStart, ret.PosEnd = m[pos], m[pos+1]
|
||||
pos += 2
|
||||
|
||||
ret.FullURL = s[ret.PosStart:ret.PosEnd]
|
||||
if strings.HasSuffix(ret.FullURL, ".") {
|
||||
// if url ends in '.', it's very likely that it is not part of the actual url but used to finish a sentence.
|
||||
@@ -67,14 +73,24 @@ func anyHashPatternExtract(s string) (ret anyHashPatternResult, ok bool) {
|
||||
}
|
||||
}
|
||||
|
||||
ret.CommitID = s[m[2]:m[3]]
|
||||
if m[5] > 0 {
|
||||
ret.SubPath = s[m[4]:m[5]]
|
||||
}
|
||||
ret.CommitID = s[m[pos]:m[pos+1]]
|
||||
pos += 2
|
||||
|
||||
lastStart, lastEnd := m[len(m)-2], m[len(m)-1]
|
||||
if lastEnd > 0 {
|
||||
ret.QueryHash = s[lastStart:lastEnd][1:]
|
||||
ret.CommitExt = s[m[pos]:m[pos+1]]
|
||||
pos += 4
|
||||
|
||||
if m[pos] > 0 {
|
||||
ret.SubPath = s[m[pos]:m[pos+1]]
|
||||
}
|
||||
pos += 2
|
||||
|
||||
if m[pos] > 0 {
|
||||
ret.QueryParams = s[m[pos]:m[pos+1]]
|
||||
}
|
||||
pos += 2
|
||||
|
||||
if m[pos] > 0 {
|
||||
ret.QueryHash = s[m[pos]:m[pos+1]][1:]
|
||||
}
|
||||
return ret, true
|
||||
}
|
||||
@@ -96,6 +112,9 @@ func fullHashPatternProcessor(ctx *RenderContext, node *html.Node) {
|
||||
continue
|
||||
}
|
||||
text := base.ShortSha(ret.CommitID)
|
||||
if ret.CommitExt != "" {
|
||||
text += ret.CommitExt
|
||||
}
|
||||
if ret.SubPath != "" {
|
||||
text += ret.SubPath
|
||||
}
|
||||
|
||||
@@ -102,6 +102,16 @@ func TestRender_CrossReferences(t *testing.T) {
|
||||
test(
|
||||
inputURL,
|
||||
`<p><a href="`+inputURL+`" rel="nofollow"><code>0123456789/foo.txt (L2-L3)</code></a></p>`)
|
||||
|
||||
inputURL = "https://example.com/repo/owner/archive/0123456789012345678901234567890123456789.tar.gz"
|
||||
test(
|
||||
inputURL,
|
||||
`<p><a href="`+inputURL+`" rel="nofollow"><code>0123456789.tar.gz</code></a></p>`)
|
||||
|
||||
inputURL = "https://example.com/owner/repo/commit/0123456789012345678901234567890123456789.patch?key=val"
|
||||
test(
|
||||
inputURL,
|
||||
`<p><a href="`+inputURL+`" rel="nofollow"><code>0123456789.patch</code></a></p>`)
|
||||
}
|
||||
|
||||
func TestRender_links(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user