@shikijs/core 1.6.3 → 1.6.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.
Files changed (2) hide show
  1. package/dist/index.mjs +35 -32
  2. package/package.json +1 -1
package/dist/index.mjs CHANGED
@@ -537,6 +537,26 @@ function tokenizeWithTheme(code, grammar, theme, colorMap, options) {
537
537
  let ruleStack = INITIAL;
538
538
  let actual = [];
539
539
  const final = [];
540
+ const themeSettingsSelectors = [];
541
+ if (options.includeExplanation) {
542
+ for (const setting of theme.settings) {
543
+ let selectors;
544
+ switch (typeof setting.scope) {
545
+ case 'string':
546
+ selectors = setting.scope.split(/,/).map(scope => scope.trim());
547
+ break;
548
+ case 'object':
549
+ selectors = setting.scope;
550
+ break;
551
+ default:
552
+ continue;
553
+ }
554
+ themeSettingsSelectors.push({
555
+ settings: setting,
556
+ selectors: selectors.map(selector => selector.split(/ /)),
557
+ });
558
+ }
559
+ }
540
560
  for (let i = 0, len = lines.length; i < len; i++) {
541
561
  const [line, lineOffset] = lines[i];
542
562
  if (line === '') {
@@ -588,7 +608,7 @@ function tokenizeWithTheme(code, grammar, theme, colorMap, options) {
588
608
  offset += tokenWithScopesText.length;
589
609
  token.explanation.push({
590
610
  content: tokenWithScopesText,
591
- scopes: explainThemeScopes(theme, tokenWithScopes.scopes),
611
+ scopes: explainThemeScopes(themeSettingsSelectors, tokenWithScopes.scopes),
592
612
  });
593
613
  tokensWithScopesIndex += 1;
594
614
  }
@@ -601,31 +621,29 @@ function tokenizeWithTheme(code, grammar, theme, colorMap, options) {
601
621
  }
602
622
  return final;
603
623
  }
604
- function explainThemeScopes(theme, scopes) {
624
+ function explainThemeScopes(themeSelectors, scopes) {
605
625
  const result = [];
606
626
  for (let i = 0, len = scopes.length; i < len; i++) {
607
627
  const parentScopes = scopes.slice(0, i);
608
628
  const scope = scopes[i];
609
629
  result[i] = {
610
630
  scopeName: scope,
611
- themeMatches: explainThemeScope(theme, scope, parentScopes),
631
+ themeMatches: explainThemeScope(themeSelectors, scope, parentScopes),
612
632
  };
613
633
  }
614
634
  return result;
615
635
  }
616
636
  function matchesOne(selector, scope) {
617
- const selectorPrefix = `${selector}.`;
618
- if (selector === scope || scope.substring(0, selectorPrefix.length) === selectorPrefix)
619
- return true;
620
- return false;
637
+ return selector === scope
638
+ || (scope.substring(0, selector.length) === selector && scope[selector.length] === '.');
621
639
  }
622
- function matches(selector, selectorParentScopes, scope, parentScopes) {
623
- if (!matchesOne(selector, scope))
640
+ function matches(selectors, scope, parentScopes) {
641
+ if (!matchesOne(selectors[selectors.length - 1], scope))
624
642
  return false;
625
- let selectorParentIndex = selectorParentScopes.length - 1;
643
+ let selectorParentIndex = selectors.length - 2;
626
644
  let parentIndex = parentScopes.length - 1;
627
645
  while (selectorParentIndex >= 0 && parentIndex >= 0) {
628
- if (matchesOne(selectorParentScopes[selectorParentIndex], parentScopes[parentIndex]))
646
+ if (matchesOne(selectors[selectorParentIndex], parentScopes[parentIndex]))
629
647
  selectorParentIndex -= 1;
630
648
  parentIndex -= 1;
631
649
  }
@@ -633,28 +651,13 @@ function matches(selector, selectorParentScopes, scope, parentScopes) {
633
651
  return true;
634
652
  return false;
635
653
  }
636
- function explainThemeScope(theme, scope, parentScopes) {
654
+ function explainThemeScope(themeSettingsSelectors, scope, parentScopes) {
637
655
  const result = [];
638
- let resultLen = 0;
639
- for (let i = 0, len = theme.settings.length; i < len; i++) {
640
- const setting = theme.settings[i];
641
- let selectors;
642
- if (typeof setting.scope === 'string')
643
- selectors = setting.scope.split(/,/).map(scope => scope.trim());
644
- else if (Array.isArray(setting.scope))
645
- selectors = setting.scope;
646
- else
647
- continue;
648
- for (let j = 0, lenJ = selectors.length; j < lenJ; j++) {
649
- const rawSelector = selectors[j];
650
- const rawSelectorPieces = rawSelector.split(/ /);
651
- const selector = rawSelectorPieces[rawSelectorPieces.length - 1];
652
- const selectorParentScopes = rawSelectorPieces.slice(0, rawSelectorPieces.length - 1);
653
- if (matches(selector, selectorParentScopes, scope, parentScopes)) {
654
- // match!
655
- result[resultLen++] = setting;
656
- // break the loop
657
- j = lenJ;
656
+ for (const { selectors, settings } of themeSettingsSelectors) {
657
+ for (const selectorPieces of selectors) {
658
+ if (matches(selectorPieces, scope, parentScopes)) {
659
+ result.push(settings);
660
+ break; // continue to the next theme settings
658
661
  }
659
662
  }
660
663
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@shikijs/core",
3
3
  "type": "module",
4
- "version": "1.6.3",
4
+ "version": "1.6.5",
5
5
  "description": "Core of Shiki",
6
6
  "author": "Pine Wu <octref@gmail.com>; Anthony Fu <anthonyfu117@hotmail.com>",
7
7
  "license": "MIT",