Render custom emojim as links
MODIFY
ui/chatview.go
ui/chatview.go
@@ -3,14 +3,13 @@ package ui
import (
"bytes"
"fmt"
+ linkshortener "github.com/Bios-Marcel/shortnotforlong"
"math"
"regexp"
"strconv"
"strings"
"sync"
- linkshortener "github.com/Bios-Marcel/shortnotforlong"
-
"github.com/Bios-Marcel/cordless/discordutil"
"github.com/Bios-Marcel/cordless/times"
"github.com/Bios-Marcel/cordless/ui/tviewutil"
@@ -31,6 +30,8 @@ import (
const dashCharacter = "\u2500"
var (
+ successiveCustomEmojiRegex = regexp.MustCompile("<a?:.+?:\\d+(><)a?:.+?:\\d+>")
+ customEmojiRegex = regexp.MustCompile("(?sm)(.?)<a?:.+?:(\\d+)>(.?)")
codeBlockRegex = regexp.MustCompile("(?sm)(^|.)?(\x60\x60\x60(.*?)?\n(.+?)\x60\x60\x60)($|.)")
colorRegex = regexp.MustCompile("\\[#.{6}\\]")
channelMentionRegex = regexp.MustCompile(`<#\d*>`)
@@ -646,6 +647,7 @@ func (chatView *ChatView) formatDefaultMessageText(message *discordgo.Message) s
messageText = strings.
NewReplacer("\\*", "*", "\\_", "_", "\\`", "`").
Replace(parseBoldAndUnderline(messageText))
+ messageText = parseCustomEmojis(messageText)
shouldShow, contains := chatView.showSpoilerContent[message.ID]
if !contains || !shouldShow {
@@ -656,6 +658,37 @@ func (chatView *ChatView) formatDefaultMessageText(message *discordgo.Message) s
return messageText
}
+func parseCustomEmojis(text string) string {
+ messageText := text
+
+ //Little hack, since the customEmojiRegex can't handle <:emoji:123><:emoji:123>
+ //And we do it this way in order to allow overlapping matches.
+ var matches [][]int
+ for resume := true; resume; resume = len(matches) > 0 {
+ matches = successiveCustomEmojiRegex.FindAllStringSubmatchIndex(messageText, -1)
+ //Iterating backwards, since the indexes would be incorrect after
+ //the first match otherwise
+ for i := len(matches) - 1; i >= 0; i-- {
+ match := matches[i]
+ messageText = messageText[:match[2]+1] + " " + messageText[match[3]-1:]
+ }
+ }
+
+ customEmojiMatches := customEmojiRegex.FindAllStringSubmatch(messageText, -1)
+ for _, match := range customEmojiMatches {
+ url := "https://cdn.discordapp.com/emojis/" + match[2]
+ if match[1] != "" && match[1] != "\n" {
+ url = match[1] + "\n" + url
+ }
+ if match[3] != "" && match[3] != "\n" {
+ url = url + "\n" + match[3]
+ }
+ messageText = strings.Replace(messageText, match[0], url, 1)
+ }
+
+ return messageText
+}
+
func trimMinAmountOfCharacterAsPrefix(charToTrim rune, text string) (string, int) {
lines := strings.Split(text, "\n")
minAmountOfCharacter := math.MaxInt32

MODIFY
ui/chatview_test.go
ui/chatview_test.go
@@ -369,6 +369,85 @@ func TestChatView_formatMessageText(t *testing.T) {
},
want: "\\`*_",
chatView: defaultChatView,
+ }, {
+ name: "single custom emoji",
+ input: &discordgo.Message{
+ ID: "OwO",
+ Content: "<:owo:123>",
+ },
+ want: "https://cdn.discordapp.com/emojis/123",
+ chatView: defaultChatView,
+ }, {
+ name: "single animated custom emoji",
+ input: &discordgo.Message{
+ ID: "OwO",
+ Content: "<a:owo:123>",
+ },
+ want: "https://cdn.discordapp.com/emojis/123",
+ chatView: defaultChatView,
+ }, {
+ //FIXME Remove space, it's useless
+ name: "two custom emoji without space",
+ input: &discordgo.Message{
+ ID: "OwO",
+ Content: "<:owo:123><:owo:123>",
+ },
+ want: "https://cdn.discordapp.com/emojis/123\n https://cdn.discordapp.com/emojis/123",
+ chatView: defaultChatView,
+ }, {
+ //FIXME Remove space, it's useless
+ name: "two custom emoji with space",
+ input: &discordgo.Message{
+ ID: "OwO",
+ Content: "<:owo:123> <:owo:123>",
+ },
+ want: "https://cdn.discordapp.com/emojis/123\n https://cdn.discordapp.com/emojis/123",
+ chatView: defaultChatView,
+ }, {
+ //FIXME Remove space, it's useless
+ name: "multiple successive emoji without spaces",
+ input: &discordgo.Message{
+ ID: "OwO",
+ Content: "<:owo:123><:owo:124><:owo:125><:owo:126>",
+ },
+ want: "https://cdn.discordapp.com/emojis/123\n https://cdn.discordapp.com/emojis/124\n https://cdn.discordapp.com/emojis/125\n https://cdn.discordapp.com/emojis/126",
+ chatView: defaultChatView,
+ }, {
+ //FIXME Remove space, it's useless
+ name: "multiple successive emoji with spaces",
+ input: &discordgo.Message{
+ ID: "OwO",
+ Content: "<:owo:123> <:owo:124> <:owo:125> <:owo:126>",
+ },
+ want: "https://cdn.discordapp.com/emojis/123\n https://cdn.discordapp.com/emojis/124\n https://cdn.discordapp.com/emojis/125\n https://cdn.discordapp.com/emojis/126",
+ chatView: defaultChatView,
+ }, {
+ //FIXME Remove spaces behind prefix and suffix of emoji
+ name: "message with custom emoji",
+ input: &discordgo.Message{
+ ID: "OwO",
+ Content: "Look, <:owo:123> what's this?",
+ },
+ want: "Look, \nhttps://cdn.discordapp.com/emojis/123\n what's this?",
+ chatView: defaultChatView,
+ }, {
+ //FIXME Remove spaces behind prefix and suffix of emoji
+ name: "message with multiple custom emoji with spaces",
+ input: &discordgo.Message{
+ ID: "OwO",
+ Content: "Look, <:owo:123> <:owo:123> what's this?",
+ },
+ want: "Look, \nhttps://cdn.discordapp.com/emojis/123\n https://cdn.discordapp.com/emojis/123\n what's this?",
+ chatView: defaultChatView,
+ }, {
+ //FIXME Remove spaces behind prefix and suffix of emoji
+ name: "message with multiple custom emoji without spaces",
+ input: &discordgo.Message{
+ ID: "OwO",
+ Content: "Look, <:owo:123><:owo:123> what's this?",
+ },
+ want: "Look, \nhttps://cdn.discordapp.com/emojis/123\n https://cdn.discordapp.com/emojis/123\n what's this?",
+ chatView: defaultChatView,
},
}
for _, tt := range tests {

Render custom emojim as links