-
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
-
108
-
109
-
110
-
111
-
112
-
113
-
114
-
115
-
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
-
124
-
125
-
126
-
127
-
128
-
129
-
130
-
131
-
132
-
133
-
134
-
135
-
136
-
137
-
138
-
139
-
140
-
141
-
142
-
143
-
144
-
145
-
146
-
147
-
148
-
149
-
150
-
151
-
152
-
153
-
154
-
155
-
156
-
157
-
158
-
159
-
160
-
161
-
162
-
163
-
164
-
165
-
166
-
167
-
168
-
169
-
170
-
171
-
172
-
173
-
174
-
175
-
176
-
177
-
178
-
179
-
180
-
181
-
182
-
183
-
184
-
185
-
186
-
187
-
188
-
189
-
190
-
191
-
192
-
193
-
194
-
195
-
196
-
197
-
198
-
199
-
200
-
201
-
202
-
203
-
204
-
205
-
206
-
207
-
208
-
209
-
210
-
211
-
212
-
213
-
214
-
215
-
216
-
217
-
218
-
219
-
220
-
221
-
222
-
223
-
224
-
225
-
226
-
227
-
228
-
229
-
230
-
231
-
232
-
233
-
234
-
235
-
236
-
237
-
238
-
239
-
240
-
241
-
242
-
243
-
244
-
245
-
246
-
247
-
248
-
249
-
250
-
251
-
252
-
253
-
254
-
255
-
256
-
257
-
258
-
259
-
260
-
261
-
262
-
263
-
264
-
265
-
266
-
267
-
268
-
269
-
270
-
271
-
272
-
273
-
274
-
275
-
276
-
277
-
278
-
279
-
280
/**
* `Node` interface's `type` property.
*/
export enum NodeType {
Text,
ChannelLink,
UserLink,
URL,
Command,
Emoji,
PreText,
Code,
Italic,
Bold,
Strike,
Quote,
Root,
}
/**
* Represents each parts of the message, its type and properties.
*
* Every node has two common properties: `type` and `source`.
* You can test the `type` against `NodeType` object (which is TypeScript enum) to narrow the node type down.
*
* ```ts
* import { NodeType } from "slack-message-parser";
* import type { Node } from "slack-message-parser";
*
* function logChannelOrUserId(node: Node): void {
* switch (node.type) {
* case NodeType.ChannelLink:
* console.log(node.channelID);
* return;
* case NodeType.UserLink:
* console.log(node.userID);
* return;
* default:
* return;
* }
* }
* ```
*
* The names of `Node` and `NodeType` are one-to-one mapped.
* e.g., the `type` property of a `Text` node is always `NodeType.Text`.
*
* `source` property contains raw unformatted text for the node.
*/
export type Node =
| Text
| ChannelLink
| UserLink
| URL
| Command
| Emoji
| PreText
| Code
| Italic
| Bold
| Strike
| Quote
| Root;
interface NodeBase {
type: NodeType;
/**
* Raw node text.
*/
source: string;
}
/**
* Texts.
*
* This node itself does not have any style-related information.
*/
export interface Text extends NodeBase {
type: NodeType.Text;
/**
* Text content.
*/
text: string;
}
/**
* Link to a Slack channel.
*/
export interface ChannelLink extends NodeBase {
type: NodeType.ChannelLink;
/**
* An ID of the destination channel.
*/
channelID: string;
/**
* Label text for the link.
*
* The channel's name will be displayed if the label is not set.
*/
label?: Node[];
}
/**
* Link to a Slack user.
*/
export interface UserLink extends NodeBase {
type: NodeType.UserLink;
/**
* An ID of the target user.
*/
userID: string;
/**
* Label text for the link.
*
* The user's name will be displayed if the label is not set.
*/
label?: Node[];
}
/**
* Generic link.
*
* e.g., `https://example.com/`, `mailto:user@example.com`
*/
export interface URL extends NodeBase {
type: NodeType.URL;
/**
* A URL of the link.
*/
url: string;
/**
* Label text for the link.
*
* The URL string itself will be displayed if the label is not set.
*/
label?: Node[];
}
/**
* Special commands such as `<!date>`, `<!everyone>`, or `<!subteam^id|@handle>`.
*
* For more details, please refer to [Formatting messages | Slack](https://api.slack.com/docs/message-formatting).
*
* @example
* ```ts
* import { parse, NodeType } from "slack-message-parser";
*
* const tree = parse("<!subteam^FOO>");
*
* // {
* // type: NodeType.Root,
* // children: [
* // {
* // type: NodeType.Command,
* // name: "subteam",
* // arguments: ["FOO"],
* // },
* // ],
* // }
* ```
*/
export interface Command extends NodeBase {
type: NodeType.Command;
/**
* A name of the command.
*/
name: string;
/**
* Command arguments, which are prefixed by `^`.
*/
arguments: string[];
/**
* Label text for the command.
*/
label?: Node[];
}
/**
* Emojis!
*/
export interface Emoji extends NodeBase {
type: NodeType.Emoji;
/**
* A name of the emoji (the text between two `:`s.)
*/
name: string;
/**
* Emoji variant.
*
* Only `skin-tone-` is currently supported.
*/
variation?: string;
}
/**
* Code block, multi-line codes.
*/
export interface PreText extends NodeBase {
type: NodeType.PreText;
/**
* Text content.
*
* This node cannot contain decorative texts.
*/
text: string;
}
/**
* Inline code
*/
export interface Code extends NodeBase {
type: NodeType.Code;
/**
* Text content.
*
* This node cannot contain decorative texts.
*/
text: string;
}
/**
* Italic text decoration context.
*/
export interface Italic extends NodeBase {
type: NodeType.Italic;
children: Node[];
}
/**
* Bold text decoration context.
*/
export interface Bold extends NodeBase {
type: NodeType.Bold;
children: Node[];
}
/**
* Strike-through decoration text context.
*/
export interface Strike extends NodeBase {
type: NodeType.Strike;
children: Node[];
}
/**
* Block quote.
*/
export interface Quote extends NodeBase {
type: NodeType.Quote;
children: Node[];
}
/**
* A node sits top of the tree.
*
* Every parse result has this node as its root node.
*/
export interface Root extends NodeBase {
type: NodeType.Root;
/**
* A list of child nodes.
*/
children: Node[];
}