-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
169 lines (159 loc) · 4.16 KB
/
index.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
/**
* Creates new EntityMessage to convert the text and entities into html or other formats.
* @class
*/
class EntityMessage {
/**
* Create a EntityMessage.
* @param {string} text - Text to be converted
* @param {Object[]} entities - Array of [entity objects](https://core.telegram.org/bots/api#messageentity) in the text
*/
constructor(text, entities) {
// validate params
if (!text || typeof text !== "string" || !entities || !entities.length) {
throw new Error(
`Expected string in field text - got ${typeof text}, array/object in field entities - got ${typeof entities}`
);
}
this.text = text;
this.entities = entities;
}
/**
* Get the HTML format.
* @return {string} HTML formatted text
*/
get html() {
return applyEntity(this.text, this.entities, "html");
}
/**
* Get the Markdown format.
* @return {string} Markdown formatted text
*/
get markdown() {
return applyEntity(this.text, this.entities, "markdown");
}
}
// Helper functions
function applyEntity(mainText = "", entities, markupType = "html") {
if (markupType !== "html" || markupType !== "markdown") return mainText;
if (!entities.length) return mainText;
if (!mainText.length) return mainText;
let content = mainText;
const addedTags = [];
for (let entity of entities) {
const {
type,
offset,
length,
url,
user,
language,
custom_emoji_id
} = entity;
const text = mainText.slice(offset, offset + length);
const [opening, closing] = (entityTypes(text, {
url,
userId: user?.id,
custom_emoji_id,
language
})[type][markupType] || ["", ""]);
if (opening !== "" || closing !== "") {
const {start: beforeStart, end: beforeEnd} = addedTags.reduce((t, {startAt, tag}) => {
let {start, end} = t;
if (startAt <= offset) {
start += tag.length;
}
if (startAt < (offset + length)) {
end += tag.length
}
return {start, end};
}, {
start: 0, end: 0
})
const start = offset + beforeStart;
const end = offset + length + beforeEnd;
addedTags.push({startAt: offset, tag: opening});
addedTags.push({startAt: offset + length, tag: closing});
content = content.slice(0, start) + opening + content.slice(start, end) + closing + content.slice(end);
}
}
return content;
}
const entityTypes = (text, {url, userId, custom_emoji_id}) => {
/**
* [markUpType]: {
* [markUpName]: [openingTag, closingTag],
* },
* */
return {
mention: {
html: [`<a href="https://t.me/${text.slice(1)}">`, `</a>`],
markdown: [`[`, `](https://t.me/${text.slice(1)})`],
},
hashtag: {
html: [``, ``],
markdown: [``, ``],
},
cashtag: {
html: [``, ``],
markdown: [``, ``],
},
bot_command: {
html: [``, ``],
markdown: [``, ``],
},
url: {
html: [`<a href="${text}">`, `</a>`],
markdown: [`[`, `](${text})`],
},
email: {
html: [`<a href="mailto:${text}">`, `</a>`],
markdown: [`[`, `](mailto:${text})`],
},
phone_number: {
html: [`<a href="tel:${text}">`, `</a>`],
markdown: [`[`, `](tel:${text})`],
},
bold: {
html: [`<b>`, `</b>`],
markdown: [`**`, `**`],
},
italic: {
html: [`<i>`, `</i>`],
markdown: [`*`, `*`],
},
underline: {
html: [`<u>`, `</u>`],
markdown: [``, ``],
},
strikethrough: {
html: [`<s>`, `</s>`],
markdown: [``, ``],
},
spoiler: {
html: [`<span class="tg-spoiler">`, `</span>`],
markdown: [``, ``],
},
code: {
html: [`<code>`, `</code>`],
markdown: [``, ``],
},
pre: {
html: [`<pre>`, `</pre>`],
markdown: [`\`\`\``, `\`\`\``],
},
text_link: {
html: [`<a href="${url}">`, `</a>`],
markdown: [`[`, `](${url})`],
},
text_mention: {
html: [`<a href="tg://user?id=${userId}">`, `</a>`],
markdown: [`[`, `](tg://user?id=${userId})`],
},
custom_emoji: {
html: [``, ``],
markdown: [``, ``],
},
}
}
module.exports = {EntityMessage};