telegram-transformer 1.2.0 → 1.4.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/LICENSE +21 -21
- package/README.md +166 -129
- package/lib/constants.d.ts +2 -2
- package/lib/constants.d.ts.map +1 -1
- package/lib/constants.js +8 -8
- package/lib/format.d.ts +6 -6
- package/lib/format.d.ts.map +1 -1
- package/lib/format.js +90 -64
- package/lib/formatters.d.ts +10 -4
- package/lib/formatters.d.ts.map +1 -1
- package/lib/formatters.js +186 -137
- package/lib/index.d.ts +6 -5
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +22 -21
- package/lib/renderNode.d.ts +41 -0
- package/lib/renderNode.d.ts.map +1 -0
- package/lib/renderNode.js +74 -0
- package/lib/sortEntities.d.ts +3 -0
- package/lib/sortEntities.d.ts.map +1 -0
- package/lib/sortEntities.js +9 -0
- package/lib/tsc.tsconfig.tsbuildinfo +1 -0
- package/lib/types.d.ts +21 -11
- package/lib/types.d.ts.map +1 -1
- package/lib/types.js +2 -2
- package/package.json +50 -52
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
The MIT License (MIT)
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2022 Ivan Odyntsov
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Ivan Odyntsov
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,129 +1,166 @@
|
|
|
1
|
-
<header>
|
|
2
|
-
|
|
3
|
-
<h1 align="center">Telegram message transformer</h1>
|
|
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
|
-
{ offset:
|
|
40
|
-
{ offset:
|
|
41
|
-
{ offset:
|
|
42
|
-
{ offset:
|
|
43
|
-
{ offset:
|
|
44
|
-
{ offset:
|
|
45
|
-
{ offset:
|
|
46
|
-
{ offset:
|
|
47
|
-
{ offset:
|
|
48
|
-
{ offset:
|
|
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
|
-
|
|
1
|
+
<header>
|
|
2
|
+
<div align="center">
|
|
3
|
+
<h1 align="center">Telegram message transformer</h1>
|
|
4
|
+
|
|
5
|
+
<a href="https://www.npmjs.com/package/telegram-transformer">
|
|
6
|
+
<img src="https://img.shields.io/npm/v/telegram-transformer.svg" alt="Latest Release">
|
|
7
|
+
</a>
|
|
8
|
+
|
|
9
|
+
</div>
|
|
10
|
+
</header>
|
|
11
|
+
|
|
12
|
+
## Introduction
|
|
13
|
+
|
|
14
|
+
Telegram transformer is a library that makes it simple for you to transform telegram message to any markup.
|
|
15
|
+
|
|
16
|
+
## Getting started
|
|
17
|
+
|
|
18
|
+
### Installation
|
|
19
|
+
|
|
20
|
+
```shellscript
|
|
21
|
+
$ npm install telegram-transformer
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
or
|
|
25
|
+
|
|
26
|
+
```shellscript
|
|
27
|
+
$ yarn add telegram-transformer
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### Example
|
|
31
|
+
|
|
32
|
+
```js
|
|
33
|
+
import formatText, { htmlFormatters } from 'telegram-transformer';
|
|
34
|
+
import { MessageEntity } from 'typegram';
|
|
35
|
+
|
|
36
|
+
const text =
|
|
37
|
+
'Hello @username #awesome_hashtag $USD /start@jobs_bot https://telegram.org do-not-reply@telegram.org +1-212-555-0123 bold text italic text underlined text strikethrough text spoiler message monowidth string monowidth block text_link custom_emoji text_mention';
|
|
38
|
+
const entities: MessageEntity[] = [
|
|
39
|
+
{ offset: 6, length: 9, type: 'mention' },
|
|
40
|
+
{ offset: 16, length: 16, type: 'hashtag' },
|
|
41
|
+
{ offset: 25, length: 7, type: 'bold' },
|
|
42
|
+
{ offset: 33, length: 4, type: 'cashtag' },
|
|
43
|
+
{ offset: 38, length: 15, type: 'bot_command' },
|
|
44
|
+
{ offset: 54, length: 20, type: 'url' },
|
|
45
|
+
{ offset: 75, length: 25, type: 'email' },
|
|
46
|
+
{ offset: 101, length: 15, type: 'phone_number' },
|
|
47
|
+
{ offset: 117, length: 9, type: 'bold' },
|
|
48
|
+
{ offset: 127, length: 11, type: 'italic' },
|
|
49
|
+
{ offset: 139, length: 5, type: 'underline' },
|
|
50
|
+
{ offset: 144, length: 10, type: 'underline' },
|
|
51
|
+
{ offset: 144, length: 5, type: 'bold' },
|
|
52
|
+
{ offset: 155, length: 18, type: 'strikethrough' },
|
|
53
|
+
{ offset: 174, length: 15, type: 'spoiler' },
|
|
54
|
+
{
|
|
55
|
+
offset: 223,
|
|
56
|
+
length: 9,
|
|
57
|
+
type: 'text_link',
|
|
58
|
+
url: 'https://telegram.org/',
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
offset: 233,
|
|
62
|
+
length: 12,
|
|
63
|
+
type: 'custom_emoji',
|
|
64
|
+
custom_emoji_id: '12345',
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
offset: 246,
|
|
68
|
+
length: 12,
|
|
69
|
+
type: 'text_mention',
|
|
70
|
+
user: {
|
|
71
|
+
id: 9876543210,
|
|
72
|
+
is_bot: false,
|
|
73
|
+
first_name: 'First Name',
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
];
|
|
77
|
+
|
|
78
|
+
const formatted = formatText(text, entities, { formatters: htmlFormatters });
|
|
79
|
+
|
|
80
|
+
console.log(formatted);
|
|
81
|
+
|
|
82
|
+
/*
|
|
83
|
+
{
|
|
84
|
+
text: "Hello <a href='https://t.me/username'>@username</a> <a href='#awesome_hashtag'>#awesome_<strong>hashtag</strong></a> $USD /start@jobs_bot <a href='https://telegram.org'>https://telegram.org</a> <a href='mailto:do-not-reply@telegram.org'>do-not-reply@telegram.org</a> <a href='tel:+1-212-555-0123'>+1-212-555-0123</a> <strong>bold text</strong> <i>italic text</i> <u>under</u><u><strong>lined</strong> text</u> <strike>strikethrough text</strike> <span class='spoiler-hover'>spoiler message</span> monowidth string monowidth block <a href='https://telegram.org/'>text_link</a> <span>custom_emoji | <b>EMOJI ID:12345</b></span> <span>text_mention: First Name | <b>ID:9876543210</b></span>",
|
|
85
|
+
entities: [
|
|
86
|
+
{ offset: 6, length: 45, type: 'mention' },
|
|
87
|
+
{ offset: 52, length: 64, type: 'hashtag' },
|
|
88
|
+
{ offset: 88, length: 24, type: 'bold' },
|
|
89
|
+
{ offset: 117, length: 4, type: 'cashtag' },
|
|
90
|
+
{ offset: 122, length: 15, type: 'bot_command' },
|
|
91
|
+
{ offset: 138, length: 55, type: 'url' },
|
|
92
|
+
{ offset: 194, length: 72, type: 'email' },
|
|
93
|
+
{ offset: 267, length: 49, type: 'phone_number' },
|
|
94
|
+
{ offset: 317, length: 26, type: 'bold' },
|
|
95
|
+
{ offset: 344, length: 18, type: 'italic' },
|
|
96
|
+
{ offset: 363, length: 12, type: 'underline' },
|
|
97
|
+
{ offset: 375, length: 34, type: 'underline' },
|
|
98
|
+
{ offset: 378, length: 22, type: 'bold' },
|
|
99
|
+
{ offset: 410, length: 35, type: 'strikethrough' },
|
|
100
|
+
{ offset: 446, length: 50, type: 'spoiler' },
|
|
101
|
+
{
|
|
102
|
+
offset: 530,
|
|
103
|
+
length: 45,
|
|
104
|
+
type: 'text_link',
|
|
105
|
+
url: 'https://telegram.org/',
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
offset: 576,
|
|
109
|
+
length: 49,
|
|
110
|
+
type: 'custom_emoji',
|
|
111
|
+
custom_emoji_id: '12345',
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
offset: 626,
|
|
115
|
+
length: 60,
|
|
116
|
+
type: 'text_mention',
|
|
117
|
+
user: {
|
|
118
|
+
id: 9876543210,
|
|
119
|
+
is_bot: false,
|
|
120
|
+
first_name: 'First Name',
|
|
121
|
+
},
|
|
122
|
+
},
|
|
123
|
+
]
|
|
124
|
+
}
|
|
125
|
+
*/
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### Custom transformer
|
|
129
|
+
|
|
130
|
+
```js
|
|
131
|
+
import formatText, { htmlFormatters } from 'telegram-transformer';
|
|
132
|
+
import { MessageEntity } from 'typegram';
|
|
133
|
+
|
|
134
|
+
const text = 'Hello @username #awesome_hashtag';
|
|
135
|
+
const entities: MessageEntity[] = [
|
|
136
|
+
{ offset: 6, length: 9, type: 'mention' },
|
|
137
|
+
{ offset: 16, length: 16, type: 'hashtag' },
|
|
138
|
+
];
|
|
139
|
+
|
|
140
|
+
const formatters = {
|
|
141
|
+
mention: (entityText: string, entity: MessageEntity, text: string) => ({
|
|
142
|
+
before: '<awesome-mention>',
|
|
143
|
+
after: '</awesome-mention>',
|
|
144
|
+
text: entityText,
|
|
145
|
+
}),
|
|
146
|
+
hashtag: (entityText: string, entity: MessageEntity, text: string) => ({
|
|
147
|
+
before: '<span class="hashtag">',
|
|
148
|
+
after: '</span>',
|
|
149
|
+
text: entityText,
|
|
150
|
+
}),
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
const formatted = formatText(text, entities, { formatters });
|
|
154
|
+
|
|
155
|
+
console.log(formatted);
|
|
156
|
+
|
|
157
|
+
/*
|
|
158
|
+
{
|
|
159
|
+
text: 'Hello <awesome-mention>@username</awesome-mention> <span class="hashtag">#awesome_hashtag</span>',
|
|
160
|
+
entities: [
|
|
161
|
+
{ offset: 6, length: 44, type: 'mention' },
|
|
162
|
+
{ offset: 51, length: 45, type: 'hashtag' }
|
|
163
|
+
]
|
|
164
|
+
}
|
|
165
|
+
*/
|
|
166
|
+
```
|
package/lib/constants.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { Options } from './types';
|
|
2
|
-
export declare const DEFAULT_OPTIONS: Required<Options>;
|
|
1
|
+
import { Options } from './types';
|
|
2
|
+
export declare const DEFAULT_OPTIONS: Required<Options>;
|
|
3
3
|
//# sourceMappingURL=constants.d.ts.map
|
package/lib/constants.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAElC,eAAO,MAAM,eAAe,EAAE,QAAQ,CAAC,OAAO,
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAElC,eAAO,MAAM,eAAe,EAAE,QAAQ,CAAC,OAAO,CAGpC,CAAC"}
|
package/lib/constants.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DEFAULT_OPTIONS = void 0;
|
|
4
|
-
const formatters_1 = require("./formatters");
|
|
5
|
-
exports.DEFAULT_OPTIONS = {
|
|
6
|
-
formatters: formatters_1.formatters,
|
|
7
|
-
startFrom: 0,
|
|
8
|
-
};
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DEFAULT_OPTIONS = void 0;
|
|
4
|
+
const formatters_1 = require("./formatters");
|
|
5
|
+
exports.DEFAULT_OPTIONS = {
|
|
6
|
+
formatters: formatters_1.formatters,
|
|
7
|
+
startFrom: 0,
|
|
8
|
+
};
|
package/lib/format.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { MessageEntity } from 'typegram';
|
|
2
|
-
import { Options } from './types';
|
|
3
|
-
export declare const format: (text: string, entities?: MessageEntity[], options?: Options) => {
|
|
4
|
-
text: string;
|
|
5
|
-
entities: MessageEntity[];
|
|
6
|
-
};
|
|
1
|
+
import type { MessageEntity } from 'typegram';
|
|
2
|
+
import { Options } from './types';
|
|
3
|
+
export declare const format: (text: string, entities?: MessageEntity[], options?: Options) => {
|
|
4
|
+
text: string;
|
|
5
|
+
entities: MessageEntity[];
|
|
6
|
+
};
|
|
7
7
|
//# sourceMappingURL=format.d.ts.map
|
package/lib/format.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"format.d.ts","sourceRoot":"","sources":["../src/format.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"format.d.ts","sourceRoot":"","sources":["../src/format.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAE9C,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAQlC,eAAO,MAAM,MAAM,SACX,MAAM,aACF,aAAa,EAAE,YAChB,OAAO;;;CAsGjB,CAAC"}
|
package/lib/format.js
CHANGED
|
@@ -1,64 +1,90 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.format = void 0;
|
|
4
|
-
const constants_1 = require("./constants");
|
|
5
|
-
const
|
|
6
|
-
const format = (text, entities = [], options = constants_1.DEFAULT_OPTIONS) => {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
const
|
|
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
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.format = void 0;
|
|
4
|
+
const constants_1 = require("./constants");
|
|
5
|
+
const renderNode_1 = require("./renderNode");
|
|
6
|
+
const format = (text, entities = [], options = constants_1.DEFAULT_OPTIONS) => {
|
|
7
|
+
var _a, _b;
|
|
8
|
+
if (!(entities === null || entities === void 0 ? void 0 : entities.length)) {
|
|
9
|
+
return { text, entities };
|
|
10
|
+
}
|
|
11
|
+
const formatters = {
|
|
12
|
+
...constants_1.DEFAULT_OPTIONS.formatters,
|
|
13
|
+
...options.formatters,
|
|
14
|
+
};
|
|
15
|
+
const startFrom = (_b = (_a = options.startFrom) !== null && _a !== void 0 ? _a : constants_1.DEFAULT_OPTIONS.startFrom) !== null && _b !== void 0 ? _b : 0;
|
|
16
|
+
const prefix = text.substring(0, startFrom);
|
|
17
|
+
const partialText = text.substring(startFrom);
|
|
18
|
+
const unchangedEntities = [];
|
|
19
|
+
const roots = [];
|
|
20
|
+
const stack = [];
|
|
21
|
+
for (const entity of entities) {
|
|
22
|
+
if (entity.offset < startFrom) {
|
|
23
|
+
unchangedEntities.push(entity);
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
const node = {
|
|
27
|
+
entity: {
|
|
28
|
+
...entity,
|
|
29
|
+
offset: entity.offset - startFrom,
|
|
30
|
+
},
|
|
31
|
+
children: [],
|
|
32
|
+
};
|
|
33
|
+
while (stack.length > 0) {
|
|
34
|
+
const top = stack[stack.length - 1];
|
|
35
|
+
if (!top) {
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
if (top.entity.offset + top.entity.length > node.entity.offset) {
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
stack.pop();
|
|
42
|
+
}
|
|
43
|
+
if (stack.length > 0) {
|
|
44
|
+
const stackitem = stack[stack.length - 1];
|
|
45
|
+
if (stackitem) {
|
|
46
|
+
stackitem.children.push(node);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
roots.push(node);
|
|
51
|
+
}
|
|
52
|
+
stack.push(node);
|
|
53
|
+
}
|
|
54
|
+
const parts = [prefix];
|
|
55
|
+
let currentPosition = prefix.length;
|
|
56
|
+
const updatedEntities = [];
|
|
57
|
+
let sourcePosition = 0;
|
|
58
|
+
if (roots.length > 0) {
|
|
59
|
+
roots.sort((a, b) => a.entity.offset - b.entity.offset);
|
|
60
|
+
for (const root of roots) {
|
|
61
|
+
const plain = partialText.substring(sourcePosition, root.entity.offset);
|
|
62
|
+
parts.push(plain);
|
|
63
|
+
currentPosition += plain.length;
|
|
64
|
+
const nodeResult = (0, renderNode_1.renderNode)({
|
|
65
|
+
node: root,
|
|
66
|
+
parts,
|
|
67
|
+
formatters,
|
|
68
|
+
partialText,
|
|
69
|
+
currentPosition,
|
|
70
|
+
text,
|
|
71
|
+
entries: updatedEntities,
|
|
72
|
+
});
|
|
73
|
+
currentPosition = nodeResult.currentPosition;
|
|
74
|
+
sourcePosition = root.entity.offset + root.entity.length;
|
|
75
|
+
}
|
|
76
|
+
const lastPlain = partialText.substring(sourcePosition);
|
|
77
|
+
parts.push(lastPlain);
|
|
78
|
+
currentPosition += lastPlain.length;
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
parts.push(partialText);
|
|
82
|
+
}
|
|
83
|
+
const newEntities = [...unchangedEntities, ...updatedEntities];
|
|
84
|
+
newEntities.sort((a, b) => a.offset - b.offset);
|
|
85
|
+
return {
|
|
86
|
+
text: parts.join(''),
|
|
87
|
+
entities: newEntities,
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
exports.format = format;
|
package/lib/formatters.d.ts
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
export declare const
|
|
4
|
-
export declare const
|
|
1
|
+
import type { MessageEntity } from 'typegram';
|
|
2
|
+
import { FormatterFn, FormattersDict } from './types';
|
|
3
|
+
export declare const abstractDefaultFormatter: FormatterFn<MessageEntity.AbstractMessageEntity>;
|
|
4
|
+
export declare const defaultFormatter: FormatterFn<MessageEntity.CommonMessageEntity>;
|
|
5
|
+
export declare const defaultTextMentaionFormatter: FormatterFn<MessageEntity.TextMentionMessageEntity>;
|
|
6
|
+
export declare const defaultTextLinkFormatter: FormatterFn<MessageEntity.TextLinkMessageEntity>;
|
|
7
|
+
export declare const defaultPreFormatter: FormatterFn<MessageEntity.PreMessageEntity>;
|
|
8
|
+
export declare const defaultCustomEmojiFormatter: FormatterFn<MessageEntity.CustomEmojiMessageEntity>;
|
|
9
|
+
export declare const formatters: FormattersDict;
|
|
10
|
+
export declare const htmlFormatters: FormattersDict;
|
|
5
11
|
//# sourceMappingURL=formatters.d.ts.map
|
package/lib/formatters.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"formatters.d.ts","sourceRoot":"","sources":["../src/formatters.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAEtD,eAAO,MAAM,gBAAgB,EAAE,
|
|
1
|
+
{"version":3,"file":"formatters.d.ts","sourceRoot":"","sources":["../src/formatters.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAC9C,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAEtD,eAAO,MAAM,wBAAwB,EAAE,WAAW,CAChD,aAAa,CAAC,qBAAqB,CAUpC,CAAC;AAEF,eAAO,MAAM,gBAAgB,EAAE,WAAW,CACxC,aAAa,CAAC,mBAAmB,CAGlC,CAAC;AAEF,eAAO,MAAM,4BAA4B,EAAE,WAAW,CACpD,aAAa,CAAC,wBAAwB,CASvC,CAAC;AAEF,eAAO,MAAM,wBAAwB,EAAE,WAAW,CAChD,aAAa,CAAC,qBAAqB,CAGpC,CAAC;AAEF,eAAO,MAAM,mBAAmB,EAAE,WAAW,CAC3C,aAAa,CAAC,gBAAgB,CAG/B,CAAC;AAEF,eAAO,MAAM,2BAA2B,EAAE,WAAW,CACnD,aAAa,CAAC,wBAAwB,CAGvC,CAAC;AAEF,eAAO,MAAM,UAAU,EAAE,cAkBxB,CAAC;AAEF,eAAO,MAAM,cAAc,EAAE,cAmJ5B,CAAC"}
|