@iletai/nzb 1.4.2 → 1.4.3
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/dist/telegram/formatter.js +20 -8
- package/package.json +1 -1
|
@@ -7,7 +7,7 @@ export function escapeHtml(text) {
|
|
|
7
7
|
}
|
|
8
8
|
/**
|
|
9
9
|
* Split a long message into chunks that fit within Telegram's message limit.
|
|
10
|
-
* HTML-aware: if a split falls inside a <pre> block, close and reopen it.
|
|
10
|
+
* HTML-aware: if a split falls inside a <pre> or <blockquote> block, close and reopen it.
|
|
11
11
|
*/
|
|
12
12
|
export function chunkMessage(text) {
|
|
13
13
|
if (text.length <= TELEGRAM_MAX_LENGTH) {
|
|
@@ -28,14 +28,19 @@ export function chunkMessage(text) {
|
|
|
28
28
|
splitAt = CHUNK_TARGET;
|
|
29
29
|
}
|
|
30
30
|
const segment = remaining.slice(0, splitAt);
|
|
31
|
-
//
|
|
32
|
-
const
|
|
33
|
-
const
|
|
34
|
-
const
|
|
35
|
-
|
|
31
|
+
// Check for unclosed block-level tags
|
|
32
|
+
const preOpens = (segment.match(/<pre/g) || []).length;
|
|
33
|
+
const preCloses = (segment.match(/<\/pre>/g) || []).length;
|
|
34
|
+
const bqOpens = (segment.match(/<blockquote/g) || []).length;
|
|
35
|
+
const bqCloses = (segment.match(/<\/blockquote>/g) || []).length;
|
|
36
|
+
if (preOpens > preCloses) {
|
|
36
37
|
chunks.push(segment + "\n</pre>");
|
|
37
38
|
remaining = "<pre>" + remaining.slice(splitAt).trimStart();
|
|
38
39
|
}
|
|
40
|
+
else if (bqOpens > bqCloses) {
|
|
41
|
+
chunks.push(segment + "</blockquote>");
|
|
42
|
+
remaining = "<blockquote>" + remaining.slice(splitAt).trimStart();
|
|
43
|
+
}
|
|
39
44
|
else {
|
|
40
45
|
chunks.push(segment);
|
|
41
46
|
remaining = remaining.slice(splitAt).trimStart();
|
|
@@ -98,10 +103,10 @@ export function toTelegramHTML(text) {
|
|
|
98
103
|
out = out.replace(/^#{1,6}\s+(.+)$/gm, (_m, title) => `**${title.trim()}**`);
|
|
99
104
|
// 6. Remove horizontal rules
|
|
100
105
|
out = out.replace(/^[-*_]{3,}\s*$/gm, "");
|
|
101
|
-
// 7.
|
|
106
|
+
// 7. Strip blockquote markers but keep content for inline formatting (processed later at step 14b)
|
|
102
107
|
out = out.replace(/(?:^>\s?(.*)$\n?)+/gm, (block) => {
|
|
103
108
|
const content = block.replace(/^>\s?/gm, "").trim();
|
|
104
|
-
return
|
|
109
|
+
return `\x00BQ_START\x00${content}\x00BQ_END\x00\n`;
|
|
105
110
|
});
|
|
106
111
|
// 8. Unordered lists: - item or * item → • item
|
|
107
112
|
out = out.replace(/^(\s*)[-*]\s+/gm, "$1• ");
|
|
@@ -124,6 +129,13 @@ export function toTelegramHTML(text) {
|
|
|
124
129
|
});
|
|
125
130
|
// 14. Underline __text__ → <u>
|
|
126
131
|
out = out.replace(/__(.+?)__/g, (_m, inner) => stashToken(`<u>${escapeHtml(inner)}</u>`));
|
|
132
|
+
// 14b. Wrap blockquote markers → <blockquote> (inner content already formatted by steps 10-14)
|
|
133
|
+
out = out.replace(/\x00BQ_START\x00([\s\S]*?)\x00BQ_END\x00/g, (_m, content) => {
|
|
134
|
+
// Escape plain text while preserving stash tokens (same KEEP pattern as bold/italic)
|
|
135
|
+
const escaped = escapeHtml(content.replace(/\x00S\d+\x00/g, (tok) => `\x00KEEP${tok}\x00KEEP`));
|
|
136
|
+
const restored = escaped.replace(/\x00KEEP\x00S(\d+)\x00\x00KEEP/g, (_m2, i) => stash[+i]);
|
|
137
|
+
return stashToken(`<blockquote>${restored}</blockquote>`);
|
|
138
|
+
});
|
|
127
139
|
// 15. Escape remaining plain text
|
|
128
140
|
out = escapeHtml(out);
|
|
129
141
|
// 16. Restore stashed tokens
|