From 7972504293f7404f184f157de62c403563ce2465 Mon Sep 17 00:00:00 2001 From: ayn2op Date: Tue, 1 Sep 2026 23:59:37 -0400 Subject: [PATCH] refactor: use default markdown link parser --- go.mod | 2 +- go.sum | 4 ++-- internal/markdown/renderer.go | 24 ++++++++++++------------ internal/markdown/renderer_test.go | 9 ++------- internal/ui/chat/composer.go | 8 ++++---- internal/ui/chat/messages_list.go | 11 +++-------- 6 files changed, 24 insertions(+), 34 deletions(-) diff --git a/go.mod b/go.mod index d269bed..db2ba5c 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/BurntSushi/toml v1.6.0 github.com/alecthomas/chroma/v2 v2.27.0 github.com/andybalholm/brotli v1.2.3 - github.com/ayn2op/arikawa/v3 v3.0.0-20260901025723-b0907384f632 + github.com/ayn2op/arikawa/v3 v3.0.0-20260902041141-a95cdb6cf16c github.com/ayn2op/ningen/v3 v3.0.1-0.20260901025733-eea1e4226d7d github.com/ayn2op/tview v0.0.0-20260831000059-9f61521d46b4 github.com/bogdanfinn/fhttp v0.6.8 diff --git a/go.sum b/go.sum index 9eb03b3..d412198 100644 --- a/go.sum +++ b/go.sum @@ -10,8 +10,8 @@ github.com/alecthomas/repr v0.5.2 h1:SU73FTI9D1P5UNtvseffFSGmdNci/O6RsqzeXJtP0Qs github.com/alecthomas/repr v0.5.2/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4= github.com/andybalholm/brotli v1.2.3 h1:8H1qwOkl2LPfjf3YezB90JnCliZb6SInJ/OJkEbA5NQ= github.com/andybalholm/brotli v1.2.3/go.mod h1:rzTDkvFWvIrjDXZHkuS16NPggd91W3kUSvPlQ1pLaKY= -github.com/ayn2op/arikawa/v3 v3.0.0-20260901025723-b0907384f632 h1:7hDRO3TADdG6dTWO1rmMh3l66YA9s2TjhQs4lN4EmvI= -github.com/ayn2op/arikawa/v3 v3.0.0-20260901025723-b0907384f632/go.mod h1:LpZgYi6xrXOxabrlvCqkrBW5XTWCnRz+uNH3/JF90Xo= +github.com/ayn2op/arikawa/v3 v3.0.0-20260902041141-a95cdb6cf16c h1:HaOekXoQjIq80wbNKl1vqDQ6lnYuHZPld1gNRYMuIk4= +github.com/ayn2op/arikawa/v3 v3.0.0-20260902041141-a95cdb6cf16c/go.mod h1:LpZgYi6xrXOxabrlvCqkrBW5XTWCnRz+uNH3/JF90Xo= github.com/ayn2op/ningen/v3 v3.0.1-0.20260901025733-eea1e4226d7d h1:T/Zbub1uuWEDwHjmlqTWF2TtAyvbPzHw4/7TVd3N3tQ= github.com/ayn2op/ningen/v3 v3.0.1-0.20260901025733-eea1e4226d7d/go.mod h1:RlTP0PbcLrhcL0z2RhNotSh0MFXb/OYA07FIoNL3lOE= github.com/ayn2op/tview v0.0.0-20260831000059-9f61521d46b4 h1:mYUivDN9S69PScIxEkQm5ujKuIdhUT522N3FP/FJcMY= diff --git a/internal/markdown/renderer.go b/internal/markdown/renderer.go index e9023e6..8a764fd 100644 --- a/internal/markdown/renderer.go +++ b/internal/markdown/renderer.go @@ -7,7 +7,7 @@ import ( "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/lexers" "github.com/alecthomas/chroma/v2/styles" - arikawamarkdown "github.com/ayn2op/arikawa/v3/markdown" + md "github.com/ayn2op/arikawa/v3/markdown" "github.com/ayn2op/discordo/internal/config" "github.com/ayn2op/tview" "github.com/ayn2op/tview/text" @@ -118,8 +118,8 @@ func (r *Renderer) RenderText(source []byte, node ast.Node, base tcell.Style) te } else { builder.NewLine() } - case *arikawamarkdown.Inline: - if r.cfg.Markdown.MaskSpoilers && node.Attr&arikawamarkdown.AttrSpoiler != 0 { + case *md.Inline: + if r.cfg.Markdown.MaskSpoilers && node.Attr&md.AttrSpoiler != 0 { if entering { builder.Write("[spoiler]", currentStyle()) } @@ -130,11 +130,11 @@ func (r *Renderer) RenderText(source []byte, node ast.Node, base tcell.Style) te } else { popStyle() } - case *arikawamarkdown.Mention: + case *md.Mention: if entering { builder.Write(mentionText(node), tview.MergeStyle(currentStyle(), theme.MentionStyle.Style)) } - case *arikawamarkdown.Emoji: + case *md.Emoji: if entering { builder.Write(":"+node.Name+":", tview.MergeStyle(currentStyle(), theme.EmojiStyle.Style)) } @@ -253,7 +253,7 @@ func applyChromaStyle(base tcell.Style, entry chroma.StyleEntry) tcell.Style { return style } -func mentionText(node *arikawamarkdown.Mention) string { +func mentionText(node *md.Mention) string { switch { case node.Channel != nil: return "#" + node.Channel.Name @@ -270,20 +270,20 @@ func mentionText(node *arikawamarkdown.Mention) string { } } -func applyInlineAttr(style tcell.Style, attr arikawamarkdown.Attribute, inLink bool) tcell.Style { - if attr&arikawamarkdown.AttrBold != 0 { +func applyInlineAttr(style tcell.Style, attr md.Attribute, inLink bool) tcell.Style { + if attr&md.AttrBold != 0 { style = style.Bold(true) } - if attr&arikawamarkdown.AttrItalics != 0 { + if attr&md.AttrItalics != 0 { style = style.Italic(true) } - if attr&arikawamarkdown.AttrUnderline != 0 { + if attr&md.AttrUnderline != 0 { style = style.Underline(true) } - if attr&arikawamarkdown.AttrStrikethrough != 0 { + if attr&md.AttrStrikethrough != 0 { style = style.StrikeThrough(true) } - if attr&arikawamarkdown.AttrMonospace != 0 { + if attr&md.AttrMonospace != 0 { // Avoid reverse-video inside links. Link labels like `hash` should still // look like links, not highlighted blocks. if !inLink { diff --git a/internal/markdown/renderer_test.go b/internal/markdown/renderer_test.go index 483edf4..a8ef0bd 100644 --- a/internal/markdown/renderer_test.go +++ b/internal/markdown/renderer_test.go @@ -4,19 +4,14 @@ import ( "strings" "testing" - arikawamarkdown "github.com/ayn2op/arikawa/v3/markdown" + md "github.com/ayn2op/arikawa/v3/markdown" "github.com/ayn2op/discordo/internal/config" "github.com/gdamore/tcell/v3" - "github.com/yuin/goldmark/parser" - "github.com/yuin/goldmark/text" ) func TestRendererMasksSpoilers(t *testing.T) { source := []byte("before ||secret|| after") - node := parser.NewParser( - parser.WithBlockParsers(arikawamarkdown.BlockParsers()...), - parser.WithInlineParsers(arikawamarkdown.InlineParserWithLink()...), - ).Parse(text.NewReader(source)) + node := md.Parse(source) for _, test := range []struct { mask bool diff --git a/internal/ui/chat/composer.go b/internal/ui/chat/composer.go index 6d9e319..d85894e 100644 --- a/internal/ui/chat/composer.go +++ b/internal/ui/chat/composer.go @@ -16,7 +16,7 @@ import ( "github.com/ayn2op/arikawa/v3/api" "github.com/ayn2op/arikawa/v3/discord" "github.com/ayn2op/arikawa/v3/gateway" - arikawamarkdown "github.com/ayn2op/arikawa/v3/markdown" + md "github.com/ayn2op/arikawa/v3/markdown" "github.com/ayn2op/arikawa/v3/state" "github.com/ayn2op/arikawa/v3/utils/json/option" "github.com/ayn2op/arikawa/v3/utils/sendpart" @@ -372,12 +372,12 @@ func (c *composer) processText(channel *discord.Channel, src []byte) string { canMention = true ) - ast.Walk(arikawamarkdown.Parse(src), func(node ast.Node, enter bool) (ast.WalkStatus, error) { + ast.Walk(md.Parse(src), func(node ast.Node, enter bool) (ast.WalkStatus, error) { switch node := node.(type) { case *ast.CodeBlock, *ast.FencedCodeBlock: canMention = !enter - case *arikawamarkdown.Inline: - if (node.Attr & arikawamarkdown.AttrMonospace) != 0 { + case *md.Inline: + if (node.Attr & md.AttrMonospace) != 0 { canMention = !enter } case *ast.Text: diff --git a/internal/ui/chat/messages_list.go b/internal/ui/chat/messages_list.go index 6d87727..7fe326a 100644 --- a/internal/ui/chat/messages_list.go +++ b/internal/ui/chat/messages_list.go @@ -22,7 +22,7 @@ import ( "github.com/ayn2op/arikawa/v3/api" "github.com/ayn2op/arikawa/v3/discord" "github.com/ayn2op/arikawa/v3/gateway" - arikawamarkdown "github.com/ayn2op/arikawa/v3/markdown" + md "github.com/ayn2op/arikawa/v3/markdown" "github.com/ayn2op/arikawa/v3/state" "github.com/ayn2op/arikawa/v3/utils/json/option" "github.com/ayn2op/discordo/internal/config" @@ -41,8 +41,6 @@ import ( "github.com/rivo/uniseg" "github.com/skratchdot/open-golang/open" "github.com/yuin/goldmark/ast" - "github.com/yuin/goldmark/parser" - goldtext "github.com/yuin/goldmark/text" "golang.design/x/clipboard" ) @@ -446,7 +444,7 @@ func (ml *messagesList) renderContentWithMarkdown(message discord.Message, baseS // Keep one rendering path for both normal messages and embed fragments so we preserve mention/link parsing behavior consistently across both. if forceMarkdown || ml.cfg.Markdown.Enabled { c := []byte(message.Content) - root := arikawamarkdown.ParseWithMessage(c, *ml.chat.state.Cabinet, &message, false) + root := md.ParseWithMessage(c, *ml.chat.state.Cabinet, &message) return ml.renderer.RenderText(c, root, baseStyle), root, c } @@ -1048,10 +1046,7 @@ func (ml *messagesList) download() tview.Cmd { func extractURLs(content string) []string { src := []byte(content) - node := parser.NewParser( - parser.WithBlockParsers(arikawamarkdown.BlockParsers()...), - parser.WithInlineParsers(arikawamarkdown.InlineParserWithLink()...), - ).Parse(goldtext.NewReader(src)) + node := md.Parse(src) return urlsFromAST(node, src) }