refactor: migrate markdown parser to arikawa

This commit is contained in:
ayn2op 2026-09-01 22:54:15 -04:00
parent 5df8f14d7b
commit 5003a15cc8
6 changed files with 32 additions and 32 deletions

6
go.mod
View file

@ -13,8 +13,8 @@ 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-20260827004545-b90631eb8a21
github.com/ayn2op/ningen/v3 v3.0.1-0.20260827010304-ee3a9ce6c2e4
github.com/ayn2op/arikawa/v3 v3.0.0-20260901025723-b0907384f632
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
github.com/bogdanfinn/tls-client v1.15.1
@ -44,7 +44,7 @@ require (
github.com/danieljoos/wincred v1.2.3 // indirect
github.com/dchest/jsmin v1.0.0 // indirect
github.com/dlclark/regexp2/v2 v2.7.1 // indirect
github.com/ebitengine/purego v0.10.2 // indirect
github.com/ebitengine/purego v0.11.0 // indirect
github.com/gdamore/encoding v1.0.1 // indirect
github.com/godbus/dbus/v5 v5.2.2 // indirect
github.com/gorilla/schema v1.4.1 // indirect

12
go.sum
View file

@ -10,10 +10,10 @@ 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-20260827004545-b90631eb8a21 h1:+6W+QtSmIV8Dry4iRtlVNOaH2Z5yzKxvr3uy0ajVG3E=
github.com/ayn2op/arikawa/v3 v3.0.0-20260827004545-b90631eb8a21/go.mod h1:C7NGoHHt0MQH/plbA9ly4YUQwG0v+n0XdgXmdEPoRpU=
github.com/ayn2op/ningen/v3 v3.0.1-0.20260827010304-ee3a9ce6c2e4 h1:Jl3hKkjANCCGPHTyrxLqy+FVdvdftGY41E5f4/1YrRA=
github.com/ayn2op/ningen/v3 v3.0.1-0.20260827010304-ee3a9ce6c2e4/go.mod h1:p+rsTJpVomHc8gyyLY0/Wwzj9qsPKoXgwCg2yIlG04A=
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/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=
github.com/ayn2op/tview v0.0.0-20260831000059-9f61521d46b4/go.mod h1:iFzkW7DBltCaeLMR9SvXVUAw/zgKIbgE8JaUU8W8crk=
github.com/bdandy/go-errors v1.2.2 h1:WdFv/oukjTJCLa79UfkGmwX7ZxONAihKu4V0mLIs11Q=
@ -42,8 +42,8 @@ github.com/dchest/jsmin v1.0.0 h1:Y2hWXmGZiRxtl+VcTksyucgTlYxnhPzTozCwx9gy9zI=
github.com/dchest/jsmin v1.0.0/go.mod h1:AVBIund7Mr7lKXT70hKT2YgL3XEXUaUk5iw9DZ8b0Uc=
github.com/dlclark/regexp2/v2 v2.7.1 h1:yqDtwI1ptXXvEUNpYTk2lad4jLtAcKqkzepn4savSk4=
github.com/dlclark/regexp2/v2 v2.7.1/go.mod h1:avUrQvPaLz2DrFNHJF0taWAFFX2C1GMSSoeiqFjcBmU=
github.com/ebitengine/purego v0.10.2 h1:W809HbnvzAxgdm+aOvlSekrM16wGCdT/e76+9tS7gzE=
github.com/ebitengine/purego v0.10.2/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
github.com/ebitengine/purego v0.11.0 h1:jhp/D+Nyv7UUW8HAcmcjt2N2rYrYi9m3SL21k0Ua/NI=
github.com/ebitengine/purego v0.11.0/go.mod h1:DCHPP08djqhNSoTfImcnHYQRZmd0qhakvrozqaEYhGQ=
github.com/gdamore/encoding v1.0.1 h1:YzKZckdBL6jVt2Gc+5p82qhrGiqMdG/eNs6Wy0u3Uhw=
github.com/gdamore/encoding v1.0.1/go.mod h1:0Z0cMFinngz9kS1QfMjCP8TY7em3bZYeeklsSDPivEo=
github.com/gdamore/tcell/v3 v3.4.2 h1:gGW+6z2Bz5Wl2mNwFlm9+eRmg2JQrWcKjSkL1LRfpNU=

View file

@ -7,8 +7,8 @@ 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"
"github.com/ayn2op/discordo/internal/config"
"github.com/ayn2op/ningen/v3/discordmd"
"github.com/ayn2op/tview"
"github.com/ayn2op/tview/text"
"github.com/gdamore/tcell/v3"
@ -118,8 +118,8 @@ func (r *Renderer) RenderText(source []byte, node ast.Node, base tcell.Style) te
} else {
builder.NewLine()
}
case *discordmd.Inline:
if r.cfg.Markdown.MaskSpoilers && node.Attr&discordmd.AttrSpoiler != 0 {
case *arikawamarkdown.Inline:
if r.cfg.Markdown.MaskSpoilers && node.Attr&arikawamarkdown.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 *discordmd.Mention:
case *arikawamarkdown.Mention:
if entering {
builder.Write(mentionText(node), tview.MergeStyle(currentStyle(), theme.MentionStyle.Style))
}
case *discordmd.Emoji:
case *arikawamarkdown.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 *discordmd.Mention) string {
func mentionText(node *arikawamarkdown.Mention) string {
switch {
case node.Channel != nil:
return "#" + node.Channel.Name
@ -270,20 +270,20 @@ func mentionText(node *discordmd.Mention) string {
}
}
func applyInlineAttr(style tcell.Style, attr discordmd.Attribute, inLink bool) tcell.Style {
if attr&discordmd.AttrBold != 0 {
func applyInlineAttr(style tcell.Style, attr arikawamarkdown.Attribute, inLink bool) tcell.Style {
if attr&arikawamarkdown.AttrBold != 0 {
style = style.Bold(true)
}
if attr&discordmd.AttrItalics != 0 {
if attr&arikawamarkdown.AttrItalics != 0 {
style = style.Italic(true)
}
if attr&discordmd.AttrUnderline != 0 {
if attr&arikawamarkdown.AttrUnderline != 0 {
style = style.Underline(true)
}
if attr&discordmd.AttrStrikethrough != 0 {
if attr&arikawamarkdown.AttrStrikethrough != 0 {
style = style.StrikeThrough(true)
}
if attr&discordmd.AttrMonospace != 0 {
if attr&arikawamarkdown.AttrMonospace != 0 {
// Avoid reverse-video inside links. Link labels like `hash` should still
// look like links, not highlighted blocks.
if !inLink {

View file

@ -4,8 +4,8 @@ import (
"strings"
"testing"
arikawamarkdown "github.com/ayn2op/arikawa/v3/markdown"
"github.com/ayn2op/discordo/internal/config"
"github.com/ayn2op/ningen/v3/discordmd"
"github.com/gdamore/tcell/v3"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/text"
@ -14,8 +14,8 @@ import (
func TestRendererMasksSpoilers(t *testing.T) {
source := []byte("before ||secret|| after")
node := parser.NewParser(
parser.WithBlockParsers(discordmd.BlockParsers()...),
parser.WithInlineParsers(discordmd.InlineParserWithLink()...),
parser.WithBlockParsers(arikawamarkdown.BlockParsers()...),
parser.WithInlineParsers(arikawamarkdown.InlineParserWithLink()...),
).Parse(text.NewReader(source))
for _, test := range []struct {

View file

@ -16,6 +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"
"github.com/ayn2op/arikawa/v3/state"
"github.com/ayn2op/arikawa/v3/utils/json/option"
"github.com/ayn2op/arikawa/v3/utils/sendpart"
@ -25,7 +26,6 @@ import (
"github.com/ayn2op/discordo/internal/ui"
"github.com/ayn2op/discordo/internal/ui/chat/mentionslist"
"github.com/ayn2op/ningen/v3"
"github.com/ayn2op/ningen/v3/discordmd"
"github.com/ayn2op/tview"
"github.com/ayn2op/tview/help"
"github.com/ayn2op/tview/keybind"
@ -372,12 +372,12 @@ func (c *composer) processText(channel *discord.Channel, src []byte) string {
canMention = true
)
ast.Walk(discordmd.Parse(src), func(node ast.Node, enter bool) (ast.WalkStatus, error) {
ast.Walk(arikawamarkdown.Parse(src), func(node ast.Node, enter bool) (ast.WalkStatus, error) {
switch node := node.(type) {
case *ast.CodeBlock, *ast.FencedCodeBlock:
canMention = !enter
case *discordmd.Inline:
if (node.Attr & discordmd.AttrMonospace) != 0 {
case *arikawamarkdown.Inline:
if (node.Attr & arikawamarkdown.AttrMonospace) != 0 {
canMention = !enter
}
case *ast.Text:

View file

@ -22,6 +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"
"github.com/ayn2op/arikawa/v3/state"
"github.com/ayn2op/arikawa/v3/utils/json/option"
"github.com/ayn2op/discordo/internal/config"
@ -29,7 +30,6 @@ import (
"github.com/ayn2op/discordo/internal/markdown"
"github.com/ayn2op/discordo/internal/ui"
"github.com/ayn2op/discordo/internal/ui/chat/attachmentspicker"
"github.com/ayn2op/ningen/v3/discordmd"
"github.com/ayn2op/tview"
"github.com/ayn2op/tview/help"
"github.com/ayn2op/tview/keybind"
@ -446,7 +446,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 := discordmd.ParseWithMessage(c, *ml.chat.state.Cabinet, &message, false)
root := arikawamarkdown.ParseWithMessage(c, *ml.chat.state.Cabinet, &message, false)
return ml.renderer.RenderText(c, root, baseStyle), root, c
}
@ -1049,8 +1049,8 @@ func (ml *messagesList) download() tview.Cmd {
func extractURLs(content string) []string {
src := []byte(content)
node := parser.NewParser(
parser.WithBlockParsers(discordmd.BlockParsers()...),
parser.WithInlineParsers(discordmd.InlineParserWithLink()...),
parser.WithBlockParsers(arikawamarkdown.BlockParsers()...),
parser.WithInlineParsers(arikawamarkdown.InlineParserWithLink()...),
).Parse(goldtext.NewReader(src))
return urlsFromAST(node, src)
}