-
Notifications
You must be signed in to change notification settings - Fork 1
/
emoji.go
107 lines (81 loc) · 1.9 KB
/
emoji.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package md
import (
"bytes"
"regexp"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/text"
)
const (
InlineEmojiSize = 22
LargeEmojiSize = 48
)
func EmojiURL(emojiID string, animated bool) string {
const EmojiBaseURL = "https://cdn.discordapp.com/emojis/"
if animated {
return EmojiBaseURL + emojiID + ".gif?v=1"
}
return EmojiBaseURL + emojiID + ".png?v=1"
}
type Emoji struct {
ast.BaseInline
ID string
Name string
GIF bool
Large bool // TODO
}
var KindEmoji = ast.NewNodeKind("Emoji")
// Kind implements Node.Kind.
func (e *Emoji) Kind() ast.NodeKind {
return KindEmoji
}
// Dump implements Node.Dump
func (e *Emoji) Dump(source []byte, level int) {
ast.DumpHelper(e, source, level, nil, nil)
}
func (e Emoji) EmojiURL() string {
return EmojiURL(string(e.ID), e.GIF)
}
type emoji struct {
searched bool // if a small/large check was done
large bool
}
var emojiRegex = regexp.MustCompile(`<(a?):(.+?):(\d+)>`)
func (emoji) Trigger() []byte {
// return []byte("http")
return []byte{'<'}
}
func (state *emoji) Parse(parent ast.Node, block text.Reader, pc parser.Context) ast.Node {
match := matchInline(block, '<', '>')
if match == nil {
return nil
}
var matches = emojiRegex.FindSubmatch(match)
if len(matches) != 4 {
return nil
}
var emoji = &Emoji{
BaseInline: ast.BaseInline{},
GIF: string(matches[1]) == "a",
Name: string(matches[2]),
ID: string(matches[3]),
Large: state.large,
}
// Check if emojis should be small:
if !state.searched {
state.searched = true
// Get the entire text:
source := block.Source()
// Try and delete all emoji matches:
source = emojiRegex.ReplaceAll(source, nil)
// Trim spaces:
source = bytes.TrimSpace(source)
// Check if there are letters:
if len(source) == 0 {
// No, make our emojis big:
state.large = true
emoji.Large = true
}
}
return emoji
}