Skip to content
This repository has been archived by the owner on Dec 16, 2021. It is now read-only.

Commit

Permalink
Custom emoji's are now rendered in markdown link format
Browse files Browse the repository at this point in the history
  • Loading branch information
fl4ke authored and Bios-Marcel committed Nov 6, 2019
1 parent 7a6f1f4 commit 1c48735
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
12 changes: 8 additions & 4 deletions ui/chatview.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const dashCharacter = "\u2500"

var (
successiveCustomEmojiRegex = regexp.MustCompile("<a?:.+?:\\d+(><)a?:.+?:\\d+>")
customEmojiRegex = regexp.MustCompile("(?sm)(.?)<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*>`)
Expand Down Expand Up @@ -676,12 +676,16 @@ func parseCustomEmojis(text string) string {

customEmojiMatches := customEmojiRegex.FindAllStringSubmatch(messageText, -1)
for _, match := range customEmojiMatches {
url := "https://cdn.discordapp.com/emojis/" + match[2]
custom_emoji_code := match[3]
if len(match[2]) > 0 {
custom_emoji_code = "a:" + custom_emoji_code
}
url := tviewutil.Escape("[" + custom_emoji_code + "](https://cdn.discordapp.com/emojis/" + match[4] + ")")
if match[1] != "" && match[1] != "\n" {
url = match[1] + "\n" + url
}
if match[3] != "" && match[3] != "\n" {
url = url + "\n" + match[3]
if match[5] != "" && match[5] != "\n" {
url = url + "\n" + match[5]
}
messageText = strings.Replace(messageText, match[0], url, 1)
}
Expand Down
26 changes: 17 additions & 9 deletions ui/chatview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,15 +375,15 @@ func TestChatView_formatMessageText(t *testing.T) {
ID: "OwO",
Content: "<:owo:123>",
},
want: "https://cdn.discordapp.com/emojis/123",
want: "[owo[](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",
want: "[a:owo[](https://cdn.discordapp.com/emojis/123)",
chatView: defaultChatView,
}, {
//FIXME Remove space, it's useless
Expand All @@ -392,7 +392,7 @@ func TestChatView_formatMessageText(t *testing.T) {
ID: "OwO",
Content: "<:owo:123><:owo:123>",
},
want: "https://cdn.discordapp.com/emojis/123\n https://cdn.discordapp.com/emojis/123",
want: "[owo[](https://cdn.discordapp.com/emojis/123)\n [owo[](https://cdn.discordapp.com/emojis/123)",
chatView: defaultChatView,
}, {
//FIXME Remove space, it's useless
Expand All @@ -401,7 +401,7 @@ func TestChatView_formatMessageText(t *testing.T) {
ID: "OwO",
Content: "<:owo:123> <:owo:123>",
},
want: "https://cdn.discordapp.com/emojis/123\n https://cdn.discordapp.com/emojis/123",
want: "[owo[](https://cdn.discordapp.com/emojis/123)\n [owo[](https://cdn.discordapp.com/emojis/123)",
chatView: defaultChatView,
}, {
//FIXME Remove space, it's useless
Expand All @@ -410,7 +410,7 @@ func TestChatView_formatMessageText(t *testing.T) {
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",
want: "[owo[](https://cdn.discordapp.com/emojis/123)\n [owo[](https://cdn.discordapp.com/emojis/124)\n [owo[](https://cdn.discordapp.com/emojis/125)\n [owo[](https://cdn.discordapp.com/emojis/126)",
chatView: defaultChatView,
}, {
//FIXME Remove space, it's useless
Expand All @@ -419,7 +419,7 @@ func TestChatView_formatMessageText(t *testing.T) {
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",
want: "[owo[](https://cdn.discordapp.com/emojis/123)\n [owo[](https://cdn.discordapp.com/emojis/124)\n [owo[](https://cdn.discordapp.com/emojis/125)\n [owo[](https://cdn.discordapp.com/emojis/126)",
chatView: defaultChatView,
}, {
//FIXME Remove spaces behind prefix and suffix of emoji
Expand All @@ -428,7 +428,7 @@ func TestChatView_formatMessageText(t *testing.T) {
ID: "OwO",
Content: "Look, <:owo:123> what's this?",
},
want: "Look, \nhttps://cdn.discordapp.com/emojis/123\n what's this?",
want: "Look, \n[owo[](https://cdn.discordapp.com/emojis/123)\n what's this?",
chatView: defaultChatView,
}, {
//FIXME Remove spaces behind prefix and suffix of emoji
Expand All @@ -437,7 +437,7 @@ func TestChatView_formatMessageText(t *testing.T) {
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?",
want: "Look, \n[owo[](https://cdn.discordapp.com/emojis/123)\n [owo[](https://cdn.discordapp.com/emojis/123)\n what's this?",
chatView: defaultChatView,
}, {
//FIXME Remove spaces behind prefix and suffix of emoji
Expand All @@ -446,7 +446,15 @@ func TestChatView_formatMessageText(t *testing.T) {
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?",
want: "Look, \n[owo[](https://cdn.discordapp.com/emojis/123)\n [owo[](https://cdn.discordapp.com/emojis/123)\n what's this?",
chatView: defaultChatView,
}, {
name: "message with custom animated emoji",
input: &discordgo.Message{
ID: "OwO",
Content: "Hello <a:owo:123>",
},
want: "Hello \n[a:owo[](https://cdn.discordapp.com/emojis/123)",
chatView: defaultChatView,
},
}
Expand Down

0 comments on commit 1c48735

Please sign in to comment.