@kyyinfinite/zapo-message 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (191) hide show
  1. package/CHANGELOG.md +159 -0
  2. package/LICENSE +20 -0
  3. package/README.md +532 -2
  4. package/build.config.json +28 -0
  5. package/dist/airich/airich.d.ts +180 -0
  6. package/dist/airich/airich.js +548 -0
  7. package/dist/airich/context.d.ts +24 -0
  8. package/dist/airich/context.js +48 -0
  9. package/dist/airich/encode.d.ts +3 -0
  10. package/dist/airich/encode.js +16 -0
  11. package/dist/airich/index.d.ts +20 -0
  12. package/dist/airich/index.js +11 -0
  13. package/dist/airich/inline.d.ts +45 -0
  14. package/dist/airich/inline.js +152 -0
  15. package/dist/airich/nodes.d.ts +30 -0
  16. package/dist/airich/nodes.js +121 -0
  17. package/dist/airich/primitives.d.ts +144 -0
  18. package/dist/airich/primitives.js +230 -0
  19. package/dist/airich/protocol.d.ts +35 -0
  20. package/dist/airich/protocol.js +65 -0
  21. package/dist/airich/table.d.ts +22 -0
  22. package/dist/airich/table.js +35 -0
  23. package/dist/airich/tokenizer.d.ts +13 -0
  24. package/dist/airich/tokenizer.js +717 -0
  25. package/dist/airich/transport.d.ts +15 -0
  26. package/dist/airich/transport.js +56 -0
  27. package/dist/branding.d.ts +5 -0
  28. package/dist/branding.js +9 -0
  29. package/dist/btn/builders.d.ts +246 -0
  30. package/dist/btn/builders.js +710 -0
  31. package/dist/btn/media-slot.d.ts +28 -0
  32. package/dist/btn/media-slot.js +323 -0
  33. package/dist/btn/media-source.d.ts +38 -0
  34. package/dist/btn/media-source.js +116 -0
  35. package/dist/btn/sharp-loader.d.ts +5 -0
  36. package/dist/btn/sharp-loader.js +18 -0
  37. package/dist/btn/thumbnail.d.ts +21 -0
  38. package/dist/btn/thumbnail.js +124 -0
  39. package/dist/btn/validators.d.ts +40 -0
  40. package/dist/btn/validators.js +135 -0
  41. package/dist/btn.d.ts +1 -0
  42. package/dist/btn.js +1 -0
  43. package/dist/core/async-send.d.ts +20 -0
  44. package/dist/core/async-send.js +1 -0
  45. package/dist/core/base-builder.d.ts +20 -0
  46. package/dist/core/base-builder.js +67 -0
  47. package/dist/core/errors.d.ts +30 -0
  48. package/dist/core/errors.js +61 -0
  49. package/dist/download.d.ts +24 -0
  50. package/dist/download.js +123 -0
  51. package/dist/index.d.ts +38 -0
  52. package/dist/index.js +22 -0
  53. package/dist/integration/augment.d.ts +97 -0
  54. package/dist/integration/augment.js +1 -0
  55. package/dist/integration/auto.d.ts +8 -0
  56. package/dist/integration/auto.js +79 -0
  57. package/dist/integration/index.d.ts +4 -0
  58. package/dist/integration/index.js +2 -0
  59. package/dist/integration/methods.d.ts +25 -0
  60. package/dist/integration/methods.js +24 -0
  61. package/dist/interactive.d.ts +24 -0
  62. package/dist/interactive.js +121 -0
  63. package/dist/media-gate.d.ts +99 -0
  64. package/dist/media-gate.js +322 -0
  65. package/dist/message/album.d.ts +33 -0
  66. package/dist/message/album.js +107 -0
  67. package/dist/message/contact.d.ts +29 -0
  68. package/dist/message/contact.js +123 -0
  69. package/dist/message/forward.d.ts +23 -0
  70. package/dist/message/forward.js +77 -0
  71. package/dist/message/index.d.ts +26 -0
  72. package/dist/message/index.js +13 -0
  73. package/dist/message/link-preview.d.ts +28 -0
  74. package/dist/message/link-preview.js +98 -0
  75. package/dist/message/location.d.ts +24 -0
  76. package/dist/message/location.js +94 -0
  77. package/dist/message/media.d.ts +90 -0
  78. package/dist/message/media.js +123 -0
  79. package/dist/message/poll.d.ts +24 -0
  80. package/dist/message/poll.js +98 -0
  81. package/dist/message/reaction.d.ts +20 -0
  82. package/dist/message/reaction.js +71 -0
  83. package/dist/message/sticker.d.ts +29 -0
  84. package/dist/message/sticker.js +44 -0
  85. package/dist/message/target.d.ts +18 -0
  86. package/dist/message/target.js +24 -0
  87. package/dist/message/text.d.ts +17 -0
  88. package/dist/message/text.js +29 -0
  89. package/dist/message/vcard.d.ts +9 -0
  90. package/dist/message/vcard.js +23 -0
  91. package/dist/message/webp.d.ts +6 -0
  92. package/dist/message/webp.js +57 -0
  93. package/dist/payloads.d.ts +180 -0
  94. package/dist/payloads.js +1 -0
  95. package/dist/serialize.d.ts +41 -0
  96. package/dist/serialize.js +93 -0
  97. package/dist/status.d.ts +3 -0
  98. package/dist/status.js +21 -0
  99. package/dist/zapo-client.d.ts +44 -0
  100. package/dist/zapo-client.js +1 -0
  101. package/examples/airich.ts +59 -0
  102. package/examples/buttons.ts +48 -0
  103. package/examples/client-methods.ts +58 -0
  104. package/examples/hybrid.ts +133 -0
  105. package/examples/serialize.ts +61 -0
  106. package/package.json +271 -4
  107. package/scripts/check-no-comments.mjs +101 -0
  108. package/scripts/compare-golden.mjs +461 -0
  109. package/scripts/lib-config.mjs +9 -0
  110. package/scripts/smoke-auto.mjs +75 -0
  111. package/scripts/test-external-consumer.mjs +316 -0
  112. package/scripts/verify-package.mjs +78 -0
  113. package/src/airich/airich.ts +970 -0
  114. package/src/airich/context.ts +96 -0
  115. package/src/airich/encode.ts +25 -0
  116. package/src/airich/index.ts +98 -0
  117. package/src/airich/inline.ts +272 -0
  118. package/src/airich/nodes.ts +174 -0
  119. package/src/airich/primitives.ts +474 -0
  120. package/src/airich/protocol.ts +133 -0
  121. package/src/airich/table.ts +70 -0
  122. package/src/airich/tokenizer.ts +799 -0
  123. package/src/airich/transport.ts +106 -0
  124. package/src/branding.ts +23 -0
  125. package/src/btn/builders.ts +1073 -0
  126. package/src/btn/media-slot.ts +486 -0
  127. package/src/btn/media-source.ts +189 -0
  128. package/src/btn/sharp-loader.ts +23 -0
  129. package/src/btn/thumbnail.ts +182 -0
  130. package/src/btn/validators.ts +237 -0
  131. package/src/btn.ts +8 -0
  132. package/src/core/async-send.ts +28 -0
  133. package/src/core/base-builder.ts +104 -0
  134. package/src/core/errors.ts +82 -0
  135. package/src/download.ts +212 -0
  136. package/src/index.ts +229 -0
  137. package/src/integration/augment.ts +262 -0
  138. package/src/integration/auto.ts +103 -0
  139. package/src/integration/index.ts +8 -0
  140. package/src/integration/methods.ts +45 -0
  141. package/src/interactive.ts +188 -0
  142. package/src/media-gate.ts +499 -0
  143. package/src/message/album.ts +187 -0
  144. package/src/message/contact.ts +170 -0
  145. package/src/message/forward.ts +123 -0
  146. package/src/message/index.ts +83 -0
  147. package/src/message/link-preview.ts +148 -0
  148. package/src/message/location.ts +150 -0
  149. package/src/message/media.ts +323 -0
  150. package/src/message/poll.ts +158 -0
  151. package/src/message/reaction.ts +133 -0
  152. package/src/message/sticker.ts +100 -0
  153. package/src/message/target.ts +38 -0
  154. package/src/message/text.ts +61 -0
  155. package/src/message/vcard.ts +36 -0
  156. package/src/message/webp.ts +78 -0
  157. package/src/payloads.ts +204 -0
  158. package/src/serialize.ts +180 -0
  159. package/src/status.ts +29 -0
  160. package/src/zapo-client.ts +84 -0
  161. package/test/airich-inline.test.ts +200 -0
  162. package/test/airich-primitives.test.ts +226 -0
  163. package/test/airich-tokenizer.test.ts +145 -0
  164. package/test/airich.golden.test.ts +406 -0
  165. package/test/airich.test.ts +1006 -0
  166. package/test/btn-button.test.ts +416 -0
  167. package/test/btn-header.test.ts +646 -0
  168. package/test/btn-legacy.test.ts +342 -0
  169. package/test/btn-lifecycle.test.ts +295 -0
  170. package/test/btn-list.test.ts +261 -0
  171. package/test/btn-media-source.test.ts +150 -0
  172. package/test/btn-thumbnail.test.ts +160 -0
  173. package/test/builder-compat.test.ts +264 -0
  174. package/test/helpers.ts +27 -0
  175. package/test/hybrid-airich.test.ts +345 -0
  176. package/test/hybrid-client.test.ts +239 -0
  177. package/test/hybrid-errors.test.ts +557 -0
  178. package/test/hybrid-interactive.test.ts +455 -0
  179. package/test/hybrid.golden.test.ts +349 -0
  180. package/test/hybrid.test.ts +479 -0
  181. package/test/integration-auto.test.ts +159 -0
  182. package/test/integration.fakeserver.test.ts +440 -0
  183. package/test/media-gate.test.ts +215 -0
  184. package/test/message-album.test.ts +145 -0
  185. package/test/message-builders.test.ts +401 -0
  186. package/test/message-media.test.ts +303 -0
  187. package/test/message.fakeserver.test.ts +284 -0
  188. package/test/message.golden.test.ts +254 -0
  189. package/test/serializer.test.ts +448 -0
  190. package/tsconfig.json +26 -0
  191. package/tsconfig.test.json +10 -0
@@ -0,0 +1,799 @@
1
+ export interface CodeBlockToken {
2
+ codeContent: string;
3
+ highlightType: number;
4
+ }
5
+
6
+ export interface UnifiedCodeToken {
7
+ content: string;
8
+ type: "DEFAULT" | "KEYWORD" | "METHOD" | "STR" | "NUMBER" | "COMMENT";
9
+ }
10
+
11
+ export interface TokenizeResult {
12
+ codeBlock: CodeBlockToken[];
13
+ unified_codeBlock: UnifiedCodeToken[];
14
+ }
15
+
16
+ const KEYWORDS_MAP: Record<string, ReadonlySet<string>> = {
17
+ javascript: new Set([
18
+ "break",
19
+ "case",
20
+ "catch",
21
+ "continue",
22
+ "debugger",
23
+ "delete",
24
+ "do",
25
+ "else",
26
+ "finally",
27
+ "for",
28
+ "function",
29
+ "if",
30
+ "in",
31
+ "instanceof",
32
+ "new",
33
+ "return",
34
+ "switch",
35
+ "this",
36
+ "throw",
37
+ "try",
38
+ "typeof",
39
+ "var",
40
+ "void",
41
+ "while",
42
+ "with",
43
+ "true",
44
+ "false",
45
+ "null",
46
+ "undefined",
47
+ "class",
48
+ "const",
49
+ "let",
50
+ "super",
51
+ "extends",
52
+ "export",
53
+ "import",
54
+ "yield",
55
+ "static",
56
+ "constructor",
57
+ "async",
58
+ "await",
59
+ "get",
60
+ "set",
61
+ ]),
62
+ typescript: new Set([
63
+ "abstract",
64
+ "any",
65
+ "as",
66
+ "asserts",
67
+ "bigint",
68
+ "boolean",
69
+ "declare",
70
+ "enum",
71
+ "implements",
72
+ "infer",
73
+ "interface",
74
+ "is",
75
+ "keyof",
76
+ "module",
77
+ "namespace",
78
+ "never",
79
+ "readonly",
80
+ "require",
81
+ "number",
82
+ "object",
83
+ "override",
84
+ "private",
85
+ "protected",
86
+ "public",
87
+ "satisfies",
88
+ "string",
89
+ "symbol",
90
+ "type",
91
+ "unknown",
92
+ "using",
93
+ "from",
94
+ "break",
95
+ "case",
96
+ "catch",
97
+ "continue",
98
+ "do",
99
+ "else",
100
+ "finally",
101
+ "for",
102
+ "function",
103
+ "if",
104
+ "new",
105
+ "return",
106
+ "switch",
107
+ "this",
108
+ "throw",
109
+ "try",
110
+ "var",
111
+ "void",
112
+ "while",
113
+ "class",
114
+ "const",
115
+ "let",
116
+ "extends",
117
+ "import",
118
+ "export",
119
+ "async",
120
+ "await",
121
+ ]),
122
+ python: new Set([
123
+ "False",
124
+ "None",
125
+ "True",
126
+ "and",
127
+ "as",
128
+ "assert",
129
+ "async",
130
+ "await",
131
+ "break",
132
+ "class",
133
+ "continue",
134
+ "def",
135
+ "del",
136
+ "elif",
137
+ "else",
138
+ "except",
139
+ "finally",
140
+ "for",
141
+ "from",
142
+ "global",
143
+ "if",
144
+ "import",
145
+ "in",
146
+ "is",
147
+ "lambda",
148
+ "nonlocal",
149
+ "not",
150
+ "or",
151
+ "pass",
152
+ "raise",
153
+ "return",
154
+ "try",
155
+ "while",
156
+ "with",
157
+ "yield",
158
+ ]),
159
+ java: new Set([
160
+ "abstract",
161
+ "assert",
162
+ "boolean",
163
+ "break",
164
+ "byte",
165
+ "case",
166
+ "catch",
167
+ "char",
168
+ "class",
169
+ "const",
170
+ "continue",
171
+ "default",
172
+ "do",
173
+ "double",
174
+ "else",
175
+ "enum",
176
+ "extends",
177
+ "final",
178
+ "finally",
179
+ "float",
180
+ "for",
181
+ "goto",
182
+ "if",
183
+ "implements",
184
+ "import",
185
+ "instanceof",
186
+ "int",
187
+ "interface",
188
+ "long",
189
+ "native",
190
+ "new",
191
+ "package",
192
+ "private",
193
+ "protected",
194
+ "public",
195
+ "return",
196
+ "short",
197
+ "static",
198
+ "strictfp",
199
+ "super",
200
+ "switch",
201
+ "synchronized",
202
+ "this",
203
+ "throw",
204
+ "throws",
205
+ "transient",
206
+ "try",
207
+ "void",
208
+ "volatile",
209
+ "while",
210
+ ]),
211
+ golang: new Set([
212
+ "break",
213
+ "case",
214
+ "chan",
215
+ "const",
216
+ "continue",
217
+ "default",
218
+ "defer",
219
+ "else",
220
+ "fallthrough",
221
+ "for",
222
+ "func",
223
+ "go",
224
+ "goto",
225
+ "if",
226
+ "import",
227
+ "interface",
228
+ "map",
229
+ "package",
230
+ "range",
231
+ "return",
232
+ "select",
233
+ "struct",
234
+ "switch",
235
+ "type",
236
+ "var",
237
+ ]),
238
+ c: new Set([
239
+ "auto",
240
+ "break",
241
+ "case",
242
+ "char",
243
+ "const",
244
+ "continue",
245
+ "default",
246
+ "do",
247
+ "double",
248
+ "else",
249
+ "enum",
250
+ "extern",
251
+ "float",
252
+ "for",
253
+ "goto",
254
+ "if",
255
+ "int",
256
+ "long",
257
+ "register",
258
+ "return",
259
+ "short",
260
+ "signed",
261
+ "sizeof",
262
+ "static",
263
+ "struct",
264
+ "switch",
265
+ "typedef",
266
+ "union",
267
+ "unsigned",
268
+ "void",
269
+ "volatile",
270
+ "while",
271
+ ]),
272
+ cpp: new Set([
273
+ "alignas",
274
+ "alignof",
275
+ "and",
276
+ "auto",
277
+ "bool",
278
+ "break",
279
+ "case",
280
+ "catch",
281
+ "class",
282
+ "const",
283
+ "constexpr",
284
+ "continue",
285
+ "delete",
286
+ "do",
287
+ "double",
288
+ "else",
289
+ "enum",
290
+ "explicit",
291
+ "export",
292
+ "extern",
293
+ "false",
294
+ "float",
295
+ "for",
296
+ "friend",
297
+ "if",
298
+ "inline",
299
+ "int",
300
+ "long",
301
+ "mutable",
302
+ "namespace",
303
+ "new",
304
+ "noexcept",
305
+ "nullptr",
306
+ "operator",
307
+ "private",
308
+ "protected",
309
+ "public",
310
+ "return",
311
+ "short",
312
+ "signed",
313
+ "sizeof",
314
+ "static",
315
+ "struct",
316
+ "switch",
317
+ "template",
318
+ "this",
319
+ "throw",
320
+ "true",
321
+ "try",
322
+ "typedef",
323
+ "typename",
324
+ "union",
325
+ "unsigned",
326
+ "using",
327
+ "virtual",
328
+ "void",
329
+ "while",
330
+ ]),
331
+ php: new Set([
332
+ "abstract",
333
+ "and",
334
+ "array",
335
+ "as",
336
+ "break",
337
+ "callable",
338
+ "case",
339
+ "catch",
340
+ "class",
341
+ "clone",
342
+ "const",
343
+ "continue",
344
+ "declare",
345
+ "default",
346
+ "do",
347
+ "echo",
348
+ "else",
349
+ "elseif",
350
+ "empty",
351
+ "enddeclare",
352
+ "endfor",
353
+ "endforeach",
354
+ "endif",
355
+ "endswitch",
356
+ "endwhile",
357
+ "extends",
358
+ "final",
359
+ "finally",
360
+ "fn",
361
+ "for",
362
+ "foreach",
363
+ "function",
364
+ "global",
365
+ "goto",
366
+ "if",
367
+ "implements",
368
+ "include",
369
+ "include_once",
370
+ "instanceof",
371
+ "interface",
372
+ "match",
373
+ "namespace",
374
+ "new",
375
+ "null",
376
+ "or",
377
+ "private",
378
+ "protected",
379
+ "public",
380
+ "require",
381
+ "require_once",
382
+ "return",
383
+ "static",
384
+ "switch",
385
+ "throw",
386
+ "trait",
387
+ "try",
388
+ "use",
389
+ "var",
390
+ "while",
391
+ "yield",
392
+ ]),
393
+ rust: new Set([
394
+ "as",
395
+ "break",
396
+ "const",
397
+ "continue",
398
+ "crate",
399
+ "else",
400
+ "enum",
401
+ "extern",
402
+ "false",
403
+ "fn",
404
+ "for",
405
+ "if",
406
+ "impl",
407
+ "in",
408
+ "let",
409
+ "loop",
410
+ "match",
411
+ "mod",
412
+ "move",
413
+ "mut",
414
+ "pub",
415
+ "ref",
416
+ "return",
417
+ "self",
418
+ "Self",
419
+ "static",
420
+ "struct",
421
+ "super",
422
+ "trait",
423
+ "true",
424
+ "type",
425
+ "unsafe",
426
+ "use",
427
+ "where",
428
+ "while",
429
+ ]),
430
+ html: new Set([
431
+ "html",
432
+ "head",
433
+ "body",
434
+ "div",
435
+ "span",
436
+ "p",
437
+ "a",
438
+ "img",
439
+ "video",
440
+ "audio",
441
+ "script",
442
+ "style",
443
+ "link",
444
+ "meta",
445
+ "form",
446
+ "input",
447
+ "button",
448
+ "table",
449
+ "tr",
450
+ "td",
451
+ "th",
452
+ "ul",
453
+ "ol",
454
+ "li",
455
+ "section",
456
+ "article",
457
+ "header",
458
+ "footer",
459
+ "nav",
460
+ "main",
461
+ ]),
462
+ bash: new Set([
463
+ "if",
464
+ "then",
465
+ "else",
466
+ "elif",
467
+ "fi",
468
+ "for",
469
+ "while",
470
+ "do",
471
+ "done",
472
+ "case",
473
+ "esac",
474
+ "function",
475
+ "in",
476
+ "select",
477
+ "until",
478
+ "break",
479
+ "continue",
480
+ "return",
481
+ "export",
482
+ "readonly",
483
+ "local",
484
+ "declare",
485
+ ]),
486
+ markdown: new Set(["#", "##", "###", "####", "#####", "######"]),
487
+ };
488
+
489
+ const LANGUAGE_ALIASES: Record<string, string> = {
490
+ js: "javascript",
491
+ mjs: "javascript",
492
+ cjs: "javascript",
493
+ ts: "typescript",
494
+ py: "python",
495
+ python3: "python",
496
+ sh: "bash",
497
+ shell: "bash",
498
+ zsh: "bash",
499
+ go: "golang",
500
+ "c++": "cpp",
501
+ cc: "cpp",
502
+ cxx: "cpp",
503
+ htm: "html",
504
+ md: "markdown",
505
+ };
506
+
507
+ const BLOCK_COMMENT_OPEN: Record<string, string> = {
508
+ javascript: "/*",
509
+ typescript: "/*",
510
+ java: "/*",
511
+ c: "/*",
512
+ cpp: "/*",
513
+ php: "/*",
514
+ rust: "/*",
515
+ golang: "/*",
516
+ css: "/*",
517
+ };
518
+
519
+ const HASH_COMMENT_LANGS = new Set(["python", "bash"]);
520
+
521
+ const TYPE_MAP: Record<number, UnifiedCodeToken["type"]> = {
522
+ 0: "DEFAULT",
523
+ 1: "KEYWORD",
524
+ 2: "METHOD",
525
+ 3: "STR",
526
+ 4: "NUMBER",
527
+ 5: "COMMENT",
528
+ };
529
+
530
+ const isWhitespace = (char: string): boolean => /\s/.test(char);
531
+
532
+ export function tokenizeCode(
533
+ code: string,
534
+ lang: string = "javascript",
535
+ ): TokenizeResult {
536
+ const lower = String(lang ?? "").toLowerCase();
537
+ const norm = LANGUAGE_ALIASES[lower] ?? lower;
538
+
539
+ if (!norm || norm === "txt" || norm === "text" || norm === "plaintext") {
540
+ return {
541
+ codeBlock: [
542
+ {
543
+ codeContent: code,
544
+ highlightType: 0,
545
+ },
546
+ ],
547
+ unified_codeBlock: [
548
+ {
549
+ content: code,
550
+ type: "DEFAULT",
551
+ },
552
+ ],
553
+ };
554
+ }
555
+
556
+ const keywords = KEYWORDS_MAP[norm] ?? new Set<string>();
557
+ const blockCommentOpen = BLOCK_COMMENT_OPEN[norm] ?? null;
558
+ const blockCommentClose = blockCommentOpen ? "*/" : null;
559
+ const tripleQuote = norm === "python" ? (['"""', "'''"] as const) : null;
560
+
561
+ const tokens: CodeBlockToken[] = [];
562
+
563
+ let i = 0;
564
+
565
+ const push = (content: string, type: number): void => {
566
+ if (!content) return;
567
+
568
+ const last = tokens[tokens.length - 1];
569
+
570
+ if (last && last.highlightType === type) {
571
+ last.codeContent += content;
572
+ } else {
573
+ tokens.push({
574
+ codeContent: content,
575
+ highlightType: type,
576
+ });
577
+ }
578
+ };
579
+
580
+ const isIdentifier = (char: string): boolean => {
581
+ switch (norm) {
582
+ case "css":
583
+ return /[a-zA-Z0-9_$-]/.test(char);
584
+ case "html":
585
+ return /[a-zA-Z0-9_$:-]/.test(char);
586
+ default:
587
+ return /[a-zA-Z0-9_$]/.test(char);
588
+ }
589
+ };
590
+
591
+ const startsWithAt = (needle: string, pos: number): boolean => {
592
+ if (pos + needle.length > code.length) return false;
593
+ return code.startsWith(needle, pos);
594
+ };
595
+
596
+ const scanUntil = (terminator: string, from: number): number => {
597
+ let j = from;
598
+
599
+ while (j < code.length) {
600
+ if ((code[j] ?? "") === "\\" && j + 1 < code.length) {
601
+ j += 2;
602
+ continue;
603
+ }
604
+ if (startsWithAt(terminator, j)) return j + terminator.length;
605
+ j++;
606
+ }
607
+
608
+ return code.length;
609
+ };
610
+
611
+ while (i < code.length) {
612
+ const c = code[i] ?? "";
613
+
614
+ if (isWhitespace(c)) {
615
+ let s = i;
616
+
617
+ while (i < code.length && isWhitespace(code[i] ?? "")) {
618
+ i++;
619
+ }
620
+
621
+ push(code.slice(s, i), 0);
622
+ continue;
623
+ }
624
+
625
+ if (blockCommentOpen && startsWithAt(blockCommentOpen, i)) {
626
+ const end = code.indexOf(
627
+ blockCommentClose as string,
628
+ i + blockCommentOpen.length,
629
+ );
630
+ const stop =
631
+ end === -1 ? code.length : end + (blockCommentClose as string).length;
632
+
633
+ push(code.slice(i, stop), 5);
634
+ i = stop;
635
+ continue;
636
+ }
637
+
638
+ if (norm === "html" && startsWithAt("<!--", i)) {
639
+ const end = code.indexOf("-->", i + 4);
640
+ const stop = end === -1 ? code.length : end + 3;
641
+
642
+ push(code.slice(i, stop), 5);
643
+ i = stop;
644
+ continue;
645
+ }
646
+
647
+ if (
648
+ (c === "/" && code[i + 1] === "/") ||
649
+ (c === "#" && HASH_COMMENT_LANGS.has(norm))
650
+ ) {
651
+ let s = i;
652
+
653
+ while (i < code.length && (code[i] ?? "") !== "\n") {
654
+ i++;
655
+ }
656
+
657
+ push(code.slice(s, i), 5);
658
+ continue;
659
+ }
660
+
661
+ if (
662
+ tripleQuote &&
663
+ (startsWithAt(tripleQuote[0], i) || startsWithAt(tripleQuote[1], i))
664
+ ) {
665
+ const open = startsWithAt(tripleQuote[0], i)
666
+ ? tripleQuote[0]
667
+ : tripleQuote[1];
668
+ const stop = scanUntil(open, i + 3);
669
+
670
+ push(code.slice(i, stop), 3);
671
+ i = stop;
672
+ continue;
673
+ }
674
+
675
+ if (c === '"' || c === "'" || c === "`") {
676
+ let s = i;
677
+ const q = c;
678
+
679
+ i++;
680
+
681
+ while (i < code.length) {
682
+ if ((code[i] ?? "") === "\\" && i + 1 < code.length) {
683
+ i += 2;
684
+ } else if ((code[i] ?? "") === q) {
685
+ i++;
686
+ break;
687
+ } else {
688
+ i++;
689
+ }
690
+ }
691
+
692
+ push(code.slice(s, i), 3);
693
+ continue;
694
+ }
695
+
696
+ if (/[0-9]/.test(c)) {
697
+ let s = i;
698
+
699
+ if (c === "0" && /[xXbBoO]/.test(code[i + 1] ?? "")) {
700
+ const kind = (code[i + 1] ?? "").toLowerCase();
701
+ const digitRe =
702
+ kind === "x" ? /[0-9a-fA-F]/ : kind === "b" ? /[01]/ : /[0-7]/;
703
+
704
+ i += 2;
705
+ while (i < code.length && digitRe.test(code[i] ?? "")) {
706
+ i++;
707
+ }
708
+
709
+ push(code.slice(s, i), 4);
710
+ continue;
711
+ }
712
+
713
+ while (i < code.length && /[0-9._]/.test(code[i] ?? "")) {
714
+ i++;
715
+ }
716
+
717
+ if (
718
+ ((code[i] ?? "") === "e" || (code[i] ?? "") === "E") &&
719
+ /[0-9]/.test(code[i + 1] ?? "")
720
+ ) {
721
+ i++;
722
+ while (i < code.length && /[0-9._]/.test(code[i] ?? "")) {
723
+ i++;
724
+ }
725
+ }
726
+
727
+ push(code.slice(s, i), 4);
728
+ continue;
729
+ }
730
+
731
+ if (/[a-zA-Z_$]/.test(c)) {
732
+ let s = i;
733
+
734
+ while (i < code.length && isIdentifier(code[i] ?? "")) {
735
+ i++;
736
+ }
737
+
738
+ const word = code.slice(s, i);
739
+
740
+ let type = 0;
741
+
742
+ if (
743
+ keywords.has(word) ||
744
+ (norm === "html" && keywords.has(word.toLowerCase()))
745
+ ) {
746
+ type = 1;
747
+ } else if (norm === "css") {
748
+ let j = i;
749
+
750
+ while (j < code.length && isWhitespace(code[j] ?? "")) {
751
+ j++;
752
+ }
753
+
754
+ if ((code[j] ?? "") === ":") {
755
+ type = 1;
756
+ }
757
+ } else if (norm === "html") {
758
+ let p = s - 1;
759
+
760
+ while (p >= 0 && isWhitespace(code[p] ?? "")) {
761
+ p--;
762
+ }
763
+
764
+ if (
765
+ (code[p] ?? "") === "<" ||
766
+ ((code[p] ?? "") === "/" && (code[p - 1] ?? "") === "<")
767
+ ) {
768
+ type = 1;
769
+ }
770
+ }
771
+
772
+ if (type === 0) {
773
+ let j = i;
774
+
775
+ while (j < code.length && isWhitespace(code[j] ?? "")) {
776
+ j++;
777
+ }
778
+
779
+ if ((code[j] ?? "") === "(") {
780
+ type = 2;
781
+ }
782
+ }
783
+
784
+ push(word, type);
785
+ continue;
786
+ }
787
+
788
+ push(c, 0);
789
+ i++;
790
+ }
791
+
792
+ return {
793
+ codeBlock: tokens,
794
+ unified_codeBlock: tokens.map((t) => ({
795
+ content: t.codeContent,
796
+ type: TYPE_MAP[t.highlightType] ?? "DEFAULT",
797
+ })),
798
+ };
799
+ }