generated from mattermost/mattermost-plugin-starter-template
/
locations.go
67 lines (56 loc) · 1.2 KB
/
locations.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
package apps
import (
"strings"
"github.com/mattermost/mattermost-plugin-apps/utils/md"
)
const (
LocationPostMenu Location = "/post_menu"
LocationChannelHeader Location = "/channel_header"
LocationCommand Location = "/command"
LocationInPost Location = "/in_post"
)
type Location string
type Locations []Location
func (l Location) IsTop() bool {
switch l {
case LocationChannelHeader,
LocationCommand,
LocationPostMenu:
return true
}
return false
}
func (l Location) In(other Location) bool {
return strings.HasPrefix(string(l), string(other))
}
func (l Location) Make(sub Location) Location {
out := l
if len(sub) == 0 {
return out
}
if sub[0] != '/' {
out += "/"
}
return out + sub
}
func (l Location) Markdown() md.MD {
if l[0] != '/' {
return md.MD(l)
}
tokens := strings.Split(string(l)[1:], "/")
if len(tokens) == 0 {
return md.MD(l)
}
switch Location("/" + tokens[0]) {
case LocationPostMenu:
return "Post Menu items"
case LocationChannelHeader:
return "Channel Header buttons"
case LocationCommand:
if len(tokens) < 2 {
return "Slash commands"
}
return md.Markdownf("`/%s` command", strings.Join(tokens[1:], " "))
}
return md.MD(l)
}