mirror of
https://github.com/go-gitea/gitea.git
synced 2026-11-26 10:47:04 -06:00
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
37 lines
957 B
Go
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
|
|
}
|
|
}
|