Files
diple/markdown.go
2026-07-28 20:31:27 +02:00

212 lines
6.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"strings"
"sync"
"github.com/charmbracelet/glamour"
glamouransi "github.com/charmbracelet/glamour/ansi"
"github.com/charmbracelet/glamour/styles"
"github.com/charmbracelet/lipgloss"
)
var commentMarkdownRenderers sync.Map
var quoteRailStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#777777"))
var markdownStyleName = "dark"
func renderCommentMarkdown(markdown string, width int) []string {
if strings.TrimSpace(markdown) == "" {
return nil
}
width = max(10, width)
markdown = normalizeGitHubAlerts(markdown)
var (
result []string
block []string
blockQuote bool
haveBlock bool
)
flush := func() {
if len(block) == 0 {
return
}
blockWidth := width
if blockQuote {
blockWidth = max(8, width-2)
}
lines := renderMarkdownFragment(strings.Join(block, "\n"), blockWidth)
if haveBlock && len(lines) > 0 {
result = append(result, "")
}
if blockQuote {
rail := quoteRailStyle.Render("│ ")
for i := range lines {
lines[i] = rail + lines[i]
}
}
result = append(result, lines...)
haveBlock = haveBlock || len(lines) > 0
block = nil
}
for _, line := range strings.Split(markdown, "\n") {
content, quoted := stripQuoteMarker(line)
if len(block) > 0 && quoted != blockQuote {
flush()
}
blockQuote = quoted
block = append(block, content)
}
flush()
return trimMarkdownLines(result)
}
func renderMarkdownFragment(markdown string, width int) []string {
if strings.TrimSpace(markdown) == "" {
return nil
}
renderer, err := commentMarkdownRenderer(width)
if err != nil {
return fallbackCommentLines(markdown, width)
}
rendered, err := renderer.Render(markdown)
if err != nil {
return fallbackCommentLines(markdown, width)
}
return trimMarkdownLines(strings.Split(strings.Trim(rendered, "\n"), "\n"))
}
func commentMarkdownRenderer(width int) (*glamour.TermRenderer, error) {
if cached, ok := commentMarkdownRenderers.Load(width); ok {
return cached.(*glamour.TermRenderer), nil
}
style := markdownStyleForTheme()
zero := uint(0)
style.Document.Margin = &zero
style.Code.Prefix = ""
style.Code.Suffix = ""
renderer, err := glamour.NewTermRenderer(
glamour.WithStyles(style),
glamour.WithWordWrap(width),
glamour.WithTableWrap(true),
glamour.WithPreservedNewLines(),
glamour.WithEmoji(),
)
if err != nil {
return nil, err
}
actual, _ := commentMarkdownRenderers.LoadOrStore(width, renderer)
return actual.(*glamour.TermRenderer), nil
}
func markdownStyleForTheme() glamouransi.StyleConfig {
if markdownStyleName == "notty" {
return styles.NoTTYStyleConfig
}
style := styles.DarkStyleConfig
if themeIsLight {
style = styles.LightStyleConfig
}
palette := editorMarkdownTheme
color := func(value string) *string { return &value }
truth := func(value bool) *bool { return &value }
style.Document.Color = color(palette.Text)
// Leave Text unset so inline text inherits the surrounding heading, link,
// quote, or paragraph color instead of flattening every Markdown token to
// the document foreground.
style.Text.Color = nil
style.Paragraph.Color = color(palette.Text)
style.Heading.Color = color(palette.Title)
style.H1.Color = color(palette.ActiveForeground)
style.H1.BackgroundColor = color(palette.ActiveBackground)
style.H2.Color = color(palette.Title)
style.H3.Color = color(palette.Title)
style.H4.Color = color(palette.Title)
style.H5.Color = color(palette.Title)
style.H6.Color = color(palette.Title)
style.HorizontalRule.Color = color(palette.Dim)
style.Item.Color = color(palette.Title)
style.Enumeration.Color = color(palette.Title)
style.Task.Color = color(palette.Text)
style.BlockQuote.Color = color(palette.Quote)
style.Strong.Color = color(palette.Text)
style.Strong.Bold = truth(true)
style.Emph.Color = color(palette.Text)
style.Link.Color = color(themeAuthorColor(palette, 0))
style.LinkText.Color = color(themeAuthorColor(palette, 1))
style.Image.Color = color(themeAuthorColor(palette, 0))
style.ImageText.Color = color(palette.Dim)
style.Code.Color = color(palette.Success)
style.Code.BackgroundColor = color(palette.EditorBackground)
style.CodeBlock.Color = color(palette.EditorForeground)
style.CodeBlock.BackgroundColor = color(palette.EditorBackground)
style.CodeBlock.Theme = palette.SyntaxTheme
style.CodeBlock.Chroma = nil
style.Table.Color = color(palette.Text)
style.Table.CenterSeparator = stringPointer("─")
style.Table.ColumnSeparator = stringPointer("│")
style.Table.RowSeparator = stringPointer("─")
style.DefinitionTerm.Color = color(palette.Title)
style.DefinitionDescription.Color = color(palette.Text)
return style
}
func themeAuthorColor(palette themePalette, index int) string {
if len(palette.AuthorPalette) == 0 {
return palette.Text
}
return palette.AuthorPalette[index%len(palette.AuthorPalette)]
}
func stringPointer(value string) *string {
return &value
}
func stripQuoteMarker(line string) (string, bool) {
trimmed := strings.TrimLeft(line, " \t")
if !strings.HasPrefix(trimmed, ">") {
return line, false
}
content := strings.TrimPrefix(trimmed, ">")
content = strings.TrimPrefix(content, " ")
return content, true
}
func normalizeGitHubAlerts(markdown string) string {
alerts := map[string]string{
"[!NOTE]": " **Note**",
"[!TIP]": "◆ **Tip**",
"[!IMPORTANT]": "❗ **Important**",
"[!WARNING]": "⚠ **Warning**",
"[!CAUTION]": "⛔ **Caution**",
}
lines := strings.Split(markdown, "\n")
for i, line := range lines {
trimmed := strings.TrimSpace(line)
if !strings.HasPrefix(trimmed, ">") {
continue
}
label := strings.TrimSpace(strings.TrimPrefix(trimmed, ">"))
if replacement, ok := alerts[strings.ToUpper(label)]; ok {
prefix := line[:strings.Index(line, ">")+1]
lines[i] = prefix + " " + replacement
}
}
return strings.Join(lines, "\n")
}
func fallbackCommentLines(markdown string, width int) []string {
return strings.Split(wrap(markdown, max(10, width)), "\n")
}
func trimMarkdownLines(lines []string) []string {
for len(lines) > 0 && strings.TrimSpace(lines[0]) == "" {
lines = lines[1:]
}
for len(lines) > 0 && strings.TrimSpace(lines[len(lines)-1]) == "" {
lines = lines[:len(lines)-1]
}
return lines
}