@ghostlytools/cli 1.6.1 → 1.7.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/README.md +21 -4
- package/dist/assets/{api-BpNU2LQh.js → api-BIqr8SpM.js} +9 -9
- package/dist/assets/{api-BpNU2LQh.js.map → api-BIqr8SpM.js.map} +1 -1
- package/dist/assets/{api-Cx8ZCntM.js → api-BnCmJLiQ.js} +2 -2
- package/dist/assets/{apiKit-d1wi3raD.js → apiKit-CWalYh1t.js} +2 -2
- package/dist/assets/{apiKit-d1wi3raD.js.map → apiKit-CWalYh1t.js.map} +1 -1
- package/dist/assets/{apps-B1QjRWqq.js → apps-Bx3tXbJ9.js} +2 -2
- package/dist/assets/{apps-B1QjRWqq.js.map → apps-Bx3tXbJ9.js.map} +1 -1
- package/dist/assets/{apps-C-HiIRCA.js → apps-_hVUaHIT.js} +1 -1
- package/dist/assets/{arkade-B5g4r3Ea.js → arkade-AxrcyS1C.js} +2 -2
- package/dist/assets/{arkade-B5g4r3Ea.js.map → arkade-AxrcyS1C.js.map} +1 -1
- package/dist/assets/{backup-Boqn5Y35.js → backup-Y1V1Yxqr.js} +4 -4
- package/dist/assets/{backup-Boqn5Y35.js.map → backup-Y1V1Yxqr.js.map} +1 -1
- package/dist/assets/{backup-BS9HwGyn.js → backup-d_XvXpEK.js} +1 -1
- package/dist/assets/{buttons-Bjc51aod.js → buttons-BiQyQBb0.js} +3 -3
- package/dist/assets/{buttons-Bjc51aod.js.map → buttons-BiQyQBb0.js.map} +1 -1
- package/dist/assets/buttons-LO3Ql1Hd.js +2 -0
- package/dist/assets/{envelope-DvZMFRzg.js → envelope-D3oeVhv8.js} +2 -2
- package/dist/assets/{envelope-DvZMFRzg.js.map → envelope-D3oeVhv8.js.map} +1 -1
- package/dist/assets/{groupFiles-y4lGrOC-.js → groupFiles-DU1wMGI9.js} +3 -3
- package/dist/assets/{groupFiles-y4lGrOC-.js.map → groupFiles-DU1wMGI9.js.map} +1 -1
- package/dist/assets/{host-DFGS8vJE.js → host-DDMQfR7B.js} +8 -8
- package/dist/assets/{host-DFGS8vJE.js.map → host-DDMQfR7B.js.map} +1 -1
- package/dist/assets/{hyperdhtRelay-CZLnOc4e.js → hyperdhtRelay-B2v8eqRd.js} +2 -2
- package/dist/assets/{hyperdhtRelay-CZLnOc4e.js.map → hyperdhtRelay-B2v8eqRd.js.map} +1 -1
- package/dist/assets/{irohWeb-Y9wt8zG7.js → irohWeb-9RVIbF_l.js} +2 -2
- package/dist/assets/{irohWeb-Y9wt8zG7.js.map → irohWeb-9RVIbF_l.js.map} +1 -1
- package/dist/assets/{main-xgWC0qPZ.js → main-BNwNEPEy.js} +70 -25
- package/dist/assets/main-BNwNEPEy.js.map +1 -0
- package/dist/assets/{mainline-CjQ3pmvq.js → mainline-RXv5_Fby.js} +2 -2
- package/dist/assets/{mainline-CjQ3pmvq.js.map → mainline-RXv5_Fby.js.map} +1 -1
- package/dist/assets/{networkReason-i9wu5ZJs.js → networkReason-CIn4yN9h.js} +2 -2
- package/dist/assets/{networkReason-i9wu5ZJs.js.map → networkReason-CIn4yN9h.js.map} +1 -1
- package/dist/assets/{nwc-yO0nHt9S.js → nwc-BNPzuT-6.js} +3 -3
- package/dist/assets/{nwc-yO0nHt9S.js.map → nwc-BNPzuT-6.js.map} +1 -1
- package/dist/assets/{nwcProvider-BEg4I5Bk.js → nwcProvider-C9Y6PUNC.js} +2 -2
- package/dist/assets/{nwcProvider-BEg4I5Bk.js.map → nwcProvider-C9Y6PUNC.js.map} +1 -1
- package/dist/assets/{registry-DyGu5C-Z.js → registry-B9aYHNNf.js} +3 -3
- package/dist/assets/registry-B9aYHNNf.js.map +1 -0
- package/dist/assets/{registry-CBDcdRPx.js → registry-DOkDhUAu.js} +1 -1
- package/dist/assets/{resolve-CnkY--2E.js → resolve-imkCgogd.js} +2 -2
- package/dist/assets/{resolve-CnkY--2E.js.map → resolve-imkCgogd.js.map} +1 -1
- package/dist/assets/{server-CH142eWc.js → server-CB9_JuWZ.js} +12 -12
- package/dist/assets/{server-CH142eWc.js.map → server-CB9_JuWZ.js.map} +1 -1
- package/dist/assets/{src-BXeGLwWV.js → src-BBG_NwMc.js} +28 -1
- package/dist/assets/src-BBG_NwMc.js.map +1 -0
- package/dist/assets/translate-BcP139SV.js +1067 -0
- package/dist/assets/translate-BcP139SV.js.map +1 -0
- package/dist/assets/{usdt-CdSNFUzO.js → usdt-C5Gf9-IC.js} +3 -3
- package/dist/assets/{usdt-CdSNFUzO.js.map → usdt-C5Gf9-IC.js.map} +1 -1
- package/dist/assets/{words-DA5feEav.js → words-ZUiQo4yo.js} +2 -2
- package/dist/assets/{words-DA5feEav.js.map → words-ZUiQo4yo.js.map} +1 -1
- package/dist/ghostly.mjs +1 -1
- package/package.json +1 -1
- package/dist/assets/buttons-CugVarw3.js +0 -2
- package/dist/assets/main-xgWC0qPZ.js.map +0 -1
- package/dist/assets/registry-DyGu5C-Z.js.map +0 -1
- package/dist/assets/src-BXeGLwWV.js.map +0 -1
|
@@ -0,0 +1,1067 @@
|
|
|
1
|
+
import "./src-BBG_NwMc.js";
|
|
2
|
+
import { t as CliError } from "./errors-CzXgR8Cc.js";
|
|
3
|
+
import { existsSync } from "node:fs";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { spawn } from "node:child_process";
|
|
6
|
+
import { createInterface } from "node:readline";
|
|
7
|
+
var package_default = {
|
|
8
|
+
name: "ghostly-translation-native",
|
|
9
|
+
version: "0.1.0",
|
|
10
|
+
"private": true,
|
|
11
|
+
dependencies: {
|
|
12
|
+
"@qvac/translation-nmtcpp": "0.21.1",
|
|
13
|
+
"bare-process": "4.5.1",
|
|
14
|
+
"bare-runtime": "1.36.0"
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* A long run with no spaces made of base64, base64url or hex characters: a key, a signature, a token pasted in
|
|
19
|
+
* full. It folds to one line with "Show all" and Copy. Links and money come first (a link's path is never a blob,
|
|
20
|
+
* and a Cashu token or an invoice has its own card).
|
|
21
|
+
*/
|
|
22
|
+
var blob = {
|
|
23
|
+
kind: "blob",
|
|
24
|
+
pattern: new RegExp(`(?<![A-Za-z0-9+/=_-])[A-Za-z0-9+/_-]{80,}={0,2}(?![A-Za-z0-9+/=_-])`, "g"),
|
|
25
|
+
accept: () => ({ data: null }),
|
|
26
|
+
plain: (atom) => `${atom.text.slice(0, 12)}…`
|
|
27
|
+
};
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region ../../apps/ui/src/lib/parse/links.ts
|
|
30
|
+
var CLOSERS = {
|
|
31
|
+
")": "(",
|
|
32
|
+
"]": "[",
|
|
33
|
+
"}": "{"
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* A link ends where the sentence around it takes over: trailing punctuation ("see https://x.example/a.") and a
|
|
37
|
+
* closing bracket or quote the link did not open ("(https://x.example/a)") are left as text, and so is a doubled
|
|
38
|
+
* `~~` or `||` closing the strike or spoiler the link sits in.
|
|
39
|
+
*/
|
|
40
|
+
function linkEnd(url) {
|
|
41
|
+
const count = {
|
|
42
|
+
"(": 0,
|
|
43
|
+
")": 0,
|
|
44
|
+
"[": 0,
|
|
45
|
+
"]": 0,
|
|
46
|
+
"{": 0,
|
|
47
|
+
"}": 0
|
|
48
|
+
};
|
|
49
|
+
for (const c of url) if (c in count) count[c]++;
|
|
50
|
+
let end = url.length;
|
|
51
|
+
while (end > 0) {
|
|
52
|
+
const last = url[end - 1];
|
|
53
|
+
const opener = CLOSERS[last];
|
|
54
|
+
if (/[.,;:!?'"*_>]/.test(last) || opener && count[last] > count[opener]) {
|
|
55
|
+
if (opener) count[last]--;
|
|
56
|
+
end--;
|
|
57
|
+
} else if ((last === "~" || last === "|") && url[end - 2] === last) end -= 2;
|
|
58
|
+
else break;
|
|
59
|
+
}
|
|
60
|
+
return url.slice(0, end);
|
|
61
|
+
}
|
|
62
|
+
/** Direction and other invisible format characters, which can make a link read as another address. */
|
|
63
|
+
var INVISIBLE = /[\u00ad\u061c\u200b-\u200f\u202a-\u202e\u2060-\u2069\ufeff]/g;
|
|
64
|
+
/**
|
|
65
|
+
* A link as a bubble shows it. Plain ASCII shows as typed. Anything else shows as the browser reads it: the host
|
|
66
|
+
* in punycode (a look-alike "аpple.com" reads "xn--pple-43d.com") and other characters percent-encoded, so a
|
|
67
|
+
* right-to-left override inside it cannot turn the address around. A user part ("www.paypal.com@evil.example")
|
|
68
|
+
* is left out, so what reads as the host is the one the link opens. What no URL parser takes loses its invisible
|
|
69
|
+
* characters.
|
|
70
|
+
*/
|
|
71
|
+
function shownUrl(url) {
|
|
72
|
+
let parsed;
|
|
73
|
+
try {
|
|
74
|
+
parsed = new URL(url);
|
|
75
|
+
} catch {
|
|
76
|
+
return url.replace(INVISIBLE, "");
|
|
77
|
+
}
|
|
78
|
+
if (parsed.username || parsed.password) {
|
|
79
|
+
parsed.username = "";
|
|
80
|
+
parsed.password = "";
|
|
81
|
+
return parsed.href;
|
|
82
|
+
}
|
|
83
|
+
return /^[\x21-\x7e]*$/.test(url) ? url : parsed.href;
|
|
84
|
+
}
|
|
85
|
+
/** Web addresses, over http(s) only: `javascript:` and `data:` never become links. */
|
|
86
|
+
var link = {
|
|
87
|
+
kind: "link",
|
|
88
|
+
pattern: /https?:\/\/\S+/g,
|
|
89
|
+
accept(match) {
|
|
90
|
+
const url = linkEnd(match[0]);
|
|
91
|
+
if (url.length <= match[0].indexOf("//") + 2) return null;
|
|
92
|
+
return {
|
|
93
|
+
data: { url },
|
|
94
|
+
end: match.index + url.length
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
//#endregion
|
|
99
|
+
//#region ../../apps/ui/src/lib/parse/inline.ts
|
|
100
|
+
/**
|
|
101
|
+
* The inline tokenizer: a paragraph's text in, segments out. Three passes, each linear in the text:
|
|
102
|
+
*
|
|
103
|
+
* 1. `code`: backtick spans (a run of N backticks up to the next run of exactly N on the same line). What is
|
|
104
|
+
* inside is never read again: no links, no markers.
|
|
105
|
+
* 2. Detectors, over the text between code spans: links, long blobs, times, and whatever other cards register.
|
|
106
|
+
* What they recognise is opaque too, so a `*` or `_` inside a URL stays part of it.
|
|
107
|
+
* 3. Markers, over what is left: `*bold*` (or `**bold**`), `_italic_` (or `__italic__`), `~~strike~~` and
|
|
108
|
+
* `||spoiler||`, paired within a line. A marker opens after a space, a punctuation mark or the start, before
|
|
109
|
+
* something that is not a space, and closes the other way round, so `snake_case_name`, `2*3*4` and
|
|
110
|
+
* `__init__.py` stay text. Openers left unclosed, and closers with no opener, are text too.
|
|
111
|
+
*/
|
|
112
|
+
/** What a spoiler reads as where it cannot be tapped: nothing of what it hides. */
|
|
113
|
+
var SPOILER_PLAIN = "▒▒▒";
|
|
114
|
+
/** Which runs of marker characters mean something, and what. A run of any other length is text. */
|
|
115
|
+
var MARKERS = {
|
|
116
|
+
"*": "bold",
|
|
117
|
+
"**": "bold",
|
|
118
|
+
"_": "italic",
|
|
119
|
+
"__": "italic",
|
|
120
|
+
"~~": "strike",
|
|
121
|
+
"||": "spoiler"
|
|
122
|
+
};
|
|
123
|
+
var MARKER_CHARS = /* @__PURE__ */ new Set([
|
|
124
|
+
"*",
|
|
125
|
+
"_",
|
|
126
|
+
"~",
|
|
127
|
+
"|"
|
|
128
|
+
]);
|
|
129
|
+
/** Punctuation or a symbol: what a marker may lean on besides a space. */
|
|
130
|
+
var PUNCT = /[\p{P}\p{S}]/u;
|
|
131
|
+
var SPACE = /\s/u;
|
|
132
|
+
var WORD = /[\p{L}\p{N}]/u;
|
|
133
|
+
var compiled = /* @__PURE__ */ new WeakMap();
|
|
134
|
+
function patternOf(detector) {
|
|
135
|
+
let re = compiled.get(detector);
|
|
136
|
+
if (!re) {
|
|
137
|
+
const { source, flags } = detector.pattern;
|
|
138
|
+
re = new RegExp(source, flags.includes("g") ? flags : flags + "g");
|
|
139
|
+
compiled.set(detector, re);
|
|
140
|
+
}
|
|
141
|
+
return re;
|
|
142
|
+
}
|
|
143
|
+
/** The detector's next accepted match in `text` at or after `from`. */
|
|
144
|
+
function findNext(detector, text, from, ctx) {
|
|
145
|
+
const re = patternOf(detector);
|
|
146
|
+
let at = from;
|
|
147
|
+
while (at <= text.length) {
|
|
148
|
+
re.lastIndex = at;
|
|
149
|
+
const match = re.exec(text);
|
|
150
|
+
if (!match) return null;
|
|
151
|
+
const accepted = match[0] ? detector.accept(match, text, ctx) : null;
|
|
152
|
+
const matchEnd = match.index + match[0].length;
|
|
153
|
+
if (accepted) {
|
|
154
|
+
const end = Math.min(accepted.end ?? matchEnd, matchEnd);
|
|
155
|
+
if (end > match.index) return {
|
|
156
|
+
start: match.index,
|
|
157
|
+
end,
|
|
158
|
+
data: accepted.data
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
at = Math.max(matchEnd, match.index + 1);
|
|
162
|
+
}
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
/** Pass 2: the detectors' atoms in a stretch of text outside code, and the text between them. */
|
|
166
|
+
function detect(text, detectors, ctx, out) {
|
|
167
|
+
const next = detectors.map((d) => findNext(d, text, 0, ctx));
|
|
168
|
+
let cursor = 0;
|
|
169
|
+
for (;;) {
|
|
170
|
+
let best = -1;
|
|
171
|
+
for (let i = 0; i < next.length; i++) {
|
|
172
|
+
const found = next[i];
|
|
173
|
+
if (found && (best < 0 || found.start < next[best].start)) best = i;
|
|
174
|
+
}
|
|
175
|
+
if (best < 0) break;
|
|
176
|
+
const found = next[best];
|
|
177
|
+
if (found.start > cursor) out.push(text.slice(cursor, found.start));
|
|
178
|
+
const detector = detectors[best];
|
|
179
|
+
out.push({
|
|
180
|
+
type: "atom",
|
|
181
|
+
kind: detector.kind,
|
|
182
|
+
text: text.slice(found.start, found.end),
|
|
183
|
+
data: found.data
|
|
184
|
+
});
|
|
185
|
+
cursor = found.end;
|
|
186
|
+
for (let i = 0; i < next.length; i++) if (next[i] && next[i].start < cursor) next[i] = findNext(detectors[i], text, cursor, ctx);
|
|
187
|
+
}
|
|
188
|
+
if (cursor < text.length) out.push(text.slice(cursor));
|
|
189
|
+
}
|
|
190
|
+
/** Pass 1: code spans. Backtick runs pair with the next run of the same length on the same line. */
|
|
191
|
+
function codeSpans(text) {
|
|
192
|
+
const out = [];
|
|
193
|
+
const runs = [];
|
|
194
|
+
for (let i = 0; i < text.length; i++) {
|
|
195
|
+
if (text[i] !== "`") continue;
|
|
196
|
+
let j = i;
|
|
197
|
+
while (text[j] === "`") j++;
|
|
198
|
+
runs.push({
|
|
199
|
+
at: i,
|
|
200
|
+
len: j - i
|
|
201
|
+
});
|
|
202
|
+
i = j - 1;
|
|
203
|
+
}
|
|
204
|
+
if (!runs.length) return [text];
|
|
205
|
+
const nextSame = new Array(runs.length).fill(-1);
|
|
206
|
+
const seen = /* @__PURE__ */ new Map();
|
|
207
|
+
for (let i = runs.length - 1; i >= 0; i--) {
|
|
208
|
+
nextSame[i] = seen.get(runs[i].len) ?? -1;
|
|
209
|
+
seen.set(runs[i].len, i);
|
|
210
|
+
}
|
|
211
|
+
const lineEnd = (at) => {
|
|
212
|
+
const n = text.indexOf("\n", at);
|
|
213
|
+
return n < 0 ? text.length : n;
|
|
214
|
+
};
|
|
215
|
+
let cursor = 0;
|
|
216
|
+
let endOfLine = -1;
|
|
217
|
+
for (let i = 0; i < runs.length; i++) {
|
|
218
|
+
const open = runs[i];
|
|
219
|
+
if (open.at < cursor) continue;
|
|
220
|
+
if (open.at > endOfLine) endOfLine = lineEnd(open.at);
|
|
221
|
+
const j = nextSame[i];
|
|
222
|
+
if (j < 0) continue;
|
|
223
|
+
const close = runs[j];
|
|
224
|
+
const inner = text.slice(open.at + open.len, close.at);
|
|
225
|
+
if (close.at > endOfLine || !inner.trim()) continue;
|
|
226
|
+
if (open.at > cursor) out.push(text.slice(cursor, open.at));
|
|
227
|
+
out.push({
|
|
228
|
+
type: "code",
|
|
229
|
+
text: inner.length > 2 && inner[0] === " " && inner[inner.length - 1] === " " ? inner.slice(1, -1) : inner
|
|
230
|
+
});
|
|
231
|
+
cursor = close.at + close.len;
|
|
232
|
+
i = j;
|
|
233
|
+
}
|
|
234
|
+
if (cursor < text.length) out.push(text.slice(cursor));
|
|
235
|
+
return out;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Pass 3, splitting: text into plain stretches and marker runs that could open or close something. `before` and
|
|
239
|
+
* `after` are the characters around the text in the message (an atom's or a code span's edge counts as a word
|
|
240
|
+
* character there: "*see https://x.example*" closes after the link).
|
|
241
|
+
*/
|
|
242
|
+
function splitMarkers(text, prev, nextChar, items) {
|
|
243
|
+
let from = 0;
|
|
244
|
+
for (let i = 0; i < text.length; i++) {
|
|
245
|
+
if (!MARKER_CHARS.has(text[i])) continue;
|
|
246
|
+
let j = i;
|
|
247
|
+
while (text[j] === text[i]) j++;
|
|
248
|
+
const run = text.slice(i, j);
|
|
249
|
+
if (MARKERS[run]) {
|
|
250
|
+
if (i > from) items.push({
|
|
251
|
+
t: "text",
|
|
252
|
+
s: text.slice(from, i)
|
|
253
|
+
});
|
|
254
|
+
const before = i > 0 ? text[i - 1] : prev;
|
|
255
|
+
const after = j < text.length ? text[j] : nextChar;
|
|
256
|
+
const afterNext = j + 1 < text.length ? text[j + 1] : void 0;
|
|
257
|
+
const mark = {
|
|
258
|
+
t: "mark",
|
|
259
|
+
run,
|
|
260
|
+
canOpen: after !== void 0 && !SPACE.test(after) && (before === void 0 || SPACE.test(before) || PUNCT.test(before) && before !== run[0]),
|
|
261
|
+
canClose: before !== void 0 && !SPACE.test(before) && (after === void 0 || SPACE.test(after) || PUNCT.test(after) && after !== run[0]) && !(after !== void 0 && /[.,:;]/.test(after) && afterNext !== void 0 && WORD.test(afterNext))
|
|
262
|
+
};
|
|
263
|
+
items.push(mark);
|
|
264
|
+
from = j;
|
|
265
|
+
}
|
|
266
|
+
i = j - 1;
|
|
267
|
+
}
|
|
268
|
+
if (from < text.length) items.push({
|
|
269
|
+
t: "text",
|
|
270
|
+
s: text.slice(from)
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
/** Pass 3, pairing: per-run stacks, so a closer finds its opener in O(1) and each marker is dropped at most once. */
|
|
274
|
+
function pairMarkers(items) {
|
|
275
|
+
const stack = [];
|
|
276
|
+
const depthOf = new Int32Array(items.length);
|
|
277
|
+
const byRun = /* @__PURE__ */ new Map();
|
|
278
|
+
const popTo = (depth) => {
|
|
279
|
+
while (stack.length > depth) byRun.get(items[stack.pop()].run).pop();
|
|
280
|
+
};
|
|
281
|
+
for (let i = 0; i < items.length; i++) {
|
|
282
|
+
const item = items[i];
|
|
283
|
+
if (item.t === "text") {
|
|
284
|
+
if (item.s.includes("\n")) popTo(0);
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
if (item.t !== "mark") continue;
|
|
288
|
+
const openers = byRun.get(item.run);
|
|
289
|
+
if (item.canClose && openers?.length) {
|
|
290
|
+
const opener = openers[openers.length - 1];
|
|
291
|
+
popTo(depthOf[opener]);
|
|
292
|
+
items[opener].pair = "open";
|
|
293
|
+
item.pair = "close";
|
|
294
|
+
} else if (item.canOpen && stack.length < 8) {
|
|
295
|
+
depthOf[i] = stack.length;
|
|
296
|
+
stack.push(i);
|
|
297
|
+
if (openers) openers.push(i);
|
|
298
|
+
else byRun.set(item.run, [i]);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
/** The paired items as a tree; unpaired markers are text, and neighbouring text is joined. */
|
|
303
|
+
function build(items) {
|
|
304
|
+
const root = [];
|
|
305
|
+
const parents = [root];
|
|
306
|
+
const push = (seg) => {
|
|
307
|
+
const list = parents[parents.length - 1];
|
|
308
|
+
const last = list[list.length - 1];
|
|
309
|
+
if (seg.type === "text" && last?.type === "text") last.text += seg.text;
|
|
310
|
+
else list.push(seg);
|
|
311
|
+
};
|
|
312
|
+
for (const item of items) if (item.t === "text") push({
|
|
313
|
+
type: "text",
|
|
314
|
+
text: item.s
|
|
315
|
+
});
|
|
316
|
+
else if (item.t === "seg") push(item.seg);
|
|
317
|
+
else if (item.pair === "open") {
|
|
318
|
+
const span = {
|
|
319
|
+
type: "span",
|
|
320
|
+
style: MARKERS[item.run],
|
|
321
|
+
children: []
|
|
322
|
+
};
|
|
323
|
+
push(span);
|
|
324
|
+
parents.push(span.children);
|
|
325
|
+
} else if (item.pair === "close") parents.pop();
|
|
326
|
+
else push({
|
|
327
|
+
type: "text",
|
|
328
|
+
text: item.run
|
|
329
|
+
});
|
|
330
|
+
return root;
|
|
331
|
+
}
|
|
332
|
+
/** One paragraph's segments. `detectors` is the registry's list unless a test gives its own. */
|
|
333
|
+
function tokenizeInline(text, detectors, ctx = {}) {
|
|
334
|
+
const pieces = [];
|
|
335
|
+
for (const piece of codeSpans(text)) if (typeof piece === "string") detect(piece, detectors, ctx, pieces);
|
|
336
|
+
else pieces.push(piece);
|
|
337
|
+
const items = [];
|
|
338
|
+
const edge = (piece, first) => piece === void 0 ? void 0 : typeof piece === "string" ? piece[first ? 0 : piece.length - 1] : "a";
|
|
339
|
+
pieces.forEach((piece, i) => {
|
|
340
|
+
if (typeof piece === "string") splitMarkers(piece, edge(pieces[i - 1], false), edge(pieces[i + 1], true), items);
|
|
341
|
+
else items.push({
|
|
342
|
+
t: "seg",
|
|
343
|
+
seg: piece
|
|
344
|
+
});
|
|
345
|
+
});
|
|
346
|
+
pairMarkers(items);
|
|
347
|
+
return build(items);
|
|
348
|
+
}
|
|
349
|
+
//#endregion
|
|
350
|
+
//#region ../../apps/ui/src/lib/parse/mdlink.ts
|
|
351
|
+
/** The link's host as a browser reads it: lower case, punycode, without a leading "www.". */
|
|
352
|
+
function hostOf(url) {
|
|
353
|
+
try {
|
|
354
|
+
return new URL(url).hostname.replace(/^www\./, "");
|
|
355
|
+
} catch {
|
|
356
|
+
return null;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
/** Something in a label that reads as a web address or a domain: "ghostly.tools", "https://a.example/x", "1.2.3.4". */
|
|
360
|
+
var ADDRESS = /(?:[a-z][a-z\d+.-]{0,20}:\/\/)?[\p{L}\p{N}][\p{L}\p{N}-]{0,62}(?:\.[\p{L}\p{N}][\p{L}\p{N}-]{0,62}){1,8}/giu;
|
|
361
|
+
/** Ends in a top-level domain (letters), or is an IPv4 address. "v1.2", "3.14" and "e.g." are not hosts. */
|
|
362
|
+
var LOOKS_LIKE_HOST = /\.\p{L}[\p{L}\p{N}-]{1,62}$|^\d{1,3}(?:\.\d{1,3}){3}$/u;
|
|
363
|
+
/** File extensions no top-level domain uses: "report.pdf", "Node.js" and "package.json" are names, not hosts. */
|
|
364
|
+
var NOT_A_TLD = /\.(?:[cm]?js|jsx|tsx?|json|css|html?|txt|log|csv|png|jpe?g|gif|svg|webp|toml|ya?ml|lock|wasm|pdf|rb|go)$/i;
|
|
365
|
+
/** Extensions that are country domains too (Moldova, Paraguay…): a file only as a path's last part ("docs/CHAT.md"). */
|
|
366
|
+
var FILE_IN_PATH = /\.(?:md|py|rs|sh)$/i;
|
|
367
|
+
/** Characters a browser reads as the dot between a host's labels, and ones that only look like it. */
|
|
368
|
+
var DOTS = /[\u00b7\u0701\u06d4\u2024\u2027\u2e3c\u3002\uff0e\uff61]/g;
|
|
369
|
+
/**
|
|
370
|
+
* Whether a link's text would make a reader think it goes somewhere else: it names a host that is not the link's (nor
|
|
371
|
+
* one the link's host sits under), or it holds an invisible or direction character, which can turn "moc.lapyap"
|
|
372
|
+
* into "paypal.com" on screen. The text is read as a browser would read a host ("ghostly。tools" and "ghostly.tools"
|
|
373
|
+
* are "ghostly.tools"). File names are left alone ("report.pdf", "docs/CHAT.md"); anything else that reads as a
|
|
374
|
+
* host counts, which errs on the safe side: the bubble shows the address.
|
|
375
|
+
*/
|
|
376
|
+
function labelMisleads(label, url) {
|
|
377
|
+
const target = hostOf(url);
|
|
378
|
+
if (!target) return true;
|
|
379
|
+
if (new RegExp(INVISIBLE.source).test(label)) return true;
|
|
380
|
+
const text = label.normalize("NFKC").replace(DOTS, ".");
|
|
381
|
+
for (const found of text.matchAll(ADDRESS)) {
|
|
382
|
+
const written = found[0];
|
|
383
|
+
const bare = written.replace(/^[a-z][a-z\d+.-]*:\/\//i, "");
|
|
384
|
+
if (bare === written) {
|
|
385
|
+
if (!LOOKS_LIKE_HOST.test(bare) || NOT_A_TLD.test(bare)) continue;
|
|
386
|
+
const at = found.index;
|
|
387
|
+
if (FILE_IN_PATH.test(bare) && text[at - 1] === "/" && at > 1 && !/[/:]/.test(text[at - 2])) continue;
|
|
388
|
+
}
|
|
389
|
+
const host = hostOf(`https://${bare}`);
|
|
390
|
+
if (!host) return true;
|
|
391
|
+
if (host !== target && !target.endsWith(`.${host}`)) return true;
|
|
392
|
+
}
|
|
393
|
+
return false;
|
|
394
|
+
}
|
|
395
|
+
/** A label's words without its markers, for the chat list. */
|
|
396
|
+
function labelText(label) {
|
|
397
|
+
return label.map((s) => s.type === "span" ? s.style === "spoiler" ? SPOILER_PLAIN : labelText(s.children) : s.text).join("");
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* `[text](https://…)`: the text as a link, over http(s) only (`[a](javascript:…)` stays text). The address may hold
|
|
401
|
+
* one pair of brackets ("…/Foo_(bar)"); both parts are bounded, so the pattern stays linear.
|
|
402
|
+
*/
|
|
403
|
+
var mdLink = {
|
|
404
|
+
kind: "md-link",
|
|
405
|
+
pattern: /\[([^[\]\n]{1,200})\]\((https?:\/\/[^\s()<>]{1,4096}(?:\([^\s()<>]{0,256}\)[^\s()<>]{0,1024})?)\)/gi,
|
|
406
|
+
accept(match) {
|
|
407
|
+
const [, text, url] = match;
|
|
408
|
+
if (!text.trim() || !hostOf(url)) return null;
|
|
409
|
+
return { data: {
|
|
410
|
+
url,
|
|
411
|
+
label: tokenizeInline(text, []),
|
|
412
|
+
showUrl: labelMisleads(text, url)
|
|
413
|
+
} };
|
|
414
|
+
},
|
|
415
|
+
plain(atom) {
|
|
416
|
+
return atom.data.showUrl ? shownUrl(atom.data.url) : labelText(atom.data.label);
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
//#endregion
|
|
420
|
+
//#region ../../apps/ui/src/lib/parse/mentions.ts
|
|
421
|
+
/** A marked mention, as an atom carrying how it shows (`ParseContext.mentions`). */
|
|
422
|
+
var mention = {
|
|
423
|
+
kind: "member-mention",
|
|
424
|
+
pattern: /\uE000(\d{1,2})\uE001/g,
|
|
425
|
+
accept: (match, _text, ctx) => {
|
|
426
|
+
const view = ctx.mentions?.[Number(match[1])];
|
|
427
|
+
return view ? { data: view } : null;
|
|
428
|
+
},
|
|
429
|
+
plain: (atom) => `@${atom.data.name}`
|
|
430
|
+
};
|
|
431
|
+
//#endregion
|
|
432
|
+
//#region ../../apps/ui/src/lib/parse/time.ts
|
|
433
|
+
/**
|
|
434
|
+
* Times the reader may want in their own zone: an ISO timestamp with its offset ("2026-09-25T14:00:00Z",
|
|
435
|
+
* "2026-09-25 14:00 +02:00") and a clock time with a named zone ("at 14:00 UTC", "3pm GMT+2", "9:30 am EST").
|
|
436
|
+
* Conservative on purpose: a time with no zone, a bare number, a score ("3:2") or a version ("v1.14:00") is text.
|
|
437
|
+
* A clock time with no date means that day where the message was written, the day it was sent.
|
|
438
|
+
*/
|
|
439
|
+
/** Zone abbreviations read the same way everywhere (no CST, IST or BST, which name several zones). Minutes east of UTC. */
|
|
440
|
+
var ZONES = {
|
|
441
|
+
UTC: 0,
|
|
442
|
+
GMT: 0,
|
|
443
|
+
Z: 0,
|
|
444
|
+
EST: -300,
|
|
445
|
+
EDT: -240,
|
|
446
|
+
MST: -420,
|
|
447
|
+
MDT: -360,
|
|
448
|
+
PST: -480,
|
|
449
|
+
PDT: -420,
|
|
450
|
+
CET: 60,
|
|
451
|
+
CEST: 120,
|
|
452
|
+
EET: 120,
|
|
453
|
+
EEST: 180,
|
|
454
|
+
BRT: -180,
|
|
455
|
+
JST: 540,
|
|
456
|
+
AEST: 600,
|
|
457
|
+
AEDT: 660
|
|
458
|
+
};
|
|
459
|
+
var NAMED = "UTC|GMT|EST|EDT|MST|MDT|PST|PDT|CET|CEST|EET|EEST|BRT|JST|AEST|AEDT";
|
|
460
|
+
var OFFSET = "[+-]\\d{1,2}(?::?\\d{2})?";
|
|
461
|
+
var ISO = `(\\d{4})-(\\d{2})-(\\d{2})[T ](\\d{2}):(\\d{2})(?::(\\d{2})(?:\\.\\d{1,9})?)?\\s?(Z|UTC|GMT|[+-]\\d{2}:?\\d{2})`;
|
|
462
|
+
var CLOCK = `(\\d{1,2})(?::(\\d{2}))?\\s?(?:([ap])\\.?m\\b\\.?)?\\s?(?:(UTC|GMT)(${OFFSET})?|(${NAMED}))`;
|
|
463
|
+
/** Whether a moment is one a date can hold (±8.64e15 ms): a peer picks the time a message was sent. */
|
|
464
|
+
var isMoment = (ms) => !Number.isNaN(new Date(ms).getTime());
|
|
465
|
+
/** Minutes east of UTC for "+02:00", "-3", "+0530"; null past ±14 h. */
|
|
466
|
+
function offsetMinutes(offset) {
|
|
467
|
+
const m = /^([+-])(\d{1,2})(?::?(\d{2}))?$/.exec(offset);
|
|
468
|
+
if (!m) return null;
|
|
469
|
+
const minutes = Number(m[2]) * 60 + Number(m[3] ?? 0);
|
|
470
|
+
if (Number(m[2]) > 14 || Number(m[3] ?? 0) > 59) return null;
|
|
471
|
+
return m[1] === "-" ? -minutes : minutes;
|
|
472
|
+
}
|
|
473
|
+
//#endregion
|
|
474
|
+
//#region ../../apps/ui/src/lib/parse/detectors.ts
|
|
475
|
+
/**
|
|
476
|
+
* Every inline detector, in order of precedence: where two match at the same place, the earlier one here wins;
|
|
477
|
+
* otherwise the match that starts first does. A new kind of atom is one line here (its look, if it needs one, is
|
|
478
|
+
* one line in apps/ui/src/components/rich/views.ts; without one it shows as the text it matched).
|
|
479
|
+
*/
|
|
480
|
+
var DETECTORS = [
|
|
481
|
+
mdLink,
|
|
482
|
+
link,
|
|
483
|
+
blob,
|
|
484
|
+
{
|
|
485
|
+
kind: "time",
|
|
486
|
+
pattern: new RegExp(`(?<![\\w:./+-])(?:${ISO}|${CLOCK})(?![\\w:+-])`, "gi"),
|
|
487
|
+
accept(m, _text, ctx) {
|
|
488
|
+
if (m[1] !== void 0) {
|
|
489
|
+
const [year, month, day, hour, minute, second] = [
|
|
490
|
+
1,
|
|
491
|
+
2,
|
|
492
|
+
3,
|
|
493
|
+
4,
|
|
494
|
+
5,
|
|
495
|
+
6
|
|
496
|
+
].map((i) => Number(m[i] ?? 0));
|
|
497
|
+
const zone = m[7].toUpperCase();
|
|
498
|
+
const offset = zone in ZONES ? ZONES[zone] : offsetMinutes(zone);
|
|
499
|
+
if (offset === null || month < 1 || month > 12 || day < 1 || day > 31 || hour > 23 || minute > 59 || second > 59) return null;
|
|
500
|
+
const at = Date.UTC(year, month - 1, day, hour, minute, second) - offset * 6e4;
|
|
501
|
+
if (!isMoment(at) || new Date(at + offset * 6e4).getUTCDate() !== day) return null;
|
|
502
|
+
return { data: {
|
|
503
|
+
at,
|
|
504
|
+
dated: true
|
|
505
|
+
} };
|
|
506
|
+
}
|
|
507
|
+
let hour = Number(m[8]);
|
|
508
|
+
const minute = m[9] === void 0 ? 0 : Number(m[9]);
|
|
509
|
+
const meridiem = m[10]?.toLowerCase();
|
|
510
|
+
if (m[9] === void 0 && !meridiem) return null;
|
|
511
|
+
if (minute > 59) return null;
|
|
512
|
+
if (meridiem) {
|
|
513
|
+
if (hour < 1 || hour > 12) return null;
|
|
514
|
+
hour = hour % 12 + (meridiem === "p" ? 12 : 0);
|
|
515
|
+
} else if (hour > 23) return null;
|
|
516
|
+
const offset = m[13] ? ZONES[m[13].toUpperCase()] : m[12] ? offsetMinutes(m[12]) : 0;
|
|
517
|
+
if (offset === null || offset === void 0) return null;
|
|
518
|
+
const there = new Date((ctx.sentAt ?? Date.now()) + offset * 6e4);
|
|
519
|
+
if (!isMoment(there.getTime())) return null;
|
|
520
|
+
const at = Date.UTC(there.getUTCFullYear(), there.getUTCMonth(), there.getUTCDate(), hour, minute) - offset * 6e4;
|
|
521
|
+
if (!isMoment(at)) return null;
|
|
522
|
+
return { data: {
|
|
523
|
+
at,
|
|
524
|
+
dated: false
|
|
525
|
+
} };
|
|
526
|
+
}
|
|
527
|
+
},
|
|
528
|
+
mention
|
|
529
|
+
];
|
|
530
|
+
//#endregion
|
|
531
|
+
//#region ../../apps/ui/src/lib/parse/json.ts
|
|
532
|
+
/**
|
|
533
|
+
* What pretty-printing takes on. Re-indenting adds two spaces a level on every line, so deep nesting grows the text
|
|
534
|
+
* with the square of its depth: 8 KiB of `[` and `]` would come out as about 134 million characters. A message past
|
|
535
|
+
* these bounds shows as it was written.
|
|
536
|
+
*/
|
|
537
|
+
var JSON_LIMITS = {
|
|
538
|
+
/** Characters of the message, trimmed. */
|
|
539
|
+
inputChars: 4096,
|
|
540
|
+
/** Levels of nesting. */
|
|
541
|
+
depth: 32,
|
|
542
|
+
/** Characters of the pretty-printed text. */
|
|
543
|
+
outputChars: 65536
|
|
544
|
+
};
|
|
545
|
+
/**
|
|
546
|
+
* A message that is JSON (an object or an array with something in it) shows pretty-printed. The text is
|
|
547
|
+
* re-indented, not parsed and printed again: a number too big for a double, a duplicate key or an escape stays
|
|
548
|
+
* exactly as written. Null for anything else, and for JSON too big or too deep to lay out (`JSON_LIMITS`).
|
|
549
|
+
*/
|
|
550
|
+
function prettyJson(text) {
|
|
551
|
+
const trimmed = text.trim();
|
|
552
|
+
if (trimmed.length > JSON_LIMITS.inputChars) return null;
|
|
553
|
+
const first = trimmed[0];
|
|
554
|
+
const last = trimmed[trimmed.length - 1];
|
|
555
|
+
if (!(first === "{" && last === "}" || first === "[" && last === "]")) return null;
|
|
556
|
+
let depth = 0;
|
|
557
|
+
for (const c of trimmed) if (c === "{" || c === "[") {
|
|
558
|
+
if (++depth > JSON_LIMITS.depth) return null;
|
|
559
|
+
} else if (c === "}" || c === "]") depth--;
|
|
560
|
+
try {
|
|
561
|
+
const value = JSON.parse(trimmed);
|
|
562
|
+
if (typeof value !== "object" || value === null || Object.keys(value).length === 0) return null;
|
|
563
|
+
} catch {
|
|
564
|
+
return null;
|
|
565
|
+
}
|
|
566
|
+
return reindent(trimmed);
|
|
567
|
+
}
|
|
568
|
+
/** Valid JSON, one value per line, two spaces a level; strings are copied as they are. Null past the bounds. */
|
|
569
|
+
function reindent(json) {
|
|
570
|
+
let out = "";
|
|
571
|
+
let depth = 0;
|
|
572
|
+
const newline = () => "\n" + " ".repeat(depth);
|
|
573
|
+
for (let i = 0; i < json.length; i++) {
|
|
574
|
+
const c = json[i];
|
|
575
|
+
if (c === "\"") {
|
|
576
|
+
let j = i + 1;
|
|
577
|
+
while (json[j] !== "\"") j += json[j] === "\\" ? 2 : 1;
|
|
578
|
+
out += json.slice(i, j + 1);
|
|
579
|
+
i = j;
|
|
580
|
+
} else if (c === "{" || c === "[") {
|
|
581
|
+
let j = i + 1;
|
|
582
|
+
while (/\s/.test(json[j])) j++;
|
|
583
|
+
if (json[j] === (c === "{" ? "}" : "]")) {
|
|
584
|
+
out += c + json[j];
|
|
585
|
+
i = j;
|
|
586
|
+
continue;
|
|
587
|
+
}
|
|
588
|
+
if (++depth > JSON_LIMITS.depth) return null;
|
|
589
|
+
out += c + newline();
|
|
590
|
+
} else if (c === "}" || c === "]") {
|
|
591
|
+
depth--;
|
|
592
|
+
out += newline() + c;
|
|
593
|
+
} else if (c === ",") out += "," + newline();
|
|
594
|
+
else if (c === ":") out += ": ";
|
|
595
|
+
else if (!/\s/.test(c)) out += c;
|
|
596
|
+
if (out.length > JSON_LIMITS.outputChars) return null;
|
|
597
|
+
}
|
|
598
|
+
return out;
|
|
599
|
+
}
|
|
600
|
+
//#endregion
|
|
601
|
+
//#region ../../apps/ui/src/lib/parse/blocks.ts
|
|
602
|
+
/** What may follow an opening fence as a language name ("```ts", "```c++", "```objective-c"). */
|
|
603
|
+
var LANG = /^[A-Za-z][\w+#.-]{0,19}$/;
|
|
604
|
+
/**
|
|
605
|
+
* A list item's line: an indent, then `-`, `*` or `•`, or a number (at most 9 digits) with `.` or `)`, then a space
|
|
606
|
+
* and something. "1.5 kg" and "-5 °C" are not items: the marker needs the space.
|
|
607
|
+
*/
|
|
608
|
+
var ITEM = /^( {0,6}|\t)(?:([-*\u2022])|(\d{1,9})([.)])) +(?=\S)/;
|
|
609
|
+
/** How far in an item's line starts for it to sit under the item before it (one level; deeper counts the same). */
|
|
610
|
+
var NESTED = 2;
|
|
611
|
+
/** A quote's line: `>` then a space, or `>` alone (an empty line of the quote). */
|
|
612
|
+
var QUOTE = /^ {0,3}>(?: |$)/;
|
|
613
|
+
var HEADING = /^(#{1,3}) +(?=\S)/;
|
|
614
|
+
var BLANK = /^\s*$/;
|
|
615
|
+
/**
|
|
616
|
+
* The lines of text outside code as blocks: lists, quotes and headings where a line starts with one, paragraphs for
|
|
617
|
+
* the rest. Text with none of them is one paragraph, exactly as written. A blank line next to a list, a quote or a
|
|
618
|
+
* heading is the room around it, which the block's own margin gives, so it goes; the line break between them goes
|
|
619
|
+
* too, the way the one before a fence does. Each line is read once here (twice inside a quote), so this stays linear.
|
|
620
|
+
*/
|
|
621
|
+
function proseBlocks(text, detectors, ctx, quotes) {
|
|
622
|
+
const inline = (s) => tokenizeInline(s, detectors, ctx);
|
|
623
|
+
const units = [];
|
|
624
|
+
const lines = text.split("\n");
|
|
625
|
+
let list = null;
|
|
626
|
+
let item = null;
|
|
627
|
+
let child = null;
|
|
628
|
+
const closeChild = () => {
|
|
629
|
+
if (child) {
|
|
630
|
+
child.at.segments = inline(child.text.join("\n"));
|
|
631
|
+
child = null;
|
|
632
|
+
}
|
|
633
|
+
};
|
|
634
|
+
const closeItem = () => {
|
|
635
|
+
closeChild();
|
|
636
|
+
if (item) {
|
|
637
|
+
item.at.segments = inline(item.text.join("\n"));
|
|
638
|
+
item = null;
|
|
639
|
+
}
|
|
640
|
+
};
|
|
641
|
+
const closeList = () => {
|
|
642
|
+
closeItem();
|
|
643
|
+
list = null;
|
|
644
|
+
};
|
|
645
|
+
const last = () => units[units.length - 1];
|
|
646
|
+
for (let i = 0; i < lines.length; i++) {
|
|
647
|
+
const line = lines[i];
|
|
648
|
+
if (list && BLANK.test(line)) {
|
|
649
|
+
const next = ITEM.exec(lines[i + 1] ?? "");
|
|
650
|
+
if (next && ((next[1] === " " ? NESTED : next[1].length) >= NESTED || next[3] !== void 0 === list.ordered)) continue;
|
|
651
|
+
}
|
|
652
|
+
const m = ITEM.exec(line);
|
|
653
|
+
if (m) {
|
|
654
|
+
const ordered = m[3] !== void 0;
|
|
655
|
+
const at = {
|
|
656
|
+
marker: ordered ? m[3] + m[4] : "•",
|
|
657
|
+
...ordered ? { number: Number(m[3]) } : {},
|
|
658
|
+
segments: [],
|
|
659
|
+
children: []
|
|
660
|
+
};
|
|
661
|
+
const body = line.slice(m[0].length);
|
|
662
|
+
const indent = m[1] === " " ? NESTED : m[1].length;
|
|
663
|
+
if (list && item && indent >= NESTED) {
|
|
664
|
+
closeChild();
|
|
665
|
+
const kids = item.at.children;
|
|
666
|
+
let under = kids[kids.length - 1];
|
|
667
|
+
if (!under || under.ordered !== ordered) kids.push(under = {
|
|
668
|
+
type: "list",
|
|
669
|
+
ordered,
|
|
670
|
+
items: []
|
|
671
|
+
});
|
|
672
|
+
under.items.push(at);
|
|
673
|
+
child = {
|
|
674
|
+
list: under,
|
|
675
|
+
at,
|
|
676
|
+
text: [body]
|
|
677
|
+
};
|
|
678
|
+
continue;
|
|
679
|
+
}
|
|
680
|
+
if (!list || list.ordered !== ordered) {
|
|
681
|
+
closeList();
|
|
682
|
+
list = {
|
|
683
|
+
type: "list",
|
|
684
|
+
ordered,
|
|
685
|
+
items: []
|
|
686
|
+
};
|
|
687
|
+
units.push({
|
|
688
|
+
t: "block",
|
|
689
|
+
block: list
|
|
690
|
+
});
|
|
691
|
+
}
|
|
692
|
+
closeItem();
|
|
693
|
+
list.items.push(at);
|
|
694
|
+
item = {
|
|
695
|
+
at,
|
|
696
|
+
text: [body]
|
|
697
|
+
};
|
|
698
|
+
continue;
|
|
699
|
+
}
|
|
700
|
+
if (item && /^(?: {2}|\t)/.test(line) && !BLANK.test(line)) {
|
|
701
|
+
(child ?? item).text.push(line.trimStart());
|
|
702
|
+
continue;
|
|
703
|
+
}
|
|
704
|
+
closeList();
|
|
705
|
+
if (quotes && QUOTE.test(line)) {
|
|
706
|
+
const inner = line.replace(/^ {0,3}> ?/, "");
|
|
707
|
+
const unit = last();
|
|
708
|
+
if (unit?.t === "quote") {
|
|
709
|
+
unit.lines.push(inner);
|
|
710
|
+
unit.raw.push(line);
|
|
711
|
+
} else units.push({
|
|
712
|
+
t: "quote",
|
|
713
|
+
lines: [inner],
|
|
714
|
+
raw: [line]
|
|
715
|
+
});
|
|
716
|
+
continue;
|
|
717
|
+
}
|
|
718
|
+
const h = HEADING.exec(line);
|
|
719
|
+
if (h) {
|
|
720
|
+
units.push({
|
|
721
|
+
t: "block",
|
|
722
|
+
block: {
|
|
723
|
+
type: "heading",
|
|
724
|
+
level: h[1].length,
|
|
725
|
+
segments: inline(line.slice(h[0].length))
|
|
726
|
+
}
|
|
727
|
+
});
|
|
728
|
+
continue;
|
|
729
|
+
}
|
|
730
|
+
const unit = last();
|
|
731
|
+
if (unit?.t === "para") unit.lines.push(line);
|
|
732
|
+
else units.push({
|
|
733
|
+
t: "para",
|
|
734
|
+
lines: [line]
|
|
735
|
+
});
|
|
736
|
+
}
|
|
737
|
+
closeList();
|
|
738
|
+
const merged = [];
|
|
739
|
+
for (const unit of units) {
|
|
740
|
+
const now = unit.t === "quote" && unit.lines.every((l) => BLANK.test(l)) ? {
|
|
741
|
+
t: "para",
|
|
742
|
+
lines: unit.raw
|
|
743
|
+
} : unit;
|
|
744
|
+
const before = merged[merged.length - 1];
|
|
745
|
+
if (now.t === "para" && before?.t === "para") before.lines.push(...now.lines);
|
|
746
|
+
else merged.push(now);
|
|
747
|
+
}
|
|
748
|
+
const blocks = [];
|
|
749
|
+
merged.forEach((unit, i) => {
|
|
750
|
+
if (unit.t === "block") {
|
|
751
|
+
blocks.push(unit.block);
|
|
752
|
+
return;
|
|
753
|
+
}
|
|
754
|
+
if (unit.t === "quote") {
|
|
755
|
+
blocks.push({
|
|
756
|
+
type: "quote",
|
|
757
|
+
blocks: proseBlocks(unit.lines.join("\n"), detectors, ctx, false)
|
|
758
|
+
});
|
|
759
|
+
return;
|
|
760
|
+
}
|
|
761
|
+
let { lines: body } = unit;
|
|
762
|
+
let from = 0;
|
|
763
|
+
let to = body.length;
|
|
764
|
+
if (i > 0) while (from < to && BLANK.test(body[from])) from++;
|
|
765
|
+
if (i < merged.length - 1) while (to > from && BLANK.test(body[to - 1])) to--;
|
|
766
|
+
body = body.slice(from, to);
|
|
767
|
+
const joined = body.join("\n");
|
|
768
|
+
if (joined) blocks.push({
|
|
769
|
+
type: "paragraph",
|
|
770
|
+
segments: inline(joined)
|
|
771
|
+
});
|
|
772
|
+
});
|
|
773
|
+
return blocks;
|
|
774
|
+
}
|
|
775
|
+
/**
|
|
776
|
+
* A message's text as blocks. A message that is JSON is one pretty-printed block. Otherwise a fence (```) at the
|
|
777
|
+
* start of a line opens a code block, with an optional language name after it, and the next ``` closes it; a fence
|
|
778
|
+
* that never closes is text. Everything else is lists, quotes, headings and paragraphs of inline segments
|
|
779
|
+
* (`proseBlocks`, then `tokenizeInline`).
|
|
780
|
+
*/
|
|
781
|
+
function parseMessage(text, ctx = {}, detectors = DETECTORS) {
|
|
782
|
+
const json = prettyJson(text);
|
|
783
|
+
if (json !== null) return [{
|
|
784
|
+
type: "codeblock",
|
|
785
|
+
code: json,
|
|
786
|
+
lang: "json"
|
|
787
|
+
}];
|
|
788
|
+
const blocks = [];
|
|
789
|
+
const paragraph = (from, to) => {
|
|
790
|
+
if (to > from) blocks.push(...proseBlocks(text.slice(from, to), detectors, ctx, true));
|
|
791
|
+
};
|
|
792
|
+
const fence = /(^|\n)```/g;
|
|
793
|
+
let cursor = 0;
|
|
794
|
+
for (let found = fence.exec(text); found; found = fence.exec(text)) {
|
|
795
|
+
const open = found.index + found[1].length;
|
|
796
|
+
if (open < cursor) continue;
|
|
797
|
+
const lineEnd = text.indexOf("\n", open);
|
|
798
|
+
if (lineEnd < 0) break;
|
|
799
|
+
const info = text.slice(open + 3, lineEnd).trim();
|
|
800
|
+
const lang = LANG.test(info) ? info.toLowerCase() : void 0;
|
|
801
|
+
const start = lang || !info ? lineEnd + 1 : open + 3;
|
|
802
|
+
const close = text.indexOf("```", start);
|
|
803
|
+
if (close < 0) break;
|
|
804
|
+
let code = text.slice(start, close);
|
|
805
|
+
if (code.endsWith("\n")) code = code.slice(0, -1);
|
|
806
|
+
if (!code.trim()) {
|
|
807
|
+
fence.lastIndex = close;
|
|
808
|
+
continue;
|
|
809
|
+
}
|
|
810
|
+
paragraph(cursor, found.index);
|
|
811
|
+
blocks.push({
|
|
812
|
+
type: "codeblock",
|
|
813
|
+
code,
|
|
814
|
+
...lang ? { lang } : {}
|
|
815
|
+
});
|
|
816
|
+
fence.lastIndex = close + 3;
|
|
817
|
+
cursor = close + 3;
|
|
818
|
+
if (text[cursor] === "\n") cursor++;
|
|
819
|
+
}
|
|
820
|
+
paragraph(cursor, text.length);
|
|
821
|
+
return blocks;
|
|
822
|
+
}
|
|
823
|
+
/** How much of a long message the detector reads: its language shows long before, and the cost grows with it. */
|
|
824
|
+
var DETECT_CHARS = 2e3;
|
|
825
|
+
var UNDETERMINED = "und";
|
|
826
|
+
var NONE = {
|
|
827
|
+
from: null,
|
|
828
|
+
detected: null
|
|
829
|
+
};
|
|
830
|
+
/** A run of letters, digits and token characters with a digit in it: a number, a token, an invoice, an address, a code. */
|
|
831
|
+
var CODE_RUN = /[A-Za-z0-9_+/=:.-]*\d[A-Za-z0-9_+/=:.-]*|\p{N}+/gu;
|
|
832
|
+
var EMOJI = /\p{Extended_Pictographic}|\p{Emoji_Modifier}|\p{Regional_Indicator}||️/gu;
|
|
833
|
+
var LETTER = /\p{L}/gu;
|
|
834
|
+
function segmentsText(segments) {
|
|
835
|
+
return segments.map((segment) => {
|
|
836
|
+
if (segment.type === "text") return segment.text;
|
|
837
|
+
if (segment.type === "span") return segmentsText(segment.children);
|
|
838
|
+
return " ";
|
|
839
|
+
}).join("");
|
|
840
|
+
}
|
|
841
|
+
function blockText(block) {
|
|
842
|
+
switch (block.type) {
|
|
843
|
+
case "codeblock": return " ";
|
|
844
|
+
case "paragraph":
|
|
845
|
+
case "heading": return segmentsText(block.segments);
|
|
846
|
+
case "quote": return block.blocks.map(blockText).join(" ");
|
|
847
|
+
case "list": return block.items.map((item) => [segmentsText(item.segments), ...item.children.map(blockText)].join(" ")).join(" ");
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
/**
|
|
851
|
+
* What of `text` is language, as the detector reads it: without code, links, mentions (`mentions`, by their place in
|
|
852
|
+
* code points), numbers and tokens, and emoji; one space between words. A message that is JSON has none.
|
|
853
|
+
*/
|
|
854
|
+
function detectableText(text, mentions = []) {
|
|
855
|
+
if (prettyJson(text) !== null) return "";
|
|
856
|
+
let unmentioned = text;
|
|
857
|
+
if (mentions.length) {
|
|
858
|
+
const points = Array.from(text);
|
|
859
|
+
for (const { o, l } of mentions) points.fill(" ", o, Math.min(o + l, points.length));
|
|
860
|
+
unmentioned = points.join("");
|
|
861
|
+
}
|
|
862
|
+
return parseMessage(unmentioned).map(blockText).join(" ").replace(CODE_RUN, " ").replace(EMOJI, " ").replace(/\s+/g, " ").trim();
|
|
863
|
+
}
|
|
864
|
+
var baseCode = (code) => code.toLowerCase().split(/[-_]/)[0];
|
|
865
|
+
/**
|
|
866
|
+
* Whether `message` is translated for reading, and from which language. Only text a contact or member sent: never
|
|
867
|
+
* mine, a system line, a file, a payment, a status card or a call line. A short message (under `MIN_LETTERS`) is
|
|
868
|
+
* taken to be in the chat's language, when it has one; anything else in what the detector is sure of. The app's
|
|
869
|
+
* language and a language with no model are shown as they are.
|
|
870
|
+
*/
|
|
871
|
+
function translateFrom(message, { appLanguage, detect, canTranslate, chatLanguage }) {
|
|
872
|
+
if (message.sender !== "peer") return NONE;
|
|
873
|
+
if (message.file || message.paymentId || message.card || message.callEvent || message.systemEvent) return NONE;
|
|
874
|
+
const text = detectableText(message.text, message.mentions);
|
|
875
|
+
const letters = text.match(LETTER)?.length ?? 0;
|
|
876
|
+
if (letters === 0) return NONE;
|
|
877
|
+
const usable = (language) => language && language !== UNDETERMINED && language !== appLanguage && canTranslate(language) ? language : null;
|
|
878
|
+
if (letters < 4) return {
|
|
879
|
+
from: usable(chatLanguage && baseCode(chatLanguage)),
|
|
880
|
+
detected: null
|
|
881
|
+
};
|
|
882
|
+
const top = detect(text.slice(0, DETECT_CHARS))[0];
|
|
883
|
+
if (!top || top.probability < .5) return NONE;
|
|
884
|
+
const detected = baseCode(top.code);
|
|
885
|
+
if (detected === UNDETERMINED) return NONE;
|
|
886
|
+
return {
|
|
887
|
+
from: usable(detected),
|
|
888
|
+
detected
|
|
889
|
+
};
|
|
890
|
+
}
|
|
891
|
+
//#endregion
|
|
892
|
+
//#region src/translate.ts
|
|
893
|
+
/**
|
|
894
|
+
* `--translate <lang>` (docs/AUTO-TRANSLATE.md): received messages get `translation: { from, text }`, made on this machine
|
|
895
|
+
* by the translation runtime (native/translation) the Desktop downloads: `bare runtime.mjs <models>`, one JSON line per
|
|
896
|
+
* request. Which messages, and from which language, is the app's rule (translateWhich.ts): only a contact's or a member's
|
|
897
|
+
* text, never mine, and only what the detector is sure of. Nothing is fetched here. A message's text goes to the runtime
|
|
898
|
+
* only, never to stderr: what is said there is the runtime's own word or a path.
|
|
899
|
+
*/
|
|
900
|
+
/** The runtime this CLI starts, in `<home>/translation-runtime/<version>/`; models in `<home>/translation-models/<pair>/`, as on Desktop. */
|
|
901
|
+
var RUNTIME_VERSION = package_default.version;
|
|
902
|
+
function translationPaths(home) {
|
|
903
|
+
return {
|
|
904
|
+
runtime: join(home, "translation-runtime", RUNTIME_VERSION),
|
|
905
|
+
models: join(home, "translation-models")
|
|
906
|
+
};
|
|
907
|
+
}
|
|
908
|
+
/** `--translate`'s language, checked before anything starts: an ISO 639 code, as the runtime's pairs take. */
|
|
909
|
+
function translateTarget(value) {
|
|
910
|
+
if (value === void 0) return void 0;
|
|
911
|
+
if (typeof value !== "string" || !/^[a-z]{2,3}$/.test(value)) throw new CliError("usage", `--translate takes a language code (en, pt, es, ...), not ${JSON.stringify(value)}`);
|
|
912
|
+
return value;
|
|
913
|
+
}
|
|
914
|
+
/**
|
|
915
|
+
* The language detector (`@qvac/langdetect-text`'s `detectMultiple`), which the rule takes as a parameter. It is not a
|
|
916
|
+
* dependency of the CLI yet: until it is, `--translate` says so once and prints messages as they are.
|
|
917
|
+
*/
|
|
918
|
+
async function loadDetector() {
|
|
919
|
+
return null;
|
|
920
|
+
}
|
|
921
|
+
/** `events` and `chat history` with `--translate`: their events or messages one by one, oldest first, as `listen` takes them. */
|
|
922
|
+
async function translateAnswer(answer, translator) {
|
|
923
|
+
const each = async (list, one) => {
|
|
924
|
+
const out = [];
|
|
925
|
+
for (const item of list) out.push(await one(item));
|
|
926
|
+
return out;
|
|
927
|
+
};
|
|
928
|
+
if (Array.isArray(answer.events)) return {
|
|
929
|
+
...answer,
|
|
930
|
+
events: await each(answer.events, translator.event)
|
|
931
|
+
};
|
|
932
|
+
if (Array.isArray(answer.messages)) return {
|
|
933
|
+
...answer,
|
|
934
|
+
messages: await each(answer.messages, translator.message)
|
|
935
|
+
};
|
|
936
|
+
return answer;
|
|
937
|
+
}
|
|
938
|
+
/** A translator, or null when the runtime or the detector is not there: said once, and messages stay as they are. */
|
|
939
|
+
function openTranslator({ home, to, detect, warn }) {
|
|
940
|
+
const { runtime, models } = translationPaths(home);
|
|
941
|
+
const bare = join(runtime, process.platform === "win32" ? "bare.exe" : "bare"), script = join(runtime, "runtime.mjs");
|
|
942
|
+
if (!existsSync(bare) || !existsSync(script)) {
|
|
943
|
+
warn(`ghostly: no translation runtime in ${runtime}: messages are printed untranslated. Unpack runtime ${RUNTIME_VERSION} there and a model in ${join(models, "<from>-<to>")}`);
|
|
944
|
+
return null;
|
|
945
|
+
}
|
|
946
|
+
if (!detect) {
|
|
947
|
+
warn("ghostly: this build of the CLI has no language detector: messages are printed untranslated");
|
|
948
|
+
return null;
|
|
949
|
+
}
|
|
950
|
+
let child = null, next = 0, closed = false, stopped = false;
|
|
951
|
+
const waiting = /* @__PURE__ */ new Map();
|
|
952
|
+
const said = /* @__PURE__ */ new Set();
|
|
953
|
+
const kill = () => child?.kill();
|
|
954
|
+
const start = () => {
|
|
955
|
+
const started = spawn(bare, [script, models], { stdio: [
|
|
956
|
+
"pipe",
|
|
957
|
+
"pipe",
|
|
958
|
+
"ignore"
|
|
959
|
+
] });
|
|
960
|
+
started.stdin.on("error", () => {});
|
|
961
|
+
createInterface({ input: started.stdout }).on("line", (line) => {
|
|
962
|
+
let answer;
|
|
963
|
+
try {
|
|
964
|
+
answer = JSON.parse(line);
|
|
965
|
+
} catch {
|
|
966
|
+
return;
|
|
967
|
+
}
|
|
968
|
+
const id = answer?.id, done = waiting.get(id);
|
|
969
|
+
waiting.delete(id);
|
|
970
|
+
done?.(answer);
|
|
971
|
+
});
|
|
972
|
+
const gone = (why) => {
|
|
973
|
+
if (stopped) return;
|
|
974
|
+
stopped = true;
|
|
975
|
+
child = null;
|
|
976
|
+
if (!closed) warn(`ghostly: the translation runtime stopped (${why}): messages are printed untranslated from here`);
|
|
977
|
+
for (const done of waiting.values()) done(null);
|
|
978
|
+
waiting.clear();
|
|
979
|
+
};
|
|
980
|
+
started.on("error", (error) => gone(error.message));
|
|
981
|
+
started.on("exit", (code, signal) => gone(signal ?? `exit ${code}`));
|
|
982
|
+
process.once("exit", kill);
|
|
983
|
+
return started;
|
|
984
|
+
};
|
|
985
|
+
const ask = (pair, text) => new Promise((resolve) => {
|
|
986
|
+
if (closed || stopped) return resolve(null);
|
|
987
|
+
child ??= start();
|
|
988
|
+
const id = ++next;
|
|
989
|
+
waiting.set(id, (answer) => {
|
|
990
|
+
const texts = answer?.texts;
|
|
991
|
+
if (Array.isArray(texts) && typeof texts[0] === "string") return resolve(texts[0]);
|
|
992
|
+
if (typeof answer?.error === "string" && !said.has(pair)) {
|
|
993
|
+
said.add(pair);
|
|
994
|
+
warn(`ghostly: no translation ${pair}: ${answer.error}`);
|
|
995
|
+
}
|
|
996
|
+
resolve(null);
|
|
997
|
+
});
|
|
998
|
+
child.stdin.write(JSON.stringify({
|
|
999
|
+
id,
|
|
1000
|
+
op: "translate",
|
|
1001
|
+
pair,
|
|
1002
|
+
texts: [text]
|
|
1003
|
+
}) + "\n");
|
|
1004
|
+
});
|
|
1005
|
+
const has = (pair) => existsSync(join(models, pair));
|
|
1006
|
+
const canTranslate = (from) => has(`${from}-${to}`) || from !== "en" && to !== "en" && has(`${from}-en`) && has(`en-${to}`);
|
|
1007
|
+
/** Per chat or group, the last language the others wrote with confidence: a short message is taken to be in it. */
|
|
1008
|
+
const languages = /* @__PURE__ */ new Map();
|
|
1009
|
+
async function message(shown) {
|
|
1010
|
+
if (typeof shown?.text !== "string") return shown;
|
|
1011
|
+
const place = shown.chat ?? "";
|
|
1012
|
+
let which;
|
|
1013
|
+
try {
|
|
1014
|
+
which = translateFrom({
|
|
1015
|
+
sender: shown.from === "peer" ? "peer" : "me",
|
|
1016
|
+
text: shown.text,
|
|
1017
|
+
file: shown.file,
|
|
1018
|
+
paymentId: shown.paymentId,
|
|
1019
|
+
card: shown.card,
|
|
1020
|
+
systemEvent: shown.event,
|
|
1021
|
+
mentions: shown.mentions?.map((m) => ({
|
|
1022
|
+
o: m.offset,
|
|
1023
|
+
l: m.length
|
|
1024
|
+
}))
|
|
1025
|
+
}, {
|
|
1026
|
+
appLanguage: to,
|
|
1027
|
+
detect,
|
|
1028
|
+
canTranslate,
|
|
1029
|
+
chatLanguage: languages.get(place)
|
|
1030
|
+
});
|
|
1031
|
+
} catch {
|
|
1032
|
+
return shown;
|
|
1033
|
+
}
|
|
1034
|
+
const { from, detected } = which;
|
|
1035
|
+
if (detected) languages.set(place, detected);
|
|
1036
|
+
if (!from) return shown;
|
|
1037
|
+
const text = await ask(`${from}-${to}`, shown.text);
|
|
1038
|
+
return text === null || text.trim() === shown.text.trim() ? shown : {
|
|
1039
|
+
...shown,
|
|
1040
|
+
translation: {
|
|
1041
|
+
from,
|
|
1042
|
+
text
|
|
1043
|
+
}
|
|
1044
|
+
};
|
|
1045
|
+
}
|
|
1046
|
+
return {
|
|
1047
|
+
message,
|
|
1048
|
+
async event(event) {
|
|
1049
|
+
const shown = event.message;
|
|
1050
|
+
if (!shown || typeof shown !== "object" || event.type === "chat.joined") return event;
|
|
1051
|
+
const translated = await message(shown);
|
|
1052
|
+
return translated === shown ? event : {
|
|
1053
|
+
...event,
|
|
1054
|
+
message: translated
|
|
1055
|
+
};
|
|
1056
|
+
},
|
|
1057
|
+
close() {
|
|
1058
|
+
closed = true;
|
|
1059
|
+
process.off("exit", kill);
|
|
1060
|
+
child?.stdin.end();
|
|
1061
|
+
}
|
|
1062
|
+
};
|
|
1063
|
+
}
|
|
1064
|
+
//#endregion
|
|
1065
|
+
export { loadDetector, openTranslator, translateAnswer, translateTarget };
|
|
1066
|
+
|
|
1067
|
+
//# sourceMappingURL=translate-BcP139SV.js.map
|