clikcode 1.0.4 → 1.0.5
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 +25 -14
- package/dist/chunk-23TDEBGZ.js +71 -0
- package/dist/chunk-25T3GVS7.js +425 -0
- package/dist/chunk-2BBELGHY.js +11 -0
- package/dist/chunk-2EOL5PZL.js +208 -0
- package/dist/chunk-2GAB36IE.js +26 -0
- package/dist/chunk-2OLS7V66.js +2440 -0
- package/dist/chunk-2QFO2URB.js +179 -0
- package/dist/chunk-2QWPQRSU.js +254 -0
- package/dist/chunk-2YPAPAR3.js +184 -0
- package/dist/chunk-2ZAG5372.js +33 -0
- package/dist/chunk-32IKAIZC.js +12507 -0
- package/dist/chunk-34AZEGYW.js +95 -0
- package/dist/chunk-34JQYQCA.js +175 -0
- package/dist/chunk-34ZISPAO.js +67 -0
- package/dist/chunk-3AAAARE6.js +46 -0
- package/dist/chunk-3AJ6Q3OY.js +510 -0
- package/dist/chunk-3B2CHX7M.js +884 -0
- package/dist/chunk-3BUFORRX.js +59 -0
- package/dist/chunk-3QLHYYFK.js +30 -0
- package/dist/chunk-3RXBMYKZ.js +140 -0
- package/dist/chunk-3TFNIJWS.js +52 -0
- package/dist/chunk-3W3IPDFP.js +119 -0
- package/dist/chunk-3WMS7SEE.js +417 -0
- package/dist/chunk-435ZNSZ3.js +23 -0
- package/dist/chunk-4AQ5NINV.js +39 -0
- package/dist/chunk-4GVEJ5FK.js +168 -0
- package/dist/chunk-4GXAO3UY.js +262 -0
- package/dist/chunk-4L2ZZM4Q.js +332 -0
- package/dist/chunk-4LPDG57B.js +17 -0
- package/dist/chunk-4NN3XQQT.js +223 -0
- package/dist/chunk-4QZEZME7.js +54 -0
- package/dist/chunk-4R6CSC3S.js +38 -0
- package/dist/chunk-4RHXTL3W.js +514 -0
- package/dist/chunk-4SJMPTVQ.js +18 -0
- package/dist/chunk-4VY7G6TX.js +114 -0
- package/dist/chunk-5642PBF7.js +124 -0
- package/dist/chunk-5B6TVO4A.js +33 -0
- package/dist/chunk-5BJLDUPL.js +62 -0
- package/dist/chunk-5HMFFJWU.js +19 -0
- package/dist/chunk-5IKLK33T.js +69 -0
- package/dist/chunk-5NQ5FFMX.js +44 -0
- package/dist/chunk-5QNNW5L6.js +176 -0
- package/dist/chunk-5SM5UAWN.js +239 -0
- package/dist/chunk-5WYJTA6O.js +74 -0
- package/dist/chunk-5ZSR2Y5T.js +227 -0
- package/dist/chunk-5ZWUM5GH.js +39 -0
- package/dist/chunk-64E7D4HY.js +364 -0
- package/dist/chunk-6CK466BC.js +99 -0
- package/dist/chunk-6ES45HHO.js +138 -0
- package/dist/chunk-6HYEZFGL.js +277 -0
- package/dist/chunk-6Y7HLXGW.js +29 -0
- package/dist/chunk-6YKK4FYR.js +258 -0
- package/dist/chunk-74WNMLMS.js +117 -0
- package/dist/chunk-7AULZ7OD.js +55 -0
- package/dist/chunk-7DOKGSUN.js +101 -0
- package/dist/chunk-7IH5LTIZ.js +195 -0
- package/dist/chunk-7ISTS5NS.js +21 -0
- package/dist/chunk-7NMUTGOW.js +527 -0
- package/dist/chunk-7PNHUZEL.js +153 -0
- package/dist/chunk-7TSQLONI.js +88 -0
- package/dist/chunk-AE7TOLD5.js +30 -0
- package/dist/chunk-AEMHSXKA.js +130 -0
- package/dist/chunk-AKRYTS2D.js +211 -0
- package/dist/chunk-AMNSE36L.js +59 -0
- package/dist/chunk-AZVD74ZV.js +23 -0
- package/dist/chunk-B35DVOTK.js +1981 -0
- package/dist/chunk-B4HNJRAH.js +626 -0
- package/dist/chunk-B5WYPPHS.js +13 -0
- package/dist/chunk-BBOVDXQM.js +59 -0
- package/dist/chunk-BD3ECZLK.js +62 -0
- package/dist/chunk-BDGSOT74.js +4578 -0
- package/dist/chunk-BF5VEOPK.js +31 -0
- package/dist/chunk-BFFRW4XH.js +47 -0
- package/dist/chunk-BFPC6PEW.js +208 -0
- package/dist/chunk-BGEMY2JJ.js +376 -0
- package/dist/chunk-BISGNHKD.js +35 -0
- package/dist/chunk-BMRAEBRY.js +21 -0
- package/dist/chunk-BNTXAZXW.js +127 -0
- package/dist/chunk-BOQBXGSN.js +75 -0
- package/dist/chunk-BTJDSTQP.js +213 -0
- package/dist/chunk-BUOPJQP5.js +218 -0
- package/dist/chunk-BXFJS65Q.js +52 -0
- package/dist/chunk-BXPQFSX5.js +59 -0
- package/dist/chunk-C57LIWNY.js +1269 -0
- package/dist/chunk-C6YVCCWR.js +74 -0
- package/dist/chunk-CAADDF6M.js +167 -0
- package/dist/chunk-CBANJFBO.js +69 -0
- package/dist/chunk-CERF3RIB.js +597 -0
- package/dist/chunk-COAS5ETR.js +46 -0
- package/dist/chunk-CQ7J6UQ7.js +78 -0
- package/dist/chunk-CRUVYGO2.js +26 -0
- package/dist/chunk-CVFVFRGN.js +58 -0
- package/dist/chunk-D2UK66HM.js +37 -0
- package/dist/chunk-D2W7SA4R.js +18 -0
- package/dist/chunk-DA44ZENT.js +501 -0
- package/dist/chunk-DAJ76TOC.js +99 -0
- package/dist/chunk-DCZ6CIUM.js +147 -0
- package/dist/chunk-DF35KMXM.js +1314 -0
- package/dist/chunk-DGASQT26.js +151 -0
- package/dist/chunk-DJT57C5P.js +55 -0
- package/dist/chunk-DKDPZTD3.js +44 -0
- package/dist/chunk-DQYVQ2N7.js +77 -0
- package/dist/chunk-DRTP2RTY.js +266 -0
- package/dist/chunk-DW2BMUKY.js +61 -0
- package/dist/chunk-DW5T5ZVU.js +307 -0
- package/dist/chunk-DXKNDNFC.js +164 -0
- package/dist/chunk-DZEQ7UYR.js +291 -0
- package/dist/chunk-E4F4OUAC.js +463 -0
- package/dist/chunk-EE4B6A7W.js +69 -0
- package/dist/chunk-EISKIYWG.js +23 -0
- package/dist/chunk-EKAQK7UQ.js +235 -0
- package/dist/chunk-EOBTJV32.js +123 -0
- package/dist/chunk-ESF3DIZY.js +325 -0
- package/dist/chunk-EVOHOZ4U.js +45 -0
- package/dist/chunk-EXN4KHLB.js +110 -0
- package/dist/chunk-F252T6Y3.js +78 -0
- package/dist/chunk-F3DP4ZA2.js +40 -0
- package/dist/chunk-F6F47QAY.js +58 -0
- package/dist/chunk-FA3Y23ZW.js +57 -0
- package/dist/chunk-FBKXE4EJ.js +640 -0
- package/dist/chunk-FCGEB3H6.js +23 -0
- package/dist/chunk-FF7ZVYJF.js +3732 -0
- package/dist/chunk-FFGKN6L3.js +39 -0
- package/dist/chunk-FFGU4GCC.js +94 -0
- package/dist/chunk-FSFDAOVP.js +74 -0
- package/dist/chunk-FT5TX7OX.js +1573 -0
- package/dist/chunk-G3QP67Z7.js +232 -0
- package/dist/chunk-G5ORJKV2.js +179 -0
- package/dist/chunk-GFNC62PE.js +111 -0
- package/dist/chunk-GGK43Q3N.js +67 -0
- package/dist/chunk-GGYKY4GF.js +11537 -0
- package/dist/chunk-GGYNHGNJ.js +55 -0
- package/dist/chunk-GLY7RM3C.js +354 -0
- package/dist/chunk-GQBZU6EO.js +20 -0
- package/dist/chunk-GSXENUZL.js +9 -0
- package/dist/chunk-GTOAT5D3.js +40 -0
- package/dist/chunk-GWCMDPQU.js +1007 -0
- package/dist/chunk-GXYSMYHJ.js +152 -0
- package/dist/chunk-GYMAI5NY.js +14 -0
- package/dist/chunk-H2VZMKPE.js +30 -0
- package/dist/chunk-H4THUNBM.js +2091 -0
- package/dist/chunk-HGRVSNUZ.js +361 -0
- package/dist/chunk-HI4VY473.js +27 -0
- package/dist/chunk-HOJRSD3Q.js +447 -0
- package/dist/chunk-HRX25W35.js +82 -0
- package/dist/chunk-HSE4AQYV.js +55 -0
- package/dist/chunk-HU5SSUFX.js +383 -0
- package/dist/chunk-I3GDBJLH.js +196 -0
- package/dist/chunk-I75EDU75.js +684 -0
- package/dist/chunk-IBA7DMIO.js +20 -0
- package/dist/chunk-IC6N4NZA.js +231 -0
- package/dist/chunk-IF43HIGV.js +284 -0
- package/dist/chunk-IGZ6C2MC.js +439 -0
- package/dist/chunk-IMLTL2MY.js +192 -0
- package/dist/chunk-IMZL6SGR.js +33 -0
- package/dist/chunk-IS4USJPY.js +516 -0
- package/dist/chunk-ISFSPN2F.js +82 -0
- package/dist/chunk-IXE7GTFI.js +55 -0
- package/dist/chunk-IZ77QJQG.js +36 -0
- package/dist/chunk-J2PACWRQ.js +34 -0
- package/dist/chunk-J6EGFVQX.js +180 -0
- package/dist/chunk-JEKOD43D.js +22 -0
- package/dist/chunk-JGIWATSG.js +30 -0
- package/dist/chunk-JYBONKD4.js +43 -0
- package/dist/chunk-K2NLG2HK.js +454 -0
- package/dist/chunk-KCQ4TBLS.js +164 -0
- package/dist/chunk-KD5KIVMS.js +176 -0
- package/dist/chunk-KHW6TX6I.js +63 -0
- package/dist/chunk-KI5GZFCE.js +520 -0
- package/dist/chunk-KN34B7NO.js +105 -0
- package/dist/chunk-KYUHIFP3.js +136 -0
- package/dist/chunk-KZL5VS4B.js +300 -0
- package/dist/chunk-L3VW3QIH.js +48 -0
- package/dist/chunk-LA6MG4ET.js +2110 -0
- package/dist/chunk-LAVFOA3S.js +66 -0
- package/dist/chunk-LB4OA77N.js +13 -0
- package/dist/chunk-LBFRF2K4.js +196 -0
- package/dist/chunk-LM6EIA4B.js +93 -0
- package/dist/chunk-LPCNRGJ7.js +692 -0
- package/dist/chunk-LSLNL5T3.js +168 -0
- package/dist/chunk-LVRRWP4W.js +568 -0
- package/dist/chunk-LWCFBRHL.js +33 -0
- package/dist/chunk-LXFTMGZZ.js +166 -0
- package/dist/chunk-M33LVA4M.js +151 -0
- package/dist/chunk-M3FO5UMK.js +4944 -0
- package/dist/chunk-M5QNDBAY.js +66 -0
- package/dist/chunk-M6ND6KBE.js +1291 -0
- package/dist/chunk-M7H3WQTE.js +76 -0
- package/dist/chunk-MCBRVPA7.js +191 -0
- package/dist/chunk-MCSDWZET.js +91 -0
- package/dist/chunk-MDT5XDXL.js +289 -0
- package/dist/chunk-MJG3IQY4.js +91 -0
- package/dist/chunk-MOU3N2UL.js +44 -0
- package/dist/chunk-MP4KZOQC.js +3451 -0
- package/dist/chunk-MPAJ446F.js +66 -0
- package/dist/chunk-MVJZZ6AL.js +16 -0
- package/dist/chunk-MXS6S2PY.js +253 -0
- package/dist/chunk-N3SFDYM7.js +291 -0
- package/dist/chunk-NAOX3JUB.js +13 -0
- package/dist/chunk-NEKKIBCX.js +283 -0
- package/dist/chunk-NKDATMEM.js +133 -0
- package/dist/chunk-NR4FMC4K.js +395 -0
- package/dist/chunk-NSOHOJBR.js +221 -0
- package/dist/chunk-NTDB6QKF.js +29 -0
- package/dist/chunk-NWEYRBWK.js +25 -0
- package/dist/chunk-O2YSNYWD.js +23 -0
- package/dist/chunk-O3EW7LAY.js +54 -0
- package/dist/chunk-O7BNJIXU.js +366 -0
- package/dist/chunk-OARL2NXZ.js +116 -0
- package/dist/chunk-OHKZUEJW.js +351 -0
- package/dist/chunk-OKU45BXA.js +647 -0
- package/dist/chunk-OL4JBD3J.js +59 -0
- package/dist/chunk-OLUO5DDR.js +192 -0
- package/dist/chunk-OQ245JLA.js +196 -0
- package/dist/chunk-P72UZAQI.js +289 -0
- package/dist/chunk-PAL2ENB6.js +20 -0
- package/dist/chunk-PAXYQZH7.js +53 -0
- package/dist/chunk-PDPEM5VW.js +51 -0
- package/dist/chunk-PEWCDI2C.js +668 -0
- package/dist/chunk-PGZ54EDX.js +27 -0
- package/dist/chunk-PRWVLDZF.js +73 -0
- package/dist/chunk-PSQQ75VJ.js +52 -0
- package/dist/chunk-PUOVIFHW.js +118 -0
- package/dist/chunk-PWBF2X3V.js +107 -0
- package/dist/chunk-PWXII6HF.js +43 -0
- package/dist/chunk-Q35VLJRT.js +80 -0
- package/dist/chunk-QJP7EYGL.js +128 -0
- package/dist/chunk-QPQKX2MK.js +8836 -0
- package/dist/chunk-QQRLPY25.js +1053 -0
- package/dist/chunk-QUS4ER2W.js +407 -0
- package/dist/chunk-QYHI3YET.js +29 -0
- package/dist/chunk-QYRU2UQI.js +32 -0
- package/dist/chunk-R6XVVQGC.js +13 -0
- package/dist/chunk-R7QN6ONS.js +483 -0
- package/dist/chunk-RD23MPGC.js +221 -0
- package/dist/chunk-RDDWQBEM.js +185 -0
- package/dist/chunk-RDLLDYFO.js +104 -0
- package/dist/chunk-RE4TGORR.js +84 -0
- package/dist/chunk-RJLHVYQY.js +217 -0
- package/dist/chunk-RJLPTWHW.js +905 -0
- package/dist/chunk-RKUVLRZI.js +295 -0
- package/dist/chunk-ROGXPGYR.js +37 -0
- package/dist/chunk-RSZGEL5U.js +163 -0
- package/dist/chunk-RVUOIJXN.js +909 -0
- package/dist/chunk-RW7F4JRP.js +11 -0
- package/dist/chunk-S4UO3AO3.js +53 -0
- package/dist/chunk-S5JAAF6S.js +585 -0
- package/dist/chunk-SNU37TQI.js +56 -0
- package/dist/chunk-SQVUDO2O.js +31 -0
- package/dist/chunk-SYVHIYQT.js +66 -0
- package/dist/chunk-SZBQMTG2.js +85 -0
- package/dist/chunk-T5VCXA3J.js +25 -0
- package/dist/chunk-TK6NSP56.js +30 -0
- package/dist/chunk-TKWVMA6L.js +22 -0
- package/dist/chunk-TQDKJHWX.js +59 -0
- package/dist/chunk-TQDM7S5N.js +127 -0
- package/dist/chunk-TULAXS77.js +23 -0
- package/dist/chunk-TVT45XNH.js +4625 -0
- package/dist/chunk-TWCVV2JO.js +467 -0
- package/dist/chunk-U4DKAD6S.js +47 -0
- package/dist/chunk-UA5K2BTI.js +564 -0
- package/dist/chunk-UAMKCMOK.js +40 -0
- package/dist/chunk-UBMVADBF.js +51 -0
- package/dist/chunk-UFQ5A5PW.js +645 -0
- package/dist/chunk-UOD2SOLL.js +1301 -0
- package/dist/chunk-UTSB4LOW.js +156 -0
- package/dist/chunk-UVXNIUTN.js +412 -0
- package/dist/chunk-UXVPXYPE.js +90 -0
- package/dist/chunk-UYEEMIWR.js +374 -0
- package/dist/chunk-VBGM5ZBW.js +54 -0
- package/dist/chunk-VEWPCZPG.js +762 -0
- package/dist/chunk-VIRDLZ5E.js +108 -0
- package/dist/chunk-VMPO7WKN.js +52 -0
- package/dist/chunk-W2NVSDO5.js +5122 -0
- package/dist/chunk-W3YYPBEA.js +176 -0
- package/dist/chunk-W5AYOICN.js +280 -0
- package/dist/chunk-W5GZOIUF.js +27 -0
- package/dist/chunk-WHKNLZ3C.js +76 -0
- package/dist/chunk-WKBEPPQ5.js +1409 -0
- package/dist/chunk-WM3Z6QKQ.js +151 -0
- package/dist/chunk-WMI2TPMA.js +31 -0
- package/dist/chunk-WNX5VD7S.js +150 -0
- package/dist/chunk-WOVTYST3.js +60 -0
- package/dist/chunk-WPB5WEZJ.js +546 -0
- package/dist/chunk-WUO5EOAT.js +44 -0
- package/dist/chunk-WWLJMP6D.js +133 -0
- package/dist/chunk-WZMLH3XP.js +58 -0
- package/dist/chunk-WZYVLUVQ.js +79 -0
- package/dist/chunk-X2OCPVU5.js +135 -0
- package/dist/chunk-X3CL6DTR.js +95 -0
- package/dist/chunk-X6WF7JEV.js +354 -0
- package/dist/chunk-XAHGBPWT.js +44 -0
- package/dist/chunk-XCKQ5BM2.js +228 -0
- package/dist/chunk-XIBYREEY.js +106 -0
- package/dist/chunk-XKW5NDMR.js +378 -0
- package/dist/chunk-XRDALAO3.js +100 -0
- package/dist/chunk-XUP22EAS.js +41 -0
- package/dist/chunk-XWAC3MKW.js +243 -0
- package/dist/chunk-Y3R7JMXB.js +59 -0
- package/dist/chunk-Y54LTP4W.js +82 -0
- package/dist/chunk-Y7UECI4M.js +99 -0
- package/dist/chunk-YACGDFHC.js +47 -0
- package/dist/chunk-YHNIQPKY.js +71 -0
- package/dist/chunk-YMBR6HYP.js +111 -0
- package/dist/chunk-YMCH3L2S.js +26 -0
- package/dist/chunk-Z4ZU6T7V.js +99 -0
- package/dist/chunk-ZDPF67ZZ.js +173 -0
- package/dist/chunk-ZFC2Q67K.js +101 -0
- package/dist/chunk-ZGHNNSCT.js +114 -0
- package/dist/chunk-ZGX3ERAT.js +173 -0
- package/dist/chunk-ZI3YHEBY.js +37 -0
- package/dist/chunk-ZN7G6JLS.js +639 -0
- package/dist/chunk-ZNPFQMXT.js +47 -0
- package/dist/chunk-ZOU3LV4U.js +63 -0
- package/dist/chunk-ZT7B4H2V.js +117 -0
- package/dist/chunk-ZZCBDNZI.js +58 -0
- package/dist/cli.js +494 -112923
- package/dist/computer-tools.js +36642 -21259
- package/dist/harness-catalog.cjs +88 -43
- package/dist/index.js +1 -1
- package/dist/local-model-supervisor.cjs +555 -0
- package/package.json +3 -4
|
@@ -0,0 +1,3732 @@
|
|
|
1
|
+
import { createRequire as __clikcodeCreateRequire } from 'node:module';
|
|
2
|
+
const require = __clikcodeCreateRequire(import.meta.url);
|
|
3
|
+
import {
|
|
4
|
+
readDenyReason,
|
|
5
|
+
resolvePath
|
|
6
|
+
} from "./chunk-IMLTL2MY.js";
|
|
7
|
+
import {
|
|
8
|
+
conversationIdFor,
|
|
9
|
+
describeCommands
|
|
10
|
+
} from "./chunk-BUOPJQP5.js";
|
|
11
|
+
import {
|
|
12
|
+
activitiesBetween,
|
|
13
|
+
asFileDiffs,
|
|
14
|
+
marksBetween,
|
|
15
|
+
readTurnActivities,
|
|
16
|
+
recordTurnActivity,
|
|
17
|
+
runningTurnActivity,
|
|
18
|
+
stopOpenActivities
|
|
19
|
+
} from "./chunk-3B2CHX7M.js";
|
|
20
|
+
import {
|
|
21
|
+
FAILURE_TEXT_LIMIT
|
|
22
|
+
} from "./chunk-CQ7J6UQ7.js";
|
|
23
|
+
import {
|
|
24
|
+
SHIMMER_WIDTH,
|
|
25
|
+
appendThought,
|
|
26
|
+
endLineShown,
|
|
27
|
+
orderMarks
|
|
28
|
+
} from "./chunk-XKW5NDMR.js";
|
|
29
|
+
import {
|
|
30
|
+
isAgentToolName
|
|
31
|
+
} from "./chunk-W3YYPBEA.js";
|
|
32
|
+
import {
|
|
33
|
+
formatElapsed
|
|
34
|
+
} from "./chunk-EE4B6A7W.js";
|
|
35
|
+
import {
|
|
36
|
+
SAFE_LINK,
|
|
37
|
+
expandTabs
|
|
38
|
+
} from "./chunk-4QZEZME7.js";
|
|
39
|
+
import {
|
|
40
|
+
source_default
|
|
41
|
+
} from "./chunk-LVRRWP4W.js";
|
|
42
|
+
import {
|
|
43
|
+
HYPERLINK_CLOSE,
|
|
44
|
+
closeOpenHyperlink,
|
|
45
|
+
displayTokens,
|
|
46
|
+
hyperlinkOpen,
|
|
47
|
+
linksOn,
|
|
48
|
+
stripAnsi,
|
|
49
|
+
terminalCellWidth,
|
|
50
|
+
visibleSlice
|
|
51
|
+
} from "./chunk-G3QP67Z7.js";
|
|
52
|
+
|
|
53
|
+
// src/session/title.ts
|
|
54
|
+
var SESSION_TITLE_MAX = 20;
|
|
55
|
+
function normalizeSessionTitle(raw) {
|
|
56
|
+
const text = raw.replace(/\s+/g, " ").replace(/^["'`]|["'`]$/g, "").replace(/[.。]+$/, "").trim();
|
|
57
|
+
if (!text) return void 0;
|
|
58
|
+
if (text.length <= SESSION_TITLE_MAX) return text;
|
|
59
|
+
const cut = text.slice(0, SESSION_TITLE_MAX);
|
|
60
|
+
const space = cut.lastIndexOf(" ");
|
|
61
|
+
return (space >= SESSION_TITLE_MAX / 2 ? cut.slice(0, space) : cut).trimEnd();
|
|
62
|
+
}
|
|
63
|
+
function titleFromFirstMessage(message) {
|
|
64
|
+
return normalizeSessionTitle(message);
|
|
65
|
+
}
|
|
66
|
+
var LEGACY_OPEN = "<clikcode-title>";
|
|
67
|
+
var LEGACY_CLOSE = "</clikcode-title>";
|
|
68
|
+
function carriesTitleRequest(text) {
|
|
69
|
+
return new RegExp(`\\n\\n${LEGACY_OPEN}[\\s\\S]*${LEGACY_CLOSE}\\s*$`).test(text);
|
|
70
|
+
}
|
|
71
|
+
function stripRepeatedTitles(text) {
|
|
72
|
+
return text.replace(new RegExp(`(^|\\n)[ \\t]*${LEGACY_OPEN}[\\s\\S]*?${LEGACY_CLOSE}[ \\t]*\\r?\\n?`, "g"), "$1").replace(/^\n+/, "");
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// src/turn/resource-note-text.ts
|
|
76
|
+
var OPEN = "<clikcode_resource_note>";
|
|
77
|
+
var CLOSE = "</clikcode_resource_note>";
|
|
78
|
+
var LEADING_NOTES = /^(?:\s*<clikcode_resource_note>[\s\S]*?<\/clikcode_resource_note>)+\s*/;
|
|
79
|
+
function wrapResourceNote(text) {
|
|
80
|
+
return `${OPEN}
|
|
81
|
+
${text.trim()}
|
|
82
|
+
${CLOSE}`;
|
|
83
|
+
}
|
|
84
|
+
function stripResourceNotes(content) {
|
|
85
|
+
return content.replace(LEADING_NOTES, "");
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// src/turn/failover-prompt.ts
|
|
89
|
+
var FAILOVER_PREAMBLE = "Earlier in this chat (carried over from another account, provider or machine):";
|
|
90
|
+
var EARLIER_PREAMBLES = [
|
|
91
|
+
"The conversation below took place earlier in this chat; you are continuing it. Treat it as what you already know, answer only the current request, and do not mention this note or that the chat moved.",
|
|
92
|
+
"Continue the same ClikCode conversation after an account or provider failover. Preserve all prior decisions, files, and task state. Do not repeat completed work."
|
|
93
|
+
];
|
|
94
|
+
var FRAME_TAGS = "message|conversation|current_request|touched_files|requests|tool_digest|attachments|open_todos";
|
|
95
|
+
var ESCAPE = new RegExp(`<(\\/?)(${FRAME_TAGS})\\b`, "gi");
|
|
96
|
+
var UNESCAPE = new RegExp(`<(\\/?)(${FRAME_TAGS})\\b`, "gi");
|
|
97
|
+
function escapeFailoverContent(text) {
|
|
98
|
+
return text.replace(ESCAPE, "<$1$2");
|
|
99
|
+
}
|
|
100
|
+
function unescapeFailoverContent(text) {
|
|
101
|
+
return text.replace(UNESCAPE, "<$1$2");
|
|
102
|
+
}
|
|
103
|
+
function failoverPromptRequest(text) {
|
|
104
|
+
const framed = (value) => [FAILOVER_PREAMBLE, ...EARLIER_PREAMBLES].some((preamble) => value.startsWith(preamble));
|
|
105
|
+
if (!framed(text)) return void 0;
|
|
106
|
+
let current = text;
|
|
107
|
+
while (framed(current)) {
|
|
108
|
+
const request = /<current_request>\n([\s\S]*?)\n<\/current_request>\s*$/.exec(current)?.[1];
|
|
109
|
+
const next = unescapeFailoverContent(request ?? "").trim();
|
|
110
|
+
if (next.length >= current.length) return "";
|
|
111
|
+
current = next;
|
|
112
|
+
}
|
|
113
|
+
return current;
|
|
114
|
+
}
|
|
115
|
+
var LEGACY_INTERRUPTED_TURN_REQUEST = "Continue the interrupted latest request. Inspect the current workspace first and finish the remaining work without repeating completed steps.";
|
|
116
|
+
var providerBoundaryNote = (label) => `[ClikCode: the following turns ran on ${label}]`;
|
|
117
|
+
var PROVIDER_BOUNDARY_NOTE = /^\[ClikCode: the following turns ran on [^\n]*\](?:\s*\n|\s*$)\s*/;
|
|
118
|
+
function isClikCodeOpening(text) {
|
|
119
|
+
const opening = text.replace(/^[\s"']+/, "");
|
|
120
|
+
return CLIKCODE_OPENINGS.some((start) => opening.startsWith(start)) || carriesTitleRequest(text);
|
|
121
|
+
}
|
|
122
|
+
var CLIKCODE_OPENINGS = [...[FAILOVER_PREAMBLE, ...EARLIER_PREAMBLES].map((preamble) => preamble.slice(0, 40)), providerBoundaryNote("").slice(0, -1)];
|
|
123
|
+
function normalizeImportedTranscript(messages) {
|
|
124
|
+
const normalized = [];
|
|
125
|
+
for (const original of messages) {
|
|
126
|
+
if (original.role !== "user") {
|
|
127
|
+
if (original.role === "assistant" && /^Background work finished: (?:failed )?tool\.$/.test(original.content.trim())) continue;
|
|
128
|
+
normalized.push(original);
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
if (/^<task-notification\b[^>]*>[\s\S]*?<\/task-notification>\s*$/.test(original.content.trim())) continue;
|
|
132
|
+
const content = stripResourceNotes(original.content.replace(PROVIDER_BOUNDARY_NOTE, ""));
|
|
133
|
+
if (content !== original.content && !content.trim()) continue;
|
|
134
|
+
const message = content === original.content ? original : { ...original, content };
|
|
135
|
+
if (message.content.trim() === LEGACY_INTERRUPTED_TURN_REQUEST) continue;
|
|
136
|
+
const request = failoverPromptRequest(message.content);
|
|
137
|
+
if (request === void 0) normalized.push(message);
|
|
138
|
+
else if (request && request !== LEGACY_INTERRUPTED_TURN_REQUEST) normalized.push({ ...message, content: request });
|
|
139
|
+
}
|
|
140
|
+
return normalized;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// src/harness/protocol/wording.ts
|
|
144
|
+
var ACTIONS = {
|
|
145
|
+
/** End the running turn. Ctrl+C, the stop button, and the palette's
|
|
146
|
+
* command. Esc does too, once the turn has an answer. Before that, Esc
|
|
147
|
+
* puts the prompt back in the composer. */
|
|
148
|
+
stop: { verb: "stop", key: "Ctrl+C" },
|
|
149
|
+
/** Esc, when it is what stops the turn (nothing waiting to take back, not
|
|
150
|
+
* reading back): the waiting line says so, as Claude Code's and Codex's
|
|
151
|
+
* "esc to interrupt" do. Before an answer the prompt comes back too. */
|
|
152
|
+
escStop: { verb: "stop", key: "Esc" },
|
|
153
|
+
/** Mid-turn, a message waiting: take the newest back into the composer to
|
|
154
|
+
* edit. Nothing running is touched. */
|
|
155
|
+
takeBack: { verb: "edit", key: "Esc" },
|
|
156
|
+
/** Mid-turn, nothing typed, a message already waiting: put it into the
|
|
157
|
+
* chat at the next pause. The turn keeps running, and so do its
|
|
158
|
+
* sub-agents. Enter AGAIN -- the first Enter is the one that queued it. */
|
|
159
|
+
sendNow: { verb: "send into the chat", key: "Enter again" },
|
|
160
|
+
/** Send what is typed: steered into the turn or queued behind it (/send). */
|
|
161
|
+
send: { verb: "send", key: "Enter" },
|
|
162
|
+
/** Take the highlighted command from the palette. */
|
|
163
|
+
apply: { verb: "apply", key: "Enter" }
|
|
164
|
+
};
|
|
165
|
+
var STOPPED = "Stopped";
|
|
166
|
+
function keyHint(action) {
|
|
167
|
+
const { verb, key } = ACTIONS[action];
|
|
168
|
+
return keyHintFor(key, verb);
|
|
169
|
+
}
|
|
170
|
+
function keyHintFor(key, verb) {
|
|
171
|
+
return `${key.toLowerCase()} ${verb}`;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// src/harness/protocol/turn-flow.ts
|
|
175
|
+
var TENSES = {
|
|
176
|
+
Read: ["Reading", "Read"],
|
|
177
|
+
Edit: ["Editing", "Edited"],
|
|
178
|
+
Write: ["Writing", "Wrote"],
|
|
179
|
+
Grep: ["Searching", "Searched"],
|
|
180
|
+
Search: ["Searching", "Searched"],
|
|
181
|
+
Glob: ["Finding", "Found"],
|
|
182
|
+
List: ["Listing", "Listed"],
|
|
183
|
+
Fetch: ["Fetching", "Fetched"],
|
|
184
|
+
"Web search": ["Searching the web", "Searched the web"]
|
|
185
|
+
};
|
|
186
|
+
function tensedLabel(label, running) {
|
|
187
|
+
if (label.startsWith("$ ") || label === "$") return label;
|
|
188
|
+
for (const [verb, [ing, ed]] of Object.entries(TENSES)) {
|
|
189
|
+
if (label === verb || label.startsWith(`${verb} `)) return `${running ? ing : ed}${label.slice(verb.length)}`;
|
|
190
|
+
}
|
|
191
|
+
if (label.includes(" \u203A ")) return `${running ? "Calling" : "Called"} ${label}`;
|
|
192
|
+
return label;
|
|
193
|
+
}
|
|
194
|
+
var plural = (count, one, many = `${one}s`) => `${count} ${count === 1 ? one : many}`;
|
|
195
|
+
function activityResult(event) {
|
|
196
|
+
if (event.kind !== "tool-done") return void 0;
|
|
197
|
+
const lines = (event.output?.filter((line) => line.trim()).length ?? 0) + (event.outputOmitted ?? 0);
|
|
198
|
+
if (!lines) return void 0;
|
|
199
|
+
if (event.category === "read") return plural(lines, "line");
|
|
200
|
+
if (event.category !== "search") return void 0;
|
|
201
|
+
if (/^(Glob|List)\b/.test(event.label)) return plural(lines, /^List\b/.test(event.label) ? "entry" : "file", /^List\b/.test(event.label) ? "entries" : "files");
|
|
202
|
+
return plural(lines, "match", "matches");
|
|
203
|
+
}
|
|
204
|
+
function exploreSummary(events) {
|
|
205
|
+
const running = events.some((event) => event.kind === "tool-start");
|
|
206
|
+
let reads = 0;
|
|
207
|
+
let searches = 0;
|
|
208
|
+
let lists = 0;
|
|
209
|
+
for (const event of events) {
|
|
210
|
+
if (event.category === "read") reads += 1;
|
|
211
|
+
else if (/^List\b/.test(event.label)) lists += 1;
|
|
212
|
+
else searches += 1;
|
|
213
|
+
}
|
|
214
|
+
const parts = [
|
|
215
|
+
...reads ? [`${running ? "reading" : "read"} ${plural(reads, "file")}`] : [],
|
|
216
|
+
...searches ? [`${running ? "searching" : "searched"} ${plural(searches, "pattern")}`] : [],
|
|
217
|
+
...lists ? [`${running ? "listing" : "listed"} ${plural(lists, "directory", "directories")}`] : []
|
|
218
|
+
].join(", ");
|
|
219
|
+
return parts.charAt(0).toUpperCase() + parts.slice(1);
|
|
220
|
+
}
|
|
221
|
+
function reasoningHeading(thought) {
|
|
222
|
+
const heading2 = thought ? /\*\*([^*\n]{3,80})\*\*/.exec(thought)?.[1]?.trim() : void 0;
|
|
223
|
+
return heading2 || void 0;
|
|
224
|
+
}
|
|
225
|
+
function reasoningBody(thought) {
|
|
226
|
+
return thought.replace(/\*\*([^*\n]{3,80})\*\*/, "").replace(/^\s*\*\*[^*\n]*$/, "").replace(/\*\*|__/g, "").replace(/\s+/g, " ").trim();
|
|
227
|
+
}
|
|
228
|
+
function answerWriting(state) {
|
|
229
|
+
return state.writingAt !== void 0 && (state.callAt === void 0 || state.writingAt > state.callAt);
|
|
230
|
+
}
|
|
231
|
+
var oneLine = (text, max) => {
|
|
232
|
+
const flat = text.replace(/\s+/g, " ").trim();
|
|
233
|
+
return flat.length > max ? `${flat.slice(0, max - 1)}\u2026` : flat;
|
|
234
|
+
};
|
|
235
|
+
function thoughtRow(thought) {
|
|
236
|
+
const heading2 = thought.heading?.replace(/\s+/g, " ").trim();
|
|
237
|
+
return `Thought for ${formatElapsed(Math.max(1e3, thought.durationMs))}${heading2 ? ` \xB7 ${heading2}` : ""}`;
|
|
238
|
+
}
|
|
239
|
+
function decisionRow(decision) {
|
|
240
|
+
switch (decision.kind) {
|
|
241
|
+
case "approved":
|
|
242
|
+
return { text: `\u2713 approved${decision.always ? " (always)" : ""} ${oneLine(decision.label, 80)}`, tone: "ok" };
|
|
243
|
+
case "denied":
|
|
244
|
+
return { text: `\u2717 denied ${oneLine(decision.label, 80)}`, tone: "no" };
|
|
245
|
+
case "told":
|
|
246
|
+
return { text: `\u21B3 told: ${oneLine(decision.text, 120)}`, tone: "told" };
|
|
247
|
+
case "answered": {
|
|
248
|
+
const values = decision.values?.length ? `: ${oneLine(decision.values.join(", "), 80)}` : "";
|
|
249
|
+
return { text: `\u2713 answered ${oneLine(decision.label, 60)}${values}`, tone: "ok" };
|
|
250
|
+
}
|
|
251
|
+
case "declined":
|
|
252
|
+
return { text: `\u2717 declined ${oneLine(decision.label, 80)}`, tone: "no" };
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
function isApprovalWaitPhase(phase) {
|
|
256
|
+
return Boolean(phase && /^waiting for (?:your |the )?(?:approval|answer|permission|input)\b/i.test(phase.trim()));
|
|
257
|
+
}
|
|
258
|
+
function turnStatus(state) {
|
|
259
|
+
if (state.asking) return { label: "waiting for you", tone: "asking" };
|
|
260
|
+
if (state.toolPhase) return { label: state.toolPhase, tone: "tool" };
|
|
261
|
+
const heading2 = reasoningHeading(state.thought);
|
|
262
|
+
const phase = state.phase?.replace(/(…|\.\.\.)$/, "").trim();
|
|
263
|
+
const thinking = !phase || /^thinking$/i.test(phase);
|
|
264
|
+
const working = /^working$/i.test(phase ?? "");
|
|
265
|
+
if (state.waitingOn && !heading2 && !state.writing && (thinking || /^background work$/i.test(phase ?? ""))) return { label: state.waitingOn, tone: "thinking" };
|
|
266
|
+
const label = heading2 ?? (working ? "Working" : !thinking ? phase : state.writing ? "writing" : "Working");
|
|
267
|
+
return { label, tone: "thinking" };
|
|
268
|
+
}
|
|
269
|
+
function shimmerLevels(length, frame, width = SHIMMER_WIDTH) {
|
|
270
|
+
const span = length + width * 2;
|
|
271
|
+
const centre = frame % span - width;
|
|
272
|
+
return Array.from({ length }, (_, index) => {
|
|
273
|
+
const distance = Math.abs(index - centre) / width;
|
|
274
|
+
return distance >= 1 ? 0 : (Math.cos(distance * Math.PI) + 1) / 2;
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
function pastePlaceholder(text, index) {
|
|
278
|
+
const lines = text.replace(/\r\n/g, "\n").replace(/\n+$/, "").split("\n").length;
|
|
279
|
+
if (lines <= 3 && text.length <= 800) return void 0;
|
|
280
|
+
return `[Pasted text #${index}${lines > 1 ? ` +${lines} lines` : ""}]`;
|
|
281
|
+
}
|
|
282
|
+
function endsWithSummary(ms, filesChanged) {
|
|
283
|
+
return endLineShown(ms, filesChanged);
|
|
284
|
+
}
|
|
285
|
+
function turnSummary(state) {
|
|
286
|
+
const files = /* @__PURE__ */ new Map();
|
|
287
|
+
for (const diff of state.diffs ?? []) {
|
|
288
|
+
for (const file of diff) {
|
|
289
|
+
const key = file.path ?? `#${files.size}`;
|
|
290
|
+
const prior = files.get(key) ?? { additions: 0, removals: 0 };
|
|
291
|
+
files.set(key, { additions: prior.additions + file.additions, removals: prior.removals + file.removals });
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
const additions = [...files.values()].reduce((sum, file) => sum + file.additions, 0);
|
|
295
|
+
const removals = [...files.values()].reduce((sum, file) => sum + file.removals, 0);
|
|
296
|
+
const changed = files.size ? ` \xB7 ${plural(files.size, "file")} changed${additions ? ` +${additions}` : ""}${removals ? ` \u2212${removals}` : ""}` : "";
|
|
297
|
+
const spent = state.usage ? ` \xB7 ${state.usage}` : "";
|
|
298
|
+
const count = (value) => value && value > 0 ? Math.floor(value) : 0;
|
|
299
|
+
const agents = count(state.pendingBackground);
|
|
300
|
+
const workflows = count(state.pendingWorkflows);
|
|
301
|
+
if (agents || workflows) {
|
|
302
|
+
const waiting = [...agents ? [plural(agents, "background agent")] : [], ...workflows ? [plural(workflows, "dynamic workflow")] : []].join(" and ");
|
|
303
|
+
return `Waiting for ${waiting}${changed}${spent}`;
|
|
304
|
+
}
|
|
305
|
+
if (state.stopped) return STOPPED;
|
|
306
|
+
return `Worked for ${formatElapsed(Math.max(1e3, state.ms))}${changed}${spent}`;
|
|
307
|
+
}
|
|
308
|
+
function backgroundTerminalsLabel(count) {
|
|
309
|
+
const terminals = count && count > 0 ? Math.floor(count) : 0;
|
|
310
|
+
return terminals ? `${plural(terminals, "background terminal")} running` : void 0;
|
|
311
|
+
}
|
|
312
|
+
function waitingOnCommands(commands, now = Date.now()) {
|
|
313
|
+
return commands?.length ? describeCommands(commands, now) : void 0;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// src/turn/turn-marks.ts
|
|
317
|
+
var THOUGHT_MARK_CHARS = 2e4;
|
|
318
|
+
var HEADING_CHARS = 120;
|
|
319
|
+
var LABEL_CHARS = 200;
|
|
320
|
+
var NOTICE_CHARS = FAILURE_TEXT_LIMIT + 8;
|
|
321
|
+
var PLAN_ENTRIES = 200;
|
|
322
|
+
var PLAN_ENTRY_CHARS = 2e3;
|
|
323
|
+
var MAX_TURN_MARKS = 2e3;
|
|
324
|
+
var clip = (text, limit) => text.length > limit ? `${text.slice(0, limit - 1)}\u2026` : text;
|
|
325
|
+
var MarkSequencer = class {
|
|
326
|
+
counter = 0;
|
|
327
|
+
byId = /* @__PURE__ */ new Map();
|
|
328
|
+
openLabels = /* @__PURE__ */ new Map();
|
|
329
|
+
anonymousThought;
|
|
330
|
+
/** The newest number given. */
|
|
331
|
+
peek() {
|
|
332
|
+
return this.counter;
|
|
333
|
+
}
|
|
334
|
+
/** Never hand out a number at or below `value` again. */
|
|
335
|
+
raiseTo(value) {
|
|
336
|
+
if (value > this.counter) this.counter = value;
|
|
337
|
+
}
|
|
338
|
+
/** A number for a row that has no frames (a steer, a plan, a notice). */
|
|
339
|
+
take() {
|
|
340
|
+
this.anonymousThought = void 0;
|
|
341
|
+
return ++this.counter;
|
|
342
|
+
}
|
|
343
|
+
remember(key) {
|
|
344
|
+
let seq = this.byId.get(key);
|
|
345
|
+
if (seq === void 0) {
|
|
346
|
+
seq = ++this.counter;
|
|
347
|
+
this.byId.set(key, seq);
|
|
348
|
+
}
|
|
349
|
+
return seq;
|
|
350
|
+
}
|
|
351
|
+
/** The number of the row this frame belongs to. */
|
|
352
|
+
seqFor(event) {
|
|
353
|
+
if (event.seq !== void 0) {
|
|
354
|
+
this.raiseTo(event.seq);
|
|
355
|
+
return event.seq;
|
|
356
|
+
}
|
|
357
|
+
if (event.kind === "thinking") {
|
|
358
|
+
if (event.id) return this.remember(`thought:${event.id}`);
|
|
359
|
+
this.anonymousThought ??= ++this.counter;
|
|
360
|
+
return this.anonymousThought;
|
|
361
|
+
}
|
|
362
|
+
this.anonymousThought = void 0;
|
|
363
|
+
if (event.id) return this.remember(`call:${event.id}`);
|
|
364
|
+
if (event.kind === "tool-start") {
|
|
365
|
+
const open = this.openLabels.get(event.label);
|
|
366
|
+
if (open !== void 0) return open;
|
|
367
|
+
const seq2 = ++this.counter;
|
|
368
|
+
this.openLabels.set(event.label, seq2);
|
|
369
|
+
return seq2;
|
|
370
|
+
}
|
|
371
|
+
const key = this.openLabels.has(event.label) ? event.label : event.label === "tool" ? [...this.openLabels.keys()].pop() : void 0;
|
|
372
|
+
if (key === void 0) return ++this.counter;
|
|
373
|
+
const seq = this.openLabels.get(key);
|
|
374
|
+
this.openLabels.delete(key);
|
|
375
|
+
return seq;
|
|
376
|
+
}
|
|
377
|
+
/** `event` with its `seq` set (in place when it can be: the next one to
|
|
378
|
+
* see the same object -- the screen, after the journal -- then has it). */
|
|
379
|
+
stamp(event) {
|
|
380
|
+
if (event.seq !== void 0) {
|
|
381
|
+
this.raiseTo(event.seq);
|
|
382
|
+
return event;
|
|
383
|
+
}
|
|
384
|
+
const seq = this.seqFor(event);
|
|
385
|
+
if (Object.isFrozen(event)) return { ...event, seq };
|
|
386
|
+
event.seq = seq;
|
|
387
|
+
return event;
|
|
388
|
+
}
|
|
389
|
+
};
|
|
390
|
+
var thoughtMarkId = (id, seq) => `thought:${id ?? `#${seq}`}`;
|
|
391
|
+
function settledThought(open, now) {
|
|
392
|
+
const heading2 = open.heading ?? reasoningHeading(open.text);
|
|
393
|
+
return {
|
|
394
|
+
kind: "thought",
|
|
395
|
+
id: thoughtMarkId(open.id, open.seq),
|
|
396
|
+
seq: open.seq,
|
|
397
|
+
responseOffset: open.responseOffset,
|
|
398
|
+
durationMs: Math.max(0, now - open.startedAtMs),
|
|
399
|
+
...heading2 ? { heading: clip(heading2, HEADING_CHARS) } : {},
|
|
400
|
+
...open.text.trim() ? { text: clip(open.text, THOUGHT_MARK_CHARS) } : {}
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
function stepThought(prior, settled, event, at) {
|
|
404
|
+
if (event.id) {
|
|
405
|
+
const done = settled.find((mark) => mark.id === thoughtMarkId(event.id, event.seq));
|
|
406
|
+
if (done?.kind === "thought") {
|
|
407
|
+
const text = event.label.replace(/\s+/g, " ").trim();
|
|
408
|
+
if (!text || /^thinking$/i.test(text) || text.startsWith("\u2026") || text.length <= (done.text?.length ?? 0)) return { thought: prior, unchanged: true };
|
|
409
|
+
const heading3 = reasoningHeading(text) ?? done.heading;
|
|
410
|
+
return { thought: prior, mark: { ...done, text: clip(text, THOUGHT_MARK_CHARS), ...heading3 ? { heading: clip(heading3, HEADING_CHARS) } : {} } };
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
const was = prior ? { ...prior.id === void 0 ? {} : { id: prior.id }, text: prior.text } : void 0;
|
|
414
|
+
const next = appendThought(was, event.label, event.id);
|
|
415
|
+
if (next === was) return { thought: prior, unchanged: true };
|
|
416
|
+
const sameItem = prior !== void 0 && prior.id === next.id;
|
|
417
|
+
const settledPrior = prior && !sameItem ? settledThought(prior, at.now) : void 0;
|
|
418
|
+
const base = sameItem ? prior : { ...next.id === void 0 ? {} : { id: next.id }, text: "", seq: event.seq, responseOffset: at.offset, startedAtMs: at.now };
|
|
419
|
+
const heading2 = reasoningHeading(next.text) ?? base.heading;
|
|
420
|
+
return {
|
|
421
|
+
thought: { ...base, text: next.text, ...heading2 ? { heading: heading2 } : {} },
|
|
422
|
+
...settledPrior ? { mark: settledPrior } : {}
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
function boundMark(mark) {
|
|
426
|
+
switch (mark.kind) {
|
|
427
|
+
case "thought":
|
|
428
|
+
return { ...mark, ...mark.heading ? { heading: clip(mark.heading, HEADING_CHARS) } : {}, ...mark.text ? { text: clip(mark.text, THOUGHT_MARK_CHARS) } : {} };
|
|
429
|
+
case "notice":
|
|
430
|
+
return { ...mark, message: clip(stripAnsi(mark.message).trim(), NOTICE_CHARS) };
|
|
431
|
+
case "outcome":
|
|
432
|
+
return { ...mark, label: clip(stripAnsi(mark.label).replace(/\s+/g, " ").trim(), LABEL_CHARS) };
|
|
433
|
+
case "plan":
|
|
434
|
+
return {
|
|
435
|
+
...mark,
|
|
436
|
+
entries: mark.entries.slice(0, PLAN_ENTRIES).map((entry) => ({
|
|
437
|
+
content: clip(entry.content, PLAN_ENTRY_CHARS),
|
|
438
|
+
...entry.status ? { status: entry.status } : {},
|
|
439
|
+
...entry.priority ? { priority: entry.priority } : {}
|
|
440
|
+
}))
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
function upsertMark(marks, incoming) {
|
|
445
|
+
const mark = boundMark(incoming);
|
|
446
|
+
const index = marks.findIndex((existing) => existing.id === mark.id);
|
|
447
|
+
const next = [...marks];
|
|
448
|
+
if (index >= 0) next[index] = { ...mark, seq: marks[index].seq, responseOffset: marks[index].responseOffset };
|
|
449
|
+
else next.push(mark);
|
|
450
|
+
while (next.length > MAX_TURN_MARKS) {
|
|
451
|
+
const drop = next.findIndex((existing) => existing.kind !== "plan");
|
|
452
|
+
next.splice(drop < 0 ? 0 : drop, 1);
|
|
453
|
+
}
|
|
454
|
+
return next;
|
|
455
|
+
}
|
|
456
|
+
function readTurnMarks(value) {
|
|
457
|
+
if (!Array.isArray(value)) return [];
|
|
458
|
+
const kept = value.filter((item) => {
|
|
459
|
+
const mark = item;
|
|
460
|
+
return typeof mark === "object" && mark !== null && typeof mark.id === "string" && typeof mark.seq === "number" && Number.isFinite(mark.seq) && typeof mark.responseOffset === "number" && Number.isFinite(mark.responseOffset) && mark.responseOffset >= 0 && (mark.kind === "thought" ? typeof mark.durationMs === "number" : mark.kind === "plan" ? Array.isArray(mark.entries) : mark.kind === "notice" ? typeof mark.message === "string" : mark.kind === "outcome" && typeof mark.label === "string" && typeof mark.outcome === "string");
|
|
461
|
+
});
|
|
462
|
+
return kept.length === value.length ? value : kept;
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
// src/tui/render/wrap.ts
|
|
466
|
+
function wrapCodeLine(value, width) {
|
|
467
|
+
const safeWidth = Math.max(1, width);
|
|
468
|
+
if (!value) return [""];
|
|
469
|
+
const rows = [];
|
|
470
|
+
const { heads, rest } = breakToWidth(expandTabs(value), safeWidth);
|
|
471
|
+
rows.push(...heads, rest);
|
|
472
|
+
return rows;
|
|
473
|
+
}
|
|
474
|
+
function breakToWidth(text, width) {
|
|
475
|
+
const tokens = displayTokens(text);
|
|
476
|
+
const widths = tokens.map((token) => terminalCellWidth(token));
|
|
477
|
+
let left = widths.reduce((sum, value) => sum + value, 0);
|
|
478
|
+
const heads = [];
|
|
479
|
+
let at = 0;
|
|
480
|
+
while (left > width) {
|
|
481
|
+
let head = "";
|
|
482
|
+
let headWidth = 0;
|
|
483
|
+
while (at < tokens.length && (!widths[at] || headWidth + widths[at] <= width)) {
|
|
484
|
+
head += tokens[at];
|
|
485
|
+
headWidth += widths[at];
|
|
486
|
+
at += 1;
|
|
487
|
+
}
|
|
488
|
+
if (!headWidth && at < tokens.length) {
|
|
489
|
+
head += tokens[at];
|
|
490
|
+
headWidth += widths[at];
|
|
491
|
+
at += 1;
|
|
492
|
+
}
|
|
493
|
+
heads.push(head);
|
|
494
|
+
left -= headWidth;
|
|
495
|
+
}
|
|
496
|
+
return { heads, rest: tokens.slice(at).join("") };
|
|
497
|
+
}
|
|
498
|
+
function wrapWords(text, width) {
|
|
499
|
+
return wrapFrom(text, width).lines;
|
|
500
|
+
}
|
|
501
|
+
function wrapFrom(text, width) {
|
|
502
|
+
const safeWidth = Math.max(1, width);
|
|
503
|
+
const lines = [];
|
|
504
|
+
let current = "";
|
|
505
|
+
let currentWidth = 0;
|
|
506
|
+
let lastStart = 0;
|
|
507
|
+
let at = 0;
|
|
508
|
+
for (const word of text.split(/(\s+)/)) {
|
|
509
|
+
const start = at;
|
|
510
|
+
at += word.length;
|
|
511
|
+
if (!word) continue;
|
|
512
|
+
if (/^\s+$/.test(word)) {
|
|
513
|
+
if (currentWidth > 0) {
|
|
514
|
+
current += word;
|
|
515
|
+
currentWidth += terminalCellWidth(word);
|
|
516
|
+
}
|
|
517
|
+
continue;
|
|
518
|
+
}
|
|
519
|
+
const wordWidth = terminalCellWidth(word);
|
|
520
|
+
if (currentWidth > 0 && currentWidth + wordWidth > safeWidth) {
|
|
521
|
+
lines.push(current.replace(/\s+$/, ""));
|
|
522
|
+
current = "";
|
|
523
|
+
currentWidth = 0;
|
|
524
|
+
}
|
|
525
|
+
if (!current) lastStart = start;
|
|
526
|
+
if (wordWidth > safeWidth) {
|
|
527
|
+
const { heads, rest } = breakToWidth(word, safeWidth);
|
|
528
|
+
lines.push(...heads);
|
|
529
|
+
lastStart = start + word.length - rest.length;
|
|
530
|
+
current = rest;
|
|
531
|
+
currentWidth = terminalCellWidth(rest);
|
|
532
|
+
continue;
|
|
533
|
+
}
|
|
534
|
+
current += word;
|
|
535
|
+
currentWidth += wordWidth;
|
|
536
|
+
}
|
|
537
|
+
if (current || lines.length === 0) lines.push(current.replace(/\s+$/, ""));
|
|
538
|
+
return { lines, lastStart: current ? lastStart : -1 };
|
|
539
|
+
}
|
|
540
|
+
var liveWrap;
|
|
541
|
+
function wrapWordsLive(text, width) {
|
|
542
|
+
const previous = liveWrap;
|
|
543
|
+
const wordEnd = previous && previous.lastStart > 0 ? previous.text.slice(previous.lastStart).search(/\s/) : -1;
|
|
544
|
+
if (previous && previous.width === width && wordEnd >= 0 && text.startsWith(previous.text.slice(0, previous.lastStart + wordEnd + 1))) {
|
|
545
|
+
const tail = wrapFrom(text.slice(previous.lastStart), width);
|
|
546
|
+
liveWrap = { text, width, lines: [...previous.lines.slice(0, -1), ...tail.lines], lastStart: tail.lastStart < 0 ? -1 : previous.lastStart + tail.lastStart };
|
|
547
|
+
} else liveWrap = { text, width, ...wrapFrom(text, width) };
|
|
548
|
+
return liveWrap.lines;
|
|
549
|
+
}
|
|
550
|
+
function noticeLines(text, width, rows = 3) {
|
|
551
|
+
const safeWidth = Math.max(4, width);
|
|
552
|
+
const shortened = text.replace(/\S+/g, (word) => terminalCellWidth(word) > safeWidth ? middleSlice(word, safeWidth) : word);
|
|
553
|
+
const lines = wrapWords(shortened, safeWidth);
|
|
554
|
+
if (lines.length <= rows) return lines;
|
|
555
|
+
const kept = lines.slice(0, rows);
|
|
556
|
+
const last = kept[rows - 1];
|
|
557
|
+
kept[rows - 1] = terminalCellWidth(last) < safeWidth ? `${last}\u2026` : visibleSlice(last, safeWidth);
|
|
558
|
+
return kept;
|
|
559
|
+
}
|
|
560
|
+
function middleSlice(value, width) {
|
|
561
|
+
if (terminalCellWidth(value) <= width) return value;
|
|
562
|
+
const characters = [...value];
|
|
563
|
+
const tailRoom = Math.ceil((width - 1) / 2);
|
|
564
|
+
let tail = "";
|
|
565
|
+
for (let index = characters.length - 1; index >= 0 && terminalCellWidth(characters[index] + tail) <= tailRoom; index -= 1) tail = characters[index] + tail;
|
|
566
|
+
return `${visibleSlice(value, width - terminalCellWidth(tail))}${tail}`;
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
// src/harness/protocol/recap-line.ts
|
|
570
|
+
var RECAP_AT_END = /[ \t]*\[Did: [^\n]*\][ \t]*$/;
|
|
571
|
+
function withoutRecapLine(text) {
|
|
572
|
+
if (!text.includes("[Did: ")) return text;
|
|
573
|
+
let kept = text;
|
|
574
|
+
for (; ; ) {
|
|
575
|
+
const body = kept.replace(/\s+$/, "");
|
|
576
|
+
const lineStart = body.lastIndexOf("\n") + 1;
|
|
577
|
+
const line = body.slice(lineStart);
|
|
578
|
+
const hit = RECAP_AT_END.exec(line);
|
|
579
|
+
if (!hit) return kept === text ? text : kept.replace(/\s+$/, "");
|
|
580
|
+
kept = body.slice(0, lineStart + hit.index);
|
|
581
|
+
if (!kept.trim()) return "";
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
// src/turn/user-wait.ts
|
|
586
|
+
var clocks = /* @__PURE__ */ new Map();
|
|
587
|
+
function resetUserWait(sessionId) {
|
|
588
|
+
clocks.delete(sessionId);
|
|
589
|
+
}
|
|
590
|
+
function setUserWaiting(sessionId, waiting, now = Date.now()) {
|
|
591
|
+
const clock = clocks.get(sessionId) ?? { waitedMs: 0 };
|
|
592
|
+
if (waiting && clock.since === void 0) clock.since = now;
|
|
593
|
+
if (!waiting && clock.since !== void 0) {
|
|
594
|
+
clock.waitedMs += Math.max(0, now - clock.since);
|
|
595
|
+
delete clock.since;
|
|
596
|
+
}
|
|
597
|
+
clocks.set(sessionId, clock);
|
|
598
|
+
}
|
|
599
|
+
function userWaitedMs(sessionId, now = Date.now()) {
|
|
600
|
+
const clock = clocks.get(sessionId);
|
|
601
|
+
if (!clock) return 0;
|
|
602
|
+
return clock.waitedMs + (clock.since === void 0 ? 0 : Math.max(0, now - clock.since));
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
// src/agent/command-classifier.ts
|
|
606
|
+
var ROOTISH = String.raw`(?:/|/\*|~|~/|~/\*|\$HOME|\$\{HOME\}|\$HOME/|\$HOME/\*|/(?:bin|boot|dev|etc|home|lib|lib64|opt|proc|root|sbin|sys|usr|var)/?\*?)`;
|
|
607
|
+
var RM_FLAGS = String.raw`(?:\s+(?:-[a-zA-Z]+|--[a-z-]+))*`;
|
|
608
|
+
var DENY_PATTERNS = [
|
|
609
|
+
{ pattern: new RegExp(String.raw`\brm${RM_FLAGS}\s+(?:-[a-zA-Z]*[rR][a-zA-Z]*|--recursive)${RM_FLAGS}\s+(?:--\s+)?["']?${ROOTISH}["']?(?:\s|;|&|\||$)`), reason: "recursive delete of a root, home or system directory" },
|
|
610
|
+
{ pattern: /\brm\b[^;&|]*--no-preserve-root/, reason: "rm --no-preserve-root" },
|
|
611
|
+
{ pattern: /\bmkfs(?:\.[a-z0-9]+)?\b/, reason: "formats a filesystem" },
|
|
612
|
+
{ pattern: /\bdd\b[^;&|]*\bof=\/dev\/(?!null\b|zero\b|stdout\b|stderr\b|fd\/|tty\b)/, reason: "dd onto a block device" },
|
|
613
|
+
{ pattern: />\s*\/dev\/(?:sd[a-z]|nvme\d|hd[a-z]|vd[a-z]|mmcblk\d|disk\d)/, reason: "redirects output onto a block device" },
|
|
614
|
+
{ pattern: /\b(?:wipefs|shred)\b[^;&|]*\/dev\//, reason: "destroys a block device" },
|
|
615
|
+
{ pattern: /:\s*\(\s*\)\s*\{[^}]*:\s*\|\s*:[^}]*\}\s*;\s*:/, reason: "fork bomb" },
|
|
616
|
+
{ pattern: /\b(\w+)\s*\(\s*\)\s*\{\s*\1\s*\|\s*\1\s*&\s*\}\s*;\s*\1\b/, reason: "fork bomb" },
|
|
617
|
+
{ pattern: /\bchmod\s+(?:-[a-zA-Z]+\s+)*-R\s+(?:0?777|a\+rwx)\s+\/(?:\s|$)/, reason: "recursive chmod of /" },
|
|
618
|
+
{ pattern: /\bchown\s+(?:-[a-zA-Z]+\s+)*-R\s+\S+\s+\/(?:\s|$)/, reason: "recursive chown of /" }
|
|
619
|
+
];
|
|
620
|
+
var ASK_PATTERNS = [
|
|
621
|
+
{ pattern: /\b(?:curl|wget|fetch)\b[^;&]*\|\s*(?:sudo\s+)?(?:ba|z|da|k)?sh\b/, reason: "pipes a download into a shell" },
|
|
622
|
+
{ pattern: /\b(?:ba|z)?sh\s+<\(\s*(?:curl|wget)\b/, reason: "runs a downloaded script" },
|
|
623
|
+
{ pattern: /\bsudo\b|\bsu\s+-|\bdoas\b/, reason: "elevates privileges" },
|
|
624
|
+
{ pattern: /\bgit\s+push\b[^;&|]*(?:--force|-f\b)/, reason: "force push", always: true },
|
|
625
|
+
{ pattern: /\bgit\s+(?:reset\s+--hard|clean\s+-[a-zA-Z]*f)/, reason: "discards uncommitted work", always: true },
|
|
626
|
+
{ pattern: /\bgit\b[^;&|]*\s(?:--no-verify|--no-gpg-sign)\b|\bgit\s+commit\b[^;&|]*\s-[a-zA-Z]*n\b/, reason: "skips git hooks or signing", always: true }
|
|
627
|
+
];
|
|
628
|
+
var SAFE_PROGRAMS = /* @__PURE__ */ new Set([
|
|
629
|
+
"ls",
|
|
630
|
+
"pwd",
|
|
631
|
+
"cat",
|
|
632
|
+
"head",
|
|
633
|
+
"tail",
|
|
634
|
+
"wc",
|
|
635
|
+
"rg",
|
|
636
|
+
"grep",
|
|
637
|
+
"egrep",
|
|
638
|
+
"fgrep",
|
|
639
|
+
"find",
|
|
640
|
+
"file",
|
|
641
|
+
"stat",
|
|
642
|
+
"du",
|
|
643
|
+
"df",
|
|
644
|
+
"echo",
|
|
645
|
+
"printf",
|
|
646
|
+
"date",
|
|
647
|
+
"whoami",
|
|
648
|
+
"uname",
|
|
649
|
+
"which",
|
|
650
|
+
"type",
|
|
651
|
+
"basename",
|
|
652
|
+
"dirname",
|
|
653
|
+
"realpath",
|
|
654
|
+
"readlink",
|
|
655
|
+
"sort",
|
|
656
|
+
"uniq",
|
|
657
|
+
"cut",
|
|
658
|
+
"tr",
|
|
659
|
+
"nl",
|
|
660
|
+
"tree",
|
|
661
|
+
"diff",
|
|
662
|
+
"cmp",
|
|
663
|
+
"sha256sum",
|
|
664
|
+
"md5sum",
|
|
665
|
+
"true",
|
|
666
|
+
"test",
|
|
667
|
+
"git"
|
|
668
|
+
]);
|
|
669
|
+
var SAFE_GIT_SUBCOMMANDS = /* @__PURE__ */ new Set([
|
|
670
|
+
"status",
|
|
671
|
+
"diff",
|
|
672
|
+
"log",
|
|
673
|
+
"show",
|
|
674
|
+
"branch",
|
|
675
|
+
"rev-parse",
|
|
676
|
+
"ls-files",
|
|
677
|
+
"blame",
|
|
678
|
+
"describe",
|
|
679
|
+
"shortlog",
|
|
680
|
+
"remote",
|
|
681
|
+
"tag",
|
|
682
|
+
"stash",
|
|
683
|
+
"grep",
|
|
684
|
+
"cat-file",
|
|
685
|
+
"merge-base",
|
|
686
|
+
"rev-list",
|
|
687
|
+
"ls-tree",
|
|
688
|
+
"whatchanged",
|
|
689
|
+
"reflog"
|
|
690
|
+
]);
|
|
691
|
+
var GIT_MUTATING_ARGS = {
|
|
692
|
+
branch: /^(?:-[a-zA-Z]*[dDmMcCfu]|--delete|--move|--copy|--force|--set-upstream-to.*|--unset-upstream|--edit-description|--create-reflog|--track.*)$/,
|
|
693
|
+
remote: /^(?:add|remove|rm|rename|set-url|set-head|set-branches|prune|update)$/,
|
|
694
|
+
tag: /^(?:-[a-zA-Z]*[adfsum]|--delete|--force|--annotate|--sign|--local-user.*|--message.*|--file.*)$/,
|
|
695
|
+
stash: /^(?:push|pop|apply|drop|clear|save|branch|create|store|-[a-zA-Z]+)$/,
|
|
696
|
+
reflog: /^(?:expire|delete)$/
|
|
697
|
+
};
|
|
698
|
+
var FIND_DANGEROUS = /^-(?:exec|execdir|ok|okdir|delete|fprint|fprint0|fprintf|fls)$/;
|
|
699
|
+
var HARMLESS_REDIRECT = /\s*(?:\d?>>?\s*\/dev\/null\b|\d>&\d)/g;
|
|
700
|
+
function tokenizeSimple(command) {
|
|
701
|
+
const normalized = command.replace(HARMLESS_REDIRECT, "").replace(/&&/g, ";");
|
|
702
|
+
if (/[`$<>(){}\n\r\\]|&|\|\||;;/.test(normalized)) return void 0;
|
|
703
|
+
const pipelines = [];
|
|
704
|
+
let tokens = [];
|
|
705
|
+
let current = "";
|
|
706
|
+
let quote;
|
|
707
|
+
let hasToken = false;
|
|
708
|
+
const pushToken = () => {
|
|
709
|
+
if (hasToken) tokens.push(current);
|
|
710
|
+
current = "";
|
|
711
|
+
hasToken = false;
|
|
712
|
+
};
|
|
713
|
+
const pushCommand = () => {
|
|
714
|
+
pushToken();
|
|
715
|
+
if (!tokens.length) return false;
|
|
716
|
+
pipelines.push(tokens);
|
|
717
|
+
tokens = [];
|
|
718
|
+
return true;
|
|
719
|
+
};
|
|
720
|
+
for (const char of normalized) {
|
|
721
|
+
if (quote) {
|
|
722
|
+
if (char === quote) quote = void 0;
|
|
723
|
+
else current += char;
|
|
724
|
+
continue;
|
|
725
|
+
}
|
|
726
|
+
if (char === '"' || char === "'") {
|
|
727
|
+
quote = char;
|
|
728
|
+
hasToken = true;
|
|
729
|
+
continue;
|
|
730
|
+
}
|
|
731
|
+
if (char === " " || char === " ") {
|
|
732
|
+
pushToken();
|
|
733
|
+
continue;
|
|
734
|
+
}
|
|
735
|
+
if (char === ";" || char === "|") {
|
|
736
|
+
if (!pushCommand()) return void 0;
|
|
737
|
+
continue;
|
|
738
|
+
}
|
|
739
|
+
current += char;
|
|
740
|
+
hasToken = true;
|
|
741
|
+
}
|
|
742
|
+
if (quote) return void 0;
|
|
743
|
+
pushToken();
|
|
744
|
+
if (tokens.length) pipelines.push(tokens);
|
|
745
|
+
return pipelines.length ? pipelines : void 0;
|
|
746
|
+
}
|
|
747
|
+
function looksLikePath(token) {
|
|
748
|
+
return token.startsWith("/") || token.startsWith("~") || token.split("/").includes("..");
|
|
749
|
+
}
|
|
750
|
+
function hasOption(args, letter, long) {
|
|
751
|
+
return args.some((arg) => /^-[a-zA-Z0-9]+$/.test(arg) && arg.slice(1).includes(letter) || arg.startsWith(`-${letter}`) && !arg.startsWith("--") || (long ? long.test(arg) : false));
|
|
752
|
+
}
|
|
753
|
+
function gitArgsAreSafe(args) {
|
|
754
|
+
const rest = [...args];
|
|
755
|
+
while (rest.length && rest[0].startsWith("-")) {
|
|
756
|
+
const flag = rest.shift();
|
|
757
|
+
if (flag === "-c" || flag.startsWith("--exec-path") || flag.startsWith("--config-env")) return `git ${flag} can execute code`;
|
|
758
|
+
if (flag === "-C" || flag.startsWith("--git-dir") || flag.startsWith("--work-tree") || flag.startsWith("--namespace")) return `git ${flag} retargets the repository`;
|
|
759
|
+
}
|
|
760
|
+
const sub = rest.shift();
|
|
761
|
+
if (!sub || !SAFE_GIT_SUBCOMMANDS.has(sub)) return `git ${sub ?? ""} is not read-only`.trim();
|
|
762
|
+
if (sub === "stash" && (rest.length === 0 || !["list", "show"].includes(rest[0]))) return "git stash modifies the working tree";
|
|
763
|
+
const mutating = GIT_MUTATING_ARGS[sub];
|
|
764
|
+
if (mutating && sub !== "stash" && rest.some((arg) => mutating.test(arg))) return `git ${sub} ${rest.join(" ")} mutates the repository`;
|
|
765
|
+
if ((sub === "branch" || sub === "tag") && !rest.some((arg) => /^(?:-l|--list)$/.test(arg)) && rest.some((arg) => !arg.startsWith("-"))) {
|
|
766
|
+
return `git ${sub} ${rest.join(" ")} creates a ${sub}`;
|
|
767
|
+
}
|
|
768
|
+
if (rest.some((arg) => /^--(?:output|ext-diff|textconv|open-files-in-pager)\b/.test(arg))) return "git option that writes a file or runs a program";
|
|
769
|
+
if (sub === "grep" && rest.some((arg) => /^-O/.test(arg))) return "git grep -O runs a program";
|
|
770
|
+
return void 0;
|
|
771
|
+
}
|
|
772
|
+
function simpleCommandIsSafe(tokens, scope) {
|
|
773
|
+
const [program, ...args] = tokens;
|
|
774
|
+
if (program.includes("=")) return "sets an environment variable";
|
|
775
|
+
if (program.includes("/")) return `${program} is invoked by path`;
|
|
776
|
+
if (!SAFE_PROGRAMS.has(program)) return `${program} is not on the read-only allowlist`;
|
|
777
|
+
for (const arg of args) {
|
|
778
|
+
if (arg.startsWith("-") && !arg.includes("=")) continue;
|
|
779
|
+
const value = arg.includes("=") && arg.startsWith("-") ? arg.slice(arg.indexOf("=") + 1) : arg;
|
|
780
|
+
if (!value) continue;
|
|
781
|
+
if (!scope) {
|
|
782
|
+
if (looksLikePath(value)) return `${arg} may point outside the workspace`;
|
|
783
|
+
continue;
|
|
784
|
+
}
|
|
785
|
+
try {
|
|
786
|
+
const resolved = resolvePath(value, scope);
|
|
787
|
+
if (!resolved.confined) return `${arg} is outside the workspace`;
|
|
788
|
+
if (readDenyReason(resolved, scope)) return `${arg} is a protected location`;
|
|
789
|
+
} catch {
|
|
790
|
+
return `${arg} could not be resolved`;
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
switch (program) {
|
|
794
|
+
case "find":
|
|
795
|
+
return args.some((arg) => FIND_DANGEROUS.test(arg)) ? "find with an action that executes, writes or deletes" : void 0;
|
|
796
|
+
case "rg":
|
|
797
|
+
return args.some((arg) => /^--(?:pre|hostname-bin)(?:=|$)/.test(arg)) ? `rg ${args.find((arg) => /^--(?:pre|hostname-bin)/.test(arg))} runs a program` : void 0;
|
|
798
|
+
case "sort":
|
|
799
|
+
return hasOption(args, "o", /^--(?:output|compress-program)/) ? "sort writes a file or runs a program" : void 0;
|
|
800
|
+
case "tree":
|
|
801
|
+
return hasOption(args, "o") ? "tree -o writes a file" : void 0;
|
|
802
|
+
// `-I` takes its format glued on (`-Iseconds`), so it is not a cluster.
|
|
803
|
+
case "date":
|
|
804
|
+
return hasOption(args.filter((arg) => !arg.startsWith("-I")), "s", /^--set/) ? "date --set changes the clock" : void 0;
|
|
805
|
+
case "file":
|
|
806
|
+
return hasOption(args, "C", /^--compile/) ? "file -C writes a compiled magic file" : void 0;
|
|
807
|
+
// `uniq IN OUT` writes OUT.
|
|
808
|
+
case "uniq":
|
|
809
|
+
return args.filter((arg) => !arg.startsWith("-")).length > 1 ? "uniq with an output file writes it" : void 0;
|
|
810
|
+
case "git":
|
|
811
|
+
return gitArgsAreSafe(args);
|
|
812
|
+
default:
|
|
813
|
+
return void 0;
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
function classifyCommand(command, scope) {
|
|
817
|
+
const text = command.trim();
|
|
818
|
+
if (!text) return { tier: "deny", reason: "empty command" };
|
|
819
|
+
for (const { pattern, reason } of DENY_PATTERNS) if (pattern.test(text)) return { tier: "deny", reason };
|
|
820
|
+
for (const { pattern, reason, always } of ASK_PATTERNS) if (pattern.test(text)) return { tier: "ask", reason, ...always ? { always } : {} };
|
|
821
|
+
const pipelines = tokenizeSimple(text);
|
|
822
|
+
if (!pipelines) return { tier: "ask", reason: "uses shell features that cannot be checked statically" };
|
|
823
|
+
for (const tokens of pipelines) {
|
|
824
|
+
const unsafe = simpleCommandIsSafe(tokens, scope);
|
|
825
|
+
if (unsafe) return { tier: "ask", reason: unsafe };
|
|
826
|
+
}
|
|
827
|
+
return { tier: "safe", reason: "read-only command" };
|
|
828
|
+
}
|
|
829
|
+
function commandIsReadOnly(command, scope) {
|
|
830
|
+
let text = command.trim();
|
|
831
|
+
const wrapped = /^(?:\S*\/)?(?:ba|z|da)?sh\s+-l?c\s+(['"])([\s\S]*)\1\s*$/.exec(text);
|
|
832
|
+
if (wrapped) text = wrapped[2].trim();
|
|
833
|
+
if (!text || /…|\.\.\.$/.test(text)) return false;
|
|
834
|
+
return classifyCommand(text, scope).tier === "safe";
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
// node_modules/.pnpm/marked@15.0.12/node_modules/marked/lib/marked.esm.js
|
|
838
|
+
function _getDefaults() {
|
|
839
|
+
return {
|
|
840
|
+
async: false,
|
|
841
|
+
breaks: false,
|
|
842
|
+
extensions: null,
|
|
843
|
+
gfm: true,
|
|
844
|
+
hooks: null,
|
|
845
|
+
pedantic: false,
|
|
846
|
+
renderer: null,
|
|
847
|
+
silent: false,
|
|
848
|
+
tokenizer: null,
|
|
849
|
+
walkTokens: null
|
|
850
|
+
};
|
|
851
|
+
}
|
|
852
|
+
var _defaults = _getDefaults();
|
|
853
|
+
function changeDefaults(newDefaults) {
|
|
854
|
+
_defaults = newDefaults;
|
|
855
|
+
}
|
|
856
|
+
var noopTest = { exec: () => null };
|
|
857
|
+
function edit(regex, opt = "") {
|
|
858
|
+
let source = typeof regex === "string" ? regex : regex.source;
|
|
859
|
+
const obj = {
|
|
860
|
+
replace: (name, val) => {
|
|
861
|
+
let valSource = typeof val === "string" ? val : val.source;
|
|
862
|
+
valSource = valSource.replace(other.caret, "$1");
|
|
863
|
+
source = source.replace(name, valSource);
|
|
864
|
+
return obj;
|
|
865
|
+
},
|
|
866
|
+
getRegex: () => {
|
|
867
|
+
return new RegExp(source, opt);
|
|
868
|
+
}
|
|
869
|
+
};
|
|
870
|
+
return obj;
|
|
871
|
+
}
|
|
872
|
+
var other = {
|
|
873
|
+
codeRemoveIndent: /^(?: {1,4}| {0,3}\t)/gm,
|
|
874
|
+
outputLinkReplace: /\\([\[\]])/g,
|
|
875
|
+
indentCodeCompensation: /^(\s+)(?:```)/,
|
|
876
|
+
beginningSpace: /^\s+/,
|
|
877
|
+
endingHash: /#$/,
|
|
878
|
+
startingSpaceChar: /^ /,
|
|
879
|
+
endingSpaceChar: / $/,
|
|
880
|
+
nonSpaceChar: /[^ ]/,
|
|
881
|
+
newLineCharGlobal: /\n/g,
|
|
882
|
+
tabCharGlobal: /\t/g,
|
|
883
|
+
multipleSpaceGlobal: /\s+/g,
|
|
884
|
+
blankLine: /^[ \t]*$/,
|
|
885
|
+
doubleBlankLine: /\n[ \t]*\n[ \t]*$/,
|
|
886
|
+
blockquoteStart: /^ {0,3}>/,
|
|
887
|
+
blockquoteSetextReplace: /\n {0,3}((?:=+|-+) *)(?=\n|$)/g,
|
|
888
|
+
blockquoteSetextReplace2: /^ {0,3}>[ \t]?/gm,
|
|
889
|
+
listReplaceTabs: /^\t+/,
|
|
890
|
+
listReplaceNesting: /^ {1,4}(?=( {4})*[^ ])/g,
|
|
891
|
+
listIsTask: /^\[[ xX]\] /,
|
|
892
|
+
listReplaceTask: /^\[[ xX]\] +/,
|
|
893
|
+
anyLine: /\n.*\n/,
|
|
894
|
+
hrefBrackets: /^<(.*)>$/,
|
|
895
|
+
tableDelimiter: /[:|]/,
|
|
896
|
+
tableAlignChars: /^\||\| *$/g,
|
|
897
|
+
tableRowBlankLine: /\n[ \t]*$/,
|
|
898
|
+
tableAlignRight: /^ *-+: *$/,
|
|
899
|
+
tableAlignCenter: /^ *:-+: *$/,
|
|
900
|
+
tableAlignLeft: /^ *:-+ *$/,
|
|
901
|
+
startATag: /^<a /i,
|
|
902
|
+
endATag: /^<\/a>/i,
|
|
903
|
+
startPreScriptTag: /^<(pre|code|kbd|script)(\s|>)/i,
|
|
904
|
+
endPreScriptTag: /^<\/(pre|code|kbd|script)(\s|>)/i,
|
|
905
|
+
startAngleBracket: /^</,
|
|
906
|
+
endAngleBracket: />$/,
|
|
907
|
+
pedanticHrefTitle: /^([^'"]*[^\s])\s+(['"])(.*)\2/,
|
|
908
|
+
unicodeAlphaNumeric: /[\p{L}\p{N}]/u,
|
|
909
|
+
escapeTest: /[&<>"']/,
|
|
910
|
+
escapeReplace: /[&<>"']/g,
|
|
911
|
+
escapeTestNoEncode: /[<>"']|&(?!(#\d{1,7}|#[Xx][a-fA-F0-9]{1,6}|\w+);)/,
|
|
912
|
+
escapeReplaceNoEncode: /[<>"']|&(?!(#\d{1,7}|#[Xx][a-fA-F0-9]{1,6}|\w+);)/g,
|
|
913
|
+
unescapeTest: /&(#(?:\d+)|(?:#x[0-9A-Fa-f]+)|(?:\w+));?/ig,
|
|
914
|
+
caret: /(^|[^\[])\^/g,
|
|
915
|
+
percentDecode: /%25/g,
|
|
916
|
+
findPipe: /\|/g,
|
|
917
|
+
splitPipe: / \|/,
|
|
918
|
+
slashPipe: /\\\|/g,
|
|
919
|
+
carriageReturn: /\r\n|\r/g,
|
|
920
|
+
spaceLine: /^ +$/gm,
|
|
921
|
+
notSpaceStart: /^\S*/,
|
|
922
|
+
endingNewline: /\n$/,
|
|
923
|
+
listItemRegex: (bull) => new RegExp(`^( {0,3}${bull})((?:[ ][^\\n]*)?(?:\\n|$))`),
|
|
924
|
+
nextBulletRegex: (indent) => new RegExp(`^ {0,${Math.min(3, indent - 1)}}(?:[*+-]|\\d{1,9}[.)])((?:[ ][^\\n]*)?(?:\\n|$))`),
|
|
925
|
+
hrRegex: (indent) => new RegExp(`^ {0,${Math.min(3, indent - 1)}}((?:- *){3,}|(?:_ *){3,}|(?:\\* *){3,})(?:\\n+|$)`),
|
|
926
|
+
fencesBeginRegex: (indent) => new RegExp(`^ {0,${Math.min(3, indent - 1)}}(?:\`\`\`|~~~)`),
|
|
927
|
+
headingBeginRegex: (indent) => new RegExp(`^ {0,${Math.min(3, indent - 1)}}#`),
|
|
928
|
+
htmlBeginRegex: (indent) => new RegExp(`^ {0,${Math.min(3, indent - 1)}}<(?:[a-z].*>|!--)`, "i")
|
|
929
|
+
};
|
|
930
|
+
var newline = /^(?:[ \t]*(?:\n|$))+/;
|
|
931
|
+
var blockCode = /^((?: {4}| {0,3}\t)[^\n]+(?:\n(?:[ \t]*(?:\n|$))*)?)+/;
|
|
932
|
+
var fences = /^ {0,3}(`{3,}(?=[^`\n]*(?:\n|$))|~{3,})([^\n]*)(?:\n|$)(?:|([\s\S]*?)(?:\n|$))(?: {0,3}\1[~`]* *(?=\n|$)|$)/;
|
|
933
|
+
var hr = /^ {0,3}((?:-[\t ]*){3,}|(?:_[ \t]*){3,}|(?:\*[ \t]*){3,})(?:\n+|$)/;
|
|
934
|
+
var heading = /^ {0,3}(#{1,6})(?=\s|$)(.*)(?:\n+|$)/;
|
|
935
|
+
var bullet = /(?:[*+-]|\d{1,9}[.)])/;
|
|
936
|
+
var lheadingCore = /^(?!bull |blockCode|fences|blockquote|heading|html|table)((?:.|\n(?!\s*?\n|bull |blockCode|fences|blockquote|heading|html|table))+?)\n {0,3}(=+|-+) *(?:\n+|$)/;
|
|
937
|
+
var lheading = edit(lheadingCore).replace(/bull/g, bullet).replace(/blockCode/g, /(?: {4}| {0,3}\t)/).replace(/fences/g, / {0,3}(?:`{3,}|~{3,})/).replace(/blockquote/g, / {0,3}>/).replace(/heading/g, / {0,3}#{1,6}/).replace(/html/g, / {0,3}<[^\n>]+>\n/).replace(/\|table/g, "").getRegex();
|
|
938
|
+
var lheadingGfm = edit(lheadingCore).replace(/bull/g, bullet).replace(/blockCode/g, /(?: {4}| {0,3}\t)/).replace(/fences/g, / {0,3}(?:`{3,}|~{3,})/).replace(/blockquote/g, / {0,3}>/).replace(/heading/g, / {0,3}#{1,6}/).replace(/html/g, / {0,3}<[^\n>]+>\n/).replace(/table/g, / {0,3}\|?(?:[:\- ]*\|)+[\:\- ]*\n/).getRegex();
|
|
939
|
+
var _paragraph = /^([^\n]+(?:\n(?!hr|heading|lheading|blockquote|fences|list|html|table| +\n)[^\n]+)*)/;
|
|
940
|
+
var blockText = /^[^\n]+/;
|
|
941
|
+
var _blockLabel = /(?!\s*\])(?:\\.|[^\[\]\\])+/;
|
|
942
|
+
var def = edit(/^ {0,3}\[(label)\]: *(?:\n[ \t]*)?([^<\s][^\s]*|<.*?>)(?:(?: +(?:\n[ \t]*)?| *\n[ \t]*)(title))? *(?:\n+|$)/).replace("label", _blockLabel).replace("title", /(?:"(?:\\"?|[^"\\])*"|'[^'\n]*(?:\n[^'\n]+)*\n?'|\([^()]*\))/).getRegex();
|
|
943
|
+
var list = edit(/^( {0,3}bull)([ \t][^\n]+?)?(?:\n|$)/).replace(/bull/g, bullet).getRegex();
|
|
944
|
+
var _tag = "address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option|p|param|search|section|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul";
|
|
945
|
+
var _comment = /<!--(?:-?>|[\s\S]*?(?:-->|$))/;
|
|
946
|
+
var html = edit(
|
|
947
|
+
"^ {0,3}(?:<(script|pre|style|textarea)[\\s>][\\s\\S]*?(?:</\\1>[^\\n]*\\n+|$)|comment[^\\n]*(\\n+|$)|<\\?[\\s\\S]*?(?:\\?>\\n*|$)|<![A-Z][\\s\\S]*?(?:>\\n*|$)|<!\\[CDATA\\[[\\s\\S]*?(?:\\]\\]>\\n*|$)|</?(tag)(?: +|\\n|/?>)[\\s\\S]*?(?:(?:\\n[ ]*)+\\n|$)|<(?!script|pre|style|textarea)([a-z][\\w-]*)(?:attribute)*? */?>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:(?:\\n[ ]*)+\\n|$)|</(?!script|pre|style|textarea)[a-z][\\w-]*\\s*>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:(?:\\n[ ]*)+\\n|$))",
|
|
948
|
+
"i"
|
|
949
|
+
).replace("comment", _comment).replace("tag", _tag).replace("attribute", / +[a-zA-Z:_][\w.:-]*(?: *= *"[^"\n]*"| *= *'[^'\n]*'| *= *[^\s"'=<>`]+)?/).getRegex();
|
|
950
|
+
var paragraph = edit(_paragraph).replace("hr", hr).replace("heading", " {0,3}#{1,6}(?:\\s|$)").replace("|lheading", "").replace("|table", "").replace("blockquote", " {0,3}>").replace("fences", " {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n").replace("list", " {0,3}(?:[*+-]|1[.)]) ").replace("html", "</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|textarea|!--)").replace("tag", _tag).getRegex();
|
|
951
|
+
var blockquote = edit(/^( {0,3}> ?(paragraph|[^\n]*)(?:\n|$))+/).replace("paragraph", paragraph).getRegex();
|
|
952
|
+
var blockNormal = {
|
|
953
|
+
blockquote,
|
|
954
|
+
code: blockCode,
|
|
955
|
+
def,
|
|
956
|
+
fences,
|
|
957
|
+
heading,
|
|
958
|
+
hr,
|
|
959
|
+
html,
|
|
960
|
+
lheading,
|
|
961
|
+
list,
|
|
962
|
+
newline,
|
|
963
|
+
paragraph,
|
|
964
|
+
table: noopTest,
|
|
965
|
+
text: blockText
|
|
966
|
+
};
|
|
967
|
+
var gfmTable = edit(
|
|
968
|
+
"^ *([^\\n ].*)\\n {0,3}((?:\\| *)?:?-+:? *(?:\\| *:?-+:? *)*(?:\\| *)?)(?:\\n((?:(?! *\\n|hr|heading|blockquote|code|fences|list|html).*(?:\\n|$))*)\\n*|$)"
|
|
969
|
+
).replace("hr", hr).replace("heading", " {0,3}#{1,6}(?:\\s|$)").replace("blockquote", " {0,3}>").replace("code", "(?: {4}| {0,3} )[^\\n]").replace("fences", " {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n").replace("list", " {0,3}(?:[*+-]|1[.)]) ").replace("html", "</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|textarea|!--)").replace("tag", _tag).getRegex();
|
|
970
|
+
var blockGfm = {
|
|
971
|
+
...blockNormal,
|
|
972
|
+
lheading: lheadingGfm,
|
|
973
|
+
table: gfmTable,
|
|
974
|
+
paragraph: edit(_paragraph).replace("hr", hr).replace("heading", " {0,3}#{1,6}(?:\\s|$)").replace("|lheading", "").replace("table", gfmTable).replace("blockquote", " {0,3}>").replace("fences", " {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n").replace("list", " {0,3}(?:[*+-]|1[.)]) ").replace("html", "</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|textarea|!--)").replace("tag", _tag).getRegex()
|
|
975
|
+
};
|
|
976
|
+
var blockPedantic = {
|
|
977
|
+
...blockNormal,
|
|
978
|
+
html: edit(
|
|
979
|
+
`^ *(?:comment *(?:\\n|\\s*$)|<(tag)[\\s\\S]+?</\\1> *(?:\\n{2,}|\\s*$)|<tag(?:"[^"]*"|'[^']*'|\\s[^'"/>\\s]*)*?/?> *(?:\\n{2,}|\\s*$))`
|
|
980
|
+
).replace("comment", _comment).replace(/tag/g, "(?!(?:a|em|strong|small|s|cite|q|dfn|abbr|data|time|code|var|samp|kbd|sub|sup|i|b|u|mark|ruby|rt|rp|bdi|bdo|span|br|wbr|ins|del|img)\\b)\\w+(?!:|[^\\w\\s@]*@)\\b").getRegex(),
|
|
981
|
+
def: /^ *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +(["(][^\n]+[")]))? *(?:\n+|$)/,
|
|
982
|
+
heading: /^(#{1,6})(.*)(?:\n+|$)/,
|
|
983
|
+
fences: noopTest,
|
|
984
|
+
// fences not supported
|
|
985
|
+
lheading: /^(.+?)\n {0,3}(=+|-+) *(?:\n+|$)/,
|
|
986
|
+
paragraph: edit(_paragraph).replace("hr", hr).replace("heading", " *#{1,6} *[^\n]").replace("lheading", lheading).replace("|table", "").replace("blockquote", " {0,3}>").replace("|fences", "").replace("|list", "").replace("|html", "").replace("|tag", "").getRegex()
|
|
987
|
+
};
|
|
988
|
+
var escape = /^\\([!"#$%&'()*+,\-./:;<=>?@\[\]\\^_`{|}~])/;
|
|
989
|
+
var inlineCode = /^(`+)([^`]|[^`][\s\S]*?[^`])\1(?!`)/;
|
|
990
|
+
var br = /^( {2,}|\\)\n(?!\s*$)/;
|
|
991
|
+
var inlineText = /^(`+|[^`])(?:(?= {2,}\n)|[\s\S]*?(?:(?=[\\<!\[`*_]|\b_|$)|[^ ](?= {2,}\n)))/;
|
|
992
|
+
var _punctuation = /[\p{P}\p{S}]/u;
|
|
993
|
+
var _punctuationOrSpace = /[\s\p{P}\p{S}]/u;
|
|
994
|
+
var _notPunctuationOrSpace = /[^\s\p{P}\p{S}]/u;
|
|
995
|
+
var punctuation = edit(/^((?![*_])punctSpace)/, "u").replace(/punctSpace/g, _punctuationOrSpace).getRegex();
|
|
996
|
+
var _punctuationGfmStrongEm = /(?!~)[\p{P}\p{S}]/u;
|
|
997
|
+
var _punctuationOrSpaceGfmStrongEm = /(?!~)[\s\p{P}\p{S}]/u;
|
|
998
|
+
var _notPunctuationOrSpaceGfmStrongEm = /(?:[^\s\p{P}\p{S}]|~)/u;
|
|
999
|
+
var blockSkip = /\[[^[\]]*?\]\((?:\\.|[^\\\(\)]|\((?:\\.|[^\\\(\)])*\))*\)|`[^`]*?`|<[^<>]*?>/g;
|
|
1000
|
+
var emStrongLDelimCore = /^(?:\*+(?:((?!\*)punct)|[^\s*]))|^_+(?:((?!_)punct)|([^\s_]))/;
|
|
1001
|
+
var emStrongLDelim = edit(emStrongLDelimCore, "u").replace(/punct/g, _punctuation).getRegex();
|
|
1002
|
+
var emStrongLDelimGfm = edit(emStrongLDelimCore, "u").replace(/punct/g, _punctuationGfmStrongEm).getRegex();
|
|
1003
|
+
var emStrongRDelimAstCore = "^[^_*]*?__[^_*]*?\\*[^_*]*?(?=__)|[^*]+(?=[^*])|(?!\\*)punct(\\*+)(?=[\\s]|$)|notPunctSpace(\\*+)(?!\\*)(?=punctSpace|$)|(?!\\*)punctSpace(\\*+)(?=notPunctSpace)|[\\s](\\*+)(?!\\*)(?=punct)|(?!\\*)punct(\\*+)(?!\\*)(?=punct)|notPunctSpace(\\*+)(?=notPunctSpace)";
|
|
1004
|
+
var emStrongRDelimAst = edit(emStrongRDelimAstCore, "gu").replace(/notPunctSpace/g, _notPunctuationOrSpace).replace(/punctSpace/g, _punctuationOrSpace).replace(/punct/g, _punctuation).getRegex();
|
|
1005
|
+
var emStrongRDelimAstGfm = edit(emStrongRDelimAstCore, "gu").replace(/notPunctSpace/g, _notPunctuationOrSpaceGfmStrongEm).replace(/punctSpace/g, _punctuationOrSpaceGfmStrongEm).replace(/punct/g, _punctuationGfmStrongEm).getRegex();
|
|
1006
|
+
var emStrongRDelimUnd = edit(
|
|
1007
|
+
"^[^_*]*?\\*\\*[^_*]*?_[^_*]*?(?=\\*\\*)|[^_]+(?=[^_])|(?!_)punct(_+)(?=[\\s]|$)|notPunctSpace(_+)(?!_)(?=punctSpace|$)|(?!_)punctSpace(_+)(?=notPunctSpace)|[\\s](_+)(?!_)(?=punct)|(?!_)punct(_+)(?!_)(?=punct)",
|
|
1008
|
+
"gu"
|
|
1009
|
+
).replace(/notPunctSpace/g, _notPunctuationOrSpace).replace(/punctSpace/g, _punctuationOrSpace).replace(/punct/g, _punctuation).getRegex();
|
|
1010
|
+
var anyPunctuation = edit(/\\(punct)/, "gu").replace(/punct/g, _punctuation).getRegex();
|
|
1011
|
+
var autolink = edit(/^<(scheme:[^\s\x00-\x1f<>]*|email)>/).replace("scheme", /[a-zA-Z][a-zA-Z0-9+.-]{1,31}/).replace("email", /[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+(@)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+(?![-_])/).getRegex();
|
|
1012
|
+
var _inlineComment = edit(_comment).replace("(?:-->|$)", "-->").getRegex();
|
|
1013
|
+
var tag = edit(
|
|
1014
|
+
"^comment|^</[a-zA-Z][\\w:-]*\\s*>|^<[a-zA-Z][\\w-]*(?:attribute)*?\\s*/?>|^<\\?[\\s\\S]*?\\?>|^<![a-zA-Z]+\\s[\\s\\S]*?>|^<!\\[CDATA\\[[\\s\\S]*?\\]\\]>"
|
|
1015
|
+
).replace("comment", _inlineComment).replace("attribute", /\s+[a-zA-Z:_][\w.:-]*(?:\s*=\s*"[^"]*"|\s*=\s*'[^']*'|\s*=\s*[^\s"'=<>`]+)?/).getRegex();
|
|
1016
|
+
var _inlineLabel = /(?:\[(?:\\.|[^\[\]\\])*\]|\\.|`[^`]*`|[^\[\]\\`])*?/;
|
|
1017
|
+
var link = edit(/^!?\[(label)\]\(\s*(href)(?:(?:[ \t]*(?:\n[ \t]*)?)(title))?\s*\)/).replace("label", _inlineLabel).replace("href", /<(?:\\.|[^\n<>\\])+>|[^ \t\n\x00-\x1f]*/).replace("title", /"(?:\\"?|[^"\\])*"|'(?:\\'?|[^'\\])*'|\((?:\\\)?|[^)\\])*\)/).getRegex();
|
|
1018
|
+
var reflink = edit(/^!?\[(label)\]\[(ref)\]/).replace("label", _inlineLabel).replace("ref", _blockLabel).getRegex();
|
|
1019
|
+
var nolink = edit(/^!?\[(ref)\](?:\[\])?/).replace("ref", _blockLabel).getRegex();
|
|
1020
|
+
var reflinkSearch = edit("reflink|nolink(?!\\()", "g").replace("reflink", reflink).replace("nolink", nolink).getRegex();
|
|
1021
|
+
var inlineNormal = {
|
|
1022
|
+
_backpedal: noopTest,
|
|
1023
|
+
// only used for GFM url
|
|
1024
|
+
anyPunctuation,
|
|
1025
|
+
autolink,
|
|
1026
|
+
blockSkip,
|
|
1027
|
+
br,
|
|
1028
|
+
code: inlineCode,
|
|
1029
|
+
del: noopTest,
|
|
1030
|
+
emStrongLDelim,
|
|
1031
|
+
emStrongRDelimAst,
|
|
1032
|
+
emStrongRDelimUnd,
|
|
1033
|
+
escape,
|
|
1034
|
+
link,
|
|
1035
|
+
nolink,
|
|
1036
|
+
punctuation,
|
|
1037
|
+
reflink,
|
|
1038
|
+
reflinkSearch,
|
|
1039
|
+
tag,
|
|
1040
|
+
text: inlineText,
|
|
1041
|
+
url: noopTest
|
|
1042
|
+
};
|
|
1043
|
+
var inlinePedantic = {
|
|
1044
|
+
...inlineNormal,
|
|
1045
|
+
link: edit(/^!?\[(label)\]\((.*?)\)/).replace("label", _inlineLabel).getRegex(),
|
|
1046
|
+
reflink: edit(/^!?\[(label)\]\s*\[([^\]]*)\]/).replace("label", _inlineLabel).getRegex()
|
|
1047
|
+
};
|
|
1048
|
+
var inlineGfm = {
|
|
1049
|
+
...inlineNormal,
|
|
1050
|
+
emStrongRDelimAst: emStrongRDelimAstGfm,
|
|
1051
|
+
emStrongLDelim: emStrongLDelimGfm,
|
|
1052
|
+
url: edit(/^((?:ftp|https?):\/\/|www\.)(?:[a-zA-Z0-9\-]+\.?)+[^\s<]*|^email/, "i").replace("email", /[A-Za-z0-9._+-]+(@)[a-zA-Z0-9-_]+(?:\.[a-zA-Z0-9-_]*[a-zA-Z0-9])+(?![-_])/).getRegex(),
|
|
1053
|
+
_backpedal: /(?:[^?!.,:;*_'"~()&]+|\([^)]*\)|&(?![a-zA-Z0-9]+;$)|[?!.,:;*_'"~)]+(?!$))+/,
|
|
1054
|
+
del: /^(~~?)(?=[^\s~])((?:\\.|[^\\])*?(?:\\.|[^\s~\\]))\1(?=[^~]|$)/,
|
|
1055
|
+
text: /^([`~]+|[^`~])(?:(?= {2,}\n)|(?=[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@)|[\s\S]*?(?:(?=[\\<!\[`*~_]|\b_|https?:\/\/|ftp:\/\/|www\.|$)|[^ ](?= {2,}\n)|[^a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-](?=[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@)))/
|
|
1056
|
+
};
|
|
1057
|
+
var inlineBreaks = {
|
|
1058
|
+
...inlineGfm,
|
|
1059
|
+
br: edit(br).replace("{2,}", "*").getRegex(),
|
|
1060
|
+
text: edit(inlineGfm.text).replace("\\b_", "\\b_| {2,}\\n").replace(/\{2,\}/g, "*").getRegex()
|
|
1061
|
+
};
|
|
1062
|
+
var block = {
|
|
1063
|
+
normal: blockNormal,
|
|
1064
|
+
gfm: blockGfm,
|
|
1065
|
+
pedantic: blockPedantic
|
|
1066
|
+
};
|
|
1067
|
+
var inline = {
|
|
1068
|
+
normal: inlineNormal,
|
|
1069
|
+
gfm: inlineGfm,
|
|
1070
|
+
breaks: inlineBreaks,
|
|
1071
|
+
pedantic: inlinePedantic
|
|
1072
|
+
};
|
|
1073
|
+
var escapeReplacements = {
|
|
1074
|
+
"&": "&",
|
|
1075
|
+
"<": "<",
|
|
1076
|
+
">": ">",
|
|
1077
|
+
'"': """,
|
|
1078
|
+
"'": "'"
|
|
1079
|
+
};
|
|
1080
|
+
var getEscapeReplacement = (ch) => escapeReplacements[ch];
|
|
1081
|
+
function escape2(html2, encode) {
|
|
1082
|
+
if (encode) {
|
|
1083
|
+
if (other.escapeTest.test(html2)) {
|
|
1084
|
+
return html2.replace(other.escapeReplace, getEscapeReplacement);
|
|
1085
|
+
}
|
|
1086
|
+
} else {
|
|
1087
|
+
if (other.escapeTestNoEncode.test(html2)) {
|
|
1088
|
+
return html2.replace(other.escapeReplaceNoEncode, getEscapeReplacement);
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
return html2;
|
|
1092
|
+
}
|
|
1093
|
+
function cleanUrl(href) {
|
|
1094
|
+
try {
|
|
1095
|
+
href = encodeURI(href).replace(other.percentDecode, "%");
|
|
1096
|
+
} catch {
|
|
1097
|
+
return null;
|
|
1098
|
+
}
|
|
1099
|
+
return href;
|
|
1100
|
+
}
|
|
1101
|
+
function splitCells(tableRow, count) {
|
|
1102
|
+
const row = tableRow.replace(other.findPipe, (match, offset, str) => {
|
|
1103
|
+
let escaped = false;
|
|
1104
|
+
let curr = offset;
|
|
1105
|
+
while (--curr >= 0 && str[curr] === "\\") escaped = !escaped;
|
|
1106
|
+
if (escaped) {
|
|
1107
|
+
return "|";
|
|
1108
|
+
} else {
|
|
1109
|
+
return " |";
|
|
1110
|
+
}
|
|
1111
|
+
}), cells = row.split(other.splitPipe);
|
|
1112
|
+
let i = 0;
|
|
1113
|
+
if (!cells[0].trim()) {
|
|
1114
|
+
cells.shift();
|
|
1115
|
+
}
|
|
1116
|
+
if (cells.length > 0 && !cells.at(-1)?.trim()) {
|
|
1117
|
+
cells.pop();
|
|
1118
|
+
}
|
|
1119
|
+
if (count) {
|
|
1120
|
+
if (cells.length > count) {
|
|
1121
|
+
cells.splice(count);
|
|
1122
|
+
} else {
|
|
1123
|
+
while (cells.length < count) cells.push("");
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
for (; i < cells.length; i++) {
|
|
1127
|
+
cells[i] = cells[i].trim().replace(other.slashPipe, "|");
|
|
1128
|
+
}
|
|
1129
|
+
return cells;
|
|
1130
|
+
}
|
|
1131
|
+
function rtrim(str, c, invert) {
|
|
1132
|
+
const l = str.length;
|
|
1133
|
+
if (l === 0) {
|
|
1134
|
+
return "";
|
|
1135
|
+
}
|
|
1136
|
+
let suffLen = 0;
|
|
1137
|
+
while (suffLen < l) {
|
|
1138
|
+
const currChar = str.charAt(l - suffLen - 1);
|
|
1139
|
+
if (currChar === c && !invert) {
|
|
1140
|
+
suffLen++;
|
|
1141
|
+
} else if (currChar !== c && invert) {
|
|
1142
|
+
suffLen++;
|
|
1143
|
+
} else {
|
|
1144
|
+
break;
|
|
1145
|
+
}
|
|
1146
|
+
}
|
|
1147
|
+
return str.slice(0, l - suffLen);
|
|
1148
|
+
}
|
|
1149
|
+
function findClosingBracket(str, b) {
|
|
1150
|
+
if (str.indexOf(b[1]) === -1) {
|
|
1151
|
+
return -1;
|
|
1152
|
+
}
|
|
1153
|
+
let level = 0;
|
|
1154
|
+
for (let i = 0; i < str.length; i++) {
|
|
1155
|
+
if (str[i] === "\\") {
|
|
1156
|
+
i++;
|
|
1157
|
+
} else if (str[i] === b[0]) {
|
|
1158
|
+
level++;
|
|
1159
|
+
} else if (str[i] === b[1]) {
|
|
1160
|
+
level--;
|
|
1161
|
+
if (level < 0) {
|
|
1162
|
+
return i;
|
|
1163
|
+
}
|
|
1164
|
+
}
|
|
1165
|
+
}
|
|
1166
|
+
if (level > 0) {
|
|
1167
|
+
return -2;
|
|
1168
|
+
}
|
|
1169
|
+
return -1;
|
|
1170
|
+
}
|
|
1171
|
+
function outputLink(cap, link2, raw, lexer2, rules) {
|
|
1172
|
+
const href = link2.href;
|
|
1173
|
+
const title = link2.title || null;
|
|
1174
|
+
const text = cap[1].replace(rules.other.outputLinkReplace, "$1");
|
|
1175
|
+
lexer2.state.inLink = true;
|
|
1176
|
+
const token = {
|
|
1177
|
+
type: cap[0].charAt(0) === "!" ? "image" : "link",
|
|
1178
|
+
raw,
|
|
1179
|
+
href,
|
|
1180
|
+
title,
|
|
1181
|
+
text,
|
|
1182
|
+
tokens: lexer2.inlineTokens(text)
|
|
1183
|
+
};
|
|
1184
|
+
lexer2.state.inLink = false;
|
|
1185
|
+
return token;
|
|
1186
|
+
}
|
|
1187
|
+
function indentCodeCompensation(raw, text, rules) {
|
|
1188
|
+
const matchIndentToCode = raw.match(rules.other.indentCodeCompensation);
|
|
1189
|
+
if (matchIndentToCode === null) {
|
|
1190
|
+
return text;
|
|
1191
|
+
}
|
|
1192
|
+
const indentToCode = matchIndentToCode[1];
|
|
1193
|
+
return text.split("\n").map((node) => {
|
|
1194
|
+
const matchIndentInNode = node.match(rules.other.beginningSpace);
|
|
1195
|
+
if (matchIndentInNode === null) {
|
|
1196
|
+
return node;
|
|
1197
|
+
}
|
|
1198
|
+
const [indentInNode] = matchIndentInNode;
|
|
1199
|
+
if (indentInNode.length >= indentToCode.length) {
|
|
1200
|
+
return node.slice(indentToCode.length);
|
|
1201
|
+
}
|
|
1202
|
+
return node;
|
|
1203
|
+
}).join("\n");
|
|
1204
|
+
}
|
|
1205
|
+
var _Tokenizer = class {
|
|
1206
|
+
options;
|
|
1207
|
+
rules;
|
|
1208
|
+
// set by the lexer
|
|
1209
|
+
lexer;
|
|
1210
|
+
// set by the lexer
|
|
1211
|
+
constructor(options2) {
|
|
1212
|
+
this.options = options2 || _defaults;
|
|
1213
|
+
}
|
|
1214
|
+
space(src) {
|
|
1215
|
+
const cap = this.rules.block.newline.exec(src);
|
|
1216
|
+
if (cap && cap[0].length > 0) {
|
|
1217
|
+
return {
|
|
1218
|
+
type: "space",
|
|
1219
|
+
raw: cap[0]
|
|
1220
|
+
};
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
code(src) {
|
|
1224
|
+
const cap = this.rules.block.code.exec(src);
|
|
1225
|
+
if (cap) {
|
|
1226
|
+
const text = cap[0].replace(this.rules.other.codeRemoveIndent, "");
|
|
1227
|
+
return {
|
|
1228
|
+
type: "code",
|
|
1229
|
+
raw: cap[0],
|
|
1230
|
+
codeBlockStyle: "indented",
|
|
1231
|
+
text: !this.options.pedantic ? rtrim(text, "\n") : text
|
|
1232
|
+
};
|
|
1233
|
+
}
|
|
1234
|
+
}
|
|
1235
|
+
fences(src) {
|
|
1236
|
+
const cap = this.rules.block.fences.exec(src);
|
|
1237
|
+
if (cap) {
|
|
1238
|
+
const raw = cap[0];
|
|
1239
|
+
const text = indentCodeCompensation(raw, cap[3] || "", this.rules);
|
|
1240
|
+
return {
|
|
1241
|
+
type: "code",
|
|
1242
|
+
raw,
|
|
1243
|
+
lang: cap[2] ? cap[2].trim().replace(this.rules.inline.anyPunctuation, "$1") : cap[2],
|
|
1244
|
+
text
|
|
1245
|
+
};
|
|
1246
|
+
}
|
|
1247
|
+
}
|
|
1248
|
+
heading(src) {
|
|
1249
|
+
const cap = this.rules.block.heading.exec(src);
|
|
1250
|
+
if (cap) {
|
|
1251
|
+
let text = cap[2].trim();
|
|
1252
|
+
if (this.rules.other.endingHash.test(text)) {
|
|
1253
|
+
const trimmed = rtrim(text, "#");
|
|
1254
|
+
if (this.options.pedantic) {
|
|
1255
|
+
text = trimmed.trim();
|
|
1256
|
+
} else if (!trimmed || this.rules.other.endingSpaceChar.test(trimmed)) {
|
|
1257
|
+
text = trimmed.trim();
|
|
1258
|
+
}
|
|
1259
|
+
}
|
|
1260
|
+
return {
|
|
1261
|
+
type: "heading",
|
|
1262
|
+
raw: cap[0],
|
|
1263
|
+
depth: cap[1].length,
|
|
1264
|
+
text,
|
|
1265
|
+
tokens: this.lexer.inline(text)
|
|
1266
|
+
};
|
|
1267
|
+
}
|
|
1268
|
+
}
|
|
1269
|
+
hr(src) {
|
|
1270
|
+
const cap = this.rules.block.hr.exec(src);
|
|
1271
|
+
if (cap) {
|
|
1272
|
+
return {
|
|
1273
|
+
type: "hr",
|
|
1274
|
+
raw: rtrim(cap[0], "\n")
|
|
1275
|
+
};
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1278
|
+
blockquote(src) {
|
|
1279
|
+
const cap = this.rules.block.blockquote.exec(src);
|
|
1280
|
+
if (cap) {
|
|
1281
|
+
let lines = rtrim(cap[0], "\n").split("\n");
|
|
1282
|
+
let raw = "";
|
|
1283
|
+
let text = "";
|
|
1284
|
+
const tokens = [];
|
|
1285
|
+
while (lines.length > 0) {
|
|
1286
|
+
let inBlockquote = false;
|
|
1287
|
+
const currentLines = [];
|
|
1288
|
+
let i;
|
|
1289
|
+
for (i = 0; i < lines.length; i++) {
|
|
1290
|
+
if (this.rules.other.blockquoteStart.test(lines[i])) {
|
|
1291
|
+
currentLines.push(lines[i]);
|
|
1292
|
+
inBlockquote = true;
|
|
1293
|
+
} else if (!inBlockquote) {
|
|
1294
|
+
currentLines.push(lines[i]);
|
|
1295
|
+
} else {
|
|
1296
|
+
break;
|
|
1297
|
+
}
|
|
1298
|
+
}
|
|
1299
|
+
lines = lines.slice(i);
|
|
1300
|
+
const currentRaw = currentLines.join("\n");
|
|
1301
|
+
const currentText = currentRaw.replace(this.rules.other.blockquoteSetextReplace, "\n $1").replace(this.rules.other.blockquoteSetextReplace2, "");
|
|
1302
|
+
raw = raw ? `${raw}
|
|
1303
|
+
${currentRaw}` : currentRaw;
|
|
1304
|
+
text = text ? `${text}
|
|
1305
|
+
${currentText}` : currentText;
|
|
1306
|
+
const top = this.lexer.state.top;
|
|
1307
|
+
this.lexer.state.top = true;
|
|
1308
|
+
this.lexer.blockTokens(currentText, tokens, true);
|
|
1309
|
+
this.lexer.state.top = top;
|
|
1310
|
+
if (lines.length === 0) {
|
|
1311
|
+
break;
|
|
1312
|
+
}
|
|
1313
|
+
const lastToken = tokens.at(-1);
|
|
1314
|
+
if (lastToken?.type === "code") {
|
|
1315
|
+
break;
|
|
1316
|
+
} else if (lastToken?.type === "blockquote") {
|
|
1317
|
+
const oldToken = lastToken;
|
|
1318
|
+
const newText = oldToken.raw + "\n" + lines.join("\n");
|
|
1319
|
+
const newToken = this.blockquote(newText);
|
|
1320
|
+
tokens[tokens.length - 1] = newToken;
|
|
1321
|
+
raw = raw.substring(0, raw.length - oldToken.raw.length) + newToken.raw;
|
|
1322
|
+
text = text.substring(0, text.length - oldToken.text.length) + newToken.text;
|
|
1323
|
+
break;
|
|
1324
|
+
} else if (lastToken?.type === "list") {
|
|
1325
|
+
const oldToken = lastToken;
|
|
1326
|
+
const newText = oldToken.raw + "\n" + lines.join("\n");
|
|
1327
|
+
const newToken = this.list(newText);
|
|
1328
|
+
tokens[tokens.length - 1] = newToken;
|
|
1329
|
+
raw = raw.substring(0, raw.length - lastToken.raw.length) + newToken.raw;
|
|
1330
|
+
text = text.substring(0, text.length - oldToken.raw.length) + newToken.raw;
|
|
1331
|
+
lines = newText.substring(tokens.at(-1).raw.length).split("\n");
|
|
1332
|
+
continue;
|
|
1333
|
+
}
|
|
1334
|
+
}
|
|
1335
|
+
return {
|
|
1336
|
+
type: "blockquote",
|
|
1337
|
+
raw,
|
|
1338
|
+
tokens,
|
|
1339
|
+
text
|
|
1340
|
+
};
|
|
1341
|
+
}
|
|
1342
|
+
}
|
|
1343
|
+
list(src) {
|
|
1344
|
+
let cap = this.rules.block.list.exec(src);
|
|
1345
|
+
if (cap) {
|
|
1346
|
+
let bull = cap[1].trim();
|
|
1347
|
+
const isordered = bull.length > 1;
|
|
1348
|
+
const list2 = {
|
|
1349
|
+
type: "list",
|
|
1350
|
+
raw: "",
|
|
1351
|
+
ordered: isordered,
|
|
1352
|
+
start: isordered ? +bull.slice(0, -1) : "",
|
|
1353
|
+
loose: false,
|
|
1354
|
+
items: []
|
|
1355
|
+
};
|
|
1356
|
+
bull = isordered ? `\\d{1,9}\\${bull.slice(-1)}` : `\\${bull}`;
|
|
1357
|
+
if (this.options.pedantic) {
|
|
1358
|
+
bull = isordered ? bull : "[*+-]";
|
|
1359
|
+
}
|
|
1360
|
+
const itemRegex = this.rules.other.listItemRegex(bull);
|
|
1361
|
+
let endsWithBlankLine = false;
|
|
1362
|
+
while (src) {
|
|
1363
|
+
let endEarly = false;
|
|
1364
|
+
let raw = "";
|
|
1365
|
+
let itemContents = "";
|
|
1366
|
+
if (!(cap = itemRegex.exec(src))) {
|
|
1367
|
+
break;
|
|
1368
|
+
}
|
|
1369
|
+
if (this.rules.block.hr.test(src)) {
|
|
1370
|
+
break;
|
|
1371
|
+
}
|
|
1372
|
+
raw = cap[0];
|
|
1373
|
+
src = src.substring(raw.length);
|
|
1374
|
+
let line = cap[2].split("\n", 1)[0].replace(this.rules.other.listReplaceTabs, (t) => " ".repeat(3 * t.length));
|
|
1375
|
+
let nextLine = src.split("\n", 1)[0];
|
|
1376
|
+
let blankLine = !line.trim();
|
|
1377
|
+
let indent = 0;
|
|
1378
|
+
if (this.options.pedantic) {
|
|
1379
|
+
indent = 2;
|
|
1380
|
+
itemContents = line.trimStart();
|
|
1381
|
+
} else if (blankLine) {
|
|
1382
|
+
indent = cap[1].length + 1;
|
|
1383
|
+
} else {
|
|
1384
|
+
indent = cap[2].search(this.rules.other.nonSpaceChar);
|
|
1385
|
+
indent = indent > 4 ? 1 : indent;
|
|
1386
|
+
itemContents = line.slice(indent);
|
|
1387
|
+
indent += cap[1].length;
|
|
1388
|
+
}
|
|
1389
|
+
if (blankLine && this.rules.other.blankLine.test(nextLine)) {
|
|
1390
|
+
raw += nextLine + "\n";
|
|
1391
|
+
src = src.substring(nextLine.length + 1);
|
|
1392
|
+
endEarly = true;
|
|
1393
|
+
}
|
|
1394
|
+
if (!endEarly) {
|
|
1395
|
+
const nextBulletRegex = this.rules.other.nextBulletRegex(indent);
|
|
1396
|
+
const hrRegex = this.rules.other.hrRegex(indent);
|
|
1397
|
+
const fencesBeginRegex = this.rules.other.fencesBeginRegex(indent);
|
|
1398
|
+
const headingBeginRegex = this.rules.other.headingBeginRegex(indent);
|
|
1399
|
+
const htmlBeginRegex = this.rules.other.htmlBeginRegex(indent);
|
|
1400
|
+
while (src) {
|
|
1401
|
+
const rawLine = src.split("\n", 1)[0];
|
|
1402
|
+
let nextLineWithoutTabs;
|
|
1403
|
+
nextLine = rawLine;
|
|
1404
|
+
if (this.options.pedantic) {
|
|
1405
|
+
nextLine = nextLine.replace(this.rules.other.listReplaceNesting, " ");
|
|
1406
|
+
nextLineWithoutTabs = nextLine;
|
|
1407
|
+
} else {
|
|
1408
|
+
nextLineWithoutTabs = nextLine.replace(this.rules.other.tabCharGlobal, " ");
|
|
1409
|
+
}
|
|
1410
|
+
if (fencesBeginRegex.test(nextLine)) {
|
|
1411
|
+
break;
|
|
1412
|
+
}
|
|
1413
|
+
if (headingBeginRegex.test(nextLine)) {
|
|
1414
|
+
break;
|
|
1415
|
+
}
|
|
1416
|
+
if (htmlBeginRegex.test(nextLine)) {
|
|
1417
|
+
break;
|
|
1418
|
+
}
|
|
1419
|
+
if (nextBulletRegex.test(nextLine)) {
|
|
1420
|
+
break;
|
|
1421
|
+
}
|
|
1422
|
+
if (hrRegex.test(nextLine)) {
|
|
1423
|
+
break;
|
|
1424
|
+
}
|
|
1425
|
+
if (nextLineWithoutTabs.search(this.rules.other.nonSpaceChar) >= indent || !nextLine.trim()) {
|
|
1426
|
+
itemContents += "\n" + nextLineWithoutTabs.slice(indent);
|
|
1427
|
+
} else {
|
|
1428
|
+
if (blankLine) {
|
|
1429
|
+
break;
|
|
1430
|
+
}
|
|
1431
|
+
if (line.replace(this.rules.other.tabCharGlobal, " ").search(this.rules.other.nonSpaceChar) >= 4) {
|
|
1432
|
+
break;
|
|
1433
|
+
}
|
|
1434
|
+
if (fencesBeginRegex.test(line)) {
|
|
1435
|
+
break;
|
|
1436
|
+
}
|
|
1437
|
+
if (headingBeginRegex.test(line)) {
|
|
1438
|
+
break;
|
|
1439
|
+
}
|
|
1440
|
+
if (hrRegex.test(line)) {
|
|
1441
|
+
break;
|
|
1442
|
+
}
|
|
1443
|
+
itemContents += "\n" + nextLine;
|
|
1444
|
+
}
|
|
1445
|
+
if (!blankLine && !nextLine.trim()) {
|
|
1446
|
+
blankLine = true;
|
|
1447
|
+
}
|
|
1448
|
+
raw += rawLine + "\n";
|
|
1449
|
+
src = src.substring(rawLine.length + 1);
|
|
1450
|
+
line = nextLineWithoutTabs.slice(indent);
|
|
1451
|
+
}
|
|
1452
|
+
}
|
|
1453
|
+
if (!list2.loose) {
|
|
1454
|
+
if (endsWithBlankLine) {
|
|
1455
|
+
list2.loose = true;
|
|
1456
|
+
} else if (this.rules.other.doubleBlankLine.test(raw)) {
|
|
1457
|
+
endsWithBlankLine = true;
|
|
1458
|
+
}
|
|
1459
|
+
}
|
|
1460
|
+
let istask = null;
|
|
1461
|
+
let ischecked;
|
|
1462
|
+
if (this.options.gfm) {
|
|
1463
|
+
istask = this.rules.other.listIsTask.exec(itemContents);
|
|
1464
|
+
if (istask) {
|
|
1465
|
+
ischecked = istask[0] !== "[ ] ";
|
|
1466
|
+
itemContents = itemContents.replace(this.rules.other.listReplaceTask, "");
|
|
1467
|
+
}
|
|
1468
|
+
}
|
|
1469
|
+
list2.items.push({
|
|
1470
|
+
type: "list_item",
|
|
1471
|
+
raw,
|
|
1472
|
+
task: !!istask,
|
|
1473
|
+
checked: ischecked,
|
|
1474
|
+
loose: false,
|
|
1475
|
+
text: itemContents,
|
|
1476
|
+
tokens: []
|
|
1477
|
+
});
|
|
1478
|
+
list2.raw += raw;
|
|
1479
|
+
}
|
|
1480
|
+
const lastItem = list2.items.at(-1);
|
|
1481
|
+
if (lastItem) {
|
|
1482
|
+
lastItem.raw = lastItem.raw.trimEnd();
|
|
1483
|
+
lastItem.text = lastItem.text.trimEnd();
|
|
1484
|
+
} else {
|
|
1485
|
+
return;
|
|
1486
|
+
}
|
|
1487
|
+
list2.raw = list2.raw.trimEnd();
|
|
1488
|
+
for (let i = 0; i < list2.items.length; i++) {
|
|
1489
|
+
this.lexer.state.top = false;
|
|
1490
|
+
list2.items[i].tokens = this.lexer.blockTokens(list2.items[i].text, []);
|
|
1491
|
+
if (!list2.loose) {
|
|
1492
|
+
const spacers = list2.items[i].tokens.filter((t) => t.type === "space");
|
|
1493
|
+
const hasMultipleLineBreaks = spacers.length > 0 && spacers.some((t) => this.rules.other.anyLine.test(t.raw));
|
|
1494
|
+
list2.loose = hasMultipleLineBreaks;
|
|
1495
|
+
}
|
|
1496
|
+
}
|
|
1497
|
+
if (list2.loose) {
|
|
1498
|
+
for (let i = 0; i < list2.items.length; i++) {
|
|
1499
|
+
list2.items[i].loose = true;
|
|
1500
|
+
}
|
|
1501
|
+
}
|
|
1502
|
+
return list2;
|
|
1503
|
+
}
|
|
1504
|
+
}
|
|
1505
|
+
html(src) {
|
|
1506
|
+
const cap = this.rules.block.html.exec(src);
|
|
1507
|
+
if (cap) {
|
|
1508
|
+
const token = {
|
|
1509
|
+
type: "html",
|
|
1510
|
+
block: true,
|
|
1511
|
+
raw: cap[0],
|
|
1512
|
+
pre: cap[1] === "pre" || cap[1] === "script" || cap[1] === "style",
|
|
1513
|
+
text: cap[0]
|
|
1514
|
+
};
|
|
1515
|
+
return token;
|
|
1516
|
+
}
|
|
1517
|
+
}
|
|
1518
|
+
def(src) {
|
|
1519
|
+
const cap = this.rules.block.def.exec(src);
|
|
1520
|
+
if (cap) {
|
|
1521
|
+
const tag2 = cap[1].toLowerCase().replace(this.rules.other.multipleSpaceGlobal, " ");
|
|
1522
|
+
const href = cap[2] ? cap[2].replace(this.rules.other.hrefBrackets, "$1").replace(this.rules.inline.anyPunctuation, "$1") : "";
|
|
1523
|
+
const title = cap[3] ? cap[3].substring(1, cap[3].length - 1).replace(this.rules.inline.anyPunctuation, "$1") : cap[3];
|
|
1524
|
+
return {
|
|
1525
|
+
type: "def",
|
|
1526
|
+
tag: tag2,
|
|
1527
|
+
raw: cap[0],
|
|
1528
|
+
href,
|
|
1529
|
+
title
|
|
1530
|
+
};
|
|
1531
|
+
}
|
|
1532
|
+
}
|
|
1533
|
+
table(src) {
|
|
1534
|
+
const cap = this.rules.block.table.exec(src);
|
|
1535
|
+
if (!cap) {
|
|
1536
|
+
return;
|
|
1537
|
+
}
|
|
1538
|
+
if (!this.rules.other.tableDelimiter.test(cap[2])) {
|
|
1539
|
+
return;
|
|
1540
|
+
}
|
|
1541
|
+
const headers = splitCells(cap[1]);
|
|
1542
|
+
const aligns = cap[2].replace(this.rules.other.tableAlignChars, "").split("|");
|
|
1543
|
+
const rows = cap[3]?.trim() ? cap[3].replace(this.rules.other.tableRowBlankLine, "").split("\n") : [];
|
|
1544
|
+
const item = {
|
|
1545
|
+
type: "table",
|
|
1546
|
+
raw: cap[0],
|
|
1547
|
+
header: [],
|
|
1548
|
+
align: [],
|
|
1549
|
+
rows: []
|
|
1550
|
+
};
|
|
1551
|
+
if (headers.length !== aligns.length) {
|
|
1552
|
+
return;
|
|
1553
|
+
}
|
|
1554
|
+
for (const align of aligns) {
|
|
1555
|
+
if (this.rules.other.tableAlignRight.test(align)) {
|
|
1556
|
+
item.align.push("right");
|
|
1557
|
+
} else if (this.rules.other.tableAlignCenter.test(align)) {
|
|
1558
|
+
item.align.push("center");
|
|
1559
|
+
} else if (this.rules.other.tableAlignLeft.test(align)) {
|
|
1560
|
+
item.align.push("left");
|
|
1561
|
+
} else {
|
|
1562
|
+
item.align.push(null);
|
|
1563
|
+
}
|
|
1564
|
+
}
|
|
1565
|
+
for (let i = 0; i < headers.length; i++) {
|
|
1566
|
+
item.header.push({
|
|
1567
|
+
text: headers[i],
|
|
1568
|
+
tokens: this.lexer.inline(headers[i]),
|
|
1569
|
+
header: true,
|
|
1570
|
+
align: item.align[i]
|
|
1571
|
+
});
|
|
1572
|
+
}
|
|
1573
|
+
for (const row of rows) {
|
|
1574
|
+
item.rows.push(splitCells(row, item.header.length).map((cell, i) => {
|
|
1575
|
+
return {
|
|
1576
|
+
text: cell,
|
|
1577
|
+
tokens: this.lexer.inline(cell),
|
|
1578
|
+
header: false,
|
|
1579
|
+
align: item.align[i]
|
|
1580
|
+
};
|
|
1581
|
+
}));
|
|
1582
|
+
}
|
|
1583
|
+
return item;
|
|
1584
|
+
}
|
|
1585
|
+
lheading(src) {
|
|
1586
|
+
const cap = this.rules.block.lheading.exec(src);
|
|
1587
|
+
if (cap) {
|
|
1588
|
+
return {
|
|
1589
|
+
type: "heading",
|
|
1590
|
+
raw: cap[0],
|
|
1591
|
+
depth: cap[2].charAt(0) === "=" ? 1 : 2,
|
|
1592
|
+
text: cap[1],
|
|
1593
|
+
tokens: this.lexer.inline(cap[1])
|
|
1594
|
+
};
|
|
1595
|
+
}
|
|
1596
|
+
}
|
|
1597
|
+
paragraph(src) {
|
|
1598
|
+
const cap = this.rules.block.paragraph.exec(src);
|
|
1599
|
+
if (cap) {
|
|
1600
|
+
const text = cap[1].charAt(cap[1].length - 1) === "\n" ? cap[1].slice(0, -1) : cap[1];
|
|
1601
|
+
return {
|
|
1602
|
+
type: "paragraph",
|
|
1603
|
+
raw: cap[0],
|
|
1604
|
+
text,
|
|
1605
|
+
tokens: this.lexer.inline(text)
|
|
1606
|
+
};
|
|
1607
|
+
}
|
|
1608
|
+
}
|
|
1609
|
+
text(src) {
|
|
1610
|
+
const cap = this.rules.block.text.exec(src);
|
|
1611
|
+
if (cap) {
|
|
1612
|
+
return {
|
|
1613
|
+
type: "text",
|
|
1614
|
+
raw: cap[0],
|
|
1615
|
+
text: cap[0],
|
|
1616
|
+
tokens: this.lexer.inline(cap[0])
|
|
1617
|
+
};
|
|
1618
|
+
}
|
|
1619
|
+
}
|
|
1620
|
+
escape(src) {
|
|
1621
|
+
const cap = this.rules.inline.escape.exec(src);
|
|
1622
|
+
if (cap) {
|
|
1623
|
+
return {
|
|
1624
|
+
type: "escape",
|
|
1625
|
+
raw: cap[0],
|
|
1626
|
+
text: cap[1]
|
|
1627
|
+
};
|
|
1628
|
+
}
|
|
1629
|
+
}
|
|
1630
|
+
tag(src) {
|
|
1631
|
+
const cap = this.rules.inline.tag.exec(src);
|
|
1632
|
+
if (cap) {
|
|
1633
|
+
if (!this.lexer.state.inLink && this.rules.other.startATag.test(cap[0])) {
|
|
1634
|
+
this.lexer.state.inLink = true;
|
|
1635
|
+
} else if (this.lexer.state.inLink && this.rules.other.endATag.test(cap[0])) {
|
|
1636
|
+
this.lexer.state.inLink = false;
|
|
1637
|
+
}
|
|
1638
|
+
if (!this.lexer.state.inRawBlock && this.rules.other.startPreScriptTag.test(cap[0])) {
|
|
1639
|
+
this.lexer.state.inRawBlock = true;
|
|
1640
|
+
} else if (this.lexer.state.inRawBlock && this.rules.other.endPreScriptTag.test(cap[0])) {
|
|
1641
|
+
this.lexer.state.inRawBlock = false;
|
|
1642
|
+
}
|
|
1643
|
+
return {
|
|
1644
|
+
type: "html",
|
|
1645
|
+
raw: cap[0],
|
|
1646
|
+
inLink: this.lexer.state.inLink,
|
|
1647
|
+
inRawBlock: this.lexer.state.inRawBlock,
|
|
1648
|
+
block: false,
|
|
1649
|
+
text: cap[0]
|
|
1650
|
+
};
|
|
1651
|
+
}
|
|
1652
|
+
}
|
|
1653
|
+
link(src) {
|
|
1654
|
+
const cap = this.rules.inline.link.exec(src);
|
|
1655
|
+
if (cap) {
|
|
1656
|
+
const trimmedUrl = cap[2].trim();
|
|
1657
|
+
if (!this.options.pedantic && this.rules.other.startAngleBracket.test(trimmedUrl)) {
|
|
1658
|
+
if (!this.rules.other.endAngleBracket.test(trimmedUrl)) {
|
|
1659
|
+
return;
|
|
1660
|
+
}
|
|
1661
|
+
const rtrimSlash = rtrim(trimmedUrl.slice(0, -1), "\\");
|
|
1662
|
+
if ((trimmedUrl.length - rtrimSlash.length) % 2 === 0) {
|
|
1663
|
+
return;
|
|
1664
|
+
}
|
|
1665
|
+
} else {
|
|
1666
|
+
const lastParenIndex = findClosingBracket(cap[2], "()");
|
|
1667
|
+
if (lastParenIndex === -2) {
|
|
1668
|
+
return;
|
|
1669
|
+
}
|
|
1670
|
+
if (lastParenIndex > -1) {
|
|
1671
|
+
const start = cap[0].indexOf("!") === 0 ? 5 : 4;
|
|
1672
|
+
const linkLen = start + cap[1].length + lastParenIndex;
|
|
1673
|
+
cap[2] = cap[2].substring(0, lastParenIndex);
|
|
1674
|
+
cap[0] = cap[0].substring(0, linkLen).trim();
|
|
1675
|
+
cap[3] = "";
|
|
1676
|
+
}
|
|
1677
|
+
}
|
|
1678
|
+
let href = cap[2];
|
|
1679
|
+
let title = "";
|
|
1680
|
+
if (this.options.pedantic) {
|
|
1681
|
+
const link2 = this.rules.other.pedanticHrefTitle.exec(href);
|
|
1682
|
+
if (link2) {
|
|
1683
|
+
href = link2[1];
|
|
1684
|
+
title = link2[3];
|
|
1685
|
+
}
|
|
1686
|
+
} else {
|
|
1687
|
+
title = cap[3] ? cap[3].slice(1, -1) : "";
|
|
1688
|
+
}
|
|
1689
|
+
href = href.trim();
|
|
1690
|
+
if (this.rules.other.startAngleBracket.test(href)) {
|
|
1691
|
+
if (this.options.pedantic && !this.rules.other.endAngleBracket.test(trimmedUrl)) {
|
|
1692
|
+
href = href.slice(1);
|
|
1693
|
+
} else {
|
|
1694
|
+
href = href.slice(1, -1);
|
|
1695
|
+
}
|
|
1696
|
+
}
|
|
1697
|
+
return outputLink(cap, {
|
|
1698
|
+
href: href ? href.replace(this.rules.inline.anyPunctuation, "$1") : href,
|
|
1699
|
+
title: title ? title.replace(this.rules.inline.anyPunctuation, "$1") : title
|
|
1700
|
+
}, cap[0], this.lexer, this.rules);
|
|
1701
|
+
}
|
|
1702
|
+
}
|
|
1703
|
+
reflink(src, links) {
|
|
1704
|
+
let cap;
|
|
1705
|
+
if ((cap = this.rules.inline.reflink.exec(src)) || (cap = this.rules.inline.nolink.exec(src))) {
|
|
1706
|
+
const linkString = (cap[2] || cap[1]).replace(this.rules.other.multipleSpaceGlobal, " ");
|
|
1707
|
+
const link2 = links[linkString.toLowerCase()];
|
|
1708
|
+
if (!link2) {
|
|
1709
|
+
const text = cap[0].charAt(0);
|
|
1710
|
+
return {
|
|
1711
|
+
type: "text",
|
|
1712
|
+
raw: text,
|
|
1713
|
+
text
|
|
1714
|
+
};
|
|
1715
|
+
}
|
|
1716
|
+
return outputLink(cap, link2, cap[0], this.lexer, this.rules);
|
|
1717
|
+
}
|
|
1718
|
+
}
|
|
1719
|
+
emStrong(src, maskedSrc, prevChar = "") {
|
|
1720
|
+
let match = this.rules.inline.emStrongLDelim.exec(src);
|
|
1721
|
+
if (!match) return;
|
|
1722
|
+
if (match[3] && prevChar.match(this.rules.other.unicodeAlphaNumeric)) return;
|
|
1723
|
+
const nextChar = match[1] || match[2] || "";
|
|
1724
|
+
if (!nextChar || !prevChar || this.rules.inline.punctuation.exec(prevChar)) {
|
|
1725
|
+
const lLength = [...match[0]].length - 1;
|
|
1726
|
+
let rDelim, rLength, delimTotal = lLength, midDelimTotal = 0;
|
|
1727
|
+
const endReg = match[0][0] === "*" ? this.rules.inline.emStrongRDelimAst : this.rules.inline.emStrongRDelimUnd;
|
|
1728
|
+
endReg.lastIndex = 0;
|
|
1729
|
+
maskedSrc = maskedSrc.slice(-1 * src.length + lLength);
|
|
1730
|
+
while ((match = endReg.exec(maskedSrc)) != null) {
|
|
1731
|
+
rDelim = match[1] || match[2] || match[3] || match[4] || match[5] || match[6];
|
|
1732
|
+
if (!rDelim) continue;
|
|
1733
|
+
rLength = [...rDelim].length;
|
|
1734
|
+
if (match[3] || match[4]) {
|
|
1735
|
+
delimTotal += rLength;
|
|
1736
|
+
continue;
|
|
1737
|
+
} else if (match[5] || match[6]) {
|
|
1738
|
+
if (lLength % 3 && !((lLength + rLength) % 3)) {
|
|
1739
|
+
midDelimTotal += rLength;
|
|
1740
|
+
continue;
|
|
1741
|
+
}
|
|
1742
|
+
}
|
|
1743
|
+
delimTotal -= rLength;
|
|
1744
|
+
if (delimTotal > 0) continue;
|
|
1745
|
+
rLength = Math.min(rLength, rLength + delimTotal + midDelimTotal);
|
|
1746
|
+
const lastCharLength = [...match[0]][0].length;
|
|
1747
|
+
const raw = src.slice(0, lLength + match.index + lastCharLength + rLength);
|
|
1748
|
+
if (Math.min(lLength, rLength) % 2) {
|
|
1749
|
+
const text2 = raw.slice(1, -1);
|
|
1750
|
+
return {
|
|
1751
|
+
type: "em",
|
|
1752
|
+
raw,
|
|
1753
|
+
text: text2,
|
|
1754
|
+
tokens: this.lexer.inlineTokens(text2)
|
|
1755
|
+
};
|
|
1756
|
+
}
|
|
1757
|
+
const text = raw.slice(2, -2);
|
|
1758
|
+
return {
|
|
1759
|
+
type: "strong",
|
|
1760
|
+
raw,
|
|
1761
|
+
text,
|
|
1762
|
+
tokens: this.lexer.inlineTokens(text)
|
|
1763
|
+
};
|
|
1764
|
+
}
|
|
1765
|
+
}
|
|
1766
|
+
}
|
|
1767
|
+
codespan(src) {
|
|
1768
|
+
const cap = this.rules.inline.code.exec(src);
|
|
1769
|
+
if (cap) {
|
|
1770
|
+
let text = cap[2].replace(this.rules.other.newLineCharGlobal, " ");
|
|
1771
|
+
const hasNonSpaceChars = this.rules.other.nonSpaceChar.test(text);
|
|
1772
|
+
const hasSpaceCharsOnBothEnds = this.rules.other.startingSpaceChar.test(text) && this.rules.other.endingSpaceChar.test(text);
|
|
1773
|
+
if (hasNonSpaceChars && hasSpaceCharsOnBothEnds) {
|
|
1774
|
+
text = text.substring(1, text.length - 1);
|
|
1775
|
+
}
|
|
1776
|
+
return {
|
|
1777
|
+
type: "codespan",
|
|
1778
|
+
raw: cap[0],
|
|
1779
|
+
text
|
|
1780
|
+
};
|
|
1781
|
+
}
|
|
1782
|
+
}
|
|
1783
|
+
br(src) {
|
|
1784
|
+
const cap = this.rules.inline.br.exec(src);
|
|
1785
|
+
if (cap) {
|
|
1786
|
+
return {
|
|
1787
|
+
type: "br",
|
|
1788
|
+
raw: cap[0]
|
|
1789
|
+
};
|
|
1790
|
+
}
|
|
1791
|
+
}
|
|
1792
|
+
del(src) {
|
|
1793
|
+
const cap = this.rules.inline.del.exec(src);
|
|
1794
|
+
if (cap) {
|
|
1795
|
+
return {
|
|
1796
|
+
type: "del",
|
|
1797
|
+
raw: cap[0],
|
|
1798
|
+
text: cap[2],
|
|
1799
|
+
tokens: this.lexer.inlineTokens(cap[2])
|
|
1800
|
+
};
|
|
1801
|
+
}
|
|
1802
|
+
}
|
|
1803
|
+
autolink(src) {
|
|
1804
|
+
const cap = this.rules.inline.autolink.exec(src);
|
|
1805
|
+
if (cap) {
|
|
1806
|
+
let text, href;
|
|
1807
|
+
if (cap[2] === "@") {
|
|
1808
|
+
text = cap[1];
|
|
1809
|
+
href = "mailto:" + text;
|
|
1810
|
+
} else {
|
|
1811
|
+
text = cap[1];
|
|
1812
|
+
href = text;
|
|
1813
|
+
}
|
|
1814
|
+
return {
|
|
1815
|
+
type: "link",
|
|
1816
|
+
raw: cap[0],
|
|
1817
|
+
text,
|
|
1818
|
+
href,
|
|
1819
|
+
tokens: [
|
|
1820
|
+
{
|
|
1821
|
+
type: "text",
|
|
1822
|
+
raw: text,
|
|
1823
|
+
text
|
|
1824
|
+
}
|
|
1825
|
+
]
|
|
1826
|
+
};
|
|
1827
|
+
}
|
|
1828
|
+
}
|
|
1829
|
+
url(src) {
|
|
1830
|
+
let cap;
|
|
1831
|
+
if (cap = this.rules.inline.url.exec(src)) {
|
|
1832
|
+
let text, href;
|
|
1833
|
+
if (cap[2] === "@") {
|
|
1834
|
+
text = cap[0];
|
|
1835
|
+
href = "mailto:" + text;
|
|
1836
|
+
} else {
|
|
1837
|
+
let prevCapZero;
|
|
1838
|
+
do {
|
|
1839
|
+
prevCapZero = cap[0];
|
|
1840
|
+
cap[0] = this.rules.inline._backpedal.exec(cap[0])?.[0] ?? "";
|
|
1841
|
+
} while (prevCapZero !== cap[0]);
|
|
1842
|
+
text = cap[0];
|
|
1843
|
+
if (cap[1] === "www.") {
|
|
1844
|
+
href = "http://" + cap[0];
|
|
1845
|
+
} else {
|
|
1846
|
+
href = cap[0];
|
|
1847
|
+
}
|
|
1848
|
+
}
|
|
1849
|
+
return {
|
|
1850
|
+
type: "link",
|
|
1851
|
+
raw: cap[0],
|
|
1852
|
+
text,
|
|
1853
|
+
href,
|
|
1854
|
+
tokens: [
|
|
1855
|
+
{
|
|
1856
|
+
type: "text",
|
|
1857
|
+
raw: text,
|
|
1858
|
+
text
|
|
1859
|
+
}
|
|
1860
|
+
]
|
|
1861
|
+
};
|
|
1862
|
+
}
|
|
1863
|
+
}
|
|
1864
|
+
inlineText(src) {
|
|
1865
|
+
const cap = this.rules.inline.text.exec(src);
|
|
1866
|
+
if (cap) {
|
|
1867
|
+
const escaped = this.lexer.state.inRawBlock;
|
|
1868
|
+
return {
|
|
1869
|
+
type: "text",
|
|
1870
|
+
raw: cap[0],
|
|
1871
|
+
text: cap[0],
|
|
1872
|
+
escaped
|
|
1873
|
+
};
|
|
1874
|
+
}
|
|
1875
|
+
}
|
|
1876
|
+
};
|
|
1877
|
+
var _Lexer = class __Lexer {
|
|
1878
|
+
tokens;
|
|
1879
|
+
options;
|
|
1880
|
+
state;
|
|
1881
|
+
tokenizer;
|
|
1882
|
+
inlineQueue;
|
|
1883
|
+
constructor(options2) {
|
|
1884
|
+
this.tokens = [];
|
|
1885
|
+
this.tokens.links = /* @__PURE__ */ Object.create(null);
|
|
1886
|
+
this.options = options2 || _defaults;
|
|
1887
|
+
this.options.tokenizer = this.options.tokenizer || new _Tokenizer();
|
|
1888
|
+
this.tokenizer = this.options.tokenizer;
|
|
1889
|
+
this.tokenizer.options = this.options;
|
|
1890
|
+
this.tokenizer.lexer = this;
|
|
1891
|
+
this.inlineQueue = [];
|
|
1892
|
+
this.state = {
|
|
1893
|
+
inLink: false,
|
|
1894
|
+
inRawBlock: false,
|
|
1895
|
+
top: true
|
|
1896
|
+
};
|
|
1897
|
+
const rules = {
|
|
1898
|
+
other,
|
|
1899
|
+
block: block.normal,
|
|
1900
|
+
inline: inline.normal
|
|
1901
|
+
};
|
|
1902
|
+
if (this.options.pedantic) {
|
|
1903
|
+
rules.block = block.pedantic;
|
|
1904
|
+
rules.inline = inline.pedantic;
|
|
1905
|
+
} else if (this.options.gfm) {
|
|
1906
|
+
rules.block = block.gfm;
|
|
1907
|
+
if (this.options.breaks) {
|
|
1908
|
+
rules.inline = inline.breaks;
|
|
1909
|
+
} else {
|
|
1910
|
+
rules.inline = inline.gfm;
|
|
1911
|
+
}
|
|
1912
|
+
}
|
|
1913
|
+
this.tokenizer.rules = rules;
|
|
1914
|
+
}
|
|
1915
|
+
/**
|
|
1916
|
+
* Expose Rules
|
|
1917
|
+
*/
|
|
1918
|
+
static get rules() {
|
|
1919
|
+
return {
|
|
1920
|
+
block,
|
|
1921
|
+
inline
|
|
1922
|
+
};
|
|
1923
|
+
}
|
|
1924
|
+
/**
|
|
1925
|
+
* Static Lex Method
|
|
1926
|
+
*/
|
|
1927
|
+
static lex(src, options2) {
|
|
1928
|
+
const lexer2 = new __Lexer(options2);
|
|
1929
|
+
return lexer2.lex(src);
|
|
1930
|
+
}
|
|
1931
|
+
/**
|
|
1932
|
+
* Static Lex Inline Method
|
|
1933
|
+
*/
|
|
1934
|
+
static lexInline(src, options2) {
|
|
1935
|
+
const lexer2 = new __Lexer(options2);
|
|
1936
|
+
return lexer2.inlineTokens(src);
|
|
1937
|
+
}
|
|
1938
|
+
/**
|
|
1939
|
+
* Preprocessing
|
|
1940
|
+
*/
|
|
1941
|
+
lex(src) {
|
|
1942
|
+
src = src.replace(other.carriageReturn, "\n");
|
|
1943
|
+
this.blockTokens(src, this.tokens);
|
|
1944
|
+
for (let i = 0; i < this.inlineQueue.length; i++) {
|
|
1945
|
+
const next = this.inlineQueue[i];
|
|
1946
|
+
this.inlineTokens(next.src, next.tokens);
|
|
1947
|
+
}
|
|
1948
|
+
this.inlineQueue = [];
|
|
1949
|
+
return this.tokens;
|
|
1950
|
+
}
|
|
1951
|
+
blockTokens(src, tokens = [], lastParagraphClipped = false) {
|
|
1952
|
+
if (this.options.pedantic) {
|
|
1953
|
+
src = src.replace(other.tabCharGlobal, " ").replace(other.spaceLine, "");
|
|
1954
|
+
}
|
|
1955
|
+
while (src) {
|
|
1956
|
+
let token;
|
|
1957
|
+
if (this.options.extensions?.block?.some((extTokenizer) => {
|
|
1958
|
+
if (token = extTokenizer.call({ lexer: this }, src, tokens)) {
|
|
1959
|
+
src = src.substring(token.raw.length);
|
|
1960
|
+
tokens.push(token);
|
|
1961
|
+
return true;
|
|
1962
|
+
}
|
|
1963
|
+
return false;
|
|
1964
|
+
})) {
|
|
1965
|
+
continue;
|
|
1966
|
+
}
|
|
1967
|
+
if (token = this.tokenizer.space(src)) {
|
|
1968
|
+
src = src.substring(token.raw.length);
|
|
1969
|
+
const lastToken = tokens.at(-1);
|
|
1970
|
+
if (token.raw.length === 1 && lastToken !== void 0) {
|
|
1971
|
+
lastToken.raw += "\n";
|
|
1972
|
+
} else {
|
|
1973
|
+
tokens.push(token);
|
|
1974
|
+
}
|
|
1975
|
+
continue;
|
|
1976
|
+
}
|
|
1977
|
+
if (token = this.tokenizer.code(src)) {
|
|
1978
|
+
src = src.substring(token.raw.length);
|
|
1979
|
+
const lastToken = tokens.at(-1);
|
|
1980
|
+
if (lastToken?.type === "paragraph" || lastToken?.type === "text") {
|
|
1981
|
+
lastToken.raw += "\n" + token.raw;
|
|
1982
|
+
lastToken.text += "\n" + token.text;
|
|
1983
|
+
this.inlineQueue.at(-1).src = lastToken.text;
|
|
1984
|
+
} else {
|
|
1985
|
+
tokens.push(token);
|
|
1986
|
+
}
|
|
1987
|
+
continue;
|
|
1988
|
+
}
|
|
1989
|
+
if (token = this.tokenizer.fences(src)) {
|
|
1990
|
+
src = src.substring(token.raw.length);
|
|
1991
|
+
tokens.push(token);
|
|
1992
|
+
continue;
|
|
1993
|
+
}
|
|
1994
|
+
if (token = this.tokenizer.heading(src)) {
|
|
1995
|
+
src = src.substring(token.raw.length);
|
|
1996
|
+
tokens.push(token);
|
|
1997
|
+
continue;
|
|
1998
|
+
}
|
|
1999
|
+
if (token = this.tokenizer.hr(src)) {
|
|
2000
|
+
src = src.substring(token.raw.length);
|
|
2001
|
+
tokens.push(token);
|
|
2002
|
+
continue;
|
|
2003
|
+
}
|
|
2004
|
+
if (token = this.tokenizer.blockquote(src)) {
|
|
2005
|
+
src = src.substring(token.raw.length);
|
|
2006
|
+
tokens.push(token);
|
|
2007
|
+
continue;
|
|
2008
|
+
}
|
|
2009
|
+
if (token = this.tokenizer.list(src)) {
|
|
2010
|
+
src = src.substring(token.raw.length);
|
|
2011
|
+
tokens.push(token);
|
|
2012
|
+
continue;
|
|
2013
|
+
}
|
|
2014
|
+
if (token = this.tokenizer.html(src)) {
|
|
2015
|
+
src = src.substring(token.raw.length);
|
|
2016
|
+
tokens.push(token);
|
|
2017
|
+
continue;
|
|
2018
|
+
}
|
|
2019
|
+
if (token = this.tokenizer.def(src)) {
|
|
2020
|
+
src = src.substring(token.raw.length);
|
|
2021
|
+
const lastToken = tokens.at(-1);
|
|
2022
|
+
if (lastToken?.type === "paragraph" || lastToken?.type === "text") {
|
|
2023
|
+
lastToken.raw += "\n" + token.raw;
|
|
2024
|
+
lastToken.text += "\n" + token.raw;
|
|
2025
|
+
this.inlineQueue.at(-1).src = lastToken.text;
|
|
2026
|
+
} else if (!this.tokens.links[token.tag]) {
|
|
2027
|
+
this.tokens.links[token.tag] = {
|
|
2028
|
+
href: token.href,
|
|
2029
|
+
title: token.title
|
|
2030
|
+
};
|
|
2031
|
+
}
|
|
2032
|
+
continue;
|
|
2033
|
+
}
|
|
2034
|
+
if (token = this.tokenizer.table(src)) {
|
|
2035
|
+
src = src.substring(token.raw.length);
|
|
2036
|
+
tokens.push(token);
|
|
2037
|
+
continue;
|
|
2038
|
+
}
|
|
2039
|
+
if (token = this.tokenizer.lheading(src)) {
|
|
2040
|
+
src = src.substring(token.raw.length);
|
|
2041
|
+
tokens.push(token);
|
|
2042
|
+
continue;
|
|
2043
|
+
}
|
|
2044
|
+
let cutSrc = src;
|
|
2045
|
+
if (this.options.extensions?.startBlock) {
|
|
2046
|
+
let startIndex = Infinity;
|
|
2047
|
+
const tempSrc = src.slice(1);
|
|
2048
|
+
let tempStart;
|
|
2049
|
+
this.options.extensions.startBlock.forEach((getStartIndex) => {
|
|
2050
|
+
tempStart = getStartIndex.call({ lexer: this }, tempSrc);
|
|
2051
|
+
if (typeof tempStart === "number" && tempStart >= 0) {
|
|
2052
|
+
startIndex = Math.min(startIndex, tempStart);
|
|
2053
|
+
}
|
|
2054
|
+
});
|
|
2055
|
+
if (startIndex < Infinity && startIndex >= 0) {
|
|
2056
|
+
cutSrc = src.substring(0, startIndex + 1);
|
|
2057
|
+
}
|
|
2058
|
+
}
|
|
2059
|
+
if (this.state.top && (token = this.tokenizer.paragraph(cutSrc))) {
|
|
2060
|
+
const lastToken = tokens.at(-1);
|
|
2061
|
+
if (lastParagraphClipped && lastToken?.type === "paragraph") {
|
|
2062
|
+
lastToken.raw += "\n" + token.raw;
|
|
2063
|
+
lastToken.text += "\n" + token.text;
|
|
2064
|
+
this.inlineQueue.pop();
|
|
2065
|
+
this.inlineQueue.at(-1).src = lastToken.text;
|
|
2066
|
+
} else {
|
|
2067
|
+
tokens.push(token);
|
|
2068
|
+
}
|
|
2069
|
+
lastParagraphClipped = cutSrc.length !== src.length;
|
|
2070
|
+
src = src.substring(token.raw.length);
|
|
2071
|
+
continue;
|
|
2072
|
+
}
|
|
2073
|
+
if (token = this.tokenizer.text(src)) {
|
|
2074
|
+
src = src.substring(token.raw.length);
|
|
2075
|
+
const lastToken = tokens.at(-1);
|
|
2076
|
+
if (lastToken?.type === "text") {
|
|
2077
|
+
lastToken.raw += "\n" + token.raw;
|
|
2078
|
+
lastToken.text += "\n" + token.text;
|
|
2079
|
+
this.inlineQueue.pop();
|
|
2080
|
+
this.inlineQueue.at(-1).src = lastToken.text;
|
|
2081
|
+
} else {
|
|
2082
|
+
tokens.push(token);
|
|
2083
|
+
}
|
|
2084
|
+
continue;
|
|
2085
|
+
}
|
|
2086
|
+
if (src) {
|
|
2087
|
+
const errMsg = "Infinite loop on byte: " + src.charCodeAt(0);
|
|
2088
|
+
if (this.options.silent) {
|
|
2089
|
+
console.error(errMsg);
|
|
2090
|
+
break;
|
|
2091
|
+
} else {
|
|
2092
|
+
throw new Error(errMsg);
|
|
2093
|
+
}
|
|
2094
|
+
}
|
|
2095
|
+
}
|
|
2096
|
+
this.state.top = true;
|
|
2097
|
+
return tokens;
|
|
2098
|
+
}
|
|
2099
|
+
inline(src, tokens = []) {
|
|
2100
|
+
this.inlineQueue.push({ src, tokens });
|
|
2101
|
+
return tokens;
|
|
2102
|
+
}
|
|
2103
|
+
/**
|
|
2104
|
+
* Lexing/Compiling
|
|
2105
|
+
*/
|
|
2106
|
+
inlineTokens(src, tokens = []) {
|
|
2107
|
+
let maskedSrc = src;
|
|
2108
|
+
let match = null;
|
|
2109
|
+
if (this.tokens.links) {
|
|
2110
|
+
const links = Object.keys(this.tokens.links);
|
|
2111
|
+
if (links.length > 0) {
|
|
2112
|
+
while ((match = this.tokenizer.rules.inline.reflinkSearch.exec(maskedSrc)) != null) {
|
|
2113
|
+
if (links.includes(match[0].slice(match[0].lastIndexOf("[") + 1, -1))) {
|
|
2114
|
+
maskedSrc = maskedSrc.slice(0, match.index) + "[" + "a".repeat(match[0].length - 2) + "]" + maskedSrc.slice(this.tokenizer.rules.inline.reflinkSearch.lastIndex);
|
|
2115
|
+
}
|
|
2116
|
+
}
|
|
2117
|
+
}
|
|
2118
|
+
}
|
|
2119
|
+
while ((match = this.tokenizer.rules.inline.anyPunctuation.exec(maskedSrc)) != null) {
|
|
2120
|
+
maskedSrc = maskedSrc.slice(0, match.index) + "++" + maskedSrc.slice(this.tokenizer.rules.inline.anyPunctuation.lastIndex);
|
|
2121
|
+
}
|
|
2122
|
+
while ((match = this.tokenizer.rules.inline.blockSkip.exec(maskedSrc)) != null) {
|
|
2123
|
+
maskedSrc = maskedSrc.slice(0, match.index) + "[" + "a".repeat(match[0].length - 2) + "]" + maskedSrc.slice(this.tokenizer.rules.inline.blockSkip.lastIndex);
|
|
2124
|
+
}
|
|
2125
|
+
let keepPrevChar = false;
|
|
2126
|
+
let prevChar = "";
|
|
2127
|
+
while (src) {
|
|
2128
|
+
if (!keepPrevChar) {
|
|
2129
|
+
prevChar = "";
|
|
2130
|
+
}
|
|
2131
|
+
keepPrevChar = false;
|
|
2132
|
+
let token;
|
|
2133
|
+
if (this.options.extensions?.inline?.some((extTokenizer) => {
|
|
2134
|
+
if (token = extTokenizer.call({ lexer: this }, src, tokens)) {
|
|
2135
|
+
src = src.substring(token.raw.length);
|
|
2136
|
+
tokens.push(token);
|
|
2137
|
+
return true;
|
|
2138
|
+
}
|
|
2139
|
+
return false;
|
|
2140
|
+
})) {
|
|
2141
|
+
continue;
|
|
2142
|
+
}
|
|
2143
|
+
if (token = this.tokenizer.escape(src)) {
|
|
2144
|
+
src = src.substring(token.raw.length);
|
|
2145
|
+
tokens.push(token);
|
|
2146
|
+
continue;
|
|
2147
|
+
}
|
|
2148
|
+
if (token = this.tokenizer.tag(src)) {
|
|
2149
|
+
src = src.substring(token.raw.length);
|
|
2150
|
+
tokens.push(token);
|
|
2151
|
+
continue;
|
|
2152
|
+
}
|
|
2153
|
+
if (token = this.tokenizer.link(src)) {
|
|
2154
|
+
src = src.substring(token.raw.length);
|
|
2155
|
+
tokens.push(token);
|
|
2156
|
+
continue;
|
|
2157
|
+
}
|
|
2158
|
+
if (token = this.tokenizer.reflink(src, this.tokens.links)) {
|
|
2159
|
+
src = src.substring(token.raw.length);
|
|
2160
|
+
const lastToken = tokens.at(-1);
|
|
2161
|
+
if (token.type === "text" && lastToken?.type === "text") {
|
|
2162
|
+
lastToken.raw += token.raw;
|
|
2163
|
+
lastToken.text += token.text;
|
|
2164
|
+
} else {
|
|
2165
|
+
tokens.push(token);
|
|
2166
|
+
}
|
|
2167
|
+
continue;
|
|
2168
|
+
}
|
|
2169
|
+
if (token = this.tokenizer.emStrong(src, maskedSrc, prevChar)) {
|
|
2170
|
+
src = src.substring(token.raw.length);
|
|
2171
|
+
tokens.push(token);
|
|
2172
|
+
continue;
|
|
2173
|
+
}
|
|
2174
|
+
if (token = this.tokenizer.codespan(src)) {
|
|
2175
|
+
src = src.substring(token.raw.length);
|
|
2176
|
+
tokens.push(token);
|
|
2177
|
+
continue;
|
|
2178
|
+
}
|
|
2179
|
+
if (token = this.tokenizer.br(src)) {
|
|
2180
|
+
src = src.substring(token.raw.length);
|
|
2181
|
+
tokens.push(token);
|
|
2182
|
+
continue;
|
|
2183
|
+
}
|
|
2184
|
+
if (token = this.tokenizer.del(src)) {
|
|
2185
|
+
src = src.substring(token.raw.length);
|
|
2186
|
+
tokens.push(token);
|
|
2187
|
+
continue;
|
|
2188
|
+
}
|
|
2189
|
+
if (token = this.tokenizer.autolink(src)) {
|
|
2190
|
+
src = src.substring(token.raw.length);
|
|
2191
|
+
tokens.push(token);
|
|
2192
|
+
continue;
|
|
2193
|
+
}
|
|
2194
|
+
if (!this.state.inLink && (token = this.tokenizer.url(src))) {
|
|
2195
|
+
src = src.substring(token.raw.length);
|
|
2196
|
+
tokens.push(token);
|
|
2197
|
+
continue;
|
|
2198
|
+
}
|
|
2199
|
+
let cutSrc = src;
|
|
2200
|
+
if (this.options.extensions?.startInline) {
|
|
2201
|
+
let startIndex = Infinity;
|
|
2202
|
+
const tempSrc = src.slice(1);
|
|
2203
|
+
let tempStart;
|
|
2204
|
+
this.options.extensions.startInline.forEach((getStartIndex) => {
|
|
2205
|
+
tempStart = getStartIndex.call({ lexer: this }, tempSrc);
|
|
2206
|
+
if (typeof tempStart === "number" && tempStart >= 0) {
|
|
2207
|
+
startIndex = Math.min(startIndex, tempStart);
|
|
2208
|
+
}
|
|
2209
|
+
});
|
|
2210
|
+
if (startIndex < Infinity && startIndex >= 0) {
|
|
2211
|
+
cutSrc = src.substring(0, startIndex + 1);
|
|
2212
|
+
}
|
|
2213
|
+
}
|
|
2214
|
+
if (token = this.tokenizer.inlineText(cutSrc)) {
|
|
2215
|
+
src = src.substring(token.raw.length);
|
|
2216
|
+
if (token.raw.slice(-1) !== "_") {
|
|
2217
|
+
prevChar = token.raw.slice(-1);
|
|
2218
|
+
}
|
|
2219
|
+
keepPrevChar = true;
|
|
2220
|
+
const lastToken = tokens.at(-1);
|
|
2221
|
+
if (lastToken?.type === "text") {
|
|
2222
|
+
lastToken.raw += token.raw;
|
|
2223
|
+
lastToken.text += token.text;
|
|
2224
|
+
} else {
|
|
2225
|
+
tokens.push(token);
|
|
2226
|
+
}
|
|
2227
|
+
continue;
|
|
2228
|
+
}
|
|
2229
|
+
if (src) {
|
|
2230
|
+
const errMsg = "Infinite loop on byte: " + src.charCodeAt(0);
|
|
2231
|
+
if (this.options.silent) {
|
|
2232
|
+
console.error(errMsg);
|
|
2233
|
+
break;
|
|
2234
|
+
} else {
|
|
2235
|
+
throw new Error(errMsg);
|
|
2236
|
+
}
|
|
2237
|
+
}
|
|
2238
|
+
}
|
|
2239
|
+
return tokens;
|
|
2240
|
+
}
|
|
2241
|
+
};
|
|
2242
|
+
var _Renderer = class {
|
|
2243
|
+
options;
|
|
2244
|
+
parser;
|
|
2245
|
+
// set by the parser
|
|
2246
|
+
constructor(options2) {
|
|
2247
|
+
this.options = options2 || _defaults;
|
|
2248
|
+
}
|
|
2249
|
+
space(token) {
|
|
2250
|
+
return "";
|
|
2251
|
+
}
|
|
2252
|
+
code({ text, lang, escaped }) {
|
|
2253
|
+
const langString = (lang || "").match(other.notSpaceStart)?.[0];
|
|
2254
|
+
const code = text.replace(other.endingNewline, "") + "\n";
|
|
2255
|
+
if (!langString) {
|
|
2256
|
+
return "<pre><code>" + (escaped ? code : escape2(code, true)) + "</code></pre>\n";
|
|
2257
|
+
}
|
|
2258
|
+
return '<pre><code class="language-' + escape2(langString) + '">' + (escaped ? code : escape2(code, true)) + "</code></pre>\n";
|
|
2259
|
+
}
|
|
2260
|
+
blockquote({ tokens }) {
|
|
2261
|
+
const body = this.parser.parse(tokens);
|
|
2262
|
+
return `<blockquote>
|
|
2263
|
+
${body}</blockquote>
|
|
2264
|
+
`;
|
|
2265
|
+
}
|
|
2266
|
+
html({ text }) {
|
|
2267
|
+
return text;
|
|
2268
|
+
}
|
|
2269
|
+
heading({ tokens, depth }) {
|
|
2270
|
+
return `<h${depth}>${this.parser.parseInline(tokens)}</h${depth}>
|
|
2271
|
+
`;
|
|
2272
|
+
}
|
|
2273
|
+
hr(token) {
|
|
2274
|
+
return "<hr>\n";
|
|
2275
|
+
}
|
|
2276
|
+
list(token) {
|
|
2277
|
+
const ordered = token.ordered;
|
|
2278
|
+
const start = token.start;
|
|
2279
|
+
let body = "";
|
|
2280
|
+
for (let j = 0; j < token.items.length; j++) {
|
|
2281
|
+
const item = token.items[j];
|
|
2282
|
+
body += this.listitem(item);
|
|
2283
|
+
}
|
|
2284
|
+
const type = ordered ? "ol" : "ul";
|
|
2285
|
+
const startAttr = ordered && start !== 1 ? ' start="' + start + '"' : "";
|
|
2286
|
+
return "<" + type + startAttr + ">\n" + body + "</" + type + ">\n";
|
|
2287
|
+
}
|
|
2288
|
+
listitem(item) {
|
|
2289
|
+
let itemBody = "";
|
|
2290
|
+
if (item.task) {
|
|
2291
|
+
const checkbox = this.checkbox({ checked: !!item.checked });
|
|
2292
|
+
if (item.loose) {
|
|
2293
|
+
if (item.tokens[0]?.type === "paragraph") {
|
|
2294
|
+
item.tokens[0].text = checkbox + " " + item.tokens[0].text;
|
|
2295
|
+
if (item.tokens[0].tokens && item.tokens[0].tokens.length > 0 && item.tokens[0].tokens[0].type === "text") {
|
|
2296
|
+
item.tokens[0].tokens[0].text = checkbox + " " + escape2(item.tokens[0].tokens[0].text);
|
|
2297
|
+
item.tokens[0].tokens[0].escaped = true;
|
|
2298
|
+
}
|
|
2299
|
+
} else {
|
|
2300
|
+
item.tokens.unshift({
|
|
2301
|
+
type: "text",
|
|
2302
|
+
raw: checkbox + " ",
|
|
2303
|
+
text: checkbox + " ",
|
|
2304
|
+
escaped: true
|
|
2305
|
+
});
|
|
2306
|
+
}
|
|
2307
|
+
} else {
|
|
2308
|
+
itemBody += checkbox + " ";
|
|
2309
|
+
}
|
|
2310
|
+
}
|
|
2311
|
+
itemBody += this.parser.parse(item.tokens, !!item.loose);
|
|
2312
|
+
return `<li>${itemBody}</li>
|
|
2313
|
+
`;
|
|
2314
|
+
}
|
|
2315
|
+
checkbox({ checked }) {
|
|
2316
|
+
return "<input " + (checked ? 'checked="" ' : "") + 'disabled="" type="checkbox">';
|
|
2317
|
+
}
|
|
2318
|
+
paragraph({ tokens }) {
|
|
2319
|
+
return `<p>${this.parser.parseInline(tokens)}</p>
|
|
2320
|
+
`;
|
|
2321
|
+
}
|
|
2322
|
+
table(token) {
|
|
2323
|
+
let header = "";
|
|
2324
|
+
let cell = "";
|
|
2325
|
+
for (let j = 0; j < token.header.length; j++) {
|
|
2326
|
+
cell += this.tablecell(token.header[j]);
|
|
2327
|
+
}
|
|
2328
|
+
header += this.tablerow({ text: cell });
|
|
2329
|
+
let body = "";
|
|
2330
|
+
for (let j = 0; j < token.rows.length; j++) {
|
|
2331
|
+
const row = token.rows[j];
|
|
2332
|
+
cell = "";
|
|
2333
|
+
for (let k = 0; k < row.length; k++) {
|
|
2334
|
+
cell += this.tablecell(row[k]);
|
|
2335
|
+
}
|
|
2336
|
+
body += this.tablerow({ text: cell });
|
|
2337
|
+
}
|
|
2338
|
+
if (body) body = `<tbody>${body}</tbody>`;
|
|
2339
|
+
return "<table>\n<thead>\n" + header + "</thead>\n" + body + "</table>\n";
|
|
2340
|
+
}
|
|
2341
|
+
tablerow({ text }) {
|
|
2342
|
+
return `<tr>
|
|
2343
|
+
${text}</tr>
|
|
2344
|
+
`;
|
|
2345
|
+
}
|
|
2346
|
+
tablecell(token) {
|
|
2347
|
+
const content = this.parser.parseInline(token.tokens);
|
|
2348
|
+
const type = token.header ? "th" : "td";
|
|
2349
|
+
const tag2 = token.align ? `<${type} align="${token.align}">` : `<${type}>`;
|
|
2350
|
+
return tag2 + content + `</${type}>
|
|
2351
|
+
`;
|
|
2352
|
+
}
|
|
2353
|
+
/**
|
|
2354
|
+
* span level renderer
|
|
2355
|
+
*/
|
|
2356
|
+
strong({ tokens }) {
|
|
2357
|
+
return `<strong>${this.parser.parseInline(tokens)}</strong>`;
|
|
2358
|
+
}
|
|
2359
|
+
em({ tokens }) {
|
|
2360
|
+
return `<em>${this.parser.parseInline(tokens)}</em>`;
|
|
2361
|
+
}
|
|
2362
|
+
codespan({ text }) {
|
|
2363
|
+
return `<code>${escape2(text, true)}</code>`;
|
|
2364
|
+
}
|
|
2365
|
+
br(token) {
|
|
2366
|
+
return "<br>";
|
|
2367
|
+
}
|
|
2368
|
+
del({ tokens }) {
|
|
2369
|
+
return `<del>${this.parser.parseInline(tokens)}</del>`;
|
|
2370
|
+
}
|
|
2371
|
+
link({ href, title, tokens }) {
|
|
2372
|
+
const text = this.parser.parseInline(tokens);
|
|
2373
|
+
const cleanHref = cleanUrl(href);
|
|
2374
|
+
if (cleanHref === null) {
|
|
2375
|
+
return text;
|
|
2376
|
+
}
|
|
2377
|
+
href = cleanHref;
|
|
2378
|
+
let out = '<a href="' + href + '"';
|
|
2379
|
+
if (title) {
|
|
2380
|
+
out += ' title="' + escape2(title) + '"';
|
|
2381
|
+
}
|
|
2382
|
+
out += ">" + text + "</a>";
|
|
2383
|
+
return out;
|
|
2384
|
+
}
|
|
2385
|
+
image({ href, title, text, tokens }) {
|
|
2386
|
+
if (tokens) {
|
|
2387
|
+
text = this.parser.parseInline(tokens, this.parser.textRenderer);
|
|
2388
|
+
}
|
|
2389
|
+
const cleanHref = cleanUrl(href);
|
|
2390
|
+
if (cleanHref === null) {
|
|
2391
|
+
return escape2(text);
|
|
2392
|
+
}
|
|
2393
|
+
href = cleanHref;
|
|
2394
|
+
let out = `<img src="${href}" alt="${text}"`;
|
|
2395
|
+
if (title) {
|
|
2396
|
+
out += ` title="${escape2(title)}"`;
|
|
2397
|
+
}
|
|
2398
|
+
out += ">";
|
|
2399
|
+
return out;
|
|
2400
|
+
}
|
|
2401
|
+
text(token) {
|
|
2402
|
+
return "tokens" in token && token.tokens ? this.parser.parseInline(token.tokens) : "escaped" in token && token.escaped ? token.text : escape2(token.text);
|
|
2403
|
+
}
|
|
2404
|
+
};
|
|
2405
|
+
var _TextRenderer = class {
|
|
2406
|
+
// no need for block level renderers
|
|
2407
|
+
strong({ text }) {
|
|
2408
|
+
return text;
|
|
2409
|
+
}
|
|
2410
|
+
em({ text }) {
|
|
2411
|
+
return text;
|
|
2412
|
+
}
|
|
2413
|
+
codespan({ text }) {
|
|
2414
|
+
return text;
|
|
2415
|
+
}
|
|
2416
|
+
del({ text }) {
|
|
2417
|
+
return text;
|
|
2418
|
+
}
|
|
2419
|
+
html({ text }) {
|
|
2420
|
+
return text;
|
|
2421
|
+
}
|
|
2422
|
+
text({ text }) {
|
|
2423
|
+
return text;
|
|
2424
|
+
}
|
|
2425
|
+
link({ text }) {
|
|
2426
|
+
return "" + text;
|
|
2427
|
+
}
|
|
2428
|
+
image({ text }) {
|
|
2429
|
+
return "" + text;
|
|
2430
|
+
}
|
|
2431
|
+
br() {
|
|
2432
|
+
return "";
|
|
2433
|
+
}
|
|
2434
|
+
};
|
|
2435
|
+
var _Parser = class __Parser {
|
|
2436
|
+
options;
|
|
2437
|
+
renderer;
|
|
2438
|
+
textRenderer;
|
|
2439
|
+
constructor(options2) {
|
|
2440
|
+
this.options = options2 || _defaults;
|
|
2441
|
+
this.options.renderer = this.options.renderer || new _Renderer();
|
|
2442
|
+
this.renderer = this.options.renderer;
|
|
2443
|
+
this.renderer.options = this.options;
|
|
2444
|
+
this.renderer.parser = this;
|
|
2445
|
+
this.textRenderer = new _TextRenderer();
|
|
2446
|
+
}
|
|
2447
|
+
/**
|
|
2448
|
+
* Static Parse Method
|
|
2449
|
+
*/
|
|
2450
|
+
static parse(tokens, options2) {
|
|
2451
|
+
const parser2 = new __Parser(options2);
|
|
2452
|
+
return parser2.parse(tokens);
|
|
2453
|
+
}
|
|
2454
|
+
/**
|
|
2455
|
+
* Static Parse Inline Method
|
|
2456
|
+
*/
|
|
2457
|
+
static parseInline(tokens, options2) {
|
|
2458
|
+
const parser2 = new __Parser(options2);
|
|
2459
|
+
return parser2.parseInline(tokens);
|
|
2460
|
+
}
|
|
2461
|
+
/**
|
|
2462
|
+
* Parse Loop
|
|
2463
|
+
*/
|
|
2464
|
+
parse(tokens, top = true) {
|
|
2465
|
+
let out = "";
|
|
2466
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
2467
|
+
const anyToken = tokens[i];
|
|
2468
|
+
if (this.options.extensions?.renderers?.[anyToken.type]) {
|
|
2469
|
+
const genericToken = anyToken;
|
|
2470
|
+
const ret = this.options.extensions.renderers[genericToken.type].call({ parser: this }, genericToken);
|
|
2471
|
+
if (ret !== false || !["space", "hr", "heading", "code", "table", "blockquote", "list", "html", "paragraph", "text"].includes(genericToken.type)) {
|
|
2472
|
+
out += ret || "";
|
|
2473
|
+
continue;
|
|
2474
|
+
}
|
|
2475
|
+
}
|
|
2476
|
+
const token = anyToken;
|
|
2477
|
+
switch (token.type) {
|
|
2478
|
+
case "space": {
|
|
2479
|
+
out += this.renderer.space(token);
|
|
2480
|
+
continue;
|
|
2481
|
+
}
|
|
2482
|
+
case "hr": {
|
|
2483
|
+
out += this.renderer.hr(token);
|
|
2484
|
+
continue;
|
|
2485
|
+
}
|
|
2486
|
+
case "heading": {
|
|
2487
|
+
out += this.renderer.heading(token);
|
|
2488
|
+
continue;
|
|
2489
|
+
}
|
|
2490
|
+
case "code": {
|
|
2491
|
+
out += this.renderer.code(token);
|
|
2492
|
+
continue;
|
|
2493
|
+
}
|
|
2494
|
+
case "table": {
|
|
2495
|
+
out += this.renderer.table(token);
|
|
2496
|
+
continue;
|
|
2497
|
+
}
|
|
2498
|
+
case "blockquote": {
|
|
2499
|
+
out += this.renderer.blockquote(token);
|
|
2500
|
+
continue;
|
|
2501
|
+
}
|
|
2502
|
+
case "list": {
|
|
2503
|
+
out += this.renderer.list(token);
|
|
2504
|
+
continue;
|
|
2505
|
+
}
|
|
2506
|
+
case "html": {
|
|
2507
|
+
out += this.renderer.html(token);
|
|
2508
|
+
continue;
|
|
2509
|
+
}
|
|
2510
|
+
case "paragraph": {
|
|
2511
|
+
out += this.renderer.paragraph(token);
|
|
2512
|
+
continue;
|
|
2513
|
+
}
|
|
2514
|
+
case "text": {
|
|
2515
|
+
let textToken = token;
|
|
2516
|
+
let body = this.renderer.text(textToken);
|
|
2517
|
+
while (i + 1 < tokens.length && tokens[i + 1].type === "text") {
|
|
2518
|
+
textToken = tokens[++i];
|
|
2519
|
+
body += "\n" + this.renderer.text(textToken);
|
|
2520
|
+
}
|
|
2521
|
+
if (top) {
|
|
2522
|
+
out += this.renderer.paragraph({
|
|
2523
|
+
type: "paragraph",
|
|
2524
|
+
raw: body,
|
|
2525
|
+
text: body,
|
|
2526
|
+
tokens: [{ type: "text", raw: body, text: body, escaped: true }]
|
|
2527
|
+
});
|
|
2528
|
+
} else {
|
|
2529
|
+
out += body;
|
|
2530
|
+
}
|
|
2531
|
+
continue;
|
|
2532
|
+
}
|
|
2533
|
+
default: {
|
|
2534
|
+
const errMsg = 'Token with "' + token.type + '" type was not found.';
|
|
2535
|
+
if (this.options.silent) {
|
|
2536
|
+
console.error(errMsg);
|
|
2537
|
+
return "";
|
|
2538
|
+
} else {
|
|
2539
|
+
throw new Error(errMsg);
|
|
2540
|
+
}
|
|
2541
|
+
}
|
|
2542
|
+
}
|
|
2543
|
+
}
|
|
2544
|
+
return out;
|
|
2545
|
+
}
|
|
2546
|
+
/**
|
|
2547
|
+
* Parse Inline Tokens
|
|
2548
|
+
*/
|
|
2549
|
+
parseInline(tokens, renderer = this.renderer) {
|
|
2550
|
+
let out = "";
|
|
2551
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
2552
|
+
const anyToken = tokens[i];
|
|
2553
|
+
if (this.options.extensions?.renderers?.[anyToken.type]) {
|
|
2554
|
+
const ret = this.options.extensions.renderers[anyToken.type].call({ parser: this }, anyToken);
|
|
2555
|
+
if (ret !== false || !["escape", "html", "link", "image", "strong", "em", "codespan", "br", "del", "text"].includes(anyToken.type)) {
|
|
2556
|
+
out += ret || "";
|
|
2557
|
+
continue;
|
|
2558
|
+
}
|
|
2559
|
+
}
|
|
2560
|
+
const token = anyToken;
|
|
2561
|
+
switch (token.type) {
|
|
2562
|
+
case "escape": {
|
|
2563
|
+
out += renderer.text(token);
|
|
2564
|
+
break;
|
|
2565
|
+
}
|
|
2566
|
+
case "html": {
|
|
2567
|
+
out += renderer.html(token);
|
|
2568
|
+
break;
|
|
2569
|
+
}
|
|
2570
|
+
case "link": {
|
|
2571
|
+
out += renderer.link(token);
|
|
2572
|
+
break;
|
|
2573
|
+
}
|
|
2574
|
+
case "image": {
|
|
2575
|
+
out += renderer.image(token);
|
|
2576
|
+
break;
|
|
2577
|
+
}
|
|
2578
|
+
case "strong": {
|
|
2579
|
+
out += renderer.strong(token);
|
|
2580
|
+
break;
|
|
2581
|
+
}
|
|
2582
|
+
case "em": {
|
|
2583
|
+
out += renderer.em(token);
|
|
2584
|
+
break;
|
|
2585
|
+
}
|
|
2586
|
+
case "codespan": {
|
|
2587
|
+
out += renderer.codespan(token);
|
|
2588
|
+
break;
|
|
2589
|
+
}
|
|
2590
|
+
case "br": {
|
|
2591
|
+
out += renderer.br(token);
|
|
2592
|
+
break;
|
|
2593
|
+
}
|
|
2594
|
+
case "del": {
|
|
2595
|
+
out += renderer.del(token);
|
|
2596
|
+
break;
|
|
2597
|
+
}
|
|
2598
|
+
case "text": {
|
|
2599
|
+
out += renderer.text(token);
|
|
2600
|
+
break;
|
|
2601
|
+
}
|
|
2602
|
+
default: {
|
|
2603
|
+
const errMsg = 'Token with "' + token.type + '" type was not found.';
|
|
2604
|
+
if (this.options.silent) {
|
|
2605
|
+
console.error(errMsg);
|
|
2606
|
+
return "";
|
|
2607
|
+
} else {
|
|
2608
|
+
throw new Error(errMsg);
|
|
2609
|
+
}
|
|
2610
|
+
}
|
|
2611
|
+
}
|
|
2612
|
+
}
|
|
2613
|
+
return out;
|
|
2614
|
+
}
|
|
2615
|
+
};
|
|
2616
|
+
var _Hooks = class {
|
|
2617
|
+
options;
|
|
2618
|
+
block;
|
|
2619
|
+
constructor(options2) {
|
|
2620
|
+
this.options = options2 || _defaults;
|
|
2621
|
+
}
|
|
2622
|
+
static passThroughHooks = /* @__PURE__ */ new Set([
|
|
2623
|
+
"preprocess",
|
|
2624
|
+
"postprocess",
|
|
2625
|
+
"processAllTokens"
|
|
2626
|
+
]);
|
|
2627
|
+
/**
|
|
2628
|
+
* Process markdown before marked
|
|
2629
|
+
*/
|
|
2630
|
+
preprocess(markdown) {
|
|
2631
|
+
return markdown;
|
|
2632
|
+
}
|
|
2633
|
+
/**
|
|
2634
|
+
* Process HTML after marked is finished
|
|
2635
|
+
*/
|
|
2636
|
+
postprocess(html2) {
|
|
2637
|
+
return html2;
|
|
2638
|
+
}
|
|
2639
|
+
/**
|
|
2640
|
+
* Process all tokens before walk tokens
|
|
2641
|
+
*/
|
|
2642
|
+
processAllTokens(tokens) {
|
|
2643
|
+
return tokens;
|
|
2644
|
+
}
|
|
2645
|
+
/**
|
|
2646
|
+
* Provide function to tokenize markdown
|
|
2647
|
+
*/
|
|
2648
|
+
provideLexer() {
|
|
2649
|
+
return this.block ? _Lexer.lex : _Lexer.lexInline;
|
|
2650
|
+
}
|
|
2651
|
+
/**
|
|
2652
|
+
* Provide function to parse tokens
|
|
2653
|
+
*/
|
|
2654
|
+
provideParser() {
|
|
2655
|
+
return this.block ? _Parser.parse : _Parser.parseInline;
|
|
2656
|
+
}
|
|
2657
|
+
};
|
|
2658
|
+
var Marked = class {
|
|
2659
|
+
defaults = _getDefaults();
|
|
2660
|
+
options = this.setOptions;
|
|
2661
|
+
parse = this.parseMarkdown(true);
|
|
2662
|
+
parseInline = this.parseMarkdown(false);
|
|
2663
|
+
Parser = _Parser;
|
|
2664
|
+
Renderer = _Renderer;
|
|
2665
|
+
TextRenderer = _TextRenderer;
|
|
2666
|
+
Lexer = _Lexer;
|
|
2667
|
+
Tokenizer = _Tokenizer;
|
|
2668
|
+
Hooks = _Hooks;
|
|
2669
|
+
constructor(...args) {
|
|
2670
|
+
this.use(...args);
|
|
2671
|
+
}
|
|
2672
|
+
/**
|
|
2673
|
+
* Run callback for every token
|
|
2674
|
+
*/
|
|
2675
|
+
walkTokens(tokens, callback) {
|
|
2676
|
+
let values = [];
|
|
2677
|
+
for (const token of tokens) {
|
|
2678
|
+
values = values.concat(callback.call(this, token));
|
|
2679
|
+
switch (token.type) {
|
|
2680
|
+
case "table": {
|
|
2681
|
+
const tableToken = token;
|
|
2682
|
+
for (const cell of tableToken.header) {
|
|
2683
|
+
values = values.concat(this.walkTokens(cell.tokens, callback));
|
|
2684
|
+
}
|
|
2685
|
+
for (const row of tableToken.rows) {
|
|
2686
|
+
for (const cell of row) {
|
|
2687
|
+
values = values.concat(this.walkTokens(cell.tokens, callback));
|
|
2688
|
+
}
|
|
2689
|
+
}
|
|
2690
|
+
break;
|
|
2691
|
+
}
|
|
2692
|
+
case "list": {
|
|
2693
|
+
const listToken = token;
|
|
2694
|
+
values = values.concat(this.walkTokens(listToken.items, callback));
|
|
2695
|
+
break;
|
|
2696
|
+
}
|
|
2697
|
+
default: {
|
|
2698
|
+
const genericToken = token;
|
|
2699
|
+
if (this.defaults.extensions?.childTokens?.[genericToken.type]) {
|
|
2700
|
+
this.defaults.extensions.childTokens[genericToken.type].forEach((childTokens) => {
|
|
2701
|
+
const tokens2 = genericToken[childTokens].flat(Infinity);
|
|
2702
|
+
values = values.concat(this.walkTokens(tokens2, callback));
|
|
2703
|
+
});
|
|
2704
|
+
} else if (genericToken.tokens) {
|
|
2705
|
+
values = values.concat(this.walkTokens(genericToken.tokens, callback));
|
|
2706
|
+
}
|
|
2707
|
+
}
|
|
2708
|
+
}
|
|
2709
|
+
}
|
|
2710
|
+
return values;
|
|
2711
|
+
}
|
|
2712
|
+
use(...args) {
|
|
2713
|
+
const extensions = this.defaults.extensions || { renderers: {}, childTokens: {} };
|
|
2714
|
+
args.forEach((pack) => {
|
|
2715
|
+
const opts = { ...pack };
|
|
2716
|
+
opts.async = this.defaults.async || opts.async || false;
|
|
2717
|
+
if (pack.extensions) {
|
|
2718
|
+
pack.extensions.forEach((ext) => {
|
|
2719
|
+
if (!ext.name) {
|
|
2720
|
+
throw new Error("extension name required");
|
|
2721
|
+
}
|
|
2722
|
+
if ("renderer" in ext) {
|
|
2723
|
+
const prevRenderer = extensions.renderers[ext.name];
|
|
2724
|
+
if (prevRenderer) {
|
|
2725
|
+
extensions.renderers[ext.name] = function(...args2) {
|
|
2726
|
+
let ret = ext.renderer.apply(this, args2);
|
|
2727
|
+
if (ret === false) {
|
|
2728
|
+
ret = prevRenderer.apply(this, args2);
|
|
2729
|
+
}
|
|
2730
|
+
return ret;
|
|
2731
|
+
};
|
|
2732
|
+
} else {
|
|
2733
|
+
extensions.renderers[ext.name] = ext.renderer;
|
|
2734
|
+
}
|
|
2735
|
+
}
|
|
2736
|
+
if ("tokenizer" in ext) {
|
|
2737
|
+
if (!ext.level || ext.level !== "block" && ext.level !== "inline") {
|
|
2738
|
+
throw new Error("extension level must be 'block' or 'inline'");
|
|
2739
|
+
}
|
|
2740
|
+
const extLevel = extensions[ext.level];
|
|
2741
|
+
if (extLevel) {
|
|
2742
|
+
extLevel.unshift(ext.tokenizer);
|
|
2743
|
+
} else {
|
|
2744
|
+
extensions[ext.level] = [ext.tokenizer];
|
|
2745
|
+
}
|
|
2746
|
+
if (ext.start) {
|
|
2747
|
+
if (ext.level === "block") {
|
|
2748
|
+
if (extensions.startBlock) {
|
|
2749
|
+
extensions.startBlock.push(ext.start);
|
|
2750
|
+
} else {
|
|
2751
|
+
extensions.startBlock = [ext.start];
|
|
2752
|
+
}
|
|
2753
|
+
} else if (ext.level === "inline") {
|
|
2754
|
+
if (extensions.startInline) {
|
|
2755
|
+
extensions.startInline.push(ext.start);
|
|
2756
|
+
} else {
|
|
2757
|
+
extensions.startInline = [ext.start];
|
|
2758
|
+
}
|
|
2759
|
+
}
|
|
2760
|
+
}
|
|
2761
|
+
}
|
|
2762
|
+
if ("childTokens" in ext && ext.childTokens) {
|
|
2763
|
+
extensions.childTokens[ext.name] = ext.childTokens;
|
|
2764
|
+
}
|
|
2765
|
+
});
|
|
2766
|
+
opts.extensions = extensions;
|
|
2767
|
+
}
|
|
2768
|
+
if (pack.renderer) {
|
|
2769
|
+
const renderer = this.defaults.renderer || new _Renderer(this.defaults);
|
|
2770
|
+
for (const prop in pack.renderer) {
|
|
2771
|
+
if (!(prop in renderer)) {
|
|
2772
|
+
throw new Error(`renderer '${prop}' does not exist`);
|
|
2773
|
+
}
|
|
2774
|
+
if (["options", "parser"].includes(prop)) {
|
|
2775
|
+
continue;
|
|
2776
|
+
}
|
|
2777
|
+
const rendererProp = prop;
|
|
2778
|
+
const rendererFunc = pack.renderer[rendererProp];
|
|
2779
|
+
const prevRenderer = renderer[rendererProp];
|
|
2780
|
+
renderer[rendererProp] = (...args2) => {
|
|
2781
|
+
let ret = rendererFunc.apply(renderer, args2);
|
|
2782
|
+
if (ret === false) {
|
|
2783
|
+
ret = prevRenderer.apply(renderer, args2);
|
|
2784
|
+
}
|
|
2785
|
+
return ret || "";
|
|
2786
|
+
};
|
|
2787
|
+
}
|
|
2788
|
+
opts.renderer = renderer;
|
|
2789
|
+
}
|
|
2790
|
+
if (pack.tokenizer) {
|
|
2791
|
+
const tokenizer = this.defaults.tokenizer || new _Tokenizer(this.defaults);
|
|
2792
|
+
for (const prop in pack.tokenizer) {
|
|
2793
|
+
if (!(prop in tokenizer)) {
|
|
2794
|
+
throw new Error(`tokenizer '${prop}' does not exist`);
|
|
2795
|
+
}
|
|
2796
|
+
if (["options", "rules", "lexer"].includes(prop)) {
|
|
2797
|
+
continue;
|
|
2798
|
+
}
|
|
2799
|
+
const tokenizerProp = prop;
|
|
2800
|
+
const tokenizerFunc = pack.tokenizer[tokenizerProp];
|
|
2801
|
+
const prevTokenizer = tokenizer[tokenizerProp];
|
|
2802
|
+
tokenizer[tokenizerProp] = (...args2) => {
|
|
2803
|
+
let ret = tokenizerFunc.apply(tokenizer, args2);
|
|
2804
|
+
if (ret === false) {
|
|
2805
|
+
ret = prevTokenizer.apply(tokenizer, args2);
|
|
2806
|
+
}
|
|
2807
|
+
return ret;
|
|
2808
|
+
};
|
|
2809
|
+
}
|
|
2810
|
+
opts.tokenizer = tokenizer;
|
|
2811
|
+
}
|
|
2812
|
+
if (pack.hooks) {
|
|
2813
|
+
const hooks = this.defaults.hooks || new _Hooks();
|
|
2814
|
+
for (const prop in pack.hooks) {
|
|
2815
|
+
if (!(prop in hooks)) {
|
|
2816
|
+
throw new Error(`hook '${prop}' does not exist`);
|
|
2817
|
+
}
|
|
2818
|
+
if (["options", "block"].includes(prop)) {
|
|
2819
|
+
continue;
|
|
2820
|
+
}
|
|
2821
|
+
const hooksProp = prop;
|
|
2822
|
+
const hooksFunc = pack.hooks[hooksProp];
|
|
2823
|
+
const prevHook = hooks[hooksProp];
|
|
2824
|
+
if (_Hooks.passThroughHooks.has(prop)) {
|
|
2825
|
+
hooks[hooksProp] = (arg) => {
|
|
2826
|
+
if (this.defaults.async) {
|
|
2827
|
+
return Promise.resolve(hooksFunc.call(hooks, arg)).then((ret2) => {
|
|
2828
|
+
return prevHook.call(hooks, ret2);
|
|
2829
|
+
});
|
|
2830
|
+
}
|
|
2831
|
+
const ret = hooksFunc.call(hooks, arg);
|
|
2832
|
+
return prevHook.call(hooks, ret);
|
|
2833
|
+
};
|
|
2834
|
+
} else {
|
|
2835
|
+
hooks[hooksProp] = (...args2) => {
|
|
2836
|
+
let ret = hooksFunc.apply(hooks, args2);
|
|
2837
|
+
if (ret === false) {
|
|
2838
|
+
ret = prevHook.apply(hooks, args2);
|
|
2839
|
+
}
|
|
2840
|
+
return ret;
|
|
2841
|
+
};
|
|
2842
|
+
}
|
|
2843
|
+
}
|
|
2844
|
+
opts.hooks = hooks;
|
|
2845
|
+
}
|
|
2846
|
+
if (pack.walkTokens) {
|
|
2847
|
+
const walkTokens2 = this.defaults.walkTokens;
|
|
2848
|
+
const packWalktokens = pack.walkTokens;
|
|
2849
|
+
opts.walkTokens = function(token) {
|
|
2850
|
+
let values = [];
|
|
2851
|
+
values.push(packWalktokens.call(this, token));
|
|
2852
|
+
if (walkTokens2) {
|
|
2853
|
+
values = values.concat(walkTokens2.call(this, token));
|
|
2854
|
+
}
|
|
2855
|
+
return values;
|
|
2856
|
+
};
|
|
2857
|
+
}
|
|
2858
|
+
this.defaults = { ...this.defaults, ...opts };
|
|
2859
|
+
});
|
|
2860
|
+
return this;
|
|
2861
|
+
}
|
|
2862
|
+
setOptions(opt) {
|
|
2863
|
+
this.defaults = { ...this.defaults, ...opt };
|
|
2864
|
+
return this;
|
|
2865
|
+
}
|
|
2866
|
+
lexer(src, options2) {
|
|
2867
|
+
return _Lexer.lex(src, options2 ?? this.defaults);
|
|
2868
|
+
}
|
|
2869
|
+
parser(tokens, options2) {
|
|
2870
|
+
return _Parser.parse(tokens, options2 ?? this.defaults);
|
|
2871
|
+
}
|
|
2872
|
+
parseMarkdown(blockType) {
|
|
2873
|
+
const parse2 = (src, options2) => {
|
|
2874
|
+
const origOpt = { ...options2 };
|
|
2875
|
+
const opt = { ...this.defaults, ...origOpt };
|
|
2876
|
+
const throwError = this.onError(!!opt.silent, !!opt.async);
|
|
2877
|
+
if (this.defaults.async === true && origOpt.async === false) {
|
|
2878
|
+
return throwError(new Error("marked(): The async option was set to true by an extension. Remove async: false from the parse options object to return a Promise."));
|
|
2879
|
+
}
|
|
2880
|
+
if (typeof src === "undefined" || src === null) {
|
|
2881
|
+
return throwError(new Error("marked(): input parameter is undefined or null"));
|
|
2882
|
+
}
|
|
2883
|
+
if (typeof src !== "string") {
|
|
2884
|
+
return throwError(new Error("marked(): input parameter is of type " + Object.prototype.toString.call(src) + ", string expected"));
|
|
2885
|
+
}
|
|
2886
|
+
if (opt.hooks) {
|
|
2887
|
+
opt.hooks.options = opt;
|
|
2888
|
+
opt.hooks.block = blockType;
|
|
2889
|
+
}
|
|
2890
|
+
const lexer2 = opt.hooks ? opt.hooks.provideLexer() : blockType ? _Lexer.lex : _Lexer.lexInline;
|
|
2891
|
+
const parser2 = opt.hooks ? opt.hooks.provideParser() : blockType ? _Parser.parse : _Parser.parseInline;
|
|
2892
|
+
if (opt.async) {
|
|
2893
|
+
return Promise.resolve(opt.hooks ? opt.hooks.preprocess(src) : src).then((src2) => lexer2(src2, opt)).then((tokens) => opt.hooks ? opt.hooks.processAllTokens(tokens) : tokens).then((tokens) => opt.walkTokens ? Promise.all(this.walkTokens(tokens, opt.walkTokens)).then(() => tokens) : tokens).then((tokens) => parser2(tokens, opt)).then((html2) => opt.hooks ? opt.hooks.postprocess(html2) : html2).catch(throwError);
|
|
2894
|
+
}
|
|
2895
|
+
try {
|
|
2896
|
+
if (opt.hooks) {
|
|
2897
|
+
src = opt.hooks.preprocess(src);
|
|
2898
|
+
}
|
|
2899
|
+
let tokens = lexer2(src, opt);
|
|
2900
|
+
if (opt.hooks) {
|
|
2901
|
+
tokens = opt.hooks.processAllTokens(tokens);
|
|
2902
|
+
}
|
|
2903
|
+
if (opt.walkTokens) {
|
|
2904
|
+
this.walkTokens(tokens, opt.walkTokens);
|
|
2905
|
+
}
|
|
2906
|
+
let html2 = parser2(tokens, opt);
|
|
2907
|
+
if (opt.hooks) {
|
|
2908
|
+
html2 = opt.hooks.postprocess(html2);
|
|
2909
|
+
}
|
|
2910
|
+
return html2;
|
|
2911
|
+
} catch (e) {
|
|
2912
|
+
return throwError(e);
|
|
2913
|
+
}
|
|
2914
|
+
};
|
|
2915
|
+
return parse2;
|
|
2916
|
+
}
|
|
2917
|
+
onError(silent, async) {
|
|
2918
|
+
return (e) => {
|
|
2919
|
+
e.message += "\nPlease report this to https://github.com/markedjs/marked.";
|
|
2920
|
+
if (silent) {
|
|
2921
|
+
const msg = "<p>An error occurred:</p><pre>" + escape2(e.message + "", true) + "</pre>";
|
|
2922
|
+
if (async) {
|
|
2923
|
+
return Promise.resolve(msg);
|
|
2924
|
+
}
|
|
2925
|
+
return msg;
|
|
2926
|
+
}
|
|
2927
|
+
if (async) {
|
|
2928
|
+
return Promise.reject(e);
|
|
2929
|
+
}
|
|
2930
|
+
throw e;
|
|
2931
|
+
};
|
|
2932
|
+
}
|
|
2933
|
+
};
|
|
2934
|
+
var markedInstance = new Marked();
|
|
2935
|
+
function marked(src, opt) {
|
|
2936
|
+
return markedInstance.parse(src, opt);
|
|
2937
|
+
}
|
|
2938
|
+
marked.options = marked.setOptions = function(options2) {
|
|
2939
|
+
markedInstance.setOptions(options2);
|
|
2940
|
+
marked.defaults = markedInstance.defaults;
|
|
2941
|
+
changeDefaults(marked.defaults);
|
|
2942
|
+
return marked;
|
|
2943
|
+
};
|
|
2944
|
+
marked.getDefaults = _getDefaults;
|
|
2945
|
+
marked.defaults = _defaults;
|
|
2946
|
+
marked.use = function(...args) {
|
|
2947
|
+
markedInstance.use(...args);
|
|
2948
|
+
marked.defaults = markedInstance.defaults;
|
|
2949
|
+
changeDefaults(marked.defaults);
|
|
2950
|
+
return marked;
|
|
2951
|
+
};
|
|
2952
|
+
marked.walkTokens = function(tokens, callback) {
|
|
2953
|
+
return markedInstance.walkTokens(tokens, callback);
|
|
2954
|
+
};
|
|
2955
|
+
marked.parseInline = markedInstance.parseInline;
|
|
2956
|
+
marked.Parser = _Parser;
|
|
2957
|
+
marked.parser = _Parser.parse;
|
|
2958
|
+
marked.Renderer = _Renderer;
|
|
2959
|
+
marked.TextRenderer = _TextRenderer;
|
|
2960
|
+
marked.Lexer = _Lexer;
|
|
2961
|
+
marked.lexer = _Lexer.lex;
|
|
2962
|
+
marked.Tokenizer = _Tokenizer;
|
|
2963
|
+
marked.Hooks = _Hooks;
|
|
2964
|
+
marked.parse = marked;
|
|
2965
|
+
var options = marked.options;
|
|
2966
|
+
var setOptions = marked.setOptions;
|
|
2967
|
+
var use = marked.use;
|
|
2968
|
+
var walkTokens = marked.walkTokens;
|
|
2969
|
+
var parseInline = marked.parseInline;
|
|
2970
|
+
var parser = _Parser.parse;
|
|
2971
|
+
var lexer = _Lexer.lex;
|
|
2972
|
+
|
|
2973
|
+
// src/tui/render/memoize.ts
|
|
2974
|
+
var LruCache = class {
|
|
2975
|
+
constructor(limit) {
|
|
2976
|
+
this.limit = limit;
|
|
2977
|
+
}
|
|
2978
|
+
limit;
|
|
2979
|
+
entries = /* @__PURE__ */ new Map();
|
|
2980
|
+
get(key) {
|
|
2981
|
+
if (!this.entries.has(key)) return void 0;
|
|
2982
|
+
const value = this.entries.get(key);
|
|
2983
|
+
this.entries.delete(key);
|
|
2984
|
+
this.entries.set(key, value);
|
|
2985
|
+
return value;
|
|
2986
|
+
}
|
|
2987
|
+
set(key, value) {
|
|
2988
|
+
this.entries.delete(key);
|
|
2989
|
+
this.entries.set(key, value);
|
|
2990
|
+
while (this.entries.size > this.limit) this.entries.delete(this.entries.keys().next().value);
|
|
2991
|
+
}
|
|
2992
|
+
has(key) {
|
|
2993
|
+
return this.entries.has(key);
|
|
2994
|
+
}
|
|
2995
|
+
get size() {
|
|
2996
|
+
return this.entries.size;
|
|
2997
|
+
}
|
|
2998
|
+
};
|
|
2999
|
+
function memoizeByText(compute, limit = 256) {
|
|
3000
|
+
const cache = new LruCache(limit);
|
|
3001
|
+
return (text) => {
|
|
3002
|
+
const hit = cache.get(text);
|
|
3003
|
+
if (hit) return hit.value;
|
|
3004
|
+
const value = compute(text);
|
|
3005
|
+
cache.set(text, { value });
|
|
3006
|
+
return value;
|
|
3007
|
+
};
|
|
3008
|
+
}
|
|
3009
|
+
|
|
3010
|
+
// src/tui/render/markdown.ts
|
|
3011
|
+
function styleWords(text, style) {
|
|
3012
|
+
return text.split(/(\s+)/).map((part) => part && !/^\s+$/.test(part) ? style(part) : part).join("");
|
|
3013
|
+
}
|
|
3014
|
+
var renderInlineTokens = (tokens, hyperlinks) => {
|
|
3015
|
+
const render = (tokens2, styles = []) => tokens2.map((token) => {
|
|
3016
|
+
const apply = (value, extra = styles) => styleWords(value, (word) => extra.reduce((result, style) => style(result), word));
|
|
3017
|
+
if (token.type === "strong") return render(token.tokens ?? [], [...styles, source_default.bold]);
|
|
3018
|
+
if (token.type === "em") return render(token.tokens ?? [], [...styles, source_default.italic]);
|
|
3019
|
+
if (token.type === "del") return render(token.tokens ?? [], [...styles, source_default.strikethrough]);
|
|
3020
|
+
if (token.type === "codespan") return apply(token.text, [...styles, source_default.cyan]);
|
|
3021
|
+
if (token.type === "link") {
|
|
3022
|
+
const href = String(token.href ?? "");
|
|
3023
|
+
const label = render(token.tokens ?? [], [...styles, source_default.underline]);
|
|
3024
|
+
if (hyperlinks && SAFE_LINK.test(href)) {
|
|
3025
|
+
return styleWords(label, (word) => `${hyperlinkOpen(href)}${word}${HYPERLINK_CLOSE}`);
|
|
3026
|
+
}
|
|
3027
|
+
return token.text === href || `mailto:${token.text}` === href ? label : `${label} ${source_default.dim(`(${href})`)}`;
|
|
3028
|
+
}
|
|
3029
|
+
if (token.type === "image") return `${source_default.magenta(`[image: ${token.text || "attachment"}]`)} ${source_default.dim(`(${token.href})`)}`;
|
|
3030
|
+
if (token.type === "br") return " ";
|
|
3031
|
+
if ("tokens" in token && Array.isArray(token.tokens)) return render(token.tokens, styles);
|
|
3032
|
+
if ("text" in token && typeof token.text === "string") return apply(token.text);
|
|
3033
|
+
return typeof token.raw === "string" ? apply(token.raw) : "";
|
|
3034
|
+
}).join("");
|
|
3035
|
+
return render(tokens);
|
|
3036
|
+
};
|
|
3037
|
+
var lexInline = (text) => _Lexer.lexInline(text, { gfm: true, breaks: false });
|
|
3038
|
+
var renderInlineMarkdownWith = (text, hyperlinks) => renderInlineTokens(lexInline(text), hyperlinks);
|
|
3039
|
+
var renderInlineLinked = memoizeByText((text) => renderInlineMarkdownWith(text, true));
|
|
3040
|
+
var renderInlinePlain = memoizeByText((text) => renderInlineMarkdownWith(text, false));
|
|
3041
|
+
var renderInlineMarkdown = (text) => (linksOn() ? renderInlineLinked : renderInlinePlain)(text);
|
|
3042
|
+
var MAY_PAIR_LATER = /[*_~`[\]<>!\\&]/;
|
|
3043
|
+
var closedInline = (token) => token.type === "text" || token.type === "link" && !token.raw.startsWith("[") ? !MAY_PAIR_LATER.test(token.raw) : ["strong", "em", "del", "codespan", "link", "br", "escape"].includes(token.type);
|
|
3044
|
+
var MASKED = /\[[^[\]]*?\]\((?:\\.|[^\\()]|\((?:\\.|[^\\()])*\))*\)|`[^`]*?`|<[^<>]*?>/g;
|
|
3045
|
+
var pairedInside = (text) => !text.includes("\\") && !/[[`<]/.test(text.replace(MASKED, ""));
|
|
3046
|
+
var liveInline = { hyperlinks: false, prefix: "", rendered: "" };
|
|
3047
|
+
var renderInlineMarkdownLive = (text) => {
|
|
3048
|
+
const hyperlinks = linksOn();
|
|
3049
|
+
if (liveInline.hyperlinks !== hyperlinks || !text.startsWith(liveInline.prefix)) liveInline = { hyperlinks, prefix: "", rendered: "" };
|
|
3050
|
+
const { prefix, rendered } = liveInline;
|
|
3051
|
+
const rest = text.slice(prefix.length);
|
|
3052
|
+
const tokens = lexInline(rest);
|
|
3053
|
+
let split = 0;
|
|
3054
|
+
let offset = 0;
|
|
3055
|
+
for (const token of tokens) {
|
|
3056
|
+
if (!closedInline(token)) break;
|
|
3057
|
+
const end = offset + token.raw.length;
|
|
3058
|
+
const from = token.type === "text" ? offset + 2 : end;
|
|
3059
|
+
for (let at = end; at >= from && at > split; at -= 1) {
|
|
3060
|
+
if (rest[at - 1] === " " && rest[at - 2] !== void 0 && /\S/.test(rest[at - 2]) && rest[at] !== void 0 && /\S/.test(rest[at])) {
|
|
3061
|
+
split = at;
|
|
3062
|
+
break;
|
|
3063
|
+
}
|
|
3064
|
+
}
|
|
3065
|
+
offset = end;
|
|
3066
|
+
}
|
|
3067
|
+
if (split === 0 || !pairedInside(rest.slice(0, split))) return rendered + renderInlineTokens(tokens, hyperlinks);
|
|
3068
|
+
const settled = renderInlineTokens(lexInline(rest.slice(0, split)), hyperlinks);
|
|
3069
|
+
liveInline = { hyperlinks, prefix: prefix + rest.slice(0, split), rendered: rendered + settled };
|
|
3070
|
+
return liveInline.rendered + renderInlineTokens(lexInline(rest.slice(split)), hyperlinks);
|
|
3071
|
+
};
|
|
3072
|
+
var parseBlocks = (text) => {
|
|
3073
|
+
const blocks = [];
|
|
3074
|
+
let hasDefinitions = false;
|
|
3075
|
+
let stable;
|
|
3076
|
+
let previousType = "";
|
|
3077
|
+
const visit = (tokens, sourceEnd2, quoteDepth = 0, listDepth = 0) => {
|
|
3078
|
+
for (const token of tokens) {
|
|
3079
|
+
if (token.type === "def") hasDefinitions = true;
|
|
3080
|
+
if (token.type === "space" || token.type === "def") continue;
|
|
3081
|
+
if (token.type === "code") {
|
|
3082
|
+
const fence = /^ {0,3}(`{3,}|~{3,})/.exec(token.raw)?.[1];
|
|
3083
|
+
const open = fence !== void 0 && !new RegExp(`\\n {0,3}${fence[0]}{${fence.length},}[ \\t]*\\n*$`).test(token.raw);
|
|
3084
|
+
blocks.push({ kind: "code", lines: token.text.split(/\r?\n/), ...token.lang ? { language: token.lang } : {}, quoteDepth, indent: listDepth, sourceEnd: sourceEnd2, ...open ? { open: true } : {} });
|
|
3085
|
+
} else if (token.type === "heading") {
|
|
3086
|
+
blocks.push({ kind: "heading", text: token.text, level: token.depth, quoteDepth, sourceEnd: sourceEnd2 });
|
|
3087
|
+
} else if (token.type === "hr") {
|
|
3088
|
+
blocks.push({ kind: "rule", quoteDepth, sourceEnd: sourceEnd2 });
|
|
3089
|
+
} else if (token.type === "paragraph" || token.type === "text" || token.type === "html") {
|
|
3090
|
+
blocks.push({ kind: "paragraph", text: token.text, quoteDepth, indent: listDepth, sourceEnd: sourceEnd2 });
|
|
3091
|
+
} else if (token.type === "blockquote") {
|
|
3092
|
+
visit(token.tokens ?? [], sourceEnd2, quoteDepth + 1, listDepth);
|
|
3093
|
+
} else if (token.type === "table") {
|
|
3094
|
+
const table = token;
|
|
3095
|
+
blocks.push({
|
|
3096
|
+
kind: "table",
|
|
3097
|
+
header: table.header.map((cell) => cell.text),
|
|
3098
|
+
rows: table.rows.map((row) => row.map((cell) => cell.text)),
|
|
3099
|
+
align: table.align,
|
|
3100
|
+
quoteDepth,
|
|
3101
|
+
sourceEnd: sourceEnd2
|
|
3102
|
+
});
|
|
3103
|
+
} else if (token.type === "list") {
|
|
3104
|
+
const list2 = token;
|
|
3105
|
+
list2.items.forEach((item, index) => {
|
|
3106
|
+
const children = item.tokens ?? [];
|
|
3107
|
+
const primaryIndex = children.findIndex((child) => child.type === "text" || child.type === "paragraph");
|
|
3108
|
+
const primary = primaryIndex >= 0 ? children[primaryIndex] : void 0;
|
|
3109
|
+
const first = primary && "text" in primary && typeof primary.text === "string" ? primary.text : "";
|
|
3110
|
+
blocks.push({
|
|
3111
|
+
kind: "list-item",
|
|
3112
|
+
text: first,
|
|
3113
|
+
depth: listDepth,
|
|
3114
|
+
ordered: list2.ordered,
|
|
3115
|
+
...list2.ordered ? { number: Number(list2.start || 1) + index } : {},
|
|
3116
|
+
task: item.task,
|
|
3117
|
+
...item.task ? { checked: item.checked } : {},
|
|
3118
|
+
quoteDepth,
|
|
3119
|
+
sourceEnd: sourceEnd2
|
|
3120
|
+
});
|
|
3121
|
+
children.forEach((child, childIndex) => {
|
|
3122
|
+
if (childIndex === primaryIndex) return;
|
|
3123
|
+
visit([child], sourceEnd2, quoteDepth, listDepth + 1);
|
|
3124
|
+
});
|
|
3125
|
+
});
|
|
3126
|
+
}
|
|
3127
|
+
}
|
|
3128
|
+
};
|
|
3129
|
+
let sourceEnd = 0;
|
|
3130
|
+
const lexer2 = new _Lexer({ ...marked.defaults, gfm: true, breaks: false });
|
|
3131
|
+
for (const token of lexer2.blockTokens(text.replace(/\r\n|\r/g, "\n"))) {
|
|
3132
|
+
const sourceStart = sourceEnd;
|
|
3133
|
+
sourceEnd += token.raw.length;
|
|
3134
|
+
if (token.type === "space") {
|
|
3135
|
+
const previous = blocks[blocks.length - 1];
|
|
3136
|
+
if (previous) previous.sourceEnd = sourceEnd;
|
|
3137
|
+
previousType = "space";
|
|
3138
|
+
continue;
|
|
3139
|
+
}
|
|
3140
|
+
const blockStart = blocks.length;
|
|
3141
|
+
if (previousType === "space" && blockStart > 0) stable = { offset: sourceStart, blocks: blockStart };
|
|
3142
|
+
previousType = token.type;
|
|
3143
|
+
visit([token], sourceEnd);
|
|
3144
|
+
for (let index = blockStart; index < blocks.length - 1; index++) blocks[index].sourceEnd = sourceStart;
|
|
3145
|
+
const finalBlock = blocks[blocks.length - 1];
|
|
3146
|
+
if (finalBlock && blocks.length > blockStart) finalBlock.blockBoundary = true;
|
|
3147
|
+
}
|
|
3148
|
+
return { blocks, ...stable ? { stable } : {}, hasDefinitions };
|
|
3149
|
+
};
|
|
3150
|
+
var splitIntoBlocks = memoizeByText((text) => parseBlocks(text).blocks);
|
|
3151
|
+
function createStreamingBlockParser() {
|
|
3152
|
+
let stableText = "";
|
|
3153
|
+
let stableBlocks = [];
|
|
3154
|
+
let incremental = true;
|
|
3155
|
+
return (text) => {
|
|
3156
|
+
if (!text.startsWith(stableText)) {
|
|
3157
|
+
stableText = "";
|
|
3158
|
+
stableBlocks = [];
|
|
3159
|
+
incremental = true;
|
|
3160
|
+
}
|
|
3161
|
+
if (!incremental) return parseBlocks(text).blocks;
|
|
3162
|
+
const offset = stableText.length;
|
|
3163
|
+
const tail = parseBlocks(text.slice(offset));
|
|
3164
|
+
if (tail.hasDefinitions) {
|
|
3165
|
+
incremental = false;
|
|
3166
|
+
stableText = "";
|
|
3167
|
+
stableBlocks = [];
|
|
3168
|
+
return parseBlocks(text).blocks;
|
|
3169
|
+
}
|
|
3170
|
+
const shifted = offset === 0 ? tail.blocks : tail.blocks.map((block2) => ({ ...block2, sourceEnd: block2.sourceEnd + offset }));
|
|
3171
|
+
const blocks = [...stableBlocks, ...shifted];
|
|
3172
|
+
if (tail.stable) {
|
|
3173
|
+
stableBlocks = blocks.slice(0, stableBlocks.length + tail.stable.blocks);
|
|
3174
|
+
stableText = text.slice(0, offset + tail.stable.offset);
|
|
3175
|
+
}
|
|
3176
|
+
return blocks;
|
|
3177
|
+
};
|
|
3178
|
+
}
|
|
3179
|
+
function renderTableBlock(header, rows, width, align = []) {
|
|
3180
|
+
const columns = Math.max(1, header.length, ...rows.map((row) => row.length));
|
|
3181
|
+
const borders = columns + 1;
|
|
3182
|
+
const padding = columns * 2;
|
|
3183
|
+
const available = Math.max(columns * 3, width - borders - padding);
|
|
3184
|
+
const rendered = [header, ...rows].map((cells) => Array.from({ length: columns }, (_, index) => renderInlineMarkdown(cells[index] ?? "")));
|
|
3185
|
+
const natural = Array.from({ length: columns }, (_, index) => Math.max(3, ...rendered.map((cells) => terminalCellWidth(cells[index]))));
|
|
3186
|
+
const longestWord = Array.from({ length: columns }, (_, index) => Math.max(3, ...rendered.map((cells) => Math.max(0, ...cells[index].split(/\s+/).map((word) => terminalCellWidth(word))))));
|
|
3187
|
+
const widths = [...natural];
|
|
3188
|
+
let excess = widths.reduce((sum, value) => sum + value, 0) - available;
|
|
3189
|
+
for (const floor of [longestWord, natural.map(() => 3)]) {
|
|
3190
|
+
while (excess > 0) {
|
|
3191
|
+
let widest = -1;
|
|
3192
|
+
for (let index = 0; index < columns; index++) {
|
|
3193
|
+
if (widths[index] > floor[index] && (widest === -1 || widths[index] > widths[widest])) widest = index;
|
|
3194
|
+
}
|
|
3195
|
+
if (widest === -1) break;
|
|
3196
|
+
widths[widest] -= 1;
|
|
3197
|
+
excess -= 1;
|
|
3198
|
+
}
|
|
3199
|
+
}
|
|
3200
|
+
const line = (cells, heading2 = false) => {
|
|
3201
|
+
const wrapped = cells.map((cell, index) => wrapWords(cell, widths[index]));
|
|
3202
|
+
const height = Math.max(1, ...wrapped.map((lines) => lines.length));
|
|
3203
|
+
return Array.from({ length: height }, (_, lineIndex) => `\u2502${wrapped.map((lines, index) => {
|
|
3204
|
+
const text = lines[lineIndex] ?? "";
|
|
3205
|
+
const remaining = Math.max(0, widths[index] - terminalCellWidth(text));
|
|
3206
|
+
const left = align[index] === "right" ? remaining : align[index] === "center" ? Math.floor(remaining / 2) : 0;
|
|
3207
|
+
const padded = `${" ".repeat(left)}${text}${" ".repeat(remaining - left)}`;
|
|
3208
|
+
return ` ${heading2 ? source_default.bold(padded) : padded} `;
|
|
3209
|
+
}).join("\u2502")}\u2502`);
|
|
3210
|
+
};
|
|
3211
|
+
const separator = `\u251C${widths.map((columnWidth) => "\u2500".repeat(columnWidth + 2)).join("\u253C")}\u2524`;
|
|
3212
|
+
return [...line(rendered[0], true), separator, ...rendered.slice(1).flatMap((cells) => line(cells))].map((row) => closeOpenHyperlink(visibleSlice(row, width)));
|
|
3213
|
+
}
|
|
3214
|
+
|
|
3215
|
+
// src/turn/steer-place.ts
|
|
3216
|
+
function steerTextOffset(text, offset) {
|
|
3217
|
+
if (offset <= 0) return 0;
|
|
3218
|
+
if (offset >= text.length) return text.length;
|
|
3219
|
+
for (const block2 of splitIntoBlocks(text)) {
|
|
3220
|
+
if (block2.sourceEnd >= offset) return Math.min(block2.sourceEnd, text.length);
|
|
3221
|
+
}
|
|
3222
|
+
return text.length;
|
|
3223
|
+
}
|
|
3224
|
+
|
|
3225
|
+
// src/turn/checkpoint.ts
|
|
3226
|
+
var MAX_TOUCHED_FILES = 40;
|
|
3227
|
+
var READ_ONLY_TOOL = /^(?:read|view|open|cat|ls|list(?:_?(?:dir|directory|files))?|glob|grep|search|find|fetch|web_?(?:fetch|search)|(?:notebook|file)_?read|read_?(?:file|many_files|notebook)|codebase_?search|semantic_?search|todo_?(?:read|write)|update_?(?:todos?|plan)|think|toolsearch|get_\w+|describe_\w+|lsp\w*)$/i;
|
|
3228
|
+
var CODE_CHANGE_TOOL = /^(?:edit|multi_?edit|write|create|patch|apply_?patch|str_?replace\w*|replace|notebook_?edit|(?:edit|write|create|delete|update|replace_in)_?file|delete|remove|rename|move)$/i;
|
|
3229
|
+
var TOOL_ROW = /^(Read|List|Grep|Glob|Search|Fetch|Web search|Edit|Write|Agent) ([\s\S]+)$/;
|
|
3230
|
+
var MUTATING_ROW = /^(?:Edit|Write|Agent)$/;
|
|
3231
|
+
var MCP_ROW = /^\S+ › ([\w.-]+)/;
|
|
3232
|
+
function activityLabelIsReadOnly(label) {
|
|
3233
|
+
const text = label.trim();
|
|
3234
|
+
if (!text) return true;
|
|
3235
|
+
if (text.startsWith("$ ")) return commandIsReadOnly(text.slice(2));
|
|
3236
|
+
const row = TOOL_ROW.exec(text);
|
|
3237
|
+
if (row) return !MUTATING_ROW.test(row[1]);
|
|
3238
|
+
const mcp = MCP_ROW.exec(text);
|
|
3239
|
+
if (mcp) return READ_ONLY_TOOL.test(mcp[1]) && !CODE_CHANGE_TOOL.test(mcp[1]);
|
|
3240
|
+
const call = /^([\w.:-]+)\(([\s\S]*)\)$/.exec(text);
|
|
3241
|
+
const name = (call?.[1] ?? text).replace(/^mcp__\w+?__|^\w+__/, "");
|
|
3242
|
+
if (/^(?:bash|shell|sh|exec|execute|run|terminal|command|run_?(?:shell_?)?command|run_?terminal_?cmd|execute_?command|developer__shell)$/i.test(call?.[1] ?? "") || /shell$/i.test(call?.[1] ?? "")) {
|
|
3243
|
+
return commandIsReadOnly(call?.[2] ?? "");
|
|
3244
|
+
}
|
|
3245
|
+
if (call || /^[\w.:-]+$/.test(text)) return READ_ONLY_TOOL.test(name) && !CODE_CHANGE_TOOL.test(name);
|
|
3246
|
+
return commandIsReadOnly(text);
|
|
3247
|
+
}
|
|
3248
|
+
function touchedFilesFromActivity(event) {
|
|
3249
|
+
const text = event.label.trim();
|
|
3250
|
+
const row = TOOL_ROW.exec(text);
|
|
3251
|
+
if (row && /^(?:Edit|Write)$/.test(row[1])) {
|
|
3252
|
+
return row[2].replace(/ \([^)]*\)$/, "").replace(/ \+\d+ more$/, "").split(", ").map((file) => file.trim()).filter(Boolean);
|
|
3253
|
+
}
|
|
3254
|
+
const call = /^([\w.:-]+)\(([\s\S]*)\)$/.exec(text);
|
|
3255
|
+
if (!call) return [];
|
|
3256
|
+
const name = call[1].replace(/^mcp__\w+?__|^\w+__/, "");
|
|
3257
|
+
const target = call[2].trim();
|
|
3258
|
+
if (!target || !event.diff && !CODE_CHANGE_TOOL.test(name)) return [];
|
|
3259
|
+
if (/\s/.test(target) && !/[\\/]/.test(target)) return [];
|
|
3260
|
+
return [target];
|
|
3261
|
+
}
|
|
3262
|
+
function remapOffset(streamed, final, offset) {
|
|
3263
|
+
const clamped = Math.max(0, Math.min(streamed.length, offset));
|
|
3264
|
+
if (streamed === final || clamped === 0) return Math.min(clamped, final.length);
|
|
3265
|
+
let shared = 0;
|
|
3266
|
+
const limit = Math.min(streamed.length, final.length);
|
|
3267
|
+
while (shared < limit && streamed.charCodeAt(shared) === final.charCodeAt(shared)) shared += 1;
|
|
3268
|
+
if (clamped <= shared) return clamped;
|
|
3269
|
+
const context = streamed.slice(Math.max(0, clamped - 48), clamped).trim();
|
|
3270
|
+
if (context.length >= 8) {
|
|
3271
|
+
const at = final.indexOf(context);
|
|
3272
|
+
if (at >= 0 && final.indexOf(context, at + 1) < 0) return at + context.length;
|
|
3273
|
+
}
|
|
3274
|
+
return 0;
|
|
3275
|
+
}
|
|
3276
|
+
function remapPendingOffsets(pending, streamed, final) {
|
|
3277
|
+
if (pending.steers?.length) {
|
|
3278
|
+
pending.steers = pending.steers.map((steer) => ({ ...steer, responseOffset: remapOffset(streamed, final, steer.responseOffset ?? 0) }));
|
|
3279
|
+
}
|
|
3280
|
+
const marks = readTurnMarks(pending.marks);
|
|
3281
|
+
if (marks.length) pending.marks = marks.map((mark) => ({ ...mark, responseOffset: remapOffset(streamed, final, mark.responseOffset) }));
|
|
3282
|
+
if (pending.thought) pending.thought = { ...pending.thought, responseOffset: remapOffset(streamed, final, pending.thought.responseOffset) };
|
|
3283
|
+
const activities = readTurnActivities(pending.activities, streamed.length);
|
|
3284
|
+
if (activities.length) {
|
|
3285
|
+
pending.activities = activities.map((activity) => ({ ...activity, responseOffset: remapOffset(streamed, final, activity.responseOffset) }));
|
|
3286
|
+
}
|
|
3287
|
+
}
|
|
3288
|
+
function messageOrigin(session) {
|
|
3289
|
+
return {
|
|
3290
|
+
...session.nativeHarness ? { harness: session.nativeHarness } : {},
|
|
3291
|
+
route: session.route,
|
|
3292
|
+
provider: session.provider,
|
|
3293
|
+
model: session.reported?.model ?? session.model
|
|
3294
|
+
};
|
|
3295
|
+
}
|
|
3296
|
+
function sessionTranscriptMessages(session) {
|
|
3297
|
+
const messages = normalizeImportedTranscript(session.messages ?? []);
|
|
3298
|
+
const pending = session.pendingTurn;
|
|
3299
|
+
if (!pending) return messages;
|
|
3300
|
+
if (pending.prompt !== LEGACY_INTERRUPTED_TURN_REQUEST) {
|
|
3301
|
+
messages.push({ role: "user", content: pending.prompt, ...pending.attachments?.length ? { attachments: [...pending.attachments] } : {}, ...pending.automatic ? { automatic: pending.automatic } : {} });
|
|
3302
|
+
}
|
|
3303
|
+
const response = pending.response ?? "";
|
|
3304
|
+
const recordedActivities = readTurnActivities(pending.activities, response.length);
|
|
3305
|
+
const activities = pending.failedAt ? stopOpenActivities(recordedActivities) : recordedActivities;
|
|
3306
|
+
const marks = readTurnMarks(pending.marks);
|
|
3307
|
+
let first = true;
|
|
3308
|
+
const assistant = (from2, to) => {
|
|
3309
|
+
const text = response.slice(from2.text, to?.text);
|
|
3310
|
+
const content = text.trim() ? text : "";
|
|
3311
|
+
const own = activitiesBetween(activities, from2.offset, to?.offset, content.length, first, from2.seq, to?.seq, from2.text);
|
|
3312
|
+
const ownMarks = marksBetween(marks, from2.offset, to?.offset, content.length, first, from2.seq, to?.seq, from2.text);
|
|
3313
|
+
first = false;
|
|
3314
|
+
if (!content && !own.length && !ownMarks.length) return void 0;
|
|
3315
|
+
return {
|
|
3316
|
+
role: "assistant",
|
|
3317
|
+
content,
|
|
3318
|
+
...own.length ? { activities: own } : {},
|
|
3319
|
+
...ownMarks.length ? { marks: ownMarks } : {},
|
|
3320
|
+
origin: messageOrigin(session)
|
|
3321
|
+
};
|
|
3322
|
+
};
|
|
3323
|
+
let from = { offset: 0, text: 0 };
|
|
3324
|
+
for (const steer of orderMarks(pending.steers ?? [], (item) => ({ responseOffset: item.responseOffset ?? 0, ...item.seq === void 0 ? {} : { seq: item.seq } }))) {
|
|
3325
|
+
const raw = Math.max(from.offset, Math.min(response.length, steer.responseOffset ?? 0));
|
|
3326
|
+
const at = {
|
|
3327
|
+
offset: raw,
|
|
3328
|
+
text: Math.max(from.text, steerTextOffset(response, raw)),
|
|
3329
|
+
...steer.seq === void 0 ? {} : { seq: steer.seq }
|
|
3330
|
+
};
|
|
3331
|
+
const before = assistant(from, at);
|
|
3332
|
+
if (before) messages.push(before);
|
|
3333
|
+
messages.push({ role: "user", content: steer.text, ...steer.id ? { id: steer.id } : {}, ...steer.seq === void 0 ? {} : { seq: steer.seq } });
|
|
3334
|
+
from = at;
|
|
3335
|
+
}
|
|
3336
|
+
const remaining = assistant(from, void 0);
|
|
3337
|
+
if (remaining) messages.push(remaining);
|
|
3338
|
+
return messages;
|
|
3339
|
+
}
|
|
3340
|
+
function settledTranscriptMessages(session, runningPrompt) {
|
|
3341
|
+
const pending = session.pendingTurn;
|
|
3342
|
+
const followed = pending && (runningPrompt === void 0 || pending.prompt.trim() === runningPrompt.trim());
|
|
3343
|
+
return sessionTranscriptMessages(followed ? { ...session, pendingTurn: void 0 } : session);
|
|
3344
|
+
}
|
|
3345
|
+
function runningActivityLabel(pending) {
|
|
3346
|
+
if (pending?.failedAt) return void 0;
|
|
3347
|
+
const open = runningTurnActivity(readTurnActivities(pending?.activities, pending?.response?.length ?? 0));
|
|
3348
|
+
return open ? `running ${open.label}` : void 0;
|
|
3349
|
+
}
|
|
3350
|
+
function beginPendingTurn(session, prompt, now, automatic) {
|
|
3351
|
+
const pending = session.pendingTurn;
|
|
3352
|
+
if (pending && pending.prompt.trim() === prompt.trim() && !pending.outputStarted) {
|
|
3353
|
+
delete pending.failedAt;
|
|
3354
|
+
pending.updatedAt = now;
|
|
3355
|
+
session.updatedAt = now;
|
|
3356
|
+
return;
|
|
3357
|
+
}
|
|
3358
|
+
if (pending) session.messages = sessionTranscriptMessages(session);
|
|
3359
|
+
resetUserWait(session.id);
|
|
3360
|
+
session.pendingTurn = {
|
|
3361
|
+
prompt,
|
|
3362
|
+
...session.attachments?.length ? { attachments: [...session.attachments] } : {},
|
|
3363
|
+
...automatic ? { automatic } : {},
|
|
3364
|
+
startedAt: now,
|
|
3365
|
+
updatedAt: now,
|
|
3366
|
+
outputStarted: false
|
|
3367
|
+
};
|
|
3368
|
+
session.updatedAt = now;
|
|
3369
|
+
}
|
|
3370
|
+
function updatePendingResponse(session, text, mode, now) {
|
|
3371
|
+
const pending = session.pendingTurn;
|
|
3372
|
+
if (!pending || !text && mode === "append") return;
|
|
3373
|
+
if (mode === "replace" && (pending.response ?? "") !== text) remapPendingOffsets(pending, pending.response ?? "", text);
|
|
3374
|
+
if (!text) delete pending.response;
|
|
3375
|
+
else {
|
|
3376
|
+
const before = pending.response ?? "";
|
|
3377
|
+
const next = withoutRecapLine(mode === "replace" ? text : `${before}${text}`);
|
|
3378
|
+
if (next !== (mode === "replace" ? text : `${before}${text}`) && mode === "append") remapPendingOffsets(pending, `${before}${text}`, next);
|
|
3379
|
+
if (next) pending.response = next;
|
|
3380
|
+
else delete pending.response;
|
|
3381
|
+
}
|
|
3382
|
+
pending.outputStarted = Boolean(text || pending.activities?.length);
|
|
3383
|
+
pending.updatedAt = now;
|
|
3384
|
+
}
|
|
3385
|
+
function trackSubagent(pending, event, now) {
|
|
3386
|
+
const running = pending.subagents ?? [];
|
|
3387
|
+
if (event.parentId) {
|
|
3388
|
+
const parent = running.find((agent) => agent.id === event.parentId);
|
|
3389
|
+
if (parent && event.kind === "tool-start") {
|
|
3390
|
+
parent.step = event.label;
|
|
3391
|
+
parent.stepAt = now;
|
|
3392
|
+
}
|
|
3393
|
+
return;
|
|
3394
|
+
}
|
|
3395
|
+
if (!event.id) return;
|
|
3396
|
+
const isAgent = event.agent || event.category !== "run" && isAgentToolName(event.label);
|
|
3397
|
+
if (event.kind === "tool-start" && isAgent) {
|
|
3398
|
+
const existing = running.find((agent) => agent.id === event.id);
|
|
3399
|
+
if (existing) return;
|
|
3400
|
+
pending.subagents = [...running, { id: event.id, label: event.label, startedAt: now }];
|
|
3401
|
+
} else if ((event.kind === "tool-done" || event.kind === "tool-error") && running.some((agent) => agent.id === event.id)) {
|
|
3402
|
+
const left = running.filter((agent) => agent.id !== event.id);
|
|
3403
|
+
if (left.length) pending.subagents = left;
|
|
3404
|
+
else delete pending.subagents;
|
|
3405
|
+
}
|
|
3406
|
+
}
|
|
3407
|
+
function recordPendingActivity(session, event, now) {
|
|
3408
|
+
const pending = session.pendingTurn;
|
|
3409
|
+
if (!pending || event.kind === "thinking") return;
|
|
3410
|
+
trackSubagent(pending, event, now);
|
|
3411
|
+
const hints = pending;
|
|
3412
|
+
for (const touched of touchedFilesFromActivity(event)) {
|
|
3413
|
+
if (!hints.touchedFiles?.includes(touched)) hints.touchedFiles = [...hints.touchedFiles ?? [], touched].slice(-MAX_TOUCHED_FILES);
|
|
3414
|
+
}
|
|
3415
|
+
if (Boolean(event.diff) || !(event.kind !== "tool-start" && event.label === "tool") && !activityLabelIsReadOnly(event.label)) hints.mutatingActivity = true;
|
|
3416
|
+
const activities = readTurnActivities(pending.activities, pending.response?.length ?? 0);
|
|
3417
|
+
const next = recordTurnActivity(activities, event, pending.response?.length ?? 0);
|
|
3418
|
+
if (next === pending.activities || !next.length && !pending.activities) return;
|
|
3419
|
+
pending.activities = next;
|
|
3420
|
+
pending.outputStarted = true;
|
|
3421
|
+
pending.updatedAt = now;
|
|
3422
|
+
}
|
|
3423
|
+
function recordPendingSteer(session, text, submittedAt, responseOffset, now, id, seq) {
|
|
3424
|
+
const pending = session.pendingTurn;
|
|
3425
|
+
if (!pending || !text.trim()) return false;
|
|
3426
|
+
pending.steers = [...pending.steers ?? [], {
|
|
3427
|
+
text: text.trim(),
|
|
3428
|
+
submittedAt,
|
|
3429
|
+
responseOffset: Math.max(0, responseOffset),
|
|
3430
|
+
...id ? { id } : {},
|
|
3431
|
+
...seq === void 0 ? {} : { seq }
|
|
3432
|
+
}];
|
|
3433
|
+
pending.updatedAt = now;
|
|
3434
|
+
return true;
|
|
3435
|
+
}
|
|
3436
|
+
function recordPendingMark(session, mark, now) {
|
|
3437
|
+
const pending = session.pendingTurn;
|
|
3438
|
+
if (!pending) return;
|
|
3439
|
+
pending.marks = upsertMark(readTurnMarks(pending.marks), mark);
|
|
3440
|
+
pending.updatedAt = now;
|
|
3441
|
+
}
|
|
3442
|
+
function setPendingThought(session, thought, now) {
|
|
3443
|
+
const pending = session.pendingTurn;
|
|
3444
|
+
if (!pending) return;
|
|
3445
|
+
if (thought) pending.thought = thought;
|
|
3446
|
+
else delete pending.thought;
|
|
3447
|
+
pending.updatedAt = now;
|
|
3448
|
+
}
|
|
3449
|
+
function settleOpenThought(pending, nowMs) {
|
|
3450
|
+
if (!pending.thought) return;
|
|
3451
|
+
pending.marks = upsertMark(readTurnMarks(pending.marks), settledThought(pending.thought, nowMs));
|
|
3452
|
+
delete pending.thought;
|
|
3453
|
+
}
|
|
3454
|
+
function enqueueSessionTurn(session, submission, now) {
|
|
3455
|
+
if ((session.queuedTurns ?? []).some((item) => item.id === submission.id || submission.kind === "notification" && item.kind === "notification" && item.text === submission.text)) return;
|
|
3456
|
+
session.queuedTurns = [...session.queuedTurns ?? [], submission];
|
|
3457
|
+
session.updatedAt = now;
|
|
3458
|
+
}
|
|
3459
|
+
function consumeSessionTurn(session, id) {
|
|
3460
|
+
const queued = session.queuedTurns ?? [];
|
|
3461
|
+
const next = queued.filter((item) => item.id !== id);
|
|
3462
|
+
if (next.length === queued.length) return false;
|
|
3463
|
+
if (next.length) session.queuedTurns = next;
|
|
3464
|
+
else delete session.queuedTurns;
|
|
3465
|
+
return true;
|
|
3466
|
+
}
|
|
3467
|
+
function durableAnswer(streamed, reported) {
|
|
3468
|
+
const stream = streamed.trim();
|
|
3469
|
+
const report = reported.trim();
|
|
3470
|
+
if (!stream) return report;
|
|
3471
|
+
if (!report) return stream;
|
|
3472
|
+
const flat = (text) => text.replace(/\s+/g, " ").trim();
|
|
3473
|
+
const flatStream = flat(stream);
|
|
3474
|
+
const flatReport = flat(report);
|
|
3475
|
+
if (flatReport.length > flatStream.length && flatReport.includes(flatStream)) return report;
|
|
3476
|
+
return stream;
|
|
3477
|
+
}
|
|
3478
|
+
function finishPendingTurn(session, response, now, stopped = false) {
|
|
3479
|
+
if (!session.pendingTurn) return;
|
|
3480
|
+
settleOpenThought(session.pendingTurn, Date.parse(now));
|
|
3481
|
+
const before = session.messages?.length ?? 0;
|
|
3482
|
+
const startedAt = Date.parse(session.pendingTurn.startedAt);
|
|
3483
|
+
const { accountSwitch, pendingBackground, background } = session.pendingTurn;
|
|
3484
|
+
if (response?.trim() || session.pendingTurn.response?.trim()) {
|
|
3485
|
+
const pending = session.pendingTurn;
|
|
3486
|
+
const streamed = pending.response ?? "";
|
|
3487
|
+
const final = withoutRecapLine(durableAnswer(streamed, response ?? ""));
|
|
3488
|
+
if (streamed !== final) remapPendingOffsets(pending, streamed, final);
|
|
3489
|
+
pending.response = final;
|
|
3490
|
+
pending.outputStarted = true;
|
|
3491
|
+
}
|
|
3492
|
+
session.messages = sessionTranscriptMessages(session);
|
|
3493
|
+
delete session.pendingTurn;
|
|
3494
|
+
session.updatedAt = now;
|
|
3495
|
+
const last = session.messages[session.messages.length - 1];
|
|
3496
|
+
const ms = Date.parse(now) - startedAt - userWaitedMs(session.id, Date.parse(now));
|
|
3497
|
+
if (session.messages.length > before && last?.role === "assistant" && Number.isFinite(ms)) {
|
|
3498
|
+
const { at, ...spent } = session.lastUsage ?? { at: "" };
|
|
3499
|
+
const reported = Date.parse(at) >= startedAt && Object.keys(spent).length ? spent : void 0;
|
|
3500
|
+
last.turnEnd = { ms: Math.max(0, ms), ...stopped ? { stopped: true } : {}, ...reported ? { usage: reported } : {}, ...pendingBackground && pendingBackground > 0 ? { pendingBackground } : {}, ...background ? { background } : {} };
|
|
3501
|
+
if (accountSwitch) last.accountSwitch = accountSwitch;
|
|
3502
|
+
}
|
|
3503
|
+
}
|
|
3504
|
+
function stopPendingCalls(session, nowMs = Date.now()) {
|
|
3505
|
+
const pending = session.pendingTurn;
|
|
3506
|
+
const activities = readTurnActivities(pending?.activities, pending?.response?.length ?? 0);
|
|
3507
|
+
if (pending && activities.length) pending.activities = stopOpenActivities(activities);
|
|
3508
|
+
if (pending) settleOpenThought(pending, nowMs);
|
|
3509
|
+
}
|
|
3510
|
+
function discardPendingTurn(session, prompt) {
|
|
3511
|
+
if (!session.pendingTurn || prompt !== void 0 && session.pendingTurn.prompt !== prompt) return false;
|
|
3512
|
+
delete session.pendingTurn;
|
|
3513
|
+
return true;
|
|
3514
|
+
}
|
|
3515
|
+
function failPendingTurn(session, prompt, now) {
|
|
3516
|
+
if (!session.pendingTurn || session.pendingTurn.prompt.trim() !== prompt.trim()) return false;
|
|
3517
|
+
stopPendingCalls(session, Date.parse(now));
|
|
3518
|
+
session.pendingTurn.failedAt = now;
|
|
3519
|
+
session.updatedAt = now;
|
|
3520
|
+
return true;
|
|
3521
|
+
}
|
|
3522
|
+
|
|
3523
|
+
// src/session/canonical.ts
|
|
3524
|
+
var CANONICAL_RECORD_VERSION = 1;
|
|
3525
|
+
function rowParts(label) {
|
|
3526
|
+
const text = label.trim();
|
|
3527
|
+
if (text.startsWith("$ ")) return { name: "shell", target: text.slice(2) };
|
|
3528
|
+
const mcp = /^(\S+) › (\S+)(?: (.*))?$/.exec(text);
|
|
3529
|
+
if (mcp) return { name: `mcp__${mcp[1]}__${mcp[2]}`, ...mcp[3] ? { target: mcp[3] } : {} };
|
|
3530
|
+
const row = /^(Web search|Read|List|Grep|Glob|Search|Fetch|Edit|Write|Agent) ([\s\S]+)$/.exec(text);
|
|
3531
|
+
if (row) return { name: row[1], target: row[2] };
|
|
3532
|
+
const call = /^([\w.:-]+)\(([\s\S]*)\)$/.exec(text);
|
|
3533
|
+
if (call) return { name: call[1], ...call[2].trim() ? { target: call[2].trim() } : {} };
|
|
3534
|
+
const [first, ...rest] = text.split(" ");
|
|
3535
|
+
return { name: first || "tool", ...rest.length ? { target: rest.join(" ") } : {} };
|
|
3536
|
+
}
|
|
3537
|
+
function canonicalCall(event) {
|
|
3538
|
+
const row = rowParts(event.label);
|
|
3539
|
+
const diff = asFileDiffs(event.diff);
|
|
3540
|
+
const files = unique([
|
|
3541
|
+
...(diff ?? []).flatMap((file) => file.path ? [file.path] : []),
|
|
3542
|
+
...touchedFilesFromActivity(event)
|
|
3543
|
+
]);
|
|
3544
|
+
return {
|
|
3545
|
+
...event.id ? { id: event.id } : {},
|
|
3546
|
+
...event.parentId ? { parentId: event.parentId } : {},
|
|
3547
|
+
...event.category ? { category: event.category } : {},
|
|
3548
|
+
name: event.call?.name ?? row.name,
|
|
3549
|
+
...event.call?.input ? { input: event.call.input } : {},
|
|
3550
|
+
label: event.label,
|
|
3551
|
+
...row.target ? { target: row.target } : {},
|
|
3552
|
+
status: event.kind === "tool-error" ? "failed" : event.kind === "tool-start" ? "unfinished" : "done",
|
|
3553
|
+
...event.output?.length ? { output: [...event.output] } : {},
|
|
3554
|
+
...event.outputOmitted ? { outputOmitted: event.outputOmitted } : {},
|
|
3555
|
+
...event.outputTail ? { outputTail: true } : {},
|
|
3556
|
+
...event.exitCode !== void 0 ? { exitCode: event.exitCode } : {},
|
|
3557
|
+
...diff?.length ? { diff } : {},
|
|
3558
|
+
files,
|
|
3559
|
+
...event.agent ? { agent: true } : {}
|
|
3560
|
+
};
|
|
3561
|
+
}
|
|
3562
|
+
function unique(items) {
|
|
3563
|
+
return [...new Set([...items].map((item) => item.trim()).filter(Boolean))];
|
|
3564
|
+
}
|
|
3565
|
+
function assistantParts(message) {
|
|
3566
|
+
const activities = readTurnActivities(message.activities, message.content.length);
|
|
3567
|
+
const parts = [];
|
|
3568
|
+
let at = 0;
|
|
3569
|
+
const text = (end) => {
|
|
3570
|
+
const slice = message.content.slice(at, end);
|
|
3571
|
+
if (slice.trim()) parts.push({ type: "text", text: slice });
|
|
3572
|
+
at = Math.max(at, end);
|
|
3573
|
+
};
|
|
3574
|
+
for (const activity of orderMarks(activities, (item) => ({ responseOffset: item.responseOffset, ...item.event.seq === void 0 ? {} : { seq: item.event.seq } }))) {
|
|
3575
|
+
text(Math.min(message.content.length, Math.max(0, activity.responseOffset)));
|
|
3576
|
+
parts.push({ type: "tool", call: canonicalCall(activity.event) });
|
|
3577
|
+
}
|
|
3578
|
+
text(message.content.length);
|
|
3579
|
+
return parts;
|
|
3580
|
+
}
|
|
3581
|
+
function originOf(session) {
|
|
3582
|
+
return { sessionId: session.id, ...messageOrigin(session) };
|
|
3583
|
+
}
|
|
3584
|
+
function canonicalRecord(session) {
|
|
3585
|
+
const messages = sessionTranscriptMessages(session);
|
|
3586
|
+
const own = originOf(session);
|
|
3587
|
+
const turns = [];
|
|
3588
|
+
let current;
|
|
3589
|
+
const open = (user, attachments, origin) => {
|
|
3590
|
+
current = {
|
|
3591
|
+
index: turns.length,
|
|
3592
|
+
user,
|
|
3593
|
+
attachments: [...attachments],
|
|
3594
|
+
parts: [],
|
|
3595
|
+
assistant: "",
|
|
3596
|
+
tools: [],
|
|
3597
|
+
touchedFiles: [],
|
|
3598
|
+
interrupted: false,
|
|
3599
|
+
origin
|
|
3600
|
+
};
|
|
3601
|
+
turns.push(current);
|
|
3602
|
+
return current;
|
|
3603
|
+
};
|
|
3604
|
+
messages.forEach((message) => {
|
|
3605
|
+
const origin = message.origin ? { sessionId: session.id, ...message.origin } : own;
|
|
3606
|
+
if (message.role === "user") {
|
|
3607
|
+
open(message.automatic ? "" : message.content, message.attachments ?? [], origin);
|
|
3608
|
+
return;
|
|
3609
|
+
}
|
|
3610
|
+
const turn = current ?? open("", [], origin);
|
|
3611
|
+
turn.origin = origin;
|
|
3612
|
+
turn.parts.push(...assistantParts(message));
|
|
3613
|
+
});
|
|
3614
|
+
for (const turn of turns) {
|
|
3615
|
+
turn.assistant = turn.parts.flatMap((part) => part.type === "text" ? [part.text] : []).join("").trim();
|
|
3616
|
+
turn.tools = turn.parts.flatMap((part) => part.type === "tool" ? [part.call] : []);
|
|
3617
|
+
turn.touchedFiles = unique(turn.tools.flatMap((call) => call.files));
|
|
3618
|
+
}
|
|
3619
|
+
const pending = session.pendingTurn;
|
|
3620
|
+
const last = turns.at(-1);
|
|
3621
|
+
if (pending && last) {
|
|
3622
|
+
last.interrupted = true;
|
|
3623
|
+
last.touchedFiles = unique([...last.touchedFiles, ...pending.touchedFiles ?? []]);
|
|
3624
|
+
}
|
|
3625
|
+
const plan = session.plan?.entries?.length ? session.plan.entries.map((entry) => ({ ...entry })) : void 0;
|
|
3626
|
+
return {
|
|
3627
|
+
version: CANONICAL_RECORD_VERSION,
|
|
3628
|
+
conversationId: conversationIdFor(session),
|
|
3629
|
+
sessionId: session.id,
|
|
3630
|
+
workspace: session.workspace ?? process.cwd(),
|
|
3631
|
+
turns,
|
|
3632
|
+
touchedFiles: unique(turns.flatMap((turn) => turn.touchedFiles)),
|
|
3633
|
+
attachments: unique(turns.flatMap((turn) => turn.attachments)),
|
|
3634
|
+
pendingAttachments: [...session.attachments ?? []],
|
|
3635
|
+
...plan ? { plan } : {},
|
|
3636
|
+
openTodos: (plan ?? []).filter((entry) => !/^(completed|done|complete|finished|cancelled|canceled)$/i.test(entry.status?.trim() ?? ""))
|
|
3637
|
+
};
|
|
3638
|
+
}
|
|
3639
|
+
function originLabel(origin, displayName) {
|
|
3640
|
+
const name = origin.harness ? displayName?.(origin.harness) ?? origin.harness : origin.route ?? origin.provider ?? "ClikCode";
|
|
3641
|
+
return `${name}${origin.model ? ` (${origin.model})` : ""}`;
|
|
3642
|
+
}
|
|
3643
|
+
function originKey(origin) {
|
|
3644
|
+
return origin.harness ?? `${origin.route}:${origin.provider ?? ""}`;
|
|
3645
|
+
}
|
|
3646
|
+
function markProviderBoundaries(record, receiving, displayName) {
|
|
3647
|
+
let previous = receiving;
|
|
3648
|
+
const turns = record.turns.map((turn) => {
|
|
3649
|
+
const key = originKey(turn.origin);
|
|
3650
|
+
const boundary = key !== previous;
|
|
3651
|
+
previous = key;
|
|
3652
|
+
const { providerNote: _stale, ...rest } = turn;
|
|
3653
|
+
return boundary ? { ...rest, providerNote: providerBoundaryNote(originLabel(turn.origin, displayName)) } : rest;
|
|
3654
|
+
});
|
|
3655
|
+
return { ...record, turns };
|
|
3656
|
+
}
|
|
3657
|
+
function withProviderNote(turn, request) {
|
|
3658
|
+
if (!turn.providerNote) return request;
|
|
3659
|
+
return request.trim() ? `${turn.providerNote}
|
|
3660
|
+
|
|
3661
|
+
${request}` : turn.providerNote;
|
|
3662
|
+
}
|
|
3663
|
+
|
|
3664
|
+
export {
|
|
3665
|
+
withoutRecapLine,
|
|
3666
|
+
setUserWaiting,
|
|
3667
|
+
classifyCommand,
|
|
3668
|
+
normalizeSessionTitle,
|
|
3669
|
+
titleFromFirstMessage,
|
|
3670
|
+
stripRepeatedTitles,
|
|
3671
|
+
wrapResourceNote,
|
|
3672
|
+
stripResourceNotes,
|
|
3673
|
+
FAILOVER_PREAMBLE,
|
|
3674
|
+
escapeFailoverContent,
|
|
3675
|
+
failoverPromptRequest,
|
|
3676
|
+
LEGACY_INTERRUPTED_TURN_REQUEST,
|
|
3677
|
+
isClikCodeOpening,
|
|
3678
|
+
normalizeImportedTranscript,
|
|
3679
|
+
keyHint,
|
|
3680
|
+
keyHintFor,
|
|
3681
|
+
tensedLabel,
|
|
3682
|
+
activityResult,
|
|
3683
|
+
exploreSummary,
|
|
3684
|
+
reasoningHeading,
|
|
3685
|
+
reasoningBody,
|
|
3686
|
+
answerWriting,
|
|
3687
|
+
thoughtRow,
|
|
3688
|
+
decisionRow,
|
|
3689
|
+
isApprovalWaitPhase,
|
|
3690
|
+
turnStatus,
|
|
3691
|
+
shimmerLevels,
|
|
3692
|
+
pastePlaceholder,
|
|
3693
|
+
endsWithSummary,
|
|
3694
|
+
turnSummary,
|
|
3695
|
+
backgroundTerminalsLabel,
|
|
3696
|
+
waitingOnCommands,
|
|
3697
|
+
MarkSequencer,
|
|
3698
|
+
thoughtMarkId,
|
|
3699
|
+
settledThought,
|
|
3700
|
+
stepThought,
|
|
3701
|
+
upsertMark,
|
|
3702
|
+
readTurnMarks,
|
|
3703
|
+
wrapCodeLine,
|
|
3704
|
+
wrapWords,
|
|
3705
|
+
wrapWordsLive,
|
|
3706
|
+
noticeLines,
|
|
3707
|
+
renderInlineMarkdown,
|
|
3708
|
+
renderInlineMarkdownLive,
|
|
3709
|
+
splitIntoBlocks,
|
|
3710
|
+
createStreamingBlockParser,
|
|
3711
|
+
renderTableBlock,
|
|
3712
|
+
remapOffset,
|
|
3713
|
+
messageOrigin,
|
|
3714
|
+
sessionTranscriptMessages,
|
|
3715
|
+
settledTranscriptMessages,
|
|
3716
|
+
runningActivityLabel,
|
|
3717
|
+
beginPendingTurn,
|
|
3718
|
+
updatePendingResponse,
|
|
3719
|
+
recordPendingActivity,
|
|
3720
|
+
recordPendingSteer,
|
|
3721
|
+
recordPendingMark,
|
|
3722
|
+
setPendingThought,
|
|
3723
|
+
enqueueSessionTurn,
|
|
3724
|
+
consumeSessionTurn,
|
|
3725
|
+
finishPendingTurn,
|
|
3726
|
+
stopPendingCalls,
|
|
3727
|
+
discardPendingTurn,
|
|
3728
|
+
failPendingTurn,
|
|
3729
|
+
canonicalRecord,
|
|
3730
|
+
markProviderBoundaries,
|
|
3731
|
+
withProviderNote
|
|
3732
|
+
};
|