Files
gitea-MIRROR/modules/markup/html_email.go
Wei-Lun Li 7d1bc574f6 perf(markup): skip mention, email and commit regexps for text without "@" (#40471)
Post-processing runs the mention, email and cross-repo commit reference
regexps on every text node of rendered markdown, issue titles and commit
messages. All three patterns need an `@`, but none starts with a literal
the regexp engine can search for, so they took most of the
post-processing time even on text without one. Checking for `@` first
skips them with the same results.

Local Go benchmarks, i7-9700 on Windows 11, base and patched run
alternately 6 times, medians:

| | before | after |
|---|---|---|
| Markdown render of README + CONTRIBUTING + 3 docs (45 KB) | 16.1 ms |
11.4 ms |
| `PostProcessCommitMessageSubject` on the last 200 commit subjects of
this repo | 5.14 ms | 3.53 ms |
| `PostProcessIssueTitle` on the same 200 lines | 3.57 ms | 2.59 ms |

Assisted-by: Claude Code:claude-opus-5-5
2026-11-22 10:06:24 +00:00

37 lines
957 B
Go

// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package markup
import (
"strings"
"golang.org/x/net/html"
)
// emailAddressProcessor replaces raw email addresses with a mailto: link.
func emailAddressProcessor(ctx *RenderContext, node *html.Node) {
next := node.NextSibling
for node != nil && node != next {
if !strings.Contains(node.Data, "@") { // the regexp is slow, skip texts which can't match
return
}
m := globalVars().emailRegex.FindStringSubmatchIndex(node.Data)
if m == nil {
return
}
var nextByte byte
if len(node.Data) > m[3] {
nextByte = node.Data[m[3]]
}
if strings.IndexByte(":/", nextByte) != -1 {
// for cases: "git@gitea.com:owner/repo.git", "https://git@gitea.com/owner/repo.git"
return
}
mail := node.Data[m[2]:m[3]]
replaceContent(node, m[2], m[3], createLink(ctx, "mailto:"+mail, mail, "" /*mailto*/))
node = node.NextSibling.NextSibling
}
}