From ff7aa4eef39c8f887710ef0b779bc3163a5cc5c5 Mon Sep 17 00:00:00 2001 From: ayntgl <90600421+ayntgl@users.noreply.github.com> Date: Fri, 1 Oct 2021 15:07:33 +0400 Subject: [PATCH] feat: implement discord-flavored markdown renderer (#66) --- go.mod | 2 +- go.sum | 4 ++-- renderer.go | 20 ++++++++++++++++++-- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index 720f48a..7981881 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,6 @@ require ( github.com/ayntgl/discordgo v0.23.3-0.20210920064905-b79d709bbafb github.com/gdamore/tcell/v2 v2.4.1-0.20210905002822-f057f0a857a1 github.com/gen2brain/beeep v0.0.0-20210529141713-5586760f0cc1 - github.com/rivo/tview v0.0.0-20210923051754-2cb20002bc4c + github.com/rivo/tview v0.0.0-20211001102648-5508f4b00266 github.com/zalando/go-keyring v0.1.1 ) diff --git a/go.sum b/go.sum index 77a2a21..4ea87b1 100644 --- a/go.sum +++ b/go.sum @@ -33,8 +33,8 @@ github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d h1:VhgPp6v9qf9Agr/ github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d/go.mod h1:YUTz3bUH2ZwIWBy3CJBeOBEugqcmXREj14T+iG/4k4U= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/rivo/tview v0.0.0-20210923051754-2cb20002bc4c h1:ye4bWm8SafYmr0DADOKSfeVZ1Swzm9aLW+baCOcHDWE= -github.com/rivo/tview v0.0.0-20210923051754-2cb20002bc4c/go.mod h1:WIfMkQNY+oq/mWwtsjOYHIZBuwthioY2srOmljJkTnk= +github.com/rivo/tview v0.0.0-20211001102648-5508f4b00266 h1:UrmGSzDIp4gfkDuMLdXk1Tx4FjS8GTWrWJjHfnS6GmY= +github.com/rivo/tview v0.0.0-20211001102648-5508f4b00266/go.mod h1:WIfMkQNY+oq/mWwtsjOYHIZBuwthioY2srOmljJkTnk= github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= diff --git a/renderer.go b/renderer.go index 2c1f82d..51b602c 100644 --- a/renderer.go +++ b/renderer.go @@ -2,11 +2,17 @@ package main import ( "fmt" + "regexp" "strings" "github.com/ayntgl/discordgo" ) +var boldRegex = regexp.MustCompile(`(?m)\*\*(.*?)\*\*`) +var italicRegex = regexp.MustCompile(`(?m)\*(.*?)\*`) +var underlineRegex = regexp.MustCompile(`(?m)__(.*?)__`) +var strikeThroughRegex = regexp.MustCompile(`(?m)~~(.*?)~~`) + func renderMessages(cID string) { ms, err := session.ChannelMessages(cID, conf.GetMessagesLimit, "", "", "") if err != nil { @@ -39,7 +45,7 @@ func renderMessage(m *discordgo.Message) { if rm.Content != "" { rm.Content = parseMentions(rm.Content, rm.Mentions) - b.WriteString(rm.Content) + b.WriteString(parseMarkdown(rm.Content)) } b.WriteString("[::-]\n") @@ -50,7 +56,7 @@ func renderMessage(m *discordgo.Message) { // (users mentioned in the message) and render the message content. if m.Content != "" { m.Content = parseMentions(m.Content, m.Mentions) - b.WriteString(m.Content) + b.WriteString(parseMarkdown(m.Content)) } // If the edited timestamp of the message is not empty; it implies that // the message has been edited, hence render the message with edited @@ -120,3 +126,13 @@ func parseAuthor(b *strings.Builder, u *discordgo.User) { b.WriteString("[#EB459E]BOT[-] ") } } + +func parseMarkdown(md string) string { + var res string + res = boldRegex.ReplaceAllString(md, "[::b]$1[::-]") + res = italicRegex.ReplaceAllString(res, "[::i]$1[::-]") + res = underlineRegex.ReplaceAllString(res, "[::u]$1[::-]") + res = strikeThroughRegex.ReplaceAllString(res, "[::s]$1[::-]") + + return res +}