@shikijs/colorized-brackets 4.0.2 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.mjs +11 -13
  2. package/package.json +2 -2
package/dist/index.mjs CHANGED
@@ -1,5 +1,4 @@
1
1
  import { ShikiError } from "shiki";
2
-
3
2
  //#region src/themes.ts
4
3
  var themes_default = {
5
4
  "andromeeda": [
@@ -495,11 +494,12 @@ var themes_default = {
495
494
  "rgba(255, 18, 18, 0.8)"
496
495
  ]
497
496
  };
498
-
499
497
  //#endregion
500
498
  //#region src/utils.ts
499
+ const RE_SOURCE_SCOPE = /^source.\w+$/;
500
+ const RE_ESCAPE_REGEXP = /[.*+?^${}()|[\]\\]/g;
501
501
  function getEmbeddedLang(token) {
502
- return token.explanation?.[0].scopes.findLast((scope) => scope.scopeName.match(/^source.\w+$/))?.scopeName.split(".")[1];
502
+ return token.explanation?.[0].scopes.findLast((scope) => scope.scopeName.match(RE_SOURCE_SCOPE))?.scopeName.split(".")[1];
503
503
  }
504
504
  function resolveConfig(config, lang) {
505
505
  return {
@@ -508,7 +508,7 @@ function resolveConfig(config, lang) {
508
508
  };
509
509
  }
510
510
  function escapeRegExp(string) {
511
- return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
511
+ return string.replace(RE_ESCAPE_REGEXP, "\\$&");
512
512
  }
513
513
  function shouldIgnoreToken(token, scopesAllowList, scopesDenyList) {
514
514
  if (!token.explanation) return true;
@@ -520,7 +520,6 @@ function shouldIgnoreToken(token, scopesAllowList, scopesDenyList) {
520
520
  if (scopesDenyList && scopesDenyList.length && token.explanation?.some((explanation) => explanation.scopes.some((scope) => scopesDenyList.some((denied) => scope.scopeName === denied || scope.scopeName.startsWith(`${denied}.`))))) return true;
521
521
  return false;
522
522
  }
523
-
524
523
  //#endregion
525
524
  //#region src/colorizeBracketTokens.ts
526
525
  function colorizeBracketTokens(tokens, config, shikiOptions, lang) {
@@ -534,7 +533,7 @@ function colorizeBracketTokens(tokens, config, shikiOptions, lang) {
534
533
  if (!pairDefinition || shouldIgnoreToken(token, pairDefinition.scopesAllowList, pairDefinition.scopesDenyList)) continue;
535
534
  if (openers.has(token.content.trim())) openerStack.push(token);
536
535
  else if (closers.has(token.content.trim())) {
537
- const opener = openerStack.slice().reverse().find((t) => t.content.trim() === closerToOpener[token.content.trim()]);
536
+ const opener = openerStack.toReversed().find((t) => t.content.trim() === closerToOpener[token.content.trim()]);
538
537
  if (opener) {
539
538
  while (openerStack.at(-1) !== opener) {
540
539
  const unexpected = openerStack.pop();
@@ -578,7 +577,7 @@ const DEFAULT_BRACKETS_COLORS = [
578
577
  ];
579
578
  function getColor(themes, themeName, level) {
580
579
  const colors = themeName == null ? DEFAULT_BRACKETS_COLORS : getThemeColors(themeName, themes) ?? getThemeColors(themeName, themes_default) ?? DEFAULT_BRACKETS_COLORS;
581
- if (level === -1) return colors[colors.length - 1];
580
+ if (level === -1) return colors.at(-1);
582
581
  else return colors[level % (colors.length - 1)];
583
582
  }
584
583
  function getThemeColors(themeName, themes) {
@@ -587,9 +586,10 @@ function getThemeColors(themeName, themes) {
587
586
  if (startsWithName) return themes[startsWithName];
588
587
  return null;
589
588
  }
590
-
591
589
  //#endregion
592
590
  //#region src/splitBracketTokens.ts
591
+ const RE_LEADING_SPACES = /^\s*/;
592
+ const RE_TRAILING_SPACES = /\s*$/;
593
593
  function splitBracketTokens(rawToken, config, lang) {
594
594
  const resolvedConfig = resolveConfig(config, getEmbeddedLang(rawToken) ?? lang);
595
595
  if (resolvedConfig.bracketPairs.length === 0 || shouldIgnoreToken(rawToken)) return [rawToken];
@@ -626,8 +626,8 @@ function splitBracketTokens(rawToken, config, lang) {
626
626
  const start = currentExplanationStart;
627
627
  let length = explanation.content.length;
628
628
  if (explanations.length === 1) length = rawToken.content.length;
629
- else if (i === 0) length = (rawToken.content.match(/^\s*/)?.[0].length ?? 0) + explanation.content.trimStart().length;
630
- else if (i === explanations.length - 1) length = explanation.content.trimEnd().length + (rawToken.content.match(/\s*$/)?.[0].length ?? 0);
629
+ else if (i === 0) length = (rawToken.content.match(RE_LEADING_SPACES)?.[0].length ?? 0) + explanation.content.trimStart().length;
630
+ else if (i === explanations.length - 1) length = explanation.content.trimEnd().length + (rawToken.content.match(RE_TRAILING_SPACES)?.[0].length ?? 0);
631
631
  currentExplanationStart += length;
632
632
  return {
633
633
  ...explanation,
@@ -642,7 +642,6 @@ function splitBracketTokens(rawToken, config, lang) {
642
642
  }
643
643
  return tokens;
644
644
  }
645
-
646
645
  //#endregion
647
646
  //#region src/index.ts
648
647
  const jinjaLikeBracketPairs = [
@@ -737,6 +736,5 @@ const EXPLICIT_TRIGGER_REGEX = /(^|\s)colorize-brackets($|\s)/;
737
736
  function isEnabled(config, meta) {
738
737
  return !config.explicitTrigger || meta?.match(EXPLICIT_TRIGGER_REGEX) != null;
739
738
  }
740
-
741
739
  //#endregion
742
- export { transformerColorizedBrackets };
740
+ export { transformerColorizedBrackets };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@shikijs/colorized-brackets",
3
3
  "type": "module",
4
- "version": "4.0.2",
4
+ "version": "4.1.0",
5
5
  "description": "VSCode-style colorized brackets transformer for Shiki",
6
6
  "author": "Michael Moore <mscottmoore@pm.me>",
7
7
  "license": "MIT",
@@ -30,7 +30,7 @@
30
30
  "node": ">=20"
31
31
  },
32
32
  "dependencies": {
33
- "shiki": "4.0.2"
33
+ "shiki": "4.1.0"
34
34
  },
35
35
  "scripts": {
36
36
  "build": "tsdown",