Files
gitea-MIRROR/modules/markup/markdown/transform_codespan.go
Gal Be 42d09b71d3 perf(markdown): stop building a new markdown converter for every render (#40515)
Each render built a new goldmark converter. For short texts, such as
comments, this was most of the cost. Now `render` reuses converters from
a `sync.Pool`, as the TODO in `SpecializedMarkdown` suggested. Each
converter gets the `RenderInternal` of the render. The HTML output does
not change.

| Benchmark | Before | After | Allocs |
|---|---:|---:|---|
| Comment | 85.0 µs | 46.0 µs | 502 → 207 |
| `BenchmarkMarkdownRender` | 100.5 µs | 62.8 µs | 536 → 241 |
| README | 1.51 ms | 1.50 ms | 9679 → 9384 |

No benchmark got slower (AMD EPYC 9V74, Go 1.27.4, 24 rounds).

Checks:
- `go test ./modules/markup/...` and golangci-lint pass.
- Old and new HTML matched on 20,000 random documents.
- Parallel renders pass with `-race`.

Claude was used for this change. I reviewed and tested it.

---------

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: silverwind <me@silverwind.io>
2026-11-24 16:19:41 +00:00

79 lines
2.2 KiB
Go

// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package markdown
import (
"bytes"
"strings"
"gitea.dev/modules/markup"
"github.com/microcosm-cc/bluemonday/css"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/renderer/html"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
)
// renderCodeSpan renders CodeSpan elements (like goldmark upstream does) but also renders ColorPreview elements.
// See #21474 for reference
func (r *HTMLRenderer) renderCodeSpan(w util.BufWriter, source []byte, n ast.Node, entering bool) (ast.WalkStatus, error) {
if entering {
if n.Attributes() != nil {
_, _ = w.WriteString("<code")
html.RenderAttributes(w, n, html.CodeAttributeFilter)
_ = w.WriteByte('>')
} else {
_, _ = w.WriteString("<code>")
}
for c := n.FirstChild(); c != nil; c = c.NextSibling() {
switch v := c.(type) {
case *ast.Text:
segment := v.Segment
value := segment.Value(source)
if bytes.HasSuffix(value, []byte("\n")) {
r.Writer.RawWrite(w, value[:len(value)-1])
r.Writer.RawWrite(w, []byte(" "))
} else {
r.Writer.RawWrite(w, value)
}
case *ColorPreview:
_ = r.internalVar.Value.FormatWithSafeAttrsf(w, `<span class="color-preview" style="background-color: %s"></span>`, v.Color)
}
}
return ast.WalkSkipChildren, nil
}
_, _ = w.WriteString("</code>")
return ast.WalkContinue, nil
}
// cssColorHandler checks if a string is a render-able CSS color value.
// The code is from "github.com/microcosm-cc/bluemonday/css.ColorHandler", except that it doesn't handle color words like "red".
func cssColorHandler(value string) bool {
value = strings.ToLower(value)
if css.HexRGB.MatchString(value) {
return true
}
if css.RGB.MatchString(value) {
return true
}
if css.RGBA.MatchString(value) {
return true
}
if css.HSL.MatchString(value) {
return true
}
return css.HSLA.MatchString(value)
}
func (g *ASTTransformer) transformCodeSpan(_ *markup.RenderContext, v *ast.CodeSpan, reader text.Reader) {
colorContent, ok := childSingleText(v, reader.Source())
if !ok {
return
}
if cssColorHandler(colorContent) {
v.AppendChild(v, NewColorPreview(colorContent))
}
}