@nyte-ai/plugin 0.0.25-beta.1 → 0.0.25-nightly.20261101.40
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/THIRD-PARTY-NOTICES.md +49 -32
- package/dist/chunks/bash-description-0ah30s12.js +33 -0
- package/dist/chunks/bash-description-4h0hnsv4.js +68 -0
- package/dist/chunks/{bash-description-dnvgk30y.js → bash-description-4rfavmha.js} +32 -1
- package/dist/chunks/bash-description-5dpf5hpt.js +724 -0
- package/dist/chunks/{bash-description-yyd9jznf.js → bash-description-94km1hm2.js} +1 -1
- package/dist/chunks/{bash-description-svrhh5a5.js → bash-description-99k8z2r7.js} +3 -2
- package/dist/chunks/bash-description-9gw9ets8.js +162 -0
- package/dist/chunks/{bash-description-h13r0byk.js → bash-description-axnga0jz.js} +3 -3
- package/dist/chunks/bash-description-b78pre8c.js +147 -0
- package/dist/chunks/{bash-description-ye3wk5r3.js → bash-description-bas41ptb.js} +6 -24
- package/dist/chunks/{bash-description-2v3g7rd9.js → bash-description-bj5yh7tr.js} +26 -64
- package/dist/chunks/{bash-description-6z2kgtyt.js → bash-description-bkhr9zrz.js} +4 -4
- package/dist/chunks/{bash-description-hg64tqf1.js → bash-description-f754ge33.js} +49 -3
- package/dist/chunks/{bash-description-zz15raxk.js → bash-description-ff18794t.js} +1 -1
- package/dist/chunks/{bash-description-wmcbg6fw.js → bash-description-g7gfy7nw.js} +1 -1
- package/dist/chunks/{bash-description-1neewp76.js → bash-description-hp4rb8k4.js} +4 -3
- package/dist/chunks/bash-description-myvnmtt2.js +125 -0
- package/dist/chunks/{bash-description-9nn3yp18.js → bash-description-q2v8wbn8.js} +9 -1
- package/dist/chunks/bash-description-qwyy182r.js +452 -0
- package/dist/chunks/bash-description-rxpx004v.js +295 -0
- package/dist/chunks/{bash-description-eqp7ap2w.js → bash-description-z1581a29.js} +23 -9
- package/dist/src/acp/connection.d.ts +3 -1
- package/dist/src/acp/connection.js +3 -3
- package/dist/src/acp/index.d.ts +7 -4
- package/dist/src/acp/index.js +41 -8
- package/dist/src/acp/registry.d.ts +9 -0
- package/dist/src/acp/registry.js +5 -3
- package/dist/src/acp/thread.js +2 -2
- package/dist/src/acp/transcript.js +2 -2
- package/dist/src/acp/translate.d.ts +10 -2
- package/dist/src/acp/translate.js +3 -1
- package/dist/src/approvals/approvals.d.ts +1 -1
- package/dist/src/approvals/approvals.js +2 -2
- package/dist/src/approvals/schema.d.ts +3 -3
- package/dist/src/approvals/schema.js +1 -1
- package/dist/src/browser-tools.js +8 -2
- package/dist/src/canvas/canvas-compile.js +2 -2
- package/dist/src/canvas/canvas-preview.js +2 -2
- package/dist/src/canvas/canvas-protocol.d.ts +46 -0
- package/dist/src/canvas/canvas-protocol.js +11 -1
- package/dist/src/canvas/canvas.d.ts +17 -3
- package/dist/src/canvas/canvas.js +75 -31
- package/dist/src/canvas/schema.d.ts +28 -7
- package/dist/src/canvas/schema.js +2 -2
- package/dist/src/claude-code-sdk/index.d.ts +0 -10
- package/dist/src/claude-code-sdk/index.js +19 -179
- package/dist/src/claude-code-sdk/session.d.ts +22 -23
- package/dist/src/claude-code-sdk/session.js +6 -3
- package/dist/src/claude-code-sdk/transcript.d.ts +0 -6
- package/dist/src/claude-code-sdk/transcript.js +2 -4
- package/dist/src/claude-code-sdk/translate.d.ts +1 -5
- package/dist/src/claude-code-sdk/translate.js +2 -8
- package/dist/src/client/mount.d.ts +4 -2
- package/dist/src/client/mount.js +3 -0
- package/dist/src/client/notifications.d.ts +12 -4
- package/dist/src/client/notifications.js +25 -10
- package/dist/src/client/presentation.d.ts +138 -8
- package/dist/src/client/presentation.js +86 -371
- package/dist/src/codemode.js +151 -46
- package/dist/src/core.d.ts +1 -1
- package/dist/src/question/client/questions-tui.js +3 -3
- package/dist/src/question/client/questions.js +3 -3
- package/dist/src/question/question.js +84 -5
- package/dist/src/question/schema.js +3 -3
- package/dist/src/rename.d.ts +7 -35
- package/dist/src/rename.js +115 -48
- package/dist/src/subagents.d.ts +30 -0
- package/dist/src/subagents.js +323 -0
- package/dist/src/tool-search.d.ts +49 -0
- package/dist/src/tool-search.js +14 -0
- package/dist/src/web/body.d.ts +3 -0
- package/dist/src/web/body.js +6 -0
- package/dist/src/web/fetch.d.ts +16 -0
- package/dist/src/web/fetch.js +8 -0
- package/dist/src/web/html-markdown.d.ts +9 -0
- package/dist/src/web/html-markdown.js +10 -0
- package/dist/src/web/index.d.ts +24 -0
- package/dist/src/web/index.js +93 -0
- package/dist/src/web/providers.d.ts +60 -0
- package/dist/src/web/providers.js +11 -0
- package/dist/src/web/search.d.ts +21 -0
- package/dist/src/web/search.js +6 -0
- package/package.json +16 -7
- package/dist/chunks/bash-description-3j77ss7y.js +0 -86
- package/dist/chunks/bash-description-wbe8w0te.js +0 -530
- package/dist/src/web-search.d.ts +0 -78
- package/dist/src/web-search.js +0 -219
|
@@ -0,0 +1,724 @@
|
|
|
1
|
+
// src/web/html-markdown.ts
|
|
2
|
+
import { Parser } from "htmlparser2";
|
|
3
|
+
var omitted = new Set([
|
|
4
|
+
"script",
|
|
5
|
+
"style",
|
|
6
|
+
"noscript",
|
|
7
|
+
"iframe",
|
|
8
|
+
"object",
|
|
9
|
+
"embed",
|
|
10
|
+
"meta",
|
|
11
|
+
"link",
|
|
12
|
+
"template"
|
|
13
|
+
]);
|
|
14
|
+
var blocks = new Set([
|
|
15
|
+
"address",
|
|
16
|
+
"article",
|
|
17
|
+
"aside",
|
|
18
|
+
"details",
|
|
19
|
+
"dialog",
|
|
20
|
+
"div",
|
|
21
|
+
"dl",
|
|
22
|
+
"fieldset",
|
|
23
|
+
"figcaption",
|
|
24
|
+
"figure",
|
|
25
|
+
"footer",
|
|
26
|
+
"form",
|
|
27
|
+
"header",
|
|
28
|
+
"main",
|
|
29
|
+
"nav",
|
|
30
|
+
"p",
|
|
31
|
+
"section",
|
|
32
|
+
"summary"
|
|
33
|
+
]);
|
|
34
|
+
var MAX_MARKDOWN_BYTES2 = 5 * 1024 * 1024;
|
|
35
|
+
var CONTENT_BYTES = MAX_MARKDOWN_BYTES2 - 64 * 1024;
|
|
36
|
+
var encoder = new TextEncoder;
|
|
37
|
+
function htmlToMarkdown2(html) {
|
|
38
|
+
const output = [];
|
|
39
|
+
const stack = [];
|
|
40
|
+
let pendingSpace = false;
|
|
41
|
+
let pendingIndent = "";
|
|
42
|
+
let last = "";
|
|
43
|
+
let quoteDepth = 0;
|
|
44
|
+
let needsQuotePrefix = false;
|
|
45
|
+
let blockCount = 0;
|
|
46
|
+
let depth = 0;
|
|
47
|
+
let stopped = false;
|
|
48
|
+
let outputBytes = 0;
|
|
49
|
+
let activeCode;
|
|
50
|
+
let activeLink;
|
|
51
|
+
let linkOpen = false;
|
|
52
|
+
let activeMarker;
|
|
53
|
+
let activeList;
|
|
54
|
+
let activeItem;
|
|
55
|
+
let activeTable;
|
|
56
|
+
let activeCell;
|
|
57
|
+
let tableDepth = 0;
|
|
58
|
+
let fallbackSuppressedDepth = 0;
|
|
59
|
+
let fallbackOmittedDepth = 0;
|
|
60
|
+
let activeDetails;
|
|
61
|
+
const append = (value, content = false) => {
|
|
62
|
+
const limit = content ? CONTENT_BYTES : MAX_MARKDOWN_BYTES2;
|
|
63
|
+
if (!value || outputBytes >= limit)
|
|
64
|
+
return;
|
|
65
|
+
const bytes = encoder.encode(value);
|
|
66
|
+
const remaining = limit - outputBytes;
|
|
67
|
+
const next = bytes.byteLength <= remaining ? value : sliceBytes(value, remaining);
|
|
68
|
+
output.push(next);
|
|
69
|
+
outputBytes += bytes.byteLength <= remaining ? bytes.byteLength : encoder.encode(next).byteLength;
|
|
70
|
+
last = next.at(-1) ?? last;
|
|
71
|
+
};
|
|
72
|
+
const appendRaw = (value) => {
|
|
73
|
+
const before = output.length;
|
|
74
|
+
append(value);
|
|
75
|
+
const pushed = output.at(-1);
|
|
76
|
+
if (output.length > before && typeof pushed === "string")
|
|
77
|
+
output[output.length - 1] = { raw: pushed };
|
|
78
|
+
};
|
|
79
|
+
const take = (start) => {
|
|
80
|
+
const chunks = output.splice(start);
|
|
81
|
+
const value = chunks.map((chunk) => typeof chunk === "string" ? chunk : chunk.raw).join("");
|
|
82
|
+
outputBytes -= encoder.encode(value).byteLength;
|
|
83
|
+
return value;
|
|
84
|
+
};
|
|
85
|
+
const quotePrefix = () => "> ".repeat(Math.min(8, quoteDepth));
|
|
86
|
+
const prefixQuote = () => {
|
|
87
|
+
if (!needsQuotePrefix || quoteDepth === 0 || activeCell)
|
|
88
|
+
return;
|
|
89
|
+
append(quotePrefix());
|
|
90
|
+
needsQuotePrefix = false;
|
|
91
|
+
};
|
|
92
|
+
const flushSpace = () => {
|
|
93
|
+
if (!pendingSpace)
|
|
94
|
+
return;
|
|
95
|
+
const marker = activeMarker;
|
|
96
|
+
if (marker && output.length === marker.index + 1 && last !== " " && last !== `
|
|
97
|
+
`) {
|
|
98
|
+
const value = output[marker.index];
|
|
99
|
+
if (typeof value === "string")
|
|
100
|
+
output[marker.index] = ` ${value}`;
|
|
101
|
+
outputBytes++;
|
|
102
|
+
marker.leadingSpace = true;
|
|
103
|
+
pendingSpace = false;
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
if (last && last !== `
|
|
107
|
+
` && last !== " ")
|
|
108
|
+
append(" ");
|
|
109
|
+
pendingSpace = false;
|
|
110
|
+
};
|
|
111
|
+
const inline = (value, open = false) => {
|
|
112
|
+
if (open)
|
|
113
|
+
flushSpace();
|
|
114
|
+
prefixQuote();
|
|
115
|
+
if (pendingIndent) {
|
|
116
|
+
append(pendingIndent);
|
|
117
|
+
pendingIndent = "";
|
|
118
|
+
}
|
|
119
|
+
if (activeLink && !linkOpen) {
|
|
120
|
+
append("[");
|
|
121
|
+
linkOpen = true;
|
|
122
|
+
}
|
|
123
|
+
append(value);
|
|
124
|
+
};
|
|
125
|
+
const block = () => {
|
|
126
|
+
if (activeLink && linkOpen) {
|
|
127
|
+
append(`](${destination(activeLink.href)}${title(activeLink.title)})`);
|
|
128
|
+
linkOpen = false;
|
|
129
|
+
}
|
|
130
|
+
pendingSpace = false;
|
|
131
|
+
append(`
|
|
132
|
+
|
|
133
|
+
`);
|
|
134
|
+
blockCount++;
|
|
135
|
+
needsQuotePrefix = quoteDepth > 0;
|
|
136
|
+
pendingIndent = activeItem?.indent ?? "";
|
|
137
|
+
};
|
|
138
|
+
const suspendLink = (frame) => {
|
|
139
|
+
if (!activeLink)
|
|
140
|
+
return;
|
|
141
|
+
frame.suspendedLink = activeLink;
|
|
142
|
+
block();
|
|
143
|
+
activeLink = undefined;
|
|
144
|
+
linkOpen = false;
|
|
145
|
+
};
|
|
146
|
+
const text = (value) => {
|
|
147
|
+
if (activeCode) {
|
|
148
|
+
activeCode.text += value;
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
for (const part of value.split(/([\t\n\f\r ]+)/)) {
|
|
152
|
+
if (!part)
|
|
153
|
+
continue;
|
|
154
|
+
if (/^[\t\n\f\r ]+$/.test(part)) {
|
|
155
|
+
pendingSpace = true;
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
flushSpace();
|
|
159
|
+
prefixQuote();
|
|
160
|
+
if (pendingIndent) {
|
|
161
|
+
append(pendingIndent);
|
|
162
|
+
pendingIndent = "";
|
|
163
|
+
}
|
|
164
|
+
if (activeLink && !linkOpen) {
|
|
165
|
+
append("[");
|
|
166
|
+
linkOpen = true;
|
|
167
|
+
}
|
|
168
|
+
const escaped = part.replace(/([\\`*_[\]<>|])/g, "\\$1").replace(/~/g, "\\~").replace(/^([#+-])/, "\\$1").replace(/^(\d+)\./, "$1\\.");
|
|
169
|
+
append(escaped, true);
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
const finishCode = (code) => {
|
|
173
|
+
if (code.inline && !code.text)
|
|
174
|
+
return;
|
|
175
|
+
let backticks = 0;
|
|
176
|
+
let tildes = 0;
|
|
177
|
+
let currentBackticks = 0;
|
|
178
|
+
let currentTildes = 0;
|
|
179
|
+
for (const character of code.text) {
|
|
180
|
+
currentBackticks = character === "`" ? currentBackticks + 1 : 0;
|
|
181
|
+
currentTildes = character === "~" ? currentTildes + 1 : 0;
|
|
182
|
+
backticks = Math.max(backticks, currentBackticks);
|
|
183
|
+
tildes = Math.max(tildes, currentTildes);
|
|
184
|
+
}
|
|
185
|
+
if (code.inline) {
|
|
186
|
+
const fence = "`".repeat(Math.max(1, backticks + 1));
|
|
187
|
+
flushSpace();
|
|
188
|
+
prefixQuote();
|
|
189
|
+
const available = Math.max(0, CONTENT_BYTES - outputBytes - fence.length * 2);
|
|
190
|
+
let payload = sliceBytes(code.text, available);
|
|
191
|
+
while (payload) {
|
|
192
|
+
const padding = /^[ `]|[ `]$/.test(payload) && !/^ +$/.test(payload) ? " " : "";
|
|
193
|
+
const bytes = encoder.encode(payload).byteLength;
|
|
194
|
+
if (bytes + padding.length * 2 <= available) {
|
|
195
|
+
appendRaw(`${fence}${padding}${payload}${padding}${fence}`);
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
payload = sliceBytes(payload, bytes - 1);
|
|
199
|
+
}
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
if (activeCell) {
|
|
203
|
+
text(code.text);
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
const marker = backticks <= tildes ? "`" : "~";
|
|
207
|
+
const length = Math.max(3, (marker === "`" ? backticks : tildes) + 1);
|
|
208
|
+
const fence = marker.repeat(length);
|
|
209
|
+
block();
|
|
210
|
+
const prefix = `${fence}${code.language ?? ""}
|
|
211
|
+
`;
|
|
212
|
+
const quote = quoteDepth > 0 ? quotePrefix() : "";
|
|
213
|
+
let payload = code.text;
|
|
214
|
+
for (;; ) {
|
|
215
|
+
const candidate = `${prefix}${payload}${payload.endsWith(`
|
|
216
|
+
`) ? "" : `
|
|
217
|
+
`}${fence}`;
|
|
218
|
+
const value = quote ? candidate.replace(/^/gm, quote) : candidate;
|
|
219
|
+
const valueBytes = encoder.encode(value).byteLength;
|
|
220
|
+
if (outputBytes + valueBytes <= CONTENT_BYTES) {
|
|
221
|
+
appendRaw(value);
|
|
222
|
+
block();
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
if (!payload)
|
|
226
|
+
return;
|
|
227
|
+
const excess = valueBytes - Math.max(0, CONTENT_BYTES - outputBytes);
|
|
228
|
+
payload = sliceBytes(payload, Math.max(0, encoder.encode(payload).byteLength - Math.ceil(excess)));
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
const parser = new Parser({
|
|
232
|
+
onopentag(name, attributes) {
|
|
233
|
+
depth++;
|
|
234
|
+
if (depth > 1e4) {
|
|
235
|
+
if (stack.at(-1)?.suppressed) {
|
|
236
|
+
const visibleParent = stack.findLastIndex((frame) => !frame.suppressed);
|
|
237
|
+
fallbackSuppressedDepth = visibleParent + 2;
|
|
238
|
+
}
|
|
239
|
+
activeCode = undefined;
|
|
240
|
+
stopped = true;
|
|
241
|
+
}
|
|
242
|
+
if (stopped) {
|
|
243
|
+
if (omitted.has(name))
|
|
244
|
+
fallbackOmittedDepth++;
|
|
245
|
+
else
|
|
246
|
+
pendingSpace = true;
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
const suppressed = (stack.at(-1)?.suppressed ?? false) || omitted.has(name);
|
|
250
|
+
const frame = { suppressed };
|
|
251
|
+
const hidden = "hidden" in attributes || attributes["aria-hidden"]?.toLowerCase() === "true" || name === "head";
|
|
252
|
+
const details = activeDetails;
|
|
253
|
+
if (hidden || details && !details.open && !details.summary && name !== "summary")
|
|
254
|
+
frame.suppressed = true;
|
|
255
|
+
stack.push(frame);
|
|
256
|
+
if (frame.suppressed)
|
|
257
|
+
return;
|
|
258
|
+
if (activeCode && !activeCode.inline) {
|
|
259
|
+
if (name === "br")
|
|
260
|
+
activeCode.text += `
|
|
261
|
+
`;
|
|
262
|
+
if (name === "code" && attributes.class)
|
|
263
|
+
activeCode.language = attributes.class.match(/(?:language-|lang-)([^\s]+)/)?.[1];
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
if (name === "details") {
|
|
267
|
+
frame.details = { open: "open" in attributes, summary: false, previous: activeDetails };
|
|
268
|
+
activeDetails = frame.details;
|
|
269
|
+
block();
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
if (name === "summary") {
|
|
273
|
+
if (details)
|
|
274
|
+
details.summary = true;
|
|
275
|
+
block();
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
if (name === "pre") {
|
|
279
|
+
suspendLink(frame);
|
|
280
|
+
frame.code = { inline: false, text: "" };
|
|
281
|
+
activeCode = frame.code;
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
if (name === "code") {
|
|
285
|
+
if (activeCode?.inline)
|
|
286
|
+
return;
|
|
287
|
+
frame.code = { inline: true, text: "" };
|
|
288
|
+
activeCode = frame.code;
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
if (/^h[1-6]$/.test(name)) {
|
|
292
|
+
block();
|
|
293
|
+
inline(`${"#".repeat(Number(name[1]))} `);
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
if (blocks.has(name)) {
|
|
297
|
+
suspendLink(frame);
|
|
298
|
+
if (name === "p" && last === " ")
|
|
299
|
+
return;
|
|
300
|
+
block();
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
if (name === "br") {
|
|
304
|
+
pendingSpace = false;
|
|
305
|
+
inline(`
|
|
306
|
+
`);
|
|
307
|
+
needsQuotePrefix = quoteDepth > 0;
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
if (name === "hr") {
|
|
311
|
+
block();
|
|
312
|
+
inline("---");
|
|
313
|
+
block();
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
const marker = inlineMarker(name);
|
|
317
|
+
if (marker) {
|
|
318
|
+
inline(marker, true);
|
|
319
|
+
frame.marker = { index: output.length - 1, block: blockCount, previous: activeMarker };
|
|
320
|
+
activeMarker = frame.marker;
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
if (name === "a") {
|
|
324
|
+
if (activeLink && activeCode?.inline) {
|
|
325
|
+
const parent = stack.findLast((candidate) => candidate.link === activeLink && candidate.linkCode === activeCode);
|
|
326
|
+
if (parent) {
|
|
327
|
+
finishCode(activeCode);
|
|
328
|
+
if (linkOpen)
|
|
329
|
+
append(`](${destination(activeLink.href)}${title(activeLink.title)})`);
|
|
330
|
+
activeCode = parent.resumedCode;
|
|
331
|
+
parent.link = undefined;
|
|
332
|
+
parent.linkCode = undefined;
|
|
333
|
+
activeLink = undefined;
|
|
334
|
+
linkOpen = false;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
if (activeCode?.inline) {
|
|
338
|
+
frame.resumedCode = activeCode;
|
|
339
|
+
if (activeCode.text)
|
|
340
|
+
finishCode(activeCode);
|
|
341
|
+
activeCode.text = "";
|
|
342
|
+
activeCode = undefined;
|
|
343
|
+
frame.link = { href: attributes.href ?? "", title: attributes.title };
|
|
344
|
+
activeLink = frame.link;
|
|
345
|
+
linkOpen = true;
|
|
346
|
+
inline("[", true);
|
|
347
|
+
frame.linkCode = { inline: true, text: "" };
|
|
348
|
+
activeCode = frame.linkCode;
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
if (activeLink) {
|
|
352
|
+
if (linkOpen)
|
|
353
|
+
append(`](${destination(activeLink.href)}${title(activeLink.title)})`);
|
|
354
|
+
const parent = stack.findLast((candidate) => candidate.link === activeLink);
|
|
355
|
+
if (parent)
|
|
356
|
+
parent.link = undefined;
|
|
357
|
+
activeLink = undefined;
|
|
358
|
+
linkOpen = false;
|
|
359
|
+
}
|
|
360
|
+
frame.link = { href: attributes.href ?? "", title: attributes.title };
|
|
361
|
+
activeLink = frame.link;
|
|
362
|
+
linkOpen = true;
|
|
363
|
+
inline("[", true);
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
if (name === "img") {
|
|
367
|
+
const alt = (attributes.alt ?? "").replace(/([\\\]])/g, "\\$1");
|
|
368
|
+
const close = `](${destination(attributes.src ?? "")}${title(attributes.title)})`;
|
|
369
|
+
const open = "![";
|
|
370
|
+
const available = CONTENT_BYTES - outputBytes - encoder.encode(open + close).byteLength;
|
|
371
|
+
inline(`${open}${sliceBytes(alt, Math.max(0, available))}${close}`, true);
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
if (name === "blockquote") {
|
|
375
|
+
suspendLink(frame);
|
|
376
|
+
block();
|
|
377
|
+
quoteDepth++;
|
|
378
|
+
needsQuotePrefix = true;
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
if (name === "ul" || name === "ol") {
|
|
382
|
+
suspendLink(frame);
|
|
383
|
+
const start = Number.parseInt(attributes.start ?? "1");
|
|
384
|
+
frame.list = {
|
|
385
|
+
ordered: name === "ol",
|
|
386
|
+
next: Number.isNaN(start) ? 1 : start,
|
|
387
|
+
previous: activeList
|
|
388
|
+
};
|
|
389
|
+
activeList = frame.list;
|
|
390
|
+
block();
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
if (name === "li") {
|
|
394
|
+
block();
|
|
395
|
+
const value = Number.parseInt(attributes.value ?? "");
|
|
396
|
+
if (activeList?.ordered && !Number.isNaN(value))
|
|
397
|
+
activeList.next = value;
|
|
398
|
+
const bullet = activeList?.ordered ? `${activeList.next++}.` : "-";
|
|
399
|
+
const prefix = `${(activeItem?.indent ?? "").slice(0, 24)}${bullet} `;
|
|
400
|
+
frame.item = { indent: " ".repeat(prefix.length), previous: activeItem };
|
|
401
|
+
activeItem = frame.item;
|
|
402
|
+
pendingIndent = "";
|
|
403
|
+
inline(prefix);
|
|
404
|
+
return;
|
|
405
|
+
}
|
|
406
|
+
if (name === "table") {
|
|
407
|
+
suspendLink(frame);
|
|
408
|
+
tableDepth++;
|
|
409
|
+
if (tableDepth === 1) {
|
|
410
|
+
block();
|
|
411
|
+
frame.table = { start: output.length, rows: [], fallback: false, previous: activeTable };
|
|
412
|
+
activeTable = frame.table;
|
|
413
|
+
} else
|
|
414
|
+
pendingSpace = true;
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
if (name === "tr") {
|
|
418
|
+
if (tableDepth !== 1) {
|
|
419
|
+
pendingSpace = true;
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
if (activeTable)
|
|
423
|
+
activeTable.row = [];
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
if (name === "th" || name === "td") {
|
|
427
|
+
if (tableDepth !== 1) {
|
|
428
|
+
pendingSpace = true;
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
if ((attributes.colspan || attributes.rowspan) && activeTable)
|
|
432
|
+
activeTable.fallback = true;
|
|
433
|
+
frame.cell = { start: output.length };
|
|
434
|
+
activeCell = frame.cell;
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
if (name === "caption") {
|
|
438
|
+
frame.caption = { start: output.length };
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
if (name === "dt") {
|
|
442
|
+
block();
|
|
443
|
+
inline("**");
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
if (name === "dd")
|
|
447
|
+
inline(`
|
|
448
|
+
: `);
|
|
449
|
+
},
|
|
450
|
+
ontext(value) {
|
|
451
|
+
if (stopped) {
|
|
452
|
+
if (fallbackSuppressedDepth === 0 && fallbackOmittedDepth === 0)
|
|
453
|
+
text(value);
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
if (stack.at(-1)?.suppressed)
|
|
457
|
+
return;
|
|
458
|
+
text(value);
|
|
459
|
+
},
|
|
460
|
+
onclosetag(name) {
|
|
461
|
+
depth--;
|
|
462
|
+
if (stopped) {
|
|
463
|
+
if (fallbackOmittedDepth > 0 && omitted.has(name))
|
|
464
|
+
fallbackOmittedDepth--;
|
|
465
|
+
if (fallbackSuppressedDepth > 0 && depth < fallbackSuppressedDepth)
|
|
466
|
+
fallbackSuppressedDepth = 0;
|
|
467
|
+
return;
|
|
468
|
+
}
|
|
469
|
+
const frame = stack.pop();
|
|
470
|
+
if (!frame || frame.suppressed)
|
|
471
|
+
return;
|
|
472
|
+
if (frame.linkCode) {
|
|
473
|
+
if (activeLink !== frame.link || !linkOpen) {
|
|
474
|
+
if (frame.resumedCode)
|
|
475
|
+
frame.resumedCode.text += frame.linkCode.text;
|
|
476
|
+
activeCode = frame.resumedCode;
|
|
477
|
+
if (activeLink === frame.link)
|
|
478
|
+
activeLink = undefined;
|
|
479
|
+
linkOpen = false;
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
activeCode = undefined;
|
|
483
|
+
finishCode(frame.linkCode);
|
|
484
|
+
if (frame.link && linkOpen)
|
|
485
|
+
append(`](${destination(frame.link.href)}${title(frame.link.title)})`);
|
|
486
|
+
activeLink = undefined;
|
|
487
|
+
linkOpen = false;
|
|
488
|
+
activeCode = frame.resumedCode;
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
if (activeCode && !activeCode.inline && !frame.code)
|
|
492
|
+
return;
|
|
493
|
+
if (frame.code) {
|
|
494
|
+
activeCode = undefined;
|
|
495
|
+
finishCode(frame.code);
|
|
496
|
+
if (frame.suspendedLink)
|
|
497
|
+
activeLink = frame.suspendedLink;
|
|
498
|
+
return;
|
|
499
|
+
}
|
|
500
|
+
if (name === "summary") {
|
|
501
|
+
if (activeDetails)
|
|
502
|
+
activeDetails.summary = false;
|
|
503
|
+
block();
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
if (name === "details") {
|
|
507
|
+
activeDetails = frame.details?.previous;
|
|
508
|
+
block();
|
|
509
|
+
return;
|
|
510
|
+
}
|
|
511
|
+
if (name === "dt") {
|
|
512
|
+
inline("**");
|
|
513
|
+
return;
|
|
514
|
+
}
|
|
515
|
+
if (name === "dd") {
|
|
516
|
+
block();
|
|
517
|
+
return;
|
|
518
|
+
}
|
|
519
|
+
const marker = inlineMarker(name);
|
|
520
|
+
if (marker) {
|
|
521
|
+
const trailingSpace = pendingSpace;
|
|
522
|
+
pendingSpace = false;
|
|
523
|
+
if (frame.marker)
|
|
524
|
+
activeMarker = frame.marker.previous;
|
|
525
|
+
if (frame.marker && (frame.marker.block !== blockCount || output.length === frame.marker.index + 1)) {
|
|
526
|
+
output[frame.marker.index] = "";
|
|
527
|
+
pendingSpace = trailingSpace || frame.marker.leadingSpace === true;
|
|
528
|
+
return;
|
|
529
|
+
}
|
|
530
|
+
inline(marker);
|
|
531
|
+
pendingSpace = trailingSpace || frame.marker?.leadingSpace === true;
|
|
532
|
+
return;
|
|
533
|
+
}
|
|
534
|
+
if (name === "a") {
|
|
535
|
+
if (frame.link && (activeLink === frame.link || !activeLink)) {
|
|
536
|
+
activeLink = frame.link;
|
|
537
|
+
if (linkOpen || last && last !== `
|
|
538
|
+
`)
|
|
539
|
+
append(`](${destination(frame.link.href)}${title(frame.link.title)})`);
|
|
540
|
+
linkOpen = false;
|
|
541
|
+
activeLink = undefined;
|
|
542
|
+
}
|
|
543
|
+
return;
|
|
544
|
+
}
|
|
545
|
+
if (/^h[1-6]$/.test(name) || blocks.has(name)) {
|
|
546
|
+
block();
|
|
547
|
+
if (frame.suspendedLink)
|
|
548
|
+
activeLink = frame.suspendedLink;
|
|
549
|
+
return;
|
|
550
|
+
}
|
|
551
|
+
if (name === "blockquote") {
|
|
552
|
+
quoteDepth--;
|
|
553
|
+
block();
|
|
554
|
+
if (frame.suspendedLink)
|
|
555
|
+
activeLink = frame.suspendedLink;
|
|
556
|
+
return;
|
|
557
|
+
}
|
|
558
|
+
if (name === "li") {
|
|
559
|
+
activeItem = frame.item?.previous;
|
|
560
|
+
block();
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
563
|
+
if (name === "ul" || name === "ol") {
|
|
564
|
+
activeList = frame.list?.previous;
|
|
565
|
+
block();
|
|
566
|
+
if (frame.suspendedLink)
|
|
567
|
+
activeLink = frame.suspendedLink;
|
|
568
|
+
return;
|
|
569
|
+
}
|
|
570
|
+
if ((name === "th" || name === "td") && tableDepth === 1) {
|
|
571
|
+
activeCell = undefined;
|
|
572
|
+
if (frame.cell) {
|
|
573
|
+
const value = take(frame.cell.start).replace(/[\t\r\n ]+/g, " ").trim().replace(/(?<!\\)\|/g, "\\|");
|
|
574
|
+
activeTable?.row?.push(value);
|
|
575
|
+
}
|
|
576
|
+
return;
|
|
577
|
+
}
|
|
578
|
+
if (name === "tr") {
|
|
579
|
+
if (tableDepth !== 1)
|
|
580
|
+
return;
|
|
581
|
+
if (activeTable?.row)
|
|
582
|
+
activeTable.rows.push(activeTable.row);
|
|
583
|
+
if (activeTable)
|
|
584
|
+
activeTable.row = undefined;
|
|
585
|
+
pendingSpace = true;
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
if (name === "caption" && frame.caption && activeTable) {
|
|
589
|
+
activeTable.caption = take(frame.caption.start).replace(/[\t\r\n ]+/g, " ").trim();
|
|
590
|
+
return;
|
|
591
|
+
}
|
|
592
|
+
if (name !== "table")
|
|
593
|
+
return;
|
|
594
|
+
tableDepth--;
|
|
595
|
+
if (tableDepth !== 0) {
|
|
596
|
+
pendingSpace = true;
|
|
597
|
+
return;
|
|
598
|
+
}
|
|
599
|
+
const table = frame.table;
|
|
600
|
+
activeTable = table?.previous;
|
|
601
|
+
if (table) {
|
|
602
|
+
const loose = take(table.start).replace(/[\t\r\n ]+/g, " ").trim();
|
|
603
|
+
const header = table.rows[0];
|
|
604
|
+
const width = header?.length ?? 0;
|
|
605
|
+
const rectangular = width > 0 && table.rows.every((row) => row.length === width);
|
|
606
|
+
if (loose) {
|
|
607
|
+
append(loose);
|
|
608
|
+
block();
|
|
609
|
+
}
|
|
610
|
+
if (table.caption) {
|
|
611
|
+
append(table.caption);
|
|
612
|
+
block();
|
|
613
|
+
}
|
|
614
|
+
if (!table.fallback && rectangular && header !== undefined) {
|
|
615
|
+
const prefix = `${quoteDepth > 0 ? quotePrefix() : ""}${pendingIndent}`;
|
|
616
|
+
pendingIndent = "";
|
|
617
|
+
append(`${prefix}| ${header.join(" | ")} |
|
|
618
|
+
${prefix}|${" --- |".repeat(width)}`);
|
|
619
|
+
for (const row of table.rows.slice(1))
|
|
620
|
+
append(`
|
|
621
|
+
${prefix}| ${row.join(" | ")} |`);
|
|
622
|
+
} else {
|
|
623
|
+
table.rows.forEach((row, index) => {
|
|
624
|
+
if (index > 0)
|
|
625
|
+
block();
|
|
626
|
+
append(row.join(" | "));
|
|
627
|
+
});
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
block();
|
|
631
|
+
if (frame.suspendedLink)
|
|
632
|
+
activeLink = frame.suspendedLink;
|
|
633
|
+
}
|
|
634
|
+
});
|
|
635
|
+
for (let index = 0;index < html.length; index += 64 * 1024)
|
|
636
|
+
parser.write(html.slice(index, index + 64 * 1024));
|
|
637
|
+
parser.end();
|
|
638
|
+
const normalized = [];
|
|
639
|
+
let pendingText = "";
|
|
640
|
+
const flushText = () => {
|
|
641
|
+
if (!pendingText)
|
|
642
|
+
return;
|
|
643
|
+
const lines = pendingText.replace(/[ \t]+\n/g, (space) => space.startsWith(" ") ? `
|
|
644
|
+
` : `
|
|
645
|
+
`).replace(/\n{3,}/g, `
|
|
646
|
+
|
|
647
|
+
`).split(`
|
|
648
|
+
`);
|
|
649
|
+
normalized.push(lines.map((line, index) => {
|
|
650
|
+
if (line)
|
|
651
|
+
return line;
|
|
652
|
+
const before = lines[index - 1]?.match(/^(?:> )+/)?.[0];
|
|
653
|
+
const after = lines[index + 1]?.match(/^(?:> )+/)?.[0];
|
|
654
|
+
if (!before || !after || before.length === after.length)
|
|
655
|
+
return line;
|
|
656
|
+
return "> ".repeat(Math.min(before.length, after.length) / 2).trimEnd();
|
|
657
|
+
}).join(`
|
|
658
|
+
`));
|
|
659
|
+
pendingText = "";
|
|
660
|
+
};
|
|
661
|
+
for (const chunk of output) {
|
|
662
|
+
if (typeof chunk !== "string") {
|
|
663
|
+
flushText();
|
|
664
|
+
normalized.push(chunk.raw);
|
|
665
|
+
continue;
|
|
666
|
+
}
|
|
667
|
+
pendingText += chunk;
|
|
668
|
+
}
|
|
669
|
+
flushText();
|
|
670
|
+
return sliceBytes(normalized.join("").trim(), MAX_MARKDOWN_BYTES2);
|
|
671
|
+
}
|
|
672
|
+
function htmlToText2(html) {
|
|
673
|
+
let text = "";
|
|
674
|
+
let skipDepth = 0;
|
|
675
|
+
const parser = new Parser({
|
|
676
|
+
onopentag(name) {
|
|
677
|
+
if (skipDepth > 0 || ["script", "style", "noscript", "iframe", "object", "embed"].includes(name))
|
|
678
|
+
skipDepth++;
|
|
679
|
+
},
|
|
680
|
+
ontext(input) {
|
|
681
|
+
if (skipDepth === 0)
|
|
682
|
+
text += input;
|
|
683
|
+
},
|
|
684
|
+
onclosetag() {
|
|
685
|
+
if (skipDepth > 0)
|
|
686
|
+
skipDepth--;
|
|
687
|
+
}
|
|
688
|
+
});
|
|
689
|
+
parser.write(html);
|
|
690
|
+
parser.end();
|
|
691
|
+
return text.trim();
|
|
692
|
+
}
|
|
693
|
+
function sliceBytes(value, bytes) {
|
|
694
|
+
if (encoder.encode(value).byteLength <= bytes)
|
|
695
|
+
return value;
|
|
696
|
+
const characters = Array.from(value);
|
|
697
|
+
let low = 0;
|
|
698
|
+
let high = characters.length;
|
|
699
|
+
while (low < high) {
|
|
700
|
+
const middle = Math.ceil((low + high) / 2);
|
|
701
|
+
if (encoder.encode(characters.slice(0, middle).join("")).byteLength <= bytes)
|
|
702
|
+
low = middle;
|
|
703
|
+
else
|
|
704
|
+
high = middle - 1;
|
|
705
|
+
}
|
|
706
|
+
return characters.slice(0, low).join("");
|
|
707
|
+
}
|
|
708
|
+
function destination(value) {
|
|
709
|
+
return value.replace(/([\\()])/g, "\\$1").replace(/[\t\n\r ]+/g, "%20");
|
|
710
|
+
}
|
|
711
|
+
function title(value) {
|
|
712
|
+
return value ? ` "${value.replace(/[\t\n\r ]+/g, " ").trim().replace(/([\\"])/g, "\\$1")}"` : "";
|
|
713
|
+
}
|
|
714
|
+
function inlineMarker(name) {
|
|
715
|
+
if (name === "strong" || name === "b")
|
|
716
|
+
return "**";
|
|
717
|
+
if (name === "em" || name === "i")
|
|
718
|
+
return "*";
|
|
719
|
+
if (name === "s" || name === "strike" || name === "del")
|
|
720
|
+
return "~~";
|
|
721
|
+
return;
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
export { MAX_MARKDOWN_BYTES2, htmlToMarkdown2, htmlToText2 };
|