@particle-academy/fancy-code 0.8.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -446,89 +446,6 @@ var tokenizeJavaScript = (source) => {
446
446
  return tokens;
447
447
  };
448
448
 
449
- // src/engine/tokenizers/html.ts
450
- var tokenizeHtml = (source) => {
451
- const tokens = [];
452
- const len = source.length;
453
- let i = 0;
454
- while (i < len) {
455
- if (source[i] === "<" && source.slice(i, i + 4) === "<!--") {
456
- const pos2 = i;
457
- i += 4;
458
- while (i < len && source.slice(i, i + 3) !== "-->") i++;
459
- i += 3;
460
- tokens.push({ type: "comment", start: pos2, end: i });
461
- continue;
462
- }
463
- if (source[i] === "<" && source.slice(i, i + 9).toUpperCase() === "<!DOCTYPE") {
464
- const pos2 = i;
465
- while (i < len && source[i] !== ">") i++;
466
- i++;
467
- tokens.push({ type: "keyword", start: pos2, end: i });
468
- continue;
469
- }
470
- if (source[i] === "<") {
471
- tokens.push({ type: "punctuation", start: i, end: i + 1 });
472
- i++;
473
- if (i < len && source[i] === "/") {
474
- tokens.push({ type: "punctuation", start: i, end: i + 1 });
475
- i++;
476
- }
477
- if (i < len && /[a-zA-Z]/.test(source[i])) {
478
- const pos2 = i;
479
- while (i < len && /[a-zA-Z0-9-]/.test(source[i])) i++;
480
- tokens.push({ type: "tag", start: pos2, end: i });
481
- }
482
- while (i < len && source[i] !== ">" && !(source[i] === "/" && source[i + 1] === ">")) {
483
- if (source[i] === " " || source[i] === " " || source[i] === "\n" || source[i] === "\r") {
484
- i++;
485
- continue;
486
- }
487
- if (/[a-zA-Z_@:]/.test(source[i])) {
488
- const pos2 = i;
489
- while (i < len && /[a-zA-Z0-9_\-:.]/.test(source[i])) i++;
490
- tokens.push({ type: "attribute", start: pos2, end: i });
491
- while (i < len && (source[i] === " " || source[i] === " ")) i++;
492
- if (i < len && source[i] === "=") {
493
- tokens.push({ type: "operator", start: i, end: i + 1 });
494
- i++;
495
- while (i < len && (source[i] === " " || source[i] === " ")) i++;
496
- if (i < len && (source[i] === '"' || source[i] === "'")) {
497
- const quote = source[i];
498
- const pos3 = i;
499
- i++;
500
- while (i < len && source[i] !== quote) i++;
501
- i++;
502
- tokens.push({ type: "attributeValue", start: pos3, end: i });
503
- } else if (i < len && /[^\s>]/.test(source[i])) {
504
- const pos3 = i;
505
- while (i < len && /[^\s>]/.test(source[i])) i++;
506
- tokens.push({ type: "attributeValue", start: pos3, end: i });
507
- }
508
- }
509
- continue;
510
- }
511
- i++;
512
- }
513
- if (i < len && source[i] === "/") {
514
- tokens.push({ type: "punctuation", start: i, end: i + 1 });
515
- i++;
516
- }
517
- if (i < len && source[i] === ">") {
518
- tokens.push({ type: "punctuation", start: i, end: i + 1 });
519
- i++;
520
- }
521
- continue;
522
- }
523
- const pos = i;
524
- while (i < len && source[i] !== "<") i++;
525
- if (i > pos) {
526
- tokens.push({ type: "plain", start: pos, end: i });
527
- }
528
- }
529
- return tokens;
530
- };
531
-
532
449
  // src/engine/tokenizers/php.ts
533
450
  var PHP_KEYWORDS = /* @__PURE__ */ new Set([
534
451
  "abstract",
@@ -706,7 +623,7 @@ var tokenizePhp = (source) => {
706
623
  if (source[i] === "<" && source[i + 1] === "?") {
707
624
  if (i > htmlChunkStart) {
708
625
  const htmlChunk = source.slice(htmlChunkStart, i);
709
- const htmlTokens = tokenizeHtml(htmlChunk);
626
+ const htmlTokens = fancyFileCommons.tokenizeHtml(htmlChunk);
710
627
  for (const t of htmlTokens) {
711
628
  tokens.push({ type: t.type, start: t.start + htmlChunkStart, end: t.end + htmlChunkStart });
712
629
  }
@@ -733,7 +650,7 @@ var tokenizePhp = (source) => {
733
650
  }
734
651
  if (htmlChunkStart < len) {
735
652
  const htmlChunk = source.slice(htmlChunkStart);
736
- const htmlTokens = tokenizeHtml(htmlChunk);
653
+ const htmlTokens = fancyFileCommons.tokenizeHtml(htmlChunk);
737
654
  for (const t of htmlTokens) {
738
655
  tokens.push({ type: t.type, start: t.start + htmlChunkStart, end: t.end + htmlChunkStart });
739
656
  }
@@ -1112,11 +1029,6 @@ var tokenizeGo = (source) => {
1112
1029
  return tokens;
1113
1030
  };
1114
1031
 
1115
- // src/engine/tokenizer.ts
1116
- function tok(type, s, e) {
1117
- return { type, start: s, end: e };
1118
- }
1119
-
1120
1032
  // src/engine/tokenizers/markdown.ts
1121
1033
  function tokenizeMarkdown(source) {
1122
1034
  const tokens = [];
@@ -1128,26 +1040,26 @@ function tokenizeMarkdown(source) {
1128
1040
  const len = line.length;
1129
1041
  offset += len + 1;
1130
1042
  if (/^\s*(```|~~~)/.test(line)) {
1131
- tokens.push(tok("string", base, base + len));
1043
+ tokens.push(fancyFileCommons.tok("string", base, base + len));
1132
1044
  inFence = !inFence;
1133
1045
  continue;
1134
1046
  }
1135
1047
  if (inFence) {
1136
- tokens.push(tok("string", base, base + len));
1048
+ tokens.push(fancyFileCommons.tok("string", base, base + len));
1137
1049
  continue;
1138
1050
  }
1139
1051
  if (/^#{1,6}\s/.test(line)) {
1140
- tokens.push(tok("keyword", base, base + len));
1052
+ tokens.push(fancyFileCommons.tok("keyword", base, base + len));
1141
1053
  continue;
1142
1054
  }
1143
1055
  if (/^\s*>/.test(line)) {
1144
- tokens.push(tok("comment", base, base + len));
1056
+ tokens.push(fancyFileCommons.tok("comment", base, base + len));
1145
1057
  continue;
1146
1058
  }
1147
1059
  const list = line.match(/^(\s*)([-*+]|\d+\.)\s/);
1148
1060
  if (list) {
1149
1061
  const start = base + list[1].length;
1150
- tokens.push(tok("operator", start, start + list[2].length));
1062
+ tokens.push(fancyFileCommons.tok("operator", start, start + list[2].length));
1151
1063
  }
1152
1064
  pushInline(line, base, tokens);
1153
1065
  }
@@ -1157,7 +1069,7 @@ function pushInline(line, base, out) {
1157
1069
  if (line.length > 5e3) return;
1158
1070
  const spans = [];
1159
1071
  const add = (type, start, end) => {
1160
- spans.push(tok(type, base + start, base + end));
1072
+ spans.push(fancyFileCommons.tok(type, base + start, base + end));
1161
1073
  };
1162
1074
  let m;
1163
1075
  const code = /`[^`]+`/g;
@@ -1200,7 +1112,7 @@ registerLanguage({
1200
1112
  registerLanguage({
1201
1113
  name: "HTML",
1202
1114
  aliases: ["html", "htm"],
1203
- tokenize: tokenizeHtml
1115
+ tokenize: fancyFileCommons.tokenizeHtml
1204
1116
  });
1205
1117
  registerLanguage({
1206
1118
  name: "PHP",
@@ -1321,112 +1233,17 @@ function getTheme(name) {
1321
1233
  function getRegisteredThemes() {
1322
1234
  return Array.from(themeRegistry.keys());
1323
1235
  }
1324
-
1325
- // src/themes/light.ts
1326
1236
  registerTheme({
1327
1237
  name: "light",
1328
1238
  variant: "light",
1329
- colors: {
1330
- background: "#ffffff",
1331
- foreground: "#1e1e2e",
1332
- gutterBackground: "#f8fafc",
1333
- gutterForeground: "#94a3b8",
1334
- gutterBorder: "#e2e8f0",
1335
- activeLineBackground: "#f8fafc",
1336
- selectionBackground: "#dbeafe",
1337
- cursorColor: "#3b82f6",
1338
- diffAdded: "#1a7f37",
1339
- diffModified: "#0969da",
1340
- diffRemoved: "#cf222e",
1341
- keyword: "#8b5cf6",
1342
- string: "#059669",
1343
- comment: "#94a3b8",
1344
- number: "#d97706",
1345
- operator: "#0891b2",
1346
- function: "#2563eb",
1347
- type: "#d97706",
1348
- tag: "#dc2626",
1349
- attribute: "#d97706",
1350
- attributeValue: "#059669",
1351
- punctuation: "#64748b",
1352
- variable: "#1e1e2e"
1353
- }
1239
+ colors: fancyFileCommons.LIGHT_COLORS
1354
1240
  });
1355
-
1356
- // src/themes/dark.ts
1357
1241
  registerTheme({
1358
1242
  name: "dark",
1359
1243
  variant: "dark",
1360
- colors: {
1361
- background: "#18181b",
1362
- foreground: "#e4e4e7",
1363
- gutterBackground: "#18181b",
1364
- gutterForeground: "#52525b",
1365
- gutterBorder: "#27272a",
1366
- activeLineBackground: "#27272a",
1367
- selectionBackground: "#1e3a5f",
1368
- cursorColor: "#60a5fa",
1369
- diffAdded: "#3fb950",
1370
- diffModified: "#4184e4",
1371
- diffRemoved: "#f85149",
1372
- keyword: "#c084fc",
1373
- string: "#34d399",
1374
- comment: "#71717a",
1375
- number: "#fbbf24",
1376
- operator: "#22d3ee",
1377
- function: "#60a5fa",
1378
- type: "#fbbf24",
1379
- tag: "#f87171",
1380
- attribute: "#fbbf24",
1381
- attributeValue: "#34d399",
1382
- punctuation: "#a1a1aa",
1383
- variable: "#e4e4e7"
1384
- }
1244
+ colors: fancyFileCommons.DARK_COLORS
1385
1245
  });
1386
1246
 
1387
- // src/engine/highlight.ts
1388
- function escapeHtml(text) {
1389
- return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
1390
- }
1391
- var COLOR_RE = /^(#[0-9a-fA-F]{3,8}|(?:rgb|rgba|hsl|hsla)\([\d\s,.%/-]+\)|[a-zA-Z]+)$/;
1392
- function safeColor(value) {
1393
- return typeof value === "string" && COLOR_RE.test(value.trim()) ? value.trim() : "inherit";
1394
- }
1395
- function highlightCode(source, tokens, colors) {
1396
- if (tokens.length === 0) return escapeHtml(source);
1397
- const colorMap = {
1398
- keyword: colors.keyword,
1399
- string: colors.string,
1400
- comment: colors.comment,
1401
- number: colors.number,
1402
- operator: colors.operator,
1403
- function: colors.function,
1404
- type: colors.type,
1405
- tag: colors.tag,
1406
- attribute: colors.attribute,
1407
- attributeValue: colors.attributeValue,
1408
- punctuation: colors.punctuation,
1409
- variable: colors.variable,
1410
- plain: colors.foreground
1411
- };
1412
- const parts = [];
1413
- let pos = 0;
1414
- for (const token of tokens) {
1415
- if (token.start > pos) {
1416
- parts.push(escapeHtml(source.slice(pos, token.start)));
1417
- }
1418
- const text = escapeHtml(source.slice(token.start, token.end));
1419
- const color = safeColor(colorMap[token.type]);
1420
- const style = token.type === "comment" ? `color:${color};font-style:italic` : `color:${color}`;
1421
- parts.push(`<span style="${style}">${text}</span>`);
1422
- pos = token.end;
1423
- }
1424
- if (pos < source.length) {
1425
- parts.push(escapeHtml(source.slice(pos)));
1426
- }
1427
- return parts.join("");
1428
- }
1429
-
1430
1247
  // src/engine/position.ts
1431
1248
  function lineColumnToOffset(text, line, column = 1) {
1432
1249
  const lines = text.split("\n");
@@ -1494,7 +1311,7 @@ function useEditorEngine({
1494
1311
  return value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
1495
1312
  }
1496
1313
  const tokens = tokenizer(value);
1497
- return highlightCode(value, tokens, themeColors);
1314
+ return fancyFileCommons.highlightCode(value, tokens, themeColors);
1498
1315
  }, [value, tokenizer, themeColors]);
1499
1316
  const lineCount = react.useMemo(() => {
1500
1317
  let count = 1;
@@ -1819,7 +1636,7 @@ var CodeEditor = Object.assign(CodeEditorRoot, {
1819
1636
  });
1820
1637
 
1821
1638
  // src/engine/markdown-render.ts
1822
- function escapeHtml2(s) {
1639
+ function escapeHtml(s) {
1823
1640
  return s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
1824
1641
  }
1825
1642
  function escapeAttr(s) {
@@ -1860,10 +1677,10 @@ function renderMarkdown(markdown) {
1860
1677
  i++;
1861
1678
  }
1862
1679
  i++;
1863
- out.push(`<pre><code${lang ? ` class="language-${escapeAttr(escapeHtml2(lang))}"` : ""}>${escapeHtml2(body.join("\n"))}</code></pre>`);
1680
+ out.push(`<pre><code${lang ? ` class="language-${escapeAttr(escapeHtml(lang))}"` : ""}>${escapeHtml(body.join("\n"))}</code></pre>`);
1864
1681
  continue;
1865
1682
  }
1866
- const esc = escapeHtml2(line);
1683
+ const esc = escapeHtml(line);
1867
1684
  if (/^\s*$/.test(line)) {
1868
1685
  flushPara();
1869
1686
  closeList();