@openlegion/platform 0.0.0-stage → 0.1.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.
Files changed (120) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +1189 -2
  3. package/dist/body.d.ts +9 -0
  4. package/dist/body.js +28 -0
  5. package/dist/chat-history.d.ts +53 -0
  6. package/dist/chat-history.js +11 -0
  7. package/dist/client.d.ts +217 -0
  8. package/dist/client.js +367 -0
  9. package/dist/core.d.ts +47 -0
  10. package/dist/core.js +1 -0
  11. package/dist/crypto.d.ts +25 -0
  12. package/dist/crypto.js +81 -0
  13. package/dist/errors.d.ts +111 -0
  14. package/dist/errors.js +134 -0
  15. package/dist/events.d.ts +131 -0
  16. package/dist/events.js +5 -0
  17. package/dist/generated/api.d.ts +5505 -0
  18. package/dist/generated/api.js +446 -0
  19. package/dist/handler.d.ts +201 -0
  20. package/dist/handler.js +682 -0
  21. package/dist/index.d.ts +24 -0
  22. package/dist/index.js +12 -0
  23. package/dist/kit-agents.d.ts +95 -0
  24. package/dist/kit-agents.js +280 -0
  25. package/dist/kit-codes.d.ts +279 -0
  26. package/dist/kit-codes.js +44 -0
  27. package/dist/kit-connections.d.ts +21 -0
  28. package/dist/kit-connections.js +189 -0
  29. package/dist/kit-files.d.ts +8 -0
  30. package/dist/kit-files.js +117 -0
  31. package/dist/kit-model-keys.d.ts +8 -0
  32. package/dist/kit-model-keys.js +86 -0
  33. package/dist/kit-threads.d.ts +3 -0
  34. package/dist/kit-threads.js +92 -0
  35. package/dist/kit-tools.d.ts +21 -0
  36. package/dist/kit-tools.js +47 -0
  37. package/dist/kit.d.ts +130 -0
  38. package/dist/kit.js +155 -0
  39. package/dist/mcp-server.d.ts +27 -0
  40. package/dist/mcp-server.js +166 -0
  41. package/dist/next.d.ts +24 -0
  42. package/dist/next.js +6 -0
  43. package/dist/ol-signed.d.ts +82 -0
  44. package/dist/ol-signed.js +115 -0
  45. package/dist/react/add-server.d.ts +90 -0
  46. package/dist/react/add-server.js +183 -0
  47. package/dist/react/agent-chat.d.ts +925 -0
  48. package/dist/react/agent-chat.js +1917 -0
  49. package/dist/react/agent-editor.d.ts +133 -0
  50. package/dist/react/agent-editor.js +222 -0
  51. package/dist/react/agent-kit.d.ts +160 -0
  52. package/dist/react/agent-kit.js +279 -0
  53. package/dist/react/agent-list.d.ts +247 -0
  54. package/dist/react/agent-list.js +248 -0
  55. package/dist/react/approval-card.d.ts +272 -0
  56. package/dist/react/approval-card.js +372 -0
  57. package/dist/react/artifact-card.d.ts +38 -0
  58. package/dist/react/artifact-card.js +39 -0
  59. package/dist/react/avatar.d.ts +21 -0
  60. package/dist/react/avatar.js +33 -0
  61. package/dist/react/card-ledger.d.ts +30 -0
  62. package/dist/react/card-ledger.js +49 -0
  63. package/dist/react/chat-timing.d.ts +121 -0
  64. package/dist/react/chat-timing.js +229 -0
  65. package/dist/react/computer-view.d.ts +309 -0
  66. package/dist/react/computer-view.js +543 -0
  67. package/dist/react/computer-viewer.d.ts +175 -0
  68. package/dist/react/computer-viewer.js +555 -0
  69. package/dist/react/computer-wire.d.ts +178 -0
  70. package/dist/react/computer-wire.js +244 -0
  71. package/dist/react/connect-button.d.ts +145 -0
  72. package/dist/react/connect-button.js +212 -0
  73. package/dist/react/connection-request.d.ts +16 -0
  74. package/dist/react/connection-request.js +18 -0
  75. package/dist/react/connections-list.d.ts +90 -0
  76. package/dist/react/connections-list.js +76 -0
  77. package/dist/react/errors.d.ts +250 -0
  78. package/dist/react/errors.js +146 -0
  79. package/dist/react/files.d.ts +41 -0
  80. package/dist/react/files.js +78 -0
  81. package/dist/react/index.d.ts +35 -0
  82. package/dist/react/index.js +26 -0
  83. package/dist/react/limit-words.d.ts +80 -0
  84. package/dist/react/limit-words.js +53 -0
  85. package/dist/react/markdown.d.ts +42 -0
  86. package/dist/react/markdown.js +56 -0
  87. package/dist/react/md-parse.d.ts +106 -0
  88. package/dist/react/md-parse.js +638 -0
  89. package/dist/react/model-keys-card.d.ts +94 -0
  90. package/dist/react/model-keys-card.js +123 -0
  91. package/dist/react/reload.d.ts +211 -0
  92. package/dist/react/reload.js +339 -0
  93. package/dist/react/reveal.d.ts +17 -0
  94. package/dist/react/reveal.js +22 -0
  95. package/dist/react/schedule-editor.d.ts +107 -0
  96. package/dist/react/schedule-editor.js +172 -0
  97. package/dist/react/schedule-time.d.ts +66 -0
  98. package/dist/react/schedule-time.js +90 -0
  99. package/dist/react/styles.css +1274 -0
  100. package/dist/react/transport.d.ts +49 -0
  101. package/dist/react/transport.js +380 -0
  102. package/dist/react/use-connections.d.ts +155 -0
  103. package/dist/react/use-connections.js +231 -0
  104. package/dist/react/use-model-keys.d.ts +47 -0
  105. package/dist/react/use-model-keys.js +57 -0
  106. package/dist/react/use-threads.d.ts +63 -0
  107. package/dist/react/use-threads.js +73 -0
  108. package/dist/react/vault-form.d.ts +92 -0
  109. package/dist/react/vault-form.js +151 -0
  110. package/dist/schema.d.ts +47 -0
  111. package/dist/schema.js +16 -0
  112. package/dist/secrets.d.ts +15 -0
  113. package/dist/secrets.js +18 -0
  114. package/dist/stream.d.ts +129 -0
  115. package/dist/stream.js +192 -0
  116. package/dist/testing.d.ts +33 -0
  117. package/dist/testing.js +40 -0
  118. package/dist/webhooks.d.ts +31 -0
  119. package/dist/webhooks.js +72 -0
  120. package/package.json +85 -3
@@ -0,0 +1,638 @@
1
+ export const MAX_DEPTH = 8;
2
+ export const MAX_DELIMITERS = 1000;
3
+ export const MAX_LINK_TEXT = 1000;
4
+ export const MAX_COLUMNS = 64;
5
+ const SCHEMES = new Set(["http:", "https:", "mailto:"]);
6
+ function hasControlOrSpace(s) {
7
+ for (let i = 0; i < s.length; i++) {
8
+ const code = s.charCodeAt(i);
9
+ if (code <= 0x20 || (code >= 0x7f && code <= 0x9f) || SPACE.test(s[i]))
10
+ return true;
11
+ }
12
+ return false;
13
+ }
14
+ export function safeHref(raw) {
15
+ const s = raw.trim();
16
+ if (s === "" || hasControlOrSpace(s))
17
+ return null;
18
+ if (!/^(?:https?:\/\/|mailto:)/i.test(s))
19
+ return null;
20
+ let url;
21
+ try {
22
+ url = new URL(s);
23
+ }
24
+ catch {
25
+ return null;
26
+ }
27
+ if (!SCHEMES.has(url.protocol))
28
+ return null;
29
+ if (url.protocol !== "mailto:" && url.hostname === "")
30
+ return null;
31
+ return url.href;
32
+ }
33
+ export function linkHostShown(href, words) {
34
+ let url;
35
+ let host;
36
+ try {
37
+ url = new URL(href);
38
+ host = url.protocol === "mailto:" ? (decodeURIComponent(url.pathname).split("@")[1] ?? "").toLowerCase() : url.host.toLowerCase();
39
+ }
40
+ catch {
41
+ return null;
42
+ }
43
+ if (host === "")
44
+ return null;
45
+ const said = words.toLowerCase();
46
+ const bare = host.replace(/^www\./, "");
47
+ return said.includes(bare) ? null : host;
48
+ }
49
+ const FENCE_RE = /^ {0,3}(`{3,}|~{3,})[ \t]*([^`\s]*)[^`]*$/;
50
+ const HEADING_RE = /^ {0,3}(#{1,6})(?:[ \t]+(.*?))?(?:[ \t]+#+)?[ \t]*$/;
51
+ const RULE_RE = /^ {0,3}(?:(?:\*[ \t]*){3,}|(?:-[ \t]*){3,}|(?:_[ \t]*){3,})$/;
52
+ const QUOTE_RE = /^ {0,3}> ?/;
53
+ const LIST_RE = /^( {0,3})([-*+]|\d{1,9}[.)])([ \t]+|$)/;
54
+ const SETEXT_RE = /^ {0,3}(={3,}|-{3,})[ \t]*$/;
55
+ const DELIM_CELL_RE = /^:?-+:?$/;
56
+ const blank = (line) => line.trim() === "";
57
+ const indentOf = (line) => line.length - line.trimStart().length;
58
+ function listMarker(line) {
59
+ const m = LIST_RE.exec(line);
60
+ if (!m)
61
+ return null;
62
+ const marker = m[2];
63
+ const ordered = /\d/.test(marker[0]);
64
+ const empty = line.slice(m[0].length).trim() === "";
65
+ const gap = m[3].length === 0 || m[3].length > 4 || empty ? 1 : m[3].length;
66
+ return { ordered, bullet: ordered ? marker.slice(-1) : marker, start: ordered ? Number.parseInt(marker, 10) : 1, indent: m[1].length, content: m[1].length + marker.length + gap, empty };
67
+ }
68
+ function interrupts(line) {
69
+ if (FENCE_RE.test(line) || HEADING_RE.test(line) || RULE_RE.test(line) || QUOTE_RE.test(line))
70
+ return true;
71
+ const li = listMarker(line);
72
+ return li !== null && !li.empty && (!li.ordered || li.start === 1);
73
+ }
74
+ export function splitRow(line) {
75
+ let s = line.trim();
76
+ if (s.startsWith("|"))
77
+ s = s.slice(1);
78
+ if (s.endsWith("|") && !s.endsWith("\\|"))
79
+ s = s.slice(0, -1);
80
+ const cells = [];
81
+ let cell = "";
82
+ let tick = 0;
83
+ for (let i = 0; i < s.length; i++) {
84
+ const c = s[i];
85
+ if (c === "\\" && s[i + 1] === "|") {
86
+ cell += "\\|";
87
+ i++;
88
+ }
89
+ else if (c === "`") {
90
+ let n = 1;
91
+ while (s[i + n] === "`")
92
+ n++;
93
+ tick = tick === 0 ? n : tick === n ? 0 : tick;
94
+ cell += s.slice(i, i + n);
95
+ i += n - 1;
96
+ }
97
+ else if (c === "|" && tick === 0) {
98
+ cells.push(cell.trim());
99
+ cell = "";
100
+ }
101
+ else
102
+ cell += c;
103
+ }
104
+ cells.push(cell.trim());
105
+ return cells;
106
+ }
107
+ function tableAt(lines, i) {
108
+ const head = lines[i];
109
+ const delim = lines[i + 1];
110
+ if (head === undefined || delim === undefined || !head.includes("|") || !delim.includes("-"))
111
+ return null;
112
+ if (indentOf(head) > 3 || indentOf(delim) > 3)
113
+ return null;
114
+ const cells = splitRow(delim);
115
+ if (cells.length > MAX_COLUMNS || !cells.every((c) => DELIM_CELL_RE.test(c)))
116
+ return null;
117
+ if (cells.length === 1 && !delim.includes("|"))
118
+ return null;
119
+ if (splitRow(head).length !== cells.length)
120
+ return null;
121
+ return cells.map((c) => (c.startsWith(":") && c.endsWith(":") ? "center" : c.endsWith(":") ? "right" : c.startsWith(":") ? "left" : null));
122
+ }
123
+ function leafInline(leaves, src, set) {
124
+ leaves.push({ src, set });
125
+ return inline(src);
126
+ }
127
+ function blocks(lines, depth, leaves) {
128
+ const out = [];
129
+ let para = [];
130
+ const endPara = () => {
131
+ if (para.length > 0) {
132
+ const b = { type: "paragraph", children: [] };
133
+ b.children = leafInline(leaves, para.join("\n"), (c) => (b.children = c));
134
+ out.push(b);
135
+ }
136
+ para = [];
137
+ };
138
+ const containers = depth < MAX_DEPTH;
139
+ let i = 0;
140
+ while (i < lines.length) {
141
+ const line = lines[i];
142
+ if (blank(line)) {
143
+ endPara();
144
+ i++;
145
+ continue;
146
+ }
147
+ if (para.length > 0 && SETEXT_RE.test(line)) {
148
+ const level = line.trim().startsWith("=") ? 1 : 2;
149
+ const b = { type: "heading", level, children: [] };
150
+ b.children = leafInline(leaves, para.join("\n"), (c) => (b.children = c));
151
+ out.push(b);
152
+ para = [];
153
+ i++;
154
+ continue;
155
+ }
156
+ const fence = FENCE_RE.exec(line);
157
+ if (fence) {
158
+ endPara();
159
+ const mark = fence[1];
160
+ const pad = indentOf(line);
161
+ const body = [];
162
+ let j = i + 1;
163
+ for (; j < lines.length; j++) {
164
+ const close = lines[j];
165
+ const shut = close.trim();
166
+ if (indentOf(close) <= 3 && shut.length >= mark.length && (/^`+$/.test(shut) || /^~+$/.test(shut)) && shut[0] === mark[0])
167
+ break;
168
+ body.push(close.slice(Math.min(pad, indentOf(close))));
169
+ }
170
+ out.push({ type: "code", lang: fence[2].slice(0, 40), text: body.join("\n") });
171
+ leaves.push(null);
172
+ i = j + 1;
173
+ continue;
174
+ }
175
+ const heading = HEADING_RE.exec(line);
176
+ if (heading) {
177
+ endPara();
178
+ const b = { type: "heading", level: heading[1].length, children: [] };
179
+ b.children = leafInline(leaves, heading[2] ?? "", (c) => (b.children = c));
180
+ out.push(b);
181
+ i++;
182
+ continue;
183
+ }
184
+ if (RULE_RE.test(line)) {
185
+ endPara();
186
+ out.push({ type: "rule" });
187
+ leaves.push(null);
188
+ i++;
189
+ continue;
190
+ }
191
+ const align = tableAt(lines, i);
192
+ if (align) {
193
+ endPara();
194
+ const head = [];
195
+ splitRow(line).forEach((c, k) => head.push(leafInline(leaves, c, (v) => (head[k] = v))));
196
+ const rows = [];
197
+ let j = i + 2;
198
+ for (; j < lines.length; j++) {
199
+ const row = lines[j];
200
+ if (blank(row) || interrupts(row))
201
+ break;
202
+ const cells = splitRow(row);
203
+ const cellsOut = [];
204
+ align.forEach((_, k) => cellsOut.push(leafInline(leaves, cells[k] ?? "", (v) => (cellsOut[k] = v))));
205
+ rows.push(cellsOut);
206
+ }
207
+ out.push({ type: "table", align, head, rows });
208
+ i = j;
209
+ continue;
210
+ }
211
+ if (containers && QUOTE_RE.test(line)) {
212
+ endPara();
213
+ const inner = [];
214
+ let j = i;
215
+ for (; j < lines.length; j++) {
216
+ const q = lines[j];
217
+ if (QUOTE_RE.test(q))
218
+ inner.push(q.replace(QUOTE_RE, ""));
219
+ else if (!blank(q) && !interrupts(q) && inner.length > 0 && !blank(inner[inner.length - 1]))
220
+ inner.push(q);
221
+ else
222
+ break;
223
+ }
224
+ out.push({ type: "quote", children: blocks(inner, depth + 1, leaves) });
225
+ i = j;
226
+ continue;
227
+ }
228
+ const li = containers ? listMarker(line) : null;
229
+ if (li && (para.length === 0 || interrupts(line))) {
230
+ endPara();
231
+ const items = [];
232
+ let j = i;
233
+ while (j < lines.length) {
234
+ const m = listMarker(lines[j]);
235
+ if (!m || m.ordered !== li.ordered || m.bullet !== li.bullet || m.indent > li.indent + 1 || indentOf(lines[j]) > 3)
236
+ break;
237
+ const content = [lines[j].slice(m.content > lines[j].length ? lines[j].length : m.content)];
238
+ const within = Math.min(m.content, m.indent + 2);
239
+ let k = j + 1;
240
+ for (; k < lines.length; k++) {
241
+ const next = lines[k];
242
+ if (blank(next)) {
243
+ let n = k + 1;
244
+ while (n < lines.length && blank(lines[n]))
245
+ n++;
246
+ if (n < lines.length && indentOf(lines[n]) >= within) {
247
+ for (; k < n; k++)
248
+ content.push("");
249
+ k--;
250
+ continue;
251
+ }
252
+ break;
253
+ }
254
+ if (indentOf(next) >= within)
255
+ content.push(next.slice(Math.min(m.content, indentOf(next))));
256
+ else if (!listMarker(next) && !interrupts(next) && !blank(content[content.length - 1]) && tableAt(lines, k) === null)
257
+ content.push(next.trimStart());
258
+ else
259
+ break;
260
+ }
261
+ items.push(blocks(content, depth + 1, leaves));
262
+ j = k;
263
+ while (j < lines.length && blank(lines[j]) && j + 1 < lines.length && listMarker(lines[j + 1])?.ordered === li.ordered && listMarker(lines[j + 1])?.bullet === li.bullet)
264
+ j++;
265
+ }
266
+ out.push({ type: "list", ordered: li.ordered, start: li.start, items });
267
+ i = j;
268
+ continue;
269
+ }
270
+ para.push(line.trimStart());
271
+ i++;
272
+ }
273
+ endPara();
274
+ return out;
275
+ }
276
+ export function parseMarkdown(text, opts) {
277
+ const lines = text
278
+ .replace(/\r\n?/g, "\n")
279
+ .split("\n")
280
+ .map((l) => (l.includes("\t") ? l.replace(/^\t+/, (t) => " ".repeat(t.length)) : l));
281
+ const leaves = [];
282
+ const out = blocks(lines, 0, leaves);
283
+ const last = leaves[leaves.length - 1];
284
+ if (opts?.ended === true && last)
285
+ last.set(inline(last.src, false, true));
286
+ return out;
287
+ }
288
+ const ASCII_PUNCT = /[!-/:-@[-`{-~]/;
289
+ const PUNCT = /[\p{P}\p{S}]/u;
290
+ const SPACE = /\s/u;
291
+ function bareUrlAt(src, at) {
292
+ if (!src.startsWith("http://", at) && !src.startsWith("https://", at))
293
+ return 0;
294
+ const before = at === 0 ? " " : src[at - 1];
295
+ if (!SPACE.test(before) && !"(*_~\"'".includes(before))
296
+ return 0;
297
+ if (src.startsWith("](", at - 2))
298
+ return 0;
299
+ let end = at;
300
+ while (end < src.length && !SPACE.test(src[end]) && src[end] !== "<" && src[end] !== ">")
301
+ end++;
302
+ for (;;) {
303
+ const last = src[end - 1];
304
+ if (".,:;!?'\"*_~".includes(last))
305
+ end--;
306
+ else if (last === ")") {
307
+ const part = src.slice(at, end);
308
+ if ((part.match(/\)/g)?.length ?? 0) > (part.match(/\(/g)?.length ?? 0))
309
+ end--;
310
+ else
311
+ break;
312
+ }
313
+ else
314
+ break;
315
+ }
316
+ return end - at > (src.startsWith("https://", at) ? 8 : 7) ? end - at : 0;
317
+ }
318
+ function linkAt(src, open) {
319
+ let depth = 0;
320
+ let close = -1;
321
+ const limit = Math.min(src.length, open + MAX_LINK_TEXT);
322
+ for (let j = open + 1; j < limit; j++) {
323
+ const c = src[j];
324
+ if (c === "\\")
325
+ j++;
326
+ else if (c === "[")
327
+ depth++;
328
+ else if (c === "]") {
329
+ if (depth === 0) {
330
+ close = j;
331
+ break;
332
+ }
333
+ depth--;
334
+ }
335
+ }
336
+ if (close < 0 || src[close + 1] !== "(")
337
+ return null;
338
+ let j = close + 2;
339
+ while (src[j] === " " || src[j] === "\t" || src[j] === "\n")
340
+ j++;
341
+ let dest;
342
+ if (src[j] === "<") {
343
+ const end = src.indexOf(">", j);
344
+ if (end < 0)
345
+ return null;
346
+ dest = src.slice(j + 1, end);
347
+ if (/[\n<]/.test(dest))
348
+ return null;
349
+ j = end + 1;
350
+ }
351
+ else {
352
+ let parens = 0;
353
+ const start = j;
354
+ for (; j < src.length; j++) {
355
+ const c = src[j];
356
+ if (c === "\\" && ASCII_PUNCT.test(src[j + 1] ?? ""))
357
+ j++;
358
+ else if (c === "(") {
359
+ if (++parens > 32)
360
+ return null;
361
+ }
362
+ else if (c === ")") {
363
+ if (parens === 0)
364
+ break;
365
+ parens--;
366
+ }
367
+ else if (SPACE.test(c) || c.charCodeAt(0) < 0x20)
368
+ break;
369
+ }
370
+ dest = src.slice(start, j).replace(/\\([!-/:-@[-`{-~])/g, "$1");
371
+ }
372
+ while (src[j] === " " || src[j] === "\t" || src[j] === "\n")
373
+ j++;
374
+ const q = src[j];
375
+ if (q === '"' || q === "'" || q === "(") {
376
+ const shut = q === "(" ? ")" : q;
377
+ const end = src.indexOf(shut, j + 1);
378
+ if (end < 0)
379
+ return null;
380
+ j = end + 1;
381
+ while (src[j] === " " || src[j] === "\t" || src[j] === "\n")
382
+ j++;
383
+ }
384
+ if (src[j] !== ")")
385
+ return null;
386
+ return { textEnd: close, dest, end: j + 1 };
387
+ }
388
+ function plain(nodes) {
389
+ return nodes.map((n) => (n.type === "text" || n.type === "code" ? n.text : n.type === "br" ? " " : plain(n.children))).join("");
390
+ }
391
+ export function inline(src, inLink = false, ended = false) {
392
+ const flat = [];
393
+ let buf = "";
394
+ const flush = () => {
395
+ if (buf !== "")
396
+ flat.push({ kind: "node", node: { type: "text", text: buf } });
397
+ buf = "";
398
+ };
399
+ const node = (n) => {
400
+ flush();
401
+ flat.push({ kind: "node", node: n });
402
+ };
403
+ let nextLinkClose = -2;
404
+ const noCloser = new Set();
405
+ let i = 0;
406
+ while (i < src.length) {
407
+ const c = src[i];
408
+ if (c === "\\") {
409
+ const n = src[i + 1];
410
+ if (n === "\n") {
411
+ node({ type: "br" });
412
+ i += 2;
413
+ continue;
414
+ }
415
+ if (n !== undefined && ASCII_PUNCT.test(n)) {
416
+ buf += n;
417
+ i += 2;
418
+ continue;
419
+ }
420
+ buf += c;
421
+ i++;
422
+ continue;
423
+ }
424
+ if (c === "\n") {
425
+ buf = buf.replace(/[ \t]+$/, "");
426
+ node({ type: "br" });
427
+ i++;
428
+ while (src[i] === " " || src[i] === "\t")
429
+ i++;
430
+ continue;
431
+ }
432
+ if (c === "`") {
433
+ let n = 1;
434
+ while (src[i + n] === "`")
435
+ n++;
436
+ let found = -1;
437
+ if (!noCloser.has(n)) {
438
+ let from = i + n;
439
+ for (;;) {
440
+ const at = src.indexOf("`".repeat(n), from);
441
+ if (at < 0)
442
+ break;
443
+ let run = n;
444
+ while (src[at + run] === "`")
445
+ run++;
446
+ if (run === n && src[at - 1] !== "`") {
447
+ found = at;
448
+ break;
449
+ }
450
+ from = at + run;
451
+ }
452
+ if (found < 0)
453
+ noCloser.add(n);
454
+ }
455
+ if (found >= 0) {
456
+ let code = src.slice(i + n, found).replace(/\n/g, " ");
457
+ if (code.length > 2 && code.startsWith(" ") && code.endsWith(" ") && code.trim() !== "")
458
+ code = code.slice(1, -1);
459
+ node({ type: "code", text: code });
460
+ i = found + n;
461
+ }
462
+ else {
463
+ if (!ended)
464
+ buf += "`".repeat(n);
465
+ i += n;
466
+ }
467
+ continue;
468
+ }
469
+ if (c === "<" && !inLink) {
470
+ const m = /^<((?:https?|mailto):[^\s<>]*)>/i.exec(src.slice(i, i + 2048));
471
+ const href = m ? safeHref(m[1]) : null;
472
+ if (m && href) {
473
+ node({ type: "link", href, children: [{ type: "text", text: m[1] }] });
474
+ i += m[0].length;
475
+ continue;
476
+ }
477
+ buf += c;
478
+ i++;
479
+ continue;
480
+ }
481
+ const image = c === "!" && src[i + 1] === "[";
482
+ if ((c === "[" || image) && !inLink) {
483
+ const open = image ? i + 1 : i;
484
+ if (nextLinkClose !== -1 && nextLinkClose < open)
485
+ nextLinkClose = src.indexOf("](", open);
486
+ const link = nextLinkClose === -1 ? null : linkAt(src, open);
487
+ const href = link ? safeHref(link.dest) : null;
488
+ if (link && href) {
489
+ const words = inline(src.slice(open + 1, link.textEnd), true);
490
+ const children = image ? [{ type: "text", text: plain(words).trim() || href }] : words;
491
+ node({ type: "link", href, children });
492
+ i = link.end;
493
+ continue;
494
+ }
495
+ if (link) {
496
+ buf += src.slice(i, link.end);
497
+ i = link.end;
498
+ continue;
499
+ }
500
+ if (ended) {
501
+ if (src.indexOf("]", open + 1) < 0) {
502
+ i = open + 1;
503
+ continue;
504
+ }
505
+ const mid = src.indexOf("](", open + 1);
506
+ if (mid >= 0 && src.indexOf(")", mid + 2) < 0) {
507
+ for (const n of inline(src.slice(open + 1, mid), true, true))
508
+ node(n);
509
+ i = src.length;
510
+ continue;
511
+ }
512
+ }
513
+ buf += src.slice(i, open + 1);
514
+ i = open + 1;
515
+ continue;
516
+ }
517
+ if (!inLink && (c === "h" || c === "H")) {
518
+ const len = bareUrlAt(src, i);
519
+ const href = len > 0 ? safeHref(src.slice(i, i + len)) : null;
520
+ if (href) {
521
+ node({ type: "link", href, children: [{ type: "text", text: src.slice(i, i + len) }] });
522
+ i += len;
523
+ continue;
524
+ }
525
+ }
526
+ if (c === "*" || c === "_") {
527
+ let n = 1;
528
+ while (src[i + n] === c)
529
+ n++;
530
+ const before = i === 0 ? " " : src[i - 1];
531
+ const after = i + n >= src.length ? " " : src[i + n];
532
+ const left = !SPACE.test(after) && (!PUNCT.test(after) || SPACE.test(before) || PUNCT.test(before));
533
+ const right = !SPACE.test(before) && (!PUNCT.test(before) || SPACE.test(after) || PUNCT.test(after));
534
+ const open = c === "*" ? left : left && (!right || PUNCT.test(before));
535
+ const close = c === "*" ? right : right && (!left || PUNCT.test(after));
536
+ flush();
537
+ flat.push({ kind: "delim", d: { ch: c, at: flat.length, count: n, orig: n, open, close, opens: [], closes: [], active: open || close } });
538
+ i += n;
539
+ continue;
540
+ }
541
+ buf += c;
542
+ i++;
543
+ }
544
+ flush();
545
+ const delims = flat.flatMap((f) => (f.kind === "delim" ? [f.d] : []));
546
+ const matched = delims.length <= MAX_DELIMITERS;
547
+ if (matched)
548
+ matchEmphasis(delims);
549
+ const dropOpeners = ended && matched;
550
+ try {
551
+ return build(flat, dropOpeners);
552
+ }
553
+ catch {
554
+ for (const d of delims) {
555
+ d.opens = [];
556
+ d.closes = [];
557
+ }
558
+ return build(flat, false);
559
+ }
560
+ }
561
+ function matchEmphasis(delims) {
562
+ const bottom = new Map();
563
+ for (let c = 0; c < delims.length; c++) {
564
+ const closer = delims[c];
565
+ if (!closer.close || !closer.active)
566
+ continue;
567
+ while (closer.count > 0) {
568
+ const key = `${closer.ch}${closer.open ? 1 : 0}${closer.orig % 3}`;
569
+ const floor = bottom.get(key) ?? -1;
570
+ let found = -1;
571
+ for (let o = c - 1; o > floor; o--) {
572
+ const opener = delims[o];
573
+ if (!opener.active || opener.count === 0 || !opener.open || opener.ch !== closer.ch)
574
+ continue;
575
+ if ((opener.close || closer.open) && (opener.orig + closer.orig) % 3 === 0 && !(opener.orig % 3 === 0 && closer.orig % 3 === 0))
576
+ continue;
577
+ found = o;
578
+ break;
579
+ }
580
+ if (found < 0) {
581
+ bottom.set(key, c - 1);
582
+ if (!closer.open)
583
+ closer.active = false;
584
+ break;
585
+ }
586
+ const opener = delims[found];
587
+ const use = opener.count >= 2 && closer.count >= 2 ? 2 : 1;
588
+ const kind = use === 2 ? "strong" : "em";
589
+ opener.opens.unshift(kind);
590
+ closer.closes.push(kind);
591
+ opener.count -= use;
592
+ closer.count -= use;
593
+ for (let k = found + 1; k < c; k++)
594
+ delims[k].active = false;
595
+ if (opener.count === 0)
596
+ opener.active = false;
597
+ }
598
+ }
599
+ }
600
+ function build(flat, dropOpeners) {
601
+ const root = [];
602
+ const stack = [];
603
+ const here = () => (stack.length > 0 ? stack[stack.length - 1].children : root);
604
+ const text = (t) => {
605
+ if (t === "")
606
+ return;
607
+ const list = here();
608
+ const last = list[list.length - 1];
609
+ if (last?.type === "text")
610
+ list[list.length - 1] = { type: "text", text: last.text + t };
611
+ else
612
+ list.push({ type: "text", text: t });
613
+ };
614
+ for (const f of flat) {
615
+ if (f.kind === "node") {
616
+ if (f.node.type === "text")
617
+ text(f.node.text);
618
+ else
619
+ here().push(f.node);
620
+ continue;
621
+ }
622
+ const d = f.d;
623
+ const used = (list) => list.reduce((n, k) => n + (k === "strong" ? 2 : 1), 0);
624
+ for (const k of d.closes) {
625
+ const top = stack.pop();
626
+ if (!top || top.kind !== k)
627
+ throw new Error("unbalanced emphasis");
628
+ here().push({ type: k, children: top.children });
629
+ }
630
+ if (!(dropOpeners && d.ch === "*" && d.open && !d.close))
631
+ text(d.ch.repeat(d.orig - used(d.closes) - used(d.opens)));
632
+ for (const k of d.opens)
633
+ stack.push({ kind: k, children: [] });
634
+ }
635
+ if (stack.length > 0)
636
+ throw new Error("unclosed emphasis");
637
+ return root;
638
+ }