@mtcute/markdown-parser 0.16.7 → 0.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.cjs +352 -0
- package/{cjs/index.d.ts → index.d.cts} +2 -2
- package/index.d.ts +47 -1
- package/index.js +347 -1
- package/markdown-parser.test.d.ts +1 -0
- package/package.json +28 -24
- package/cjs/index.js +0 -415
- package/cjs/index.js.map +0 -1
- package/cjs/package.json +0 -3
- package/esm/index.d.ts +0 -47
- package/esm/index.js +0 -403
- package/esm/index.js.map +0 -1
package/cjs/index.js
DELETED
|
@@ -1,415 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
if (typeof globalThis !== 'undefined' && !globalThis._MTCUTE_CJS_DEPRECATION_WARNED) {
|
|
3
|
-
globalThis._MTCUTE_CJS_DEPRECATION_WARNED = true
|
|
4
|
-
console.warn("[@mtcute/markdown-parser] CommonJS support is deprecated and will be removed soon. Please consider switching to ESM, it's "+(new Date()).getFullYear()+" already.")
|
|
5
|
-
console.warn("[@mtcute/markdown-parser] Learn more about switching to ESM: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c")
|
|
6
|
-
}
|
|
7
|
-
"use strict";
|
|
8
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
9
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.md = void 0;
|
|
13
|
-
const long_1 = __importDefault(require("long"));
|
|
14
|
-
const MENTION_REGEX = /^tg:\/\/user\?id=(\d+)(?:&hash=(-?[0-9a-fA-F]+)(?:&|$)|&|$)/;
|
|
15
|
-
const EMOJI_REGEX = /^tg:\/\/emoji\?id=(-?\d+)/;
|
|
16
|
-
const TAG_BOLD = '**';
|
|
17
|
-
const TAG_ITALIC = '__';
|
|
18
|
-
const TAG_UNDERLINE = '--';
|
|
19
|
-
const TAG_STRIKE = '~~';
|
|
20
|
-
const TAG_SPOILER = '||';
|
|
21
|
-
const TAG_CODE = '`';
|
|
22
|
-
const TAG_PRE = '```';
|
|
23
|
-
const TO_BE_ESCAPED = /[*_\-~`[\\\]|]/g;
|
|
24
|
-
/**
|
|
25
|
-
* Escape a string to be safely used in Markdown.
|
|
26
|
-
*
|
|
27
|
-
* > **Note**: this function is in most cases not needed, as `md` function
|
|
28
|
-
* > handles all `string`s passed to it automatically as plain text.
|
|
29
|
-
*/
|
|
30
|
-
function escape(str) {
|
|
31
|
-
return str.replace(TO_BE_ESCAPED, s => `\\${s}`);
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Add Markdown formatting to the text given the plain text and entities contained in it.
|
|
35
|
-
*/
|
|
36
|
-
function unparse(input) {
|
|
37
|
-
if (typeof input === 'string')
|
|
38
|
-
return escape(input);
|
|
39
|
-
let text = input.text;
|
|
40
|
-
const entities = input.entities ?? [];
|
|
41
|
-
// keep track of positions of inserted escape symbols
|
|
42
|
-
const escaped = [];
|
|
43
|
-
text = text.replace(TO_BE_ESCAPED, (s, pos) => {
|
|
44
|
-
escaped.push(pos);
|
|
45
|
-
return `\\${s}`;
|
|
46
|
-
});
|
|
47
|
-
const hasEscaped = escaped.length > 0;
|
|
48
|
-
const insert = [];
|
|
49
|
-
for (const entity of entities) {
|
|
50
|
-
const type = entity._;
|
|
51
|
-
let start = entity.offset;
|
|
52
|
-
let end = start + entity.length;
|
|
53
|
-
if (start > text.length)
|
|
54
|
-
continue;
|
|
55
|
-
if (start < 0)
|
|
56
|
-
start = 0;
|
|
57
|
-
if (end > text.length)
|
|
58
|
-
end = text.length;
|
|
59
|
-
if (hasEscaped) {
|
|
60
|
-
// determine number of escape chars since the beginning of the string
|
|
61
|
-
let escapedPos = 0;
|
|
62
|
-
while (escapedPos < escaped.length && escaped[escapedPos] < start) {
|
|
63
|
-
escapedPos += 1;
|
|
64
|
-
}
|
|
65
|
-
start += escapedPos;
|
|
66
|
-
while (escapedPos < escaped.length && escaped[escapedPos] <= end) {
|
|
67
|
-
escapedPos += 1;
|
|
68
|
-
}
|
|
69
|
-
end += escapedPos;
|
|
70
|
-
}
|
|
71
|
-
let startTag;
|
|
72
|
-
let endTag;
|
|
73
|
-
switch (type) {
|
|
74
|
-
case 'messageEntityBold':
|
|
75
|
-
startTag = endTag = TAG_BOLD;
|
|
76
|
-
break;
|
|
77
|
-
case 'messageEntityItalic':
|
|
78
|
-
startTag = endTag = TAG_ITALIC;
|
|
79
|
-
break;
|
|
80
|
-
case 'messageEntityUnderline':
|
|
81
|
-
startTag = endTag = TAG_UNDERLINE;
|
|
82
|
-
break;
|
|
83
|
-
case 'messageEntityStrike':
|
|
84
|
-
startTag = endTag = TAG_STRIKE;
|
|
85
|
-
break;
|
|
86
|
-
case 'messageEntitySpoiler':
|
|
87
|
-
startTag = endTag = TAG_SPOILER;
|
|
88
|
-
break;
|
|
89
|
-
case 'messageEntityCode':
|
|
90
|
-
startTag = endTag = TAG_CODE;
|
|
91
|
-
break;
|
|
92
|
-
case 'messageEntityPre':
|
|
93
|
-
startTag = TAG_PRE;
|
|
94
|
-
if (entity.language) {
|
|
95
|
-
startTag += entity.language;
|
|
96
|
-
}
|
|
97
|
-
startTag += '\n';
|
|
98
|
-
endTag = `\n${TAG_PRE}`;
|
|
99
|
-
break;
|
|
100
|
-
case 'messageEntityTextUrl':
|
|
101
|
-
startTag = '[';
|
|
102
|
-
endTag = `](${entity.url})`;
|
|
103
|
-
break;
|
|
104
|
-
case 'messageEntityMentionName':
|
|
105
|
-
startTag = '[';
|
|
106
|
-
endTag = `](tg://user?id=${entity.userId})`;
|
|
107
|
-
break;
|
|
108
|
-
case 'messageEntityCustomEmoji':
|
|
109
|
-
startTag = '[';
|
|
110
|
-
endTag = `](tg://emoji?id=${entity.documentId.toString()})`;
|
|
111
|
-
break;
|
|
112
|
-
default:
|
|
113
|
-
continue;
|
|
114
|
-
}
|
|
115
|
-
insert.push([start, startTag]);
|
|
116
|
-
insert.push([end, endTag]);
|
|
117
|
-
}
|
|
118
|
-
// sort by offset desc
|
|
119
|
-
insert.sort((a, b) => b[0] - a[0]);
|
|
120
|
-
for (const [offset, tag] of insert) {
|
|
121
|
-
text = text.substr(0, offset) + tag + text.substr(offset);
|
|
122
|
-
}
|
|
123
|
-
return text;
|
|
124
|
-
}
|
|
125
|
-
function parse(strings, ...sub) {
|
|
126
|
-
const entities = [];
|
|
127
|
-
let result = '';
|
|
128
|
-
const stacks = {};
|
|
129
|
-
let insideCode = false;
|
|
130
|
-
let insidePre = false;
|
|
131
|
-
let insideLink = false;
|
|
132
|
-
let insideLinkUrl = false;
|
|
133
|
-
let pendingLinkUrl = '';
|
|
134
|
-
function feed(text) {
|
|
135
|
-
const len = text.length;
|
|
136
|
-
let pos = 0;
|
|
137
|
-
while (pos < len) {
|
|
138
|
-
const c = text[pos];
|
|
139
|
-
if (c === '\\') {
|
|
140
|
-
if (insideLinkUrl) {
|
|
141
|
-
pendingLinkUrl += text[pos + 1];
|
|
142
|
-
}
|
|
143
|
-
else {
|
|
144
|
-
result += text[pos + 1];
|
|
145
|
-
}
|
|
146
|
-
pos += 2;
|
|
147
|
-
continue;
|
|
148
|
-
}
|
|
149
|
-
if (insideCode) {
|
|
150
|
-
if (c === '`') {
|
|
151
|
-
// we can be certain that we're inside code
|
|
152
|
-
const ent = stacks.code.pop();
|
|
153
|
-
ent.length = result.length - ent.offset;
|
|
154
|
-
entities.push(ent);
|
|
155
|
-
insideCode = false;
|
|
156
|
-
pos += 1;
|
|
157
|
-
}
|
|
158
|
-
else {
|
|
159
|
-
pos += 1;
|
|
160
|
-
result += c;
|
|
161
|
-
}
|
|
162
|
-
continue;
|
|
163
|
-
}
|
|
164
|
-
if (insidePre) {
|
|
165
|
-
if (c === '`' || (c === '\n' && text[pos + 1] === '`')) {
|
|
166
|
-
if (c === '\n')
|
|
167
|
-
pos += 1;
|
|
168
|
-
if (text[pos + 1] === '`' && text[pos + 2] === '`') {
|
|
169
|
-
// we can be certain that we're inside pre
|
|
170
|
-
const ent = stacks.pre.pop();
|
|
171
|
-
ent.length = result.length - ent.offset;
|
|
172
|
-
entities.push(ent);
|
|
173
|
-
insidePre = false;
|
|
174
|
-
pos += 3;
|
|
175
|
-
continue;
|
|
176
|
-
// closed with single or double backtick
|
|
177
|
-
// i.e. not closed actually! this is totally valid md:
|
|
178
|
-
// ```javascript
|
|
179
|
-
// const a = ``;
|
|
180
|
-
// ```
|
|
181
|
-
// compensate that `pos` change we made earliers
|
|
182
|
-
}
|
|
183
|
-
else if (c === '\n') {
|
|
184
|
-
pos -= 1;
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
pos += 1;
|
|
188
|
-
result += c;
|
|
189
|
-
continue;
|
|
190
|
-
}
|
|
191
|
-
if (insideLink && c === ']') {
|
|
192
|
-
// we can be certain that we're inside link
|
|
193
|
-
const ent = stacks.link.pop();
|
|
194
|
-
if (text[pos + 1] !== '(') {
|
|
195
|
-
// [link text]
|
|
196
|
-
// ignore this, and add opening [
|
|
197
|
-
result = `${result.substr(0, ent.offset)}[${result.substr(ent.offset)}]`;
|
|
198
|
-
pos += 1;
|
|
199
|
-
insideLink = false;
|
|
200
|
-
continue;
|
|
201
|
-
}
|
|
202
|
-
pos += 2;
|
|
203
|
-
insideLink = false;
|
|
204
|
-
insideLinkUrl = true;
|
|
205
|
-
stacks.link.push(ent);
|
|
206
|
-
continue;
|
|
207
|
-
}
|
|
208
|
-
if (insideLinkUrl) {
|
|
209
|
-
pos += 1;
|
|
210
|
-
if (c !== ')') {
|
|
211
|
-
// not ended yet
|
|
212
|
-
pendingLinkUrl += c;
|
|
213
|
-
continue;
|
|
214
|
-
}
|
|
215
|
-
const ent = stacks.link.pop();
|
|
216
|
-
let url = pendingLinkUrl;
|
|
217
|
-
pendingLinkUrl = '';
|
|
218
|
-
insideLinkUrl = false;
|
|
219
|
-
if (!url.length)
|
|
220
|
-
continue;
|
|
221
|
-
ent.length = result.length - ent.offset;
|
|
222
|
-
let m = url.match(MENTION_REGEX);
|
|
223
|
-
if (m) {
|
|
224
|
-
const userId = Number.parseInt(m[1]);
|
|
225
|
-
const accessHash = m[2];
|
|
226
|
-
if (accessHash) {
|
|
227
|
-
ent._ = 'inputMessageEntityMentionName';
|
|
228
|
-
ent.userId = {
|
|
229
|
-
_: 'inputUser',
|
|
230
|
-
userId,
|
|
231
|
-
accessHash: long_1.default.fromString(accessHash, false, 16),
|
|
232
|
-
};
|
|
233
|
-
}
|
|
234
|
-
else {
|
|
235
|
-
ent._ = 'messageEntityMentionName';
|
|
236
|
-
ent.userId = userId;
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
else if ((m = EMOJI_REGEX.exec(url))) {
|
|
240
|
-
ent._ = 'messageEntityCustomEmoji';
|
|
241
|
-
ent.documentId = long_1.default.fromString(m[1]);
|
|
242
|
-
}
|
|
243
|
-
else {
|
|
244
|
-
if (url.match(/^\/\//))
|
|
245
|
-
url = `http:${url}`;
|
|
246
|
-
ent._ = 'messageEntityTextUrl';
|
|
247
|
-
ent.url = url;
|
|
248
|
-
}
|
|
249
|
-
entities.push(ent);
|
|
250
|
-
continue;
|
|
251
|
-
}
|
|
252
|
-
if (c === '[' && !insideLink) {
|
|
253
|
-
pos += 1;
|
|
254
|
-
insideLink = true;
|
|
255
|
-
if (!('link' in stacks))
|
|
256
|
-
stacks.link = [];
|
|
257
|
-
// eslint-disable-next-line ts/no-unsafe-argument
|
|
258
|
-
stacks.link.push({
|
|
259
|
-
offset: result.length,
|
|
260
|
-
length: 0,
|
|
261
|
-
}); // other fields are added after the second part
|
|
262
|
-
continue;
|
|
263
|
-
}
|
|
264
|
-
if (c === '`') {
|
|
265
|
-
const isPre = text[pos + 1] === '`' && text[pos + 2] === '`';
|
|
266
|
-
if (isPre) {
|
|
267
|
-
pos += 3;
|
|
268
|
-
let language = '';
|
|
269
|
-
while (pos < text.length && text[pos] !== '\n') {
|
|
270
|
-
language += text[pos++];
|
|
271
|
-
}
|
|
272
|
-
// newline
|
|
273
|
-
pos += 1;
|
|
274
|
-
if (pos > text.length) {
|
|
275
|
-
throw new Error('Malformed PRE entity, expected LF after ```');
|
|
276
|
-
}
|
|
277
|
-
if (!('pre' in stacks))
|
|
278
|
-
stacks.pre = [];
|
|
279
|
-
stacks.pre.push({
|
|
280
|
-
_: 'messageEntityPre',
|
|
281
|
-
offset: result.length,
|
|
282
|
-
length: 0,
|
|
283
|
-
language,
|
|
284
|
-
});
|
|
285
|
-
insidePre = true;
|
|
286
|
-
}
|
|
287
|
-
else {
|
|
288
|
-
pos += 1;
|
|
289
|
-
if (!('code' in stacks))
|
|
290
|
-
stacks.code = [];
|
|
291
|
-
stacks.code.push({
|
|
292
|
-
_: 'messageEntityCode',
|
|
293
|
-
offset: result.length,
|
|
294
|
-
length: 0,
|
|
295
|
-
});
|
|
296
|
-
insideCode = true;
|
|
297
|
-
}
|
|
298
|
-
continue;
|
|
299
|
-
}
|
|
300
|
-
if (c === text[pos + 1]) {
|
|
301
|
-
// maybe (?) start or end of an entity
|
|
302
|
-
let type = null;
|
|
303
|
-
switch (c) {
|
|
304
|
-
case '_':
|
|
305
|
-
type = 'Italic';
|
|
306
|
-
break;
|
|
307
|
-
case '*':
|
|
308
|
-
type = 'Bold';
|
|
309
|
-
break;
|
|
310
|
-
case '-':
|
|
311
|
-
type = 'Underline';
|
|
312
|
-
break;
|
|
313
|
-
case '~':
|
|
314
|
-
type = 'Strike';
|
|
315
|
-
break;
|
|
316
|
-
case '|':
|
|
317
|
-
type = 'Spoiler';
|
|
318
|
-
break;
|
|
319
|
-
}
|
|
320
|
-
if (type) {
|
|
321
|
-
if (!(type in stacks))
|
|
322
|
-
stacks[type] = [];
|
|
323
|
-
const isBegin = stacks[type].length === 0;
|
|
324
|
-
if (isBegin) {
|
|
325
|
-
stacks[type].push({
|
|
326
|
-
_: `messageEntity${type}`,
|
|
327
|
-
offset: result.length,
|
|
328
|
-
length: 0,
|
|
329
|
-
});
|
|
330
|
-
}
|
|
331
|
-
else {
|
|
332
|
-
// valid because isBegin is false
|
|
333
|
-
const ent = stacks[type].pop();
|
|
334
|
-
ent.length = result.length - ent.offset;
|
|
335
|
-
entities.push(ent);
|
|
336
|
-
}
|
|
337
|
-
pos += 2;
|
|
338
|
-
continue;
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
if (c === '\n') {
|
|
342
|
-
if (pos !== 0) {
|
|
343
|
-
result += '\n';
|
|
344
|
-
}
|
|
345
|
-
const nonWhitespace = text.slice(pos + 1).search(/[^ \t]/);
|
|
346
|
-
if (nonWhitespace !== -1) {
|
|
347
|
-
pos += nonWhitespace + 1;
|
|
348
|
-
}
|
|
349
|
-
else {
|
|
350
|
-
pos = len;
|
|
351
|
-
result = result.trimEnd();
|
|
352
|
-
}
|
|
353
|
-
continue;
|
|
354
|
-
}
|
|
355
|
-
// nothing matched => normal character
|
|
356
|
-
result += c;
|
|
357
|
-
pos += 1;
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
if (typeof strings === 'string')
|
|
361
|
-
strings = [strings];
|
|
362
|
-
sub.forEach((it, idx) => {
|
|
363
|
-
feed(strings[idx]);
|
|
364
|
-
if (typeof it === 'boolean' || !it)
|
|
365
|
-
return;
|
|
366
|
-
if (insideLinkUrl) {
|
|
367
|
-
if (typeof it === 'string' || typeof it === 'number') {
|
|
368
|
-
pendingLinkUrl += it;
|
|
369
|
-
}
|
|
370
|
-
else if (long_1.default.isLong(it)) {
|
|
371
|
-
pendingLinkUrl += it.toString(10);
|
|
372
|
-
}
|
|
373
|
-
else {
|
|
374
|
-
// ignore the entity, only use text
|
|
375
|
-
pendingLinkUrl += it.text;
|
|
376
|
-
}
|
|
377
|
-
return;
|
|
378
|
-
}
|
|
379
|
-
if (typeof it === 'string' || typeof it === 'number') {
|
|
380
|
-
result += it;
|
|
381
|
-
}
|
|
382
|
-
else if (long_1.default.isLong(it)) {
|
|
383
|
-
result += it.toString(10);
|
|
384
|
-
}
|
|
385
|
-
else {
|
|
386
|
-
// TextWithEntities or MessageEntity
|
|
387
|
-
const text = it.text;
|
|
388
|
-
const innerEntities = 'raw' in it ? [it.raw] : it.entities;
|
|
389
|
-
const baseOffset = result.length;
|
|
390
|
-
result += text;
|
|
391
|
-
if (innerEntities) {
|
|
392
|
-
for (const ent of innerEntities) {
|
|
393
|
-
entities.push({ ...ent, offset: ent.offset + baseOffset });
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
});
|
|
398
|
-
feed(strings[strings.length - 1]);
|
|
399
|
-
for (const [name, stack] of Object.entries(stacks)) {
|
|
400
|
-
if (stack.length) {
|
|
401
|
-
throw new Error(`Unterminated ${name} entity`);
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
return {
|
|
405
|
-
text: result,
|
|
406
|
-
entities,
|
|
407
|
-
};
|
|
408
|
-
}
|
|
409
|
-
// typedoc doesn't support this yet, so we'll have to do it manually
|
|
410
|
-
// https://github.com/TypeStrong/typedoc/issues/2436
|
|
411
|
-
exports.md = Object.assign(parse, {
|
|
412
|
-
escape,
|
|
413
|
-
unparse,
|
|
414
|
-
});
|
|
415
|
-
//# sourceMappingURL=index.js.map
|
package/cjs/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;AAAA,gDAAuB;AAGvB,MAAM,aAAa,GAAG,6DAA6D,CAAA;AACnF,MAAM,WAAW,GAAG,2BAA2B,CAAA;AAE/C,MAAM,QAAQ,GAAG,IAAI,CAAA;AACrB,MAAM,UAAU,GAAG,IAAI,CAAA;AACvB,MAAM,aAAa,GAAG,IAAI,CAAA;AAC1B,MAAM,UAAU,GAAG,IAAI,CAAA;AACvB,MAAM,WAAW,GAAG,IAAI,CAAA;AACxB,MAAM,QAAQ,GAAG,GAAG,CAAA;AACpB,MAAM,OAAO,GAAG,KAAK,CAAA;AAErB,MAAM,aAAa,GAA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IAAI,GAAG,EAAE,CAAC,IAAI,CAAA;YACpB,MAAM,aAAa,GAAG,KAAK,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,QAAQ,CAAA;YAE1D,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,CAAA;YAChC,MAAM,IAAI,IAAI,CAAA;YAEd,IAAI,aAAa,EAAE,CAAC;gBAChB,KAAK,MAAM,GAAG,IAAI,aAAa,EAAE,CAAC;oBAC9B,QAAQ,CAAC,IAAI,CAAC,EAAE,GAAG,GAAG,EAAE,MAAM,EAAE,GAAG,CAAC,MAAM,GAAG,UAAU,EAAE,CAAC,CAAA;gBAC9D,CAAC;YACL,CAAC;QACL,CAAC;IACL,CAAC,CAAC,CAAA;IAEF,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAA;IAEjC,KAAK,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;QACjD,IAAI,KAAK,CAAC,MAAM,EAAE,CAAC;YACf,MAAM,IAAI,KAAK,CAAC,gBAAgB,IAAI,SAAS,CAAC,CAAA;QAClD,CAAC;IACL,CAAC;IAED,OAAO;QACH,IAAI,EAAE,MAAM;QACZ,QAAQ;KACX,CAAA;AACL,CAAC;AAED,oEAAoE;AACpE,oDAAoD;AAEvC,QAAA,EAAE,GAmCX,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE;IACrB,MAAM;IACN,OAAO;CACV,CAAC,CAAA","sourcesContent":["import Long from 'long'\nimport type { InputText, MessageEntity, TextWithEntities, tl } from '@mtcute/core'\n\nconst MENTION_REGEX = /^tg:\\/\\/user\\?id=(\\d+)(?:&hash=(-?[0-9a-fA-F]+)(?:&|$)|&|$)/\nconst EMOJI_REGEX = /^tg:\\/\\/emoji\\?id=(-?\\d+)/\n\nconst TAG_BOLD = '**'\nconst TAG_ITALIC = '__'\nconst TAG_UNDERLINE = '--'\nconst TAG_STRIKE = '~~'\nconst TAG_SPOILER = '||'\nconst TAG_CODE = '`'\nconst TAG_PRE = '```'\n\nconst TO_BE_ESCAPED = /[*_\\-~`[\\\\\\]|]/g\n\n/**\n * Escape a string to be safely used in Markdown.\n *\n * > **Note**: this function is in most cases not needed, as `md` function\n * > handles all `string`s passed to it automatically as plain text.\n */\nfunction escape(str: string): string {\n return str.replace(TO_BE_ESCAPED, s => `\\\\${s}`)\n}\n\n/**\n * Add Markdown formatting to the text given the plain text and entities contained in it.\n */\nfunction unparse(input: InputText): string {\n if (typeof input === 'string') return escape(input)\n\n let text = input.text\n const entities = input.entities ?? []\n\n // keep track of positions of inserted escape symbols\n const escaped: number[] = []\n text = text.replace(TO_BE_ESCAPED, (s, pos: number) => {\n escaped.push(pos)\n\n return `\\\\${s}`\n })\n const hasEscaped = escaped.length > 0\n\n type InsertLater = [number, string]\n const insert: InsertLater[] = []\n\n for (const entity of entities) {\n const type = entity._\n\n let start = entity.offset\n let end = start + entity.length\n\n if (start > text.length) continue\n if (start < 0) start = 0\n if (end > text.length) end = text.length\n\n if (hasEscaped) {\n // determine number of escape chars since the beginning of the string\n let escapedPos = 0\n\n while (escapedPos < escaped.length && escaped[escapedPos] < start) {\n escapedPos += 1\n }\n start += escapedPos\n\n while (escapedPos < escaped.length && escaped[escapedPos] <= end) {\n escapedPos += 1\n }\n end += escapedPos\n }\n\n let startTag\n let endTag: string\n\n switch (type) {\n case 'messageEntityBold':\n startTag = endTag = TAG_BOLD\n break\n case 'messageEntityItalic':\n startTag = endTag = TAG_ITALIC\n break\n case 'messageEntityUnderline':\n startTag = endTag = TAG_UNDERLINE\n break\n case 'messageEntityStrike':\n startTag = endTag = TAG_STRIKE\n break\n case 'messageEntitySpoiler':\n startTag = endTag = TAG_SPOILER\n break\n case 'messageEntityCode':\n startTag = endTag = TAG_CODE\n break\n case 'messageEntityPre':\n startTag = TAG_PRE\n\n if (entity.language) {\n startTag += entity.language\n }\n\n startTag += '\\n'\n endTag = `\\n${TAG_PRE}`\n break\n case 'messageEntityTextUrl':\n startTag = '['\n endTag = `](${entity.url})`\n break\n case 'messageEntityMentionName':\n startTag = '['\n endTag = `](tg://user?id=${entity.userId})`\n break\n case 'messageEntityCustomEmoji':\n startTag = '['\n endTag = `](tg://emoji?id=${entity.documentId.toString()})`\n break\n default:\n continue\n }\n\n insert.push([start, startTag])\n insert.push([end, endTag])\n }\n\n // sort by offset desc\n insert.sort((a, b) => b[0] - a[0])\n\n for (const [offset, tag] of insert) {\n text = text.substr(0, offset) + tag + text.substr(offset)\n }\n\n return text\n}\n\nfunction parse(\n strings: TemplateStringsArray | string,\n ...sub: (InputText | MessageEntity | Long | boolean | number | undefined | null)[]\n): TextWithEntities {\n const entities: tl.TypeMessageEntity[] = []\n let result = ''\n\n const stacks: Record<string, tl.Mutable<tl.TypeMessageEntity>[]> = {}\n\n let insideCode = false\n let insidePre = false\n let insideLink = false\n\n let insideLinkUrl = false\n let pendingLinkUrl = ''\n\n function feed(text: string) {\n const len = text.length\n let pos = 0\n\n while (pos < len) {\n const c = text[pos]\n\n if (c === '\\\\') {\n if (insideLinkUrl) {\n pendingLinkUrl += text[pos + 1]\n } else {\n result += text[pos + 1]\n }\n pos += 2\n continue\n }\n\n if (insideCode) {\n if (c === '`') {\n // we can be certain that we're inside code\n\n const ent = stacks.code.pop()!\n ent.length = result.length - ent.offset\n entities.push(ent)\n insideCode = false\n pos += 1\n } else {\n pos += 1\n result += c\n }\n continue\n }\n\n if (insidePre) {\n if (c === '`' || (c === '\\n' && text[pos + 1] === '`')) {\n if (c === '\\n') pos += 1\n\n if (text[pos + 1] === '`' && text[pos + 2] === '`') {\n // we can be certain that we're inside pre\n\n const ent = stacks.pre.pop()!\n ent.length = result.length - ent.offset\n entities.push(ent)\n insidePre = false\n pos += 3\n continue\n\n // closed with single or double backtick\n // i.e. not closed actually! this is totally valid md:\n // ```javascript\n // const a = ``;\n // ```\n // compensate that `pos` change we made earliers\n } else if (c === '\\n') {\n pos -= 1\n }\n }\n\n pos += 1\n result += c\n continue\n }\n\n if (insideLink && c === ']') {\n // we can be certain that we're inside link\n\n const ent = stacks.link.pop()!\n\n if (text[pos + 1] !== '(') {\n // [link text]\n // ignore this, and add opening [\n result = `${result.substr(0, ent.offset)}[${result.substr(ent.offset)}]`\n pos += 1\n insideLink = false\n continue\n }\n\n pos += 2\n insideLink = false\n insideLinkUrl = true\n stacks.link.push(ent)\n continue\n }\n\n if (insideLinkUrl) {\n pos += 1\n\n if (c !== ')') {\n // not ended yet\n pendingLinkUrl += c\n continue\n }\n\n const ent = stacks.link.pop()!\n\n let url = pendingLinkUrl\n pendingLinkUrl = ''\n insideLinkUrl = false\n\n if (!url.length) continue\n\n ent.length = result.length - ent.offset\n\n let m = url.match(MENTION_REGEX)\n\n if (m) {\n const userId = Number.parseInt(m[1])\n const accessHash = m[2]\n\n if (accessHash) {\n (ent as tl.Mutable<tl.RawInputMessageEntityMentionName>)._ = 'inputMessageEntityMentionName'\n ;(ent as tl.Mutable<tl.RawInputMessageEntityMentionName>).userId = {\n _: 'inputUser',\n userId,\n accessHash: Long.fromString(accessHash, false, 16),\n }\n } else {\n (ent as tl.Mutable<tl.RawMessageEntityMentionName>)._ = 'messageEntityMentionName'\n ;(ent as tl.Mutable<tl.RawMessageEntityMentionName>).userId = userId\n }\n } else if ((m = EMOJI_REGEX.exec(url))) {\n (ent as tl.Mutable<tl.RawMessageEntityCustomEmoji>)._ = 'messageEntityCustomEmoji'\n ;(ent as tl.Mutable<tl.RawMessageEntityCustomEmoji>).documentId = Long.fromString(m[1])\n } else {\n if (url.match(/^\\/\\//)) url = `http:${url}`\n ;(ent as tl.Mutable<tl.RawMessageEntityTextUrl>)._ = 'messageEntityTextUrl'\n ;(ent as tl.Mutable<tl.RawMessageEntityTextUrl>).url = url\n }\n entities.push(ent)\n\n continue\n }\n\n if (c === '[' && !insideLink) {\n pos += 1\n insideLink = true\n if (!('link' in stacks)) stacks.link = []\n // eslint-disable-next-line ts/no-unsafe-argument\n stacks.link.push({\n offset: result.length,\n length: 0,\n } as any) // other fields are added after the second part\n continue\n }\n\n if (c === '`') {\n const isPre = text[pos + 1] === '`' && text[pos + 2] === '`'\n\n if (isPre) {\n pos += 3\n let language = ''\n\n while (pos < text.length && text[pos] !== '\\n') {\n language += text[pos++]\n }\n\n // newline\n pos += 1\n\n if (pos > text.length) {\n throw new Error('Malformed PRE entity, expected LF after ```')\n }\n\n if (!('pre' in stacks)) stacks.pre = []\n stacks.pre.push({\n _: 'messageEntityPre',\n offset: result.length,\n length: 0,\n language,\n })\n insidePre = true\n } else {\n pos += 1\n if (!('code' in stacks)) stacks.code = []\n stacks.code.push({\n _: 'messageEntityCode',\n offset: result.length,\n length: 0,\n })\n insideCode = true\n }\n\n continue\n }\n\n if (c === text[pos + 1]) {\n // maybe (?) start or end of an entity\n let type: 'Italic' | 'Bold' | 'Underline' | 'Strike' | 'Spoiler' | null = null\n\n switch (c) {\n case '_':\n type = 'Italic'\n break\n case '*':\n type = 'Bold'\n break\n case '-':\n type = 'Underline'\n break\n case '~':\n type = 'Strike'\n break\n case '|':\n type = 'Spoiler'\n break\n }\n\n if (type) {\n if (!(type in stacks)) stacks[type] = []\n const isBegin = stacks[type].length === 0\n\n if (isBegin) {\n stacks[type].push({\n _: `messageEntity${type}`,\n offset: result.length,\n length: 0,\n })\n } else {\n // valid because isBegin is false\n\n const ent = stacks[type].pop()!\n ent.length = result.length - ent.offset\n entities.push(ent)\n }\n\n pos += 2\n continue\n }\n }\n\n if (c === '\\n') {\n if (pos !== 0) {\n result += '\\n'\n }\n\n const nonWhitespace = text.slice(pos + 1).search(/[^ \\t]/)\n\n if (nonWhitespace !== -1) {\n pos += nonWhitespace + 1\n } else {\n pos = len\n result = result.trimEnd()\n }\n continue\n }\n\n // nothing matched => normal character\n result += c\n pos += 1\n }\n }\n\n if (typeof strings === 'string') strings = [strings] as unknown as TemplateStringsArray\n\n sub.forEach((it, idx) => {\n feed(strings[idx])\n\n if (typeof it === 'boolean' || !it) return\n\n if (insideLinkUrl) {\n if (typeof it === 'string' || typeof it === 'number') {\n pendingLinkUrl += it\n } else if (Long.isLong(it)) {\n pendingLinkUrl += it.toString(10)\n } else {\n // ignore the entity, only use text\n pendingLinkUrl += it.text\n }\n\n return\n }\n\n if (typeof it === 'string' || typeof it === 'number') {\n result += it\n } else if (Long.isLong(it)) {\n result += it.toString(10)\n } else {\n // TextWithEntities or MessageEntity\n const text = it.text\n const innerEntities = 'raw' in it ? [it.raw] : it.entities\n\n const baseOffset = result.length\n result += text\n\n if (innerEntities) {\n for (const ent of innerEntities) {\n entities.push({ ...ent, offset: ent.offset + baseOffset })\n }\n }\n }\n })\n\n feed(strings[strings.length - 1])\n\n for (const [name, stack] of Object.entries(stacks)) {\n if (stack.length) {\n throw new Error(`Unterminated ${name} entity`)\n }\n }\n\n return {\n text: result,\n entities,\n }\n}\n\n// typedoc doesn't support this yet, so we'll have to do it manually\n// https://github.com/TypeStrong/typedoc/issues/2436\n\nexport const md: {\n /**\n * Tagged template based Markdown-to-entities parser function\n *\n * Additionally, `md` function has two static methods:\n * - `md.escape` - escape a string to be safely used in Markdown\n * (should not be needed in most cases, as `md` function itself handles all `string`s\n * passed to it automatically as plain text)\n * - `md.unparse` - add Markdown formatting to the text given the plain text and entities contained in it\n *\n * @example\n * ```typescript\n * const text = md`**${user.displayName}**`\n * ```\n */\n (\n strings: TemplateStringsArray,\n ...sub: (InputText | MessageEntity | Long | boolean | number | undefined | null)[]\n ): TextWithEntities\n /**\n * A variant taking a plain JS string as input\n * and parsing it.\n *\n * Useful for cases when you already have a string\n * (e.g. from some server) and want to parse it.\n *\n * @example\n * ```typescript\n * const string = '**hello**'\n * const text = md(string)\n * ```\n */\n (string: string): TextWithEntities\n escape: typeof escape\n unparse: typeof unparse\n} = Object.assign(parse, {\n escape,\n unparse,\n})\n"]}
|
package/cjs/package.json
DELETED
package/esm/index.d.ts
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import Long from 'long';
|
|
2
|
-
import type { InputText, MessageEntity, TextWithEntities } from '@mtcute/core';
|
|
3
|
-
/**
|
|
4
|
-
* Escape a string to be safely used in Markdown.
|
|
5
|
-
*
|
|
6
|
-
* > **Note**: this function is in most cases not needed, as `md` function
|
|
7
|
-
* > handles all `string`s passed to it automatically as plain text.
|
|
8
|
-
*/
|
|
9
|
-
declare function escape(str: string): string;
|
|
10
|
-
/**
|
|
11
|
-
* Add Markdown formatting to the text given the plain text and entities contained in it.
|
|
12
|
-
*/
|
|
13
|
-
declare function unparse(input: InputText): string;
|
|
14
|
-
export declare const md: {
|
|
15
|
-
/**
|
|
16
|
-
* Tagged template based Markdown-to-entities parser function
|
|
17
|
-
*
|
|
18
|
-
* Additionally, `md` function has two static methods:
|
|
19
|
-
* - `md.escape` - escape a string to be safely used in Markdown
|
|
20
|
-
* (should not be needed in most cases, as `md` function itself handles all `string`s
|
|
21
|
-
* passed to it automatically as plain text)
|
|
22
|
-
* - `md.unparse` - add Markdown formatting to the text given the plain text and entities contained in it
|
|
23
|
-
*
|
|
24
|
-
* @example
|
|
25
|
-
* ```typescript
|
|
26
|
-
* const text = md`**${user.displayName}**`
|
|
27
|
-
* ```
|
|
28
|
-
*/
|
|
29
|
-
(strings: TemplateStringsArray, ...sub: (InputText | MessageEntity | Long | boolean | number | undefined | null)[]): TextWithEntities;
|
|
30
|
-
/**
|
|
31
|
-
* A variant taking a plain JS string as input
|
|
32
|
-
* and parsing it.
|
|
33
|
-
*
|
|
34
|
-
* Useful for cases when you already have a string
|
|
35
|
-
* (e.g. from some server) and want to parse it.
|
|
36
|
-
*
|
|
37
|
-
* @example
|
|
38
|
-
* ```typescript
|
|
39
|
-
* const string = '**hello**'
|
|
40
|
-
* const text = md(string)
|
|
41
|
-
* ```
|
|
42
|
-
*/
|
|
43
|
-
(string: string): TextWithEntities;
|
|
44
|
-
escape: typeof escape;
|
|
45
|
-
unparse: typeof unparse;
|
|
46
|
-
};
|
|
47
|
-
export {};
|