update QoL and ai integration
This commit is contained in:
89
markdown.go
89
markdown.go
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
@@ -8,11 +9,16 @@ import (
|
||||
glamouransi "github.com/charmbracelet/glamour/ansi"
|
||||
"github.com/charmbracelet/glamour/styles"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
xansi "github.com/charmbracelet/x/ansi"
|
||||
)
|
||||
|
||||
var commentMarkdownRenderers sync.Map
|
||||
var quoteRailStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#777777"))
|
||||
var markdownStyleName = "dark"
|
||||
var renderedMentionPattern = regexp.MustCompile(
|
||||
`(^|[^A-Za-z0-9_-])(@[A-Za-z0-9](?:[A-Za-z0-9-]{0,37}[A-Za-z0-9])?)([^A-Za-z0-9-]|$)`,
|
||||
)
|
||||
var sgrPattern = regexp.MustCompile(`\x1b\[[0-9:;]*m`)
|
||||
|
||||
func renderCommentMarkdown(markdown string, width int) []string {
|
||||
if strings.TrimSpace(markdown) == "" {
|
||||
@@ -73,7 +79,11 @@ func renderMarkdownFragment(markdown string, width int) []string {
|
||||
if err != nil {
|
||||
return fallbackCommentLines(markdown, width)
|
||||
}
|
||||
return trimMarkdownLines(strings.Split(strings.Trim(rendered, "\n"), "\n"))
|
||||
lines := strings.Split(strings.Trim(rendered, "\n"), "\n")
|
||||
for index := range lines {
|
||||
lines[index] = highlightRenderedMentions(lines[index])
|
||||
}
|
||||
return trimMarkdownLines(lines)
|
||||
}
|
||||
|
||||
func commentMarkdownRenderer(width int) (*glamour.TermRenderer, error) {
|
||||
@@ -197,7 +207,82 @@ func normalizeGitHubAlerts(markdown string) string {
|
||||
}
|
||||
|
||||
func fallbackCommentLines(markdown string, width int) []string {
|
||||
return strings.Split(wrap(markdown, max(10, width)), "\n")
|
||||
lines := strings.Split(wrap(markdown, max(10, width)), "\n")
|
||||
for index := range lines {
|
||||
lines[index] = highlightRenderedMentions(lines[index])
|
||||
}
|
||||
return lines
|
||||
}
|
||||
|
||||
func highlightRenderedMentions(line string) string {
|
||||
visible, offsets := visibleTextOffsets(line)
|
||||
var result strings.Builder
|
||||
activeStyle := ""
|
||||
cursor := 0
|
||||
searchFrom := 0
|
||||
for searchFrom < len(visible) {
|
||||
match := renderedMentionPattern.FindStringSubmatchIndex(visible[searchFrom:])
|
||||
if match == nil {
|
||||
break
|
||||
}
|
||||
visibleStart := searchFrom + match[4]
|
||||
visibleEnd := searchFrom + match[5]
|
||||
mentionStart := offsets[visibleStart]
|
||||
mentionEnd := offsets[visibleEnd-1] + 1
|
||||
prefix := line[cursor:mentionStart]
|
||||
result.WriteString(prefix)
|
||||
activeStyle = activeSGR(activeStyle, prefix)
|
||||
|
||||
mention := visible[visibleStart:visibleEnd]
|
||||
result.WriteString(authorStyle(strings.TrimPrefix(mention, "@")).Render(mention))
|
||||
result.WriteString(activeStyle)
|
||||
cursor = mentionEnd
|
||||
searchFrom = visibleEnd
|
||||
}
|
||||
result.WriteString(line[cursor:])
|
||||
return result.String()
|
||||
}
|
||||
|
||||
func visibleTextOffsets(line string) (string, []int) {
|
||||
var visible strings.Builder
|
||||
offsets := make([]int, 0, len(line))
|
||||
var state byte
|
||||
for offset := 0; offset < len(line); {
|
||||
sequence, _, length, nextState := xansi.GraphemeWidth.DecodeSequenceInString(
|
||||
line[offset:], state, nil,
|
||||
)
|
||||
if length == 0 {
|
||||
break
|
||||
}
|
||||
plain := xansi.Strip(sequence)
|
||||
if plain != "" {
|
||||
relative := strings.Index(sequence, plain)
|
||||
if relative < 0 {
|
||||
relative = 0
|
||||
}
|
||||
visible.WriteString(plain)
|
||||
for index := range len(plain) {
|
||||
offsets = append(offsets, offset+relative+index)
|
||||
}
|
||||
}
|
||||
offset += length
|
||||
state = nextState
|
||||
}
|
||||
return visible.String(), offsets
|
||||
}
|
||||
|
||||
func activeSGR(active, text string) string {
|
||||
for _, sequence := range sgrPattern.FindAllString(text, -1) {
|
||||
parameters := strings.TrimSuffix(strings.TrimPrefix(sequence, "\x1b["), "m")
|
||||
if parameters == "" || parameters == "0" || strings.HasPrefix(parameters, "0;") ||
|
||||
strings.HasPrefix(parameters, "0:") {
|
||||
active = ""
|
||||
}
|
||||
if parameters != "" && parameters != "0" {
|
||||
active += sequence
|
||||
}
|
||||
}
|
||||
return active
|
||||
}
|
||||
|
||||
func trimMarkdownLines(lines []string) []string {
|
||||
|
||||
Reference in New Issue
Block a user