diff --git a/modules/markup/html_codepreview.go b/modules/markup/html_codepreview.go
index c404116d558..167b9aa5102 100644
--- a/modules/markup/html_codepreview.go
+++ b/modules/markup/html_codepreview.go
@@ -25,34 +25,45 @@ type RenderCodePreviewOptions struct {
LineStart, LineStop int
}
-func renderCodeBlock(ctx *RenderContext, node *html.Node) (urlPosStart, urlPosStop int, htm template.HTML, err error) {
- m := globalVars().codePreviewPattern.FindStringSubmatchIndex(node.Data)
- if m == nil {
- return 0, 0, "", nil
- }
+func renderCodeBlock(ctx *RenderContext, node *html.Node) (urlPosStart, urlPosStop int, htm template.HTML) {
+ start := 0
+ for {
+ data := node.Data[start:]
+ m := globalVars().codePreviewPattern.FindStringSubmatchIndex(data)
+ if m == nil {
+ return 0, 0, ""
+ }
+ urlPosStart, urlPosStop = start+m[0], start+m[1]
+ start = urlPosStop
- opts := RenderCodePreviewOptions{
- FullURL: node.Data[m[0]:m[1]],
- OwnerName: node.Data[m[2]:m[3]],
- RepoName: node.Data[m[4]:m[5]],
- CommitID: node.Data[m[6]:m[7]],
- FilePath: node.Data[m[8]:m[9]],
- }
- if !httplib.IsCurrentGiteaSiteURL(ctx, opts.FullURL) {
- return 0, 0, "", nil
- }
- u, err := url.Parse(opts.FilePath)
- if err != nil {
- return 0, 0, "", err
- }
- opts.FilePath = strings.TrimPrefix(u.Path, "/")
+ opts := RenderCodePreviewOptions{
+ FullURL: data[m[0]:m[1]],
+ OwnerName: data[m[2]:m[3]],
+ RepoName: data[m[4]:m[5]],
+ CommitID: data[m[6]:m[7]],
+ FilePath: data[m[8]:m[9]],
+ }
+ if !httplib.IsCurrentGiteaSiteURL(ctx, opts.FullURL) {
+ continue
+ }
+ u, err := url.Parse(opts.FilePath)
+ if err != nil {
+ log.Error("Unable to render code preview: %v", err)
+ continue
+ }
+ opts.FilePath = strings.TrimPrefix(u.Path, "/")
- lineStartStr, lineStopStr, _ := strings.Cut(node.Data[m[10]:m[11]], "-")
- lineStart, _ := strconv.Atoi(strings.TrimPrefix(lineStartStr, "L"))
- lineStop, _ := strconv.Atoi(strings.TrimPrefix(lineStopStr, "L"))
- opts.LineStart, opts.LineStop = lineStart, lineStop
- h, err := DefaultRenderHelperFuncs.RenderRepoFileCodePreview(ctx, opts)
- return m[0], m[1], h, err
+ lineStartStr, lineStopStr, _ := strings.Cut(data[m[10]:m[11]], "-")
+ lineStart, _ := strconv.Atoi(strings.TrimPrefix(lineStartStr, "L"))
+ lineStop, _ := strconv.Atoi(strings.TrimPrefix(lineStopStr, "L"))
+ opts.LineStart, opts.LineStop = lineStart, lineStop
+ h, err := DefaultRenderHelperFuncs.RenderRepoFileCodePreview(ctx, opts)
+ if err != nil {
+ log.Error("Unable to render code preview: %v", err)
+ } else if h != "" {
+ return urlPosStart, urlPosStop, h
+ }
+ }
}
func codePreviewPatternProcessor(ctx *RenderContext, node *html.Node) {
@@ -65,11 +76,8 @@ func codePreviewPatternProcessor(ctx *RenderContext, node *html.Node) {
node = node.NextSibling
continue
}
- urlPosStart, urlPosEnd, renderedCodeBlock, err := renderCodeBlock(ctx, node)
- if err != nil || renderedCodeBlock == "" {
- if err != nil {
- log.Error("Unable to render code preview: %v", err)
- }
+ urlPosStart, urlPosEnd, renderedCodeBlock := renderCodeBlock(ctx, node)
+ if renderedCodeBlock == "" {
node = node.NextSibling
continue
}
diff --git a/modules/markup/html_codepreview_test.go b/modules/markup/html_codepreview_test.go
index 1744cc76488..9f55883ecf6 100644
--- a/modules/markup/html_codepreview_test.go
+++ b/modules/markup/html_codepreview_test.go
@@ -18,6 +18,9 @@ import (
func TestRenderCodePreview(t *testing.T) {
markup.Init(&markup.RenderHelperFuncs{
RenderRepoFileCodePreview: func(ctx context.Context, options markup.RenderCodePreviewOptions) (template.HTML, error) {
+ if options.FilePath == "missing.md" {
+ return "", assert.AnError
+ }
return "
code preview
", nil
},
})
@@ -28,4 +31,6 @@ func TestRenderCodePreview(t *testing.T) {
}
test("http://localhost:3000/owner/repo/src/commit/0123456789/foo/bar.md#L10-L20", "code preview
")
test("http://other/owner/repo/src/commit/0123456789/foo/bar.md#L10-L20", `http://other/owner/repo/src/commit/0123456789/foo/bar.md#L10-L20
`)
+ test("http://other/owner/repo/src/commit/0123456789/foo/bar.md#L10-L20 http://localhost:3000/owner/repo/src/commit/0123456789/missing.md#L1-L2 http://localhost:3000/owner/repo/src/commit/0123456789/foo/bar.md#L10-L20",
+ `http://other/owner/repo/src/commit/0123456789/foo/bar.md#L10-L20 http://localhost:3000/owner/repo/src/commit/0123456789/missing.md#L1-L2
code preview
`)
}
diff --git a/modules/markup/html_email.go b/modules/markup/html_email.go
index 17cc56c3d7c..68cd8a8f86a 100644
--- a/modules/markup/html_email.go
+++ b/modules/markup/html_email.go
@@ -11,15 +11,17 @@ import (
// emailAddressProcessor replaces raw email addresses with a mailto: link.
func emailAddressProcessor(ctx *RenderContext, node *html.Node) {
+ start := 0
next := node.NextSibling
for node != nil && node != next {
- if !strings.Contains(node.Data, "@") { // the regexp is slow, skip texts which can't match
+ if !strings.Contains(node.Data[start:], "@") { // the regexp is slow, skip texts which can't match
return
}
- m := globalVars().emailRegex.FindStringSubmatchIndex(node.Data)
+ m := globalVars().emailRegex.FindStringSubmatchIndex(node.Data[start:])
if m == nil {
return
}
+ m[2], m[3] = m[2]+start, m[3]+start
var nextByte byte
if len(node.Data) > m[3] {
@@ -27,10 +29,12 @@ func emailAddressProcessor(ctx *RenderContext, node *html.Node) {
}
if strings.IndexByte(":/", nextByte) != -1 {
// for cases: "git@gitea.com:owner/repo.git", "https://git@gitea.com/owner/repo.git"
- return
+ start = m[3]
+ continue
}
mail := node.Data[m[2]:m[3]]
replaceContent(node, m[2], m[3], createLink(ctx, "mailto:"+mail, mail, "" /*mailto*/))
+ start = 0
node = node.NextSibling.NextSibling
}
}
diff --git a/modules/markup/html_internal_test.go b/modules/markup/html_internal_test.go
index b92234e4bc8..76387f7c80c 100644
--- a/modules/markup/html_internal_test.go
+++ b/modules/markup/html_internal_test.go
@@ -308,8 +308,8 @@ func TestRender_FullIssueURLs(t *testing.T) {
assert.Equal(t, expected, result.String())
}
test := func(input, expected string) { testWithMetas(localMetas, input, expected) }
- test("Here is a link https://git.osgeo.org/gogs/postgis/postgis/pulls/6",
- "Here is a link https://git.osgeo.org/gogs/postgis/postgis/pulls/6")
+ test("Here is a link https://git.osgeo.org/gogs/postgis/postgis/pulls/6 and http://localhost:3000/test-owner/test-repo/issues/4",
+ `Here is a link https://git.osgeo.org/gogs/postgis/postgis/pulls/6 and #4`)
test("Look here http://localhost:3000/person/repo/issues/4",
`Look here person/repo#4`)
test("http://localhost:3000/person/repo/issues/4#issuecomment-1234",
diff --git a/modules/markup/html_issue.go b/modules/markup/html_issue.go
index 16e7e22daab..0aa33841dfc 100644
--- a/modules/markup/html_issue.go
+++ b/modules/markup/html_issue.go
@@ -43,17 +43,17 @@ func fullIssuePatternProcessor(ctx *RenderContext, node *html.Node) {
m[idx] += start
}
}
+ start = m[1]
// leave links to sub-pages like https://domain/org/repo/pulls/27/files or .../pulls/27/commits/{sha} to the other processors
if m[1] < len(node.Data) && node.Data[m[1]] == '/' {
- start = m[1]
continue
}
link := node.Data[m[0]:m[1]]
siteURL := httplib.ParseGiteaSiteURL(ctx, link)
if siteURL == nil {
- return
+ continue
}
isSameRepo := siteURL.OwnerName == ctx.RenderOptions.Metas["user"] && siteURL.RepoName == ctx.RenderOptions.Metas["repo"]
diff --git a/modules/markup/html_link.go b/modules/markup/html_link.go
index 1bf5dab2167..de77ab5e640 100644
--- a/modules/markup/html_link.go
+++ b/modules/markup/html_link.go
@@ -7,6 +7,7 @@ import (
"net/url"
"path"
"strings"
+ "unicode"
"gitea.dev/modules/markup/common"
"gitea.dev/modules/util"
@@ -16,15 +17,17 @@ import (
)
func shortLinkProcessor(ctx *RenderContext, node *html.Node) {
+ start := 0
next := node.NextSibling
for node != nil && node != next {
- m := globalVars().shortLinkPattern.FindStringSubmatchIndex(node.Data)
+ data := node.Data[start:]
+ m := globalVars().shortLinkPattern.FindStringSubmatchIndex(data)
if m == nil {
return
}
- content := node.Data[m[2]:m[3]]
- tail := node.Data[m[4]:m[5]]
+ content := data[m[2]:m[3]]
+ tail := data[m[4]:m[5]]
props := make(map[string]string)
// MediaWiki uses [[link|text]], while GitHub uses [[text|link]]
@@ -103,7 +106,8 @@ func shortLinkProcessor(ctx *RenderContext, node *html.Node) {
checkLink := common.CheckLinkURLScheme(link)
if !checkLink.AllowToLinkify {
- return
+ start += m[1]
+ continue
}
childNode := &html.Node{}
@@ -157,7 +161,8 @@ func shortLinkProcessor(ctx *RenderContext, node *html.Node) {
childNode.Data = name
}
linkNode.Attr = []html.Attribute{{Key: "href", Val: link}}
- replaceContent(node, m[0], m[1], linkNode)
+ replaceContent(node, start+m[0], start+m[1], linkNode)
+ start = 0
node = node.NextSibling.NextSibling
}
}
@@ -165,18 +170,18 @@ func shortLinkProcessor(ctx *RenderContext, node *html.Node) {
// linkProcessor creates links for any HTTP or HTTPS URL not captured by
// markdown.
func linkProcessor(ctx *RenderContext, node *html.Node) {
+ truncatedWordLen := 0
+ if util.IsLikelyEllipsisLeftPart(node.Data) {
+ truncatedWordLen = len(node.Data) - 1 - strings.LastIndexFunc(node.Data, unicode.IsSpace)
+ }
next := node.NextSibling
for node != nil && node != next {
m := common.GlobalVars().LinkifyRegex.FindStringIndex(node.Data)
- if m == nil {
+ if m == nil || len(node.Data)-m[0] <= truncatedWordLen {
return
}
uri := node.Data[m[0]:m[1]]
- remaining := node.Data[m[1]:]
- if util.IsLikelyEllipsisLeftPart(remaining) {
- return
- }
replaceContent(node, m[0], m[1], createLink(ctx, uri, uri, "" /*link*/))
node = node.NextSibling.NextSibling
}
diff --git a/modules/markup/html_link_test.go b/modules/markup/html_link_test.go
index 6742a0f6dac..766bc4cd5d7 100644
--- a/modules/markup/html_link_test.go
+++ b/modules/markup/html_link_test.go
@@ -19,6 +19,5 @@ func TestShortLinkProcessor(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, test.NormalizeHTMLSpaces(expected), test.NormalizeHTMLSpaces(sb.String()))
}
- test("[[name=foo|link=./link]]", `foo`)
- test("[[name=foo|link=javascript:bar]]", `[[name=foo|link=javascript:bar]]`)
+ test("[[name=foo|link=javascript:bar]] and [[name=ok|link=./link]]", `[[name=foo|link=javascript:bar]] and ok`)
}
diff --git a/modules/markup/html_test.go b/modules/markup/html_test.go
index f2ec1572e7b..08ecc5cdc60 100644
--- a/modules/markup/html_test.go
+++ b/modules/markup/html_test.go
@@ -208,7 +208,7 @@ func TestRender_links(t *testing.T) {
t.Run("LinkEllipsis", func(t *testing.T) {
input := util.EllipsisDisplayString("http://10.1.2.3", 12)
assert.Equal(t, "http://10…", input)
- test(input, "http://10…
")
+ test("https://a.com... and "+input, `https://a.com... and http://10…
`)
input = util.EllipsisDisplayString("http://10.1.2.3", 13)
assert.Equal(t, "http://10.…", input)
@@ -248,7 +248,7 @@ func TestRender_email(t *testing.T) {
// Test that should *not* be turned into email links
{"/home/gitea/mailstore/info@gitea/com", `/home/gitea/mailstore/info@gitea/com
`},
- {"git@try.gitea.io:go-gitea/gitea.git", `git@try.gitea.io:go-gitea/gitea.git
`},
+ {"git@try.gitea.io:go-gitea/gitea.git info@gitea.com", `git@try.gitea.io:go-gitea/gitea.git info@gitea.com
`},
{"https://foo:bar@gitea.io", `https://foo:bar@gitea.io
`},
{"gitea@3", `gitea@3
`},
{"gitea@gmail.c", `gitea@gmail.c
`},