-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
286 lines (253 loc) · 7.71 KB
/
main.js
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
281
282
283
284
285
286
// take slack text and translate to a Notion item
// Slack user ID to Notion user ID dictionary
const slackNotionId = {
UT9G67J1Z: "f2ca3fc5-9ca1-46ed-be8b-fb618c56558a",
U0185FAF1T5: "6718f0c7-f6e3-4c3a-9f65-e8344806b5b6",
U025P5K0S0Z: "6f7ce62c-fa2e-4440-8805-72af5f937666",
U021UR4DW5C: "8fd7689c-d795-4ae9-aa53-5846ac1569b7",
U0224KFNYRW: "7c02e0ba-2aec-4696-a91d-ecaa01b616ce",
U025J9SLXV3: "94f6b8b7-e8b0-4790-8265-f08e6b1d550c",
UT9G67YFM: "6c3a6ec1-4b99-4e5c-8214-cea14fd9b142",
};
// example message from Slack
const slackExample =
"Hi this is a message with:\n" +
"\n" +
"• *bold*, _italic_, and `code` , along with <http://endless.horse/|links> and emojis :potato: :shrimp: :wave: \n" +
"• and tagged users like HEY <@U0185FAF1T5> ";
// for emojis
import he from "he";
import fs from "fs";
// import slack to html emoji dictionary
let rawdata = fs.readFileSync("./slack_emoticons_to_html_unicode.json");
let emojis = JSON.parse(rawdata);
// following functions are for converting Slack to Notion Item
// replace the emojis codes (from Slack) in the text with actual emojis
const replaceEmojis = (string) => {
// split string based on words
var splitString = string.split(" ");
// for each word in the string:
// see if the word has the emoji marker ":"
// search keys in the emoji for the word
// replace the word with the decoded html value
splitString.forEach((word) => {
if (word.search(":") != -1) {
for (var key in emojis) {
if (word.search(":" + key + ":") != -1) {
string = string.replace(key, he.decode(emojis[key]));
// replace all the ":" in the string and return
string = string.replace(/:/gi, "");
}
}
}
});
return string;
};
// create a new Notion block item for links
const newLinkItem = (plainText, link) => {
var array = {
type: "text",
text: {
content: plainText,
link: {
type: "url",
url: link,
},
},
};
return array;
};
// create a new Notion block item for text
const newTextItem = (text) => {
var array = {
type: "text",
text: {
content: text,
},
};
return array;
};
// create a new Notion block item for users
const newUserItem = (slackUserID) => {
var array = {
type: "mention",
mention: {
// find the user's Notion ID from the Slack ID and the dictionary
type: "user",
user: { id: slackNotionId[slackUserID] },
},
};
return array;
};
// create a new Notion block item for code
const newCodeItem = (codeText) => {
var array = {
type: "text",
text: {
content: codeText,
},
annotations: {
code: true,
},
};
return array;
};
// create a new Notion block item for bold text
const newBoldItem = (boldText) => {
var array = {
type: "text",
text: {
content: boldText,
},
annotations: {
bold: true,
},
};
return array;
};
// create a new Notion block item for code text
const newItalicItem = (italicText) => {
var array = {
type: "text",
text: {
content: italicText,
},
annotations: {
italic: true,
},
};
return array;
};
// create a new Notion block item for strikethrough text
const newStrikeItem = (strikeText) => {
var array = {
type: "text",
text: {
content: strikeText,
},
annotations: {
strikethrough: true,
},
};
return array;
};
// create a new child of a page with different blocks
const newChild = (splitItem) => {
// create the Item
var notionItem = [];
// the input is a split item based on (/[\<\>]/), and then for each item
// both links and users are indicated by <text>
splitItem.forEach((item) => {
if ((item.search(/https?/) != -1) | (item.search(/mailto/) != -1)) {
// see if its a link item by searching for link text indicators
// split link into text and link
let linkSplit = item.split("|");
// create link item and push to notionItem
const linkItem = newLinkItem(linkSplit[1], linkSplit[0]);
notionItem.push(linkItem);
} else if (item.search("@") != -1) {
// see if it is a user by searching for the @ symbol
// replace indicator symbol
var string = item.replace("@", "");
// check if the string is in the table, if not just push the string as a text item
if (string in slackNotionId) {
// create a new user item and push to notionItem
const userItem = newUserItem(string);
notionItem.push(userItem);
} else {
const textItem = newTextItem(item);
notionItem.push(textItem);
}
} else if (item.search(/[\`\_\*\~]/) != -1) {
// if a string contains any special annotations (bold, italic, code, strikethrough)
// replace any emojis in string
item = replaceEmojis(item);
// kinda wack, but replace all the symbols with = on either end
// so it can break without getting rid of the original symbol
item = item.replace(/[\*](?=[a-zA-Z0-9])/, "=*");
item = item.replace(/(?<=[a-zA-Z0-9,])[\*]/, "*=");
item = item.replace(/[\`](?=[a-zA-Z0-9])/, "=`");
item = item.replace(/(?<=[a-zA-Z0-9,])[\``]/, "`=");
item = item.replace(/[\_](?=[a-zA-Z0-9])/, "=_");
item = item.replace(/(?<=[a-zA-Z0-9,])[\_]/, "_=");
item = item.replace(/[\~](?=[a-zA-Z0-9])/, "=~");
item = item.replace(/(?<=[a-zA-Z0-9,])[\~]/, "~=");
// split item based off of =
var split = item.split(/\=/gi);
// for each item, check to see what type it is, replace the indicator, and push to notionItem
split.forEach((split) => {
if (split.search("`") != -1) {
split = split.replace(/\`/gi, "");
const item = newCodeItem(split);
notionItem.push(item);
} else if (split.search("_") != -1) {
split = split.replace(/\_/gi, "");
const item = newItalicItem(split);
notionItem.push(item);
} else if (split.search(/[\*]/) != -1) {
split = split.replace(/\*/gi, "");
const item = newBoldItem(split);
notionItem.push(item);
} else if (split.search("~") != -1) {
split = split.replace(/\~/gi, "");
const item = newStrikeItem(split);
notionItem.push(item);
} else {
const textItem = newTextItem(split);
notionItem.push(textItem);
}
});
} else {
// if the string is normal, then replace emojis and push text item
var string = replaceEmojis(item);
const textItem = newTextItem(string);
notionItem.push(textItem);
}
});
console.log(notionItem);
return notionItem;
};
// create a new Notion item
const newNotionItem = (slackMessage) => {
// empty block for spacing
const emptyBlock = {
object: "block",
type: "paragraph",
paragraph: {
text: [
{
type: "text",
text: {
content: "",
},
},
],
},
};
// notion Item
const notionItem = [];
// split message on line breaks and filter empty lines
var newLineSplit = slackMessage.split("\n");
newLineSplit = newLineSplit.filter(Boolean);
// for each line in Slack message
newLineSplit.forEach((line) => {
// split line based on link/user indicators
var regex = new RegExp(/[\<\>]/);
var split = line.split(regex);
// create new child item content
var item = newChild(split);
// add child item content to formatted block
const childItem = {
object: "block",
type: "paragraph",
paragraph: { text: item },
};
// push child to notionItem
notionItem.push(childItem);
});
// add an empty block for spacing and return
notionItem.push(emptyBlock);
console.log(notionItem);
return notionItem;
};
newNotionItem(slackExample);