From 36ebaa6cf99176ebbafea4963c75bc60e082bd87 Mon Sep 17 00:00:00 2001 From: ayn2op Date: Wed, 18 Jun 2025 14:59:27 -0400 Subject: [PATCH] refactor(notifications): do not parse markdown in content --- internal/notifications/notifications.go | 18 ++--- internal/notifications/renderer.go | 93 ------------------------- 2 files changed, 4 insertions(+), 107 deletions(-) delete mode 100644 internal/notifications/renderer.go diff --git a/internal/notifications/notifications.go b/internal/notifications/notifications.go index aba5f2b..1e4117c 100644 --- a/internal/notifications/notifications.go +++ b/internal/notifications/notifications.go @@ -6,14 +6,12 @@ import ( "net/http" "os" "path/filepath" - "strings" "github.com/ayn2op/discordo/internal/config" "github.com/ayn2op/discordo/internal/consts" "github.com/diamondburned/arikawa/v3/discord" "github.com/diamondburned/arikawa/v3/gateway" "github.com/diamondburned/ningen/v3" - "github.com/diamondburned/ningen/v3/discordmd" ) func HandleIncomingMessage(state *ningen.State, msg *gateway.MessageCreateEvent, cfg *config.Config) error { @@ -36,21 +34,13 @@ func HandleIncomingMessage(state *ningen.State, msg *gateway.MessageCreateEvent, } } - // Render message - src := []byte(msg.Content) - ast := discordmd.ParseWithMessage(src, *state.Cabinet, &msg.Message, false) - buff := strings.Builder{} - if err := defaultRenderer.Render(&buff, src, ast); err != nil { - return err - } - // Handle sent files - notifContent := buff.String() + content := msg.Content if msg.Content == "" && len(msg.Attachments) > 0 { - notifContent = "Uploaded " + msg.Message.Attachments[0].Filename + content = "Uploaded " + msg.Message.Attachments[0].Filename } - if msg.Author.DisplayOrTag() == "" || notifContent == "" { + if msg.Author.DisplayOrTag() == "" || content == "" { return nil } @@ -74,7 +64,7 @@ func HandleIncomingMessage(state *ningen.State, msg *gateway.MessageCreateEvent, } shouldChime := cfg.Notifications.Sound.Enabled && (!cfg.Notifications.Sound.OnlyOnPing || (isChannelDM || state.MessageMentions(&msg.Message) == 3)) - if err := sendDesktopNotification(notifTitle, notifContent, imagePath, shouldChime, cfg.Notifications.Duration); err != nil { + if err := sendDesktopNotification(notifTitle, content, imagePath, shouldChime, cfg.Notifications.Duration); err != nil { return err } diff --git a/internal/notifications/renderer.go b/internal/notifications/renderer.go deleted file mode 100644 index e55920b..0000000 --- a/internal/notifications/renderer.go +++ /dev/null @@ -1,93 +0,0 @@ -package notifications - -import ( - "io" - - "github.com/diamondburned/ningen/v3/discordmd" - "github.com/yuin/goldmark/ast" - gmr "github.com/yuin/goldmark/renderer" -) - -// Using a modified version of the discordmd BasicRenderer -var defaultRenderer = newRenderer() - -type renderer struct { - config *gmr.Config -} - -func newRenderer() *renderer { - config := gmr.NewConfig() - return &renderer{config} -} - -func (r *renderer) AddOptions(opts ...gmr.Option) { - for _, opt := range opts { - opt.SetConfig(r.config) - } -} - -func (r *renderer) Render(w io.Writer, source []byte, n ast.Node) error { - return ast.Walk(n, func(n ast.Node, entering bool) (ast.WalkStatus, error) { - switch n := n.(type) { - case *ast.Document: - // noop - case *ast.Blockquote: - io.WriteString(w, "\"") - case *ast.Heading: - io.WriteString(w, "\n") - case *ast.FencedCodeBlock: - io.WriteString(w, "\n") - - if entering { - lines := n.Lines() - for i := range lines.Len() { - line := lines.At(i) - io.WriteString(w, "| ") - w.Write(line.Value(source)) - } - } - case *ast.AutoLink: - if entering { - w.Write(n.URL(source)) - } - case *ast.Link: - if !entering { - io.WriteString(w, " ("+string(n.Destination)+")") - } - case *discordmd.Inline: - if n.Attr&discordmd.AttrSpoiler != 0 { - if entering { - io.WriteString(w, "*spoiler*") - } - return ast.WalkSkipChildren, nil - } - case *ast.Text: - if entering { - w.Write(n.Segment.Value(source)) - switch { - case n.HardLineBreak(): - io.WriteString(w, "\n\n") - case n.SoftLineBreak(): - io.WriteString(w, "\n") - } - } - case *discordmd.Mention: - if entering { - switch { - case n.Channel != nil: - io.WriteString(w, "#"+n.Channel.Name) - case n.GuildUser != nil: - io.WriteString(w, "@"+n.GuildUser.Username) - case n.GuildRole != nil: - io.WriteString(w, "@"+n.GuildRole.Name) - } - } - case *discordmd.Emoji: - if entering { - io.WriteString(w, ":"+string(n.Name)+":") - } - } - - return ast.WalkContinue, nil - }) -}