Skip to content

Commit 08f0f69

Browse files
committed
remove dead code and fine tune performance
1 parent 50ee93c commit 08f0f69

File tree

10 files changed

+43
-66
lines changed

10 files changed

+43
-66
lines changed

Diff for: modules/markup/common/html.go

-16
This file was deleted.

Diff for: modules/markup/common/linkify.go

+15-3
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,27 @@ package common
99
import (
1010
"bytes"
1111
"regexp"
12+
"sync"
1213

1314
"github.com/yuin/goldmark"
1415
"github.com/yuin/goldmark/ast"
1516
"github.com/yuin/goldmark/parser"
1617
"github.com/yuin/goldmark/text"
1718
"github.com/yuin/goldmark/util"
19+
"mvdan.cc/xurls/v2"
1820
)
1921

20-
var wwwURLRegxp = regexp.MustCompile(`^www\.[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}((?:/|[#?])[-a-zA-Z0-9@:%_\+.~#!?&//=\(\);,'">\^{}\[\]` + "`" + `]*)?`)
22+
type GlobalVarsType struct {
23+
wwwURLRegxp *regexp.Regexp
24+
LinkRegex *regexp.Regexp // fast matching a URL link, no any extra validation.
25+
}
26+
27+
var GlobalVars = sync.OnceValue[*GlobalVarsType](func() *GlobalVarsType {
28+
v := &GlobalVarsType{}
29+
v.wwwURLRegxp = regexp.MustCompile(`^www\.[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}((?:/|[#?])[-a-zA-Z0-9@:%_\+.~#!?&//=\(\);,'">\^{}\[\]` + "`" + `]*)?`)
30+
v.LinkRegex, _ = xurls.StrictMatchingScheme("https?://")
31+
return v
32+
})
2133

2234
type linkifyParser struct{}
2335

@@ -60,10 +72,10 @@ func (s *linkifyParser) Parse(parent ast.Node, block text.Reader, pc parser.Cont
6072
var protocol []byte
6173
typ := ast.AutoLinkURL
6274
if bytes.HasPrefix(line, protoHTTP) || bytes.HasPrefix(line, protoHTTPS) || bytes.HasPrefix(line, protoFTP) {
63-
m = LinkRegex.FindSubmatchIndex(line)
75+
m = GlobalVars().LinkRegex.FindSubmatchIndex(line)
6476
}
6577
if m == nil && bytes.HasPrefix(line, domainWWW) {
66-
m = wwwURLRegxp.FindSubmatchIndex(line)
78+
m = GlobalVars().wwwURLRegxp.FindSubmatchIndex(line)
6779
protocol = []byte("http")
6880
}
6981
if m != nil {

Diff for: modules/markup/html.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func CustomLinkURLSchemes(schemes []string) {
130130
}
131131
withAuth = append(withAuth, s)
132132
}
133-
common.LinkRegex, _ = xurls.StrictMatchingScheme(strings.Join(withAuth, "|"))
133+
common.GlobalVars().LinkRegex, _ = xurls.StrictMatchingScheme(strings.Join(withAuth, "|"))
134134
}
135135

136136
type postProcessError struct {

Diff for: modules/markup/html_link.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ func shortLinkProcessor(ctx *RenderContext, node *html.Node) {
189189
func linkProcessor(ctx *RenderContext, node *html.Node) {
190190
next := node.NextSibling
191191
for node != nil && node != next {
192-
m := common.LinkRegex.FindStringIndex(node.Data)
192+
m := common.GlobalVars().LinkRegex.FindStringIndex(node.Data)
193193
if m == nil {
194194
return
195195
}
@@ -204,7 +204,7 @@ func linkProcessor(ctx *RenderContext, node *html.Node) {
204204
func descriptionLinkProcessor(ctx *RenderContext, node *html.Node) {
205205
next := node.NextSibling
206206
for node != nil && node != next {
207-
m := common.LinkRegex.FindStringIndex(node.Data)
207+
m := common.GlobalVars().LinkRegex.FindStringIndex(node.Data)
208208
if m == nil {
209209
return
210210
}

Diff for: modules/markup/internal/internal_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ func TestRenderInternal(t *testing.T) {
4444
_ = r1.init("sec", nil)
4545
protected = r1.ProtectSafeAttrs(`<div class="test"></div>`)
4646
assert.EqualValues(t, `<div data-attr-class="sec:test"></div>`, protected)
47+
assert.EqualValues(t, "data-attr-class", r1.SafeAttr("class"))
48+
assert.EqualValues(t, "sec:val", r1.SafeValue("val"))
49+
recovered, ok := r1.RecoverProtectedValue("sec:val")
50+
assert.True(t, ok)
51+
assert.EqualValues(t, "val", recovered)
52+
recovered, ok = r1.RecoverProtectedValue("other:val")
53+
assert.False(t, ok)
54+
assert.Empty(t, recovered)
4755

4856
out2 := &bytes.Buffer{}
4957
in2 := r2.init("sec-other", out2)

Diff for: modules/markup/markdown/ast.go

+1-29
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,6 @@ func NewDetails() *Details {
3434
}
3535
}
3636

37-
// IsDetails returns true if the given node implements the Details interface,
38-
// otherwise false.
39-
func IsDetails(node ast.Node) bool {
40-
_, ok := node.(*Details)
41-
return ok
42-
}
43-
4437
// Summary is a block that contains the summary of details block
4538
type Summary struct {
4639
ast.BaseBlock
@@ -66,13 +59,6 @@ func NewSummary() *Summary {
6659
}
6760
}
6861

69-
// IsSummary returns true if the given node implements the Summary interface,
70-
// otherwise false.
71-
func IsSummary(node ast.Node) bool {
72-
_, ok := node.(*Summary)
73-
return ok
74-
}
75-
7662
// TaskCheckBoxListItem is a block that represents a list item of a markdown block with a checkbox
7763
type TaskCheckBoxListItem struct {
7864
*ast.ListItem
@@ -103,14 +89,7 @@ func NewTaskCheckBoxListItem(listItem *ast.ListItem) *TaskCheckBoxListItem {
10389
}
10490
}
10591

106-
// IsTaskCheckBoxListItem returns true if the given node implements the TaskCheckBoxListItem interface,
107-
// otherwise false.
108-
func IsTaskCheckBoxListItem(node ast.Node) bool {
109-
_, ok := node.(*TaskCheckBoxListItem)
110-
return ok
111-
}
112-
113-
// Icon is an inline for a fomantic icon
92+
// Icon is an inline for a Fomantic UI icon
11493
type Icon struct {
11594
ast.BaseInline
11695
Name []byte
@@ -139,13 +118,6 @@ func NewIcon(name string) *Icon {
139118
}
140119
}
141120

142-
// IsIcon returns true if the given node implements the Icon interface,
143-
// otherwise false.
144-
func IsIcon(node ast.Node) bool {
145-
_, ok := node.(*Icon)
146-
return ok
147-
}
148-
149121
// ColorPreview is an inline for a color preview
150122
type ColorPreview struct {
151123
ast.BaseInline

Diff for: modules/markup/markdown/goldmark.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"regexp"
99
"strings"
10+
"sync"
1011

1112
"code.gitea.io/gitea/modules/container"
1213
"code.gitea.io/gitea/modules/markup"
@@ -112,13 +113,16 @@ func (g *ASTTransformer) Transform(node *ast.Document, reader text.Reader, pc pa
112113
}
113114
}
114115

115-
// NewHTMLRenderer creates a HTMLRenderer to render
116-
// in the gitea form.
116+
// it is copied from old code, which is quite doubtful whether it is correct
117+
var reValidIconName = sync.OnceValue[*regexp.Regexp](func() *regexp.Regexp {
118+
return regexp.MustCompile(`^[-\w]+$`) // old: regexp.MustCompile("^[a-z ]+$")
119+
})
120+
121+
// NewHTMLRenderer creates a HTMLRenderer to render in the gitea form.
117122
func NewHTMLRenderer(renderInternal *internal.RenderInternal, opts ...html.Option) renderer.NodeRenderer {
118123
r := &HTMLRenderer{
119124
renderInternal: renderInternal,
120125
Config: html.NewConfig(),
121-
reValidName: regexp.MustCompile("^[a-z ]+$"),
122126
}
123127
for _, opt := range opts {
124128
opt.SetHTMLOption(&r.Config)
@@ -130,7 +134,6 @@ func NewHTMLRenderer(renderInternal *internal.RenderInternal, opts ...html.Optio
130134
// renders gitea specific features.
131135
type HTMLRenderer struct {
132136
html.Config
133-
reValidName *regexp.Regexp
134137
renderInternal *internal.RenderInternal
135138
}
136139

@@ -219,7 +222,7 @@ func (r *HTMLRenderer) renderIcon(w util.BufWriter, source []byte, node ast.Node
219222
return ast.WalkContinue, nil
220223
}
221224

222-
if !r.reValidName.MatchString(name) {
225+
if !reValidIconName().MatchString(name) {
223226
// skip this
224227
return ast.WalkContinue, nil
225228
}

Diff for: modules/markup/markdown/markdown.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func SpecializedMarkdown(ctx *markup.RenderContext) *GlodmarkRender {
148148
}
149149

150150
// render calls goldmark render to convert Markdown to HTML
151-
// NOTE: The output of this method MUST get sanitized!!!
151+
// NOTE: The output of this method MUST get sanitized separately!!!
152152
func render(ctx *markup.RenderContext, input io.Reader, output io.Writer) error {
153153
converter := SpecializedMarkdown(ctx)
154154
lw := &limitWriter{
@@ -164,7 +164,7 @@ func render(ctx *markup.RenderContext, input io.Reader, output io.Writer) error
164164
}
165165

166166
log.Warn("Unable to render markdown due to panic in goldmark: %v", err)
167-
if !setting.IsProd && !setting.IsInTesting {
167+
if (!setting.IsProd && !setting.IsInTesting) || log.IsDebug() {
168168
log.Error("Panic in markdown: %v\n%s", err, log.Stack(2))
169169
}
170170
}()

Diff for: modules/markup/markdown/markdown_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -1053,15 +1053,15 @@ func TestAttention(t *testing.T) {
10531053
}
10541054

10551055
func BenchmarkSpecializedMarkdown(b *testing.B) {
1056-
// 187785 5990 ns/op
1056+
// 240856 4719 ns/op
10571057
for i := 0; i < b.N; i++ {
1058-
markdown.SpecializedMarkdown(nil)
1058+
markdown.SpecializedMarkdown(&markup.RenderContext{})
10591059
}
10601060
}
10611061

10621062
func BenchmarkMarkdownRender(b *testing.B) {
1063-
// 24698 48585 ns/op
1063+
// 23202 50840 ns/op
10641064
for i := 0; i < b.N; i++ {
1065-
markdown.RenderString(&markup.RenderContext{Ctx: context.Background()}, "https://example.com\n- a\n- b\n")
1065+
_, _ = markdown.RenderString(&markup.RenderContext{Ctx: context.Background()}, "https://example.com\n- a\n- b\n")
10661066
}
10671067
}

Diff for: modules/markup/markdown/meta_test.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ import (
1111
"github.com/stretchr/testify/assert"
1212
)
1313

14-
/*
15-
IssueTemplate is a legacy to keep the unit tests working.
16-
Copied from structs.IssueTemplate, the original type has been changed a lot to support yaml template.
17-
*/
14+
// IssueTemplate is a legacy to keep the unit tests working.
15+
// Copied from structs.IssueTemplate, the original type has been changed a lot to support yaml template.
1816
type IssueTemplate struct {
1917
Name string `json:"name" yaml:"name"`
2018
Title string `json:"title" yaml:"title"`

0 commit comments

Comments
 (0)