fix(markup): continue scanning text nodes after skipped matches (#40506)

Several markup processors stopped scanning a text node after skipping
one match. An external issue or code preview URL hid later local ones,
an SSH remote like `git@host:org/repo.git` hid a later email address, a
short link with a disallowed scheme hid later short links, and a text
node ending in an ellipsis left all its URLs unlinked.

The processors now continue past skipped matches. Only a URL in the last
word of a text node ending in an ellipsis is treated as truncated.

Fixes: https://github.com/go-gitea/gitea/issues/40505

Assisted-by: OpenCode:gemini-4-neon
Assisted-by: Claude Code:claude-opus-5-5

---------

Co-authored-by: silverwind <me@silverwind.io>
This commit is contained in:
T Mohan Aditya
2026-11-27 05:42:42 +00:00
committed by GitHub
parent 4f1f6d7774
commit c6bdd3efd6
8 changed files with 73 additions and 52 deletions

View File

@@ -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
}

View File

@@ -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 "<div>code preview</div>", nil
},
})
@@ -28,4 +31,6 @@ func TestRenderCodePreview(t *testing.T) {
}
test("http://localhost:3000/owner/repo/src/commit/0123456789/foo/bar.md#L10-L20", "<p><div>code preview</div></p>")
test("http://other/owner/repo/src/commit/0123456789/foo/bar.md#L10-L20", `<p><a href="http://other/owner/repo/src/commit/0123456789/foo/bar.md#L10-L20" rel="nofollow">http://other/owner/repo/src/commit/0123456789/foo/bar.md#L10-L20</a></p>`)
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",
`<p><a href="http://other/owner/repo/src/commit/0123456789/foo/bar.md#L10-L20" rel="nofollow">http://other/owner/repo/src/commit/0123456789/foo/bar.md#L10-L20</a> <a href="http://localhost:3000/owner/repo/src/commit/0123456789/missing.md#L1-L2" rel="nofollow">http://localhost:3000/owner/repo/src/commit/0123456789/missing.md#L1-L2</a> <div>code preview</div></p>`)
}

View File

@@ -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
}
}

View File

@@ -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 <a href="http://localhost:3000/test-owner/test-repo/issues/4" class="ref-issue">#4</a>`)
test("Look here http://localhost:3000/person/repo/issues/4",
`Look here <a href="http://localhost:3000/person/repo/issues/4" class="ref-issue">person/repo#4</a>`)
test("http://localhost:3000/person/repo/issues/4#issuecomment-1234",

View File

@@ -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"]

View File

@@ -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
}

View File

@@ -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]]", `<a href="/base/link">foo</a>`)
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 <a href="/base/link">ok</a>`)
}

View File

@@ -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, "<p>http://10…</p>")
test("https://a.com... and "+input, `<p><a href="https://a.com" rel="nofollow">https://a.com</a>... and http://10…</p>`)
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", `<p>/home/gitea/mailstore/info@gitea/com</p>`},
{"git@try.gitea.io:go-gitea/gitea.git", `<p>git@try.gitea.io:go-gitea/gitea.git</p>`},
{"git@try.gitea.io:go-gitea/gitea.git info@gitea.com", `<p>git@try.gitea.io:go-gitea/gitea.git <a href="mailto:info@gitea.com" rel="nofollow">info@gitea.com</a></p>`},
{"https://foo:bar@gitea.io", `<p><a href="https://foo:bar@gitea.io" rel="nofollow">https://foo:bar@gitea.io</a></p>`},
{"gitea@3", `<p>gitea@3</p>`},
{"gitea@gmail.c", `<p>gitea@gmail.c</p>`},