fix: sluggish mouse scroll
This commit is contained in:
54
markdown.go
54
markdown.go
@@ -13,6 +13,7 @@ import (
|
||||
)
|
||||
|
||||
var commentMarkdownRenderers sync.Map
|
||||
var commentMarkdownLines = newMarkdownLineCache(512)
|
||||
var quoteRailStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#777777"))
|
||||
var markdownStyleName = "dark"
|
||||
var renderedMentionPattern = regexp.MustCompile(
|
||||
@@ -20,12 +21,63 @@ var renderedMentionPattern = regexp.MustCompile(
|
||||
)
|
||||
var sgrPattern = regexp.MustCompile(`\x1b\[[0-9:;]*m`)
|
||||
|
||||
type markdownLineCacheKey struct {
|
||||
markdown string
|
||||
width int
|
||||
}
|
||||
|
||||
type markdownLineCache struct {
|
||||
mu sync.Mutex
|
||||
limit int
|
||||
entries map[markdownLineCacheKey][]string
|
||||
order []markdownLineCacheKey
|
||||
}
|
||||
|
||||
func newMarkdownLineCache(limit int) *markdownLineCache {
|
||||
return &markdownLineCache{
|
||||
limit: limit, entries: make(map[markdownLineCacheKey][]string),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *markdownLineCache) get(key markdownLineCacheKey) ([]string, bool) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
lines, ok := c.entries[key]
|
||||
return append([]string(nil), lines...), ok
|
||||
}
|
||||
|
||||
func (c *markdownLineCache) put(key markdownLineCacheKey, lines []string) []string {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
if cached, ok := c.entries[key]; ok {
|
||||
return append([]string(nil), cached...)
|
||||
}
|
||||
if len(c.entries) >= c.limit {
|
||||
delete(c.entries, c.order[0])
|
||||
c.order = c.order[1:]
|
||||
}
|
||||
c.entries[key] = append([]string(nil), lines...)
|
||||
c.order = append(c.order, key)
|
||||
return append([]string(nil), lines...)
|
||||
}
|
||||
|
||||
func (c *markdownLineCache) clear() {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
c.entries = make(map[markdownLineCacheKey][]string)
|
||||
c.order = nil
|
||||
}
|
||||
|
||||
func renderCommentMarkdown(markdown string, width int) []string {
|
||||
if strings.TrimSpace(markdown) == "" {
|
||||
return nil
|
||||
}
|
||||
width = max(10, width)
|
||||
markdown = normalizeGitHubAlerts(markdown)
|
||||
cacheKey := markdownLineCacheKey{markdown: markdown, width: width}
|
||||
if lines, ok := commentMarkdownLines.get(cacheKey); ok {
|
||||
return lines
|
||||
}
|
||||
var (
|
||||
result []string
|
||||
block []string
|
||||
@@ -64,7 +116,7 @@ func renderCommentMarkdown(markdown string, width int) []string {
|
||||
block = append(block, content)
|
||||
}
|
||||
flush()
|
||||
return trimMarkdownLines(result)
|
||||
return commentMarkdownLines.put(cacheKey, trimMarkdownLines(result))
|
||||
}
|
||||
|
||||
func renderMarkdownFragment(markdown string, width int) []string {
|
||||
|
||||
Reference in New Issue
Block a user