@shikijs/core 1.12.1 → 1.13.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/chunk-tokens.d.mts +5 -2
- package/dist/index.mjs +28 -24
- package/package.json +1 -1
package/dist/chunk-tokens.d.mts
CHANGED
|
@@ -837,7 +837,7 @@ interface CodeToTokensWithThemesOptions<Languages = string, Themes = string> {
|
|
|
837
837
|
}
|
|
838
838
|
interface ThemedTokenScopeExplanation {
|
|
839
839
|
scopeName: string;
|
|
840
|
-
themeMatches
|
|
840
|
+
themeMatches?: IRawThemeSetting[];
|
|
841
841
|
}
|
|
842
842
|
interface ThemedTokenExplanation {
|
|
843
843
|
content: string;
|
|
@@ -945,9 +945,12 @@ interface TokenizeWithThemeOptions {
|
|
|
945
945
|
/**
|
|
946
946
|
* Include explanation of why a token is given a color.
|
|
947
947
|
*
|
|
948
|
+
* You can optionally pass `scopeName` to only include explanation for scopes,
|
|
949
|
+
* which is more performant than full explanation.
|
|
950
|
+
*
|
|
948
951
|
* @default false
|
|
949
952
|
*/
|
|
950
|
-
includeExplanation?: boolean;
|
|
953
|
+
includeExplanation?: boolean | 'scopeName';
|
|
951
954
|
/**
|
|
952
955
|
* A map of color names to new color values.
|
|
953
956
|
*
|
package/dist/index.mjs
CHANGED
|
@@ -635,26 +635,6 @@ function _tokenizeWithTheme(code, grammar, theme, colorMap, options) {
|
|
|
635
635
|
: INITIAL;
|
|
636
636
|
let actual = [];
|
|
637
637
|
const final = [];
|
|
638
|
-
const themeSettingsSelectors = [];
|
|
639
|
-
if (options.includeExplanation) {
|
|
640
|
-
for (const setting of theme.settings) {
|
|
641
|
-
let selectors;
|
|
642
|
-
switch (typeof setting.scope) {
|
|
643
|
-
case 'string':
|
|
644
|
-
selectors = setting.scope.split(/,/).map(scope => scope.trim());
|
|
645
|
-
break;
|
|
646
|
-
case 'object':
|
|
647
|
-
selectors = setting.scope;
|
|
648
|
-
break;
|
|
649
|
-
default:
|
|
650
|
-
continue;
|
|
651
|
-
}
|
|
652
|
-
themeSettingsSelectors.push({
|
|
653
|
-
settings: setting,
|
|
654
|
-
selectors: selectors.map(selector => selector.split(/ /)),
|
|
655
|
-
});
|
|
656
|
-
}
|
|
657
|
-
}
|
|
658
638
|
for (let i = 0, len = lines.length; i < len; i++) {
|
|
659
639
|
const [line, lineOffset] = lines[i];
|
|
660
640
|
if (line === '') {
|
|
@@ -698,6 +678,26 @@ function _tokenizeWithTheme(code, grammar, theme, colorMap, options) {
|
|
|
698
678
|
fontStyle,
|
|
699
679
|
};
|
|
700
680
|
if (options.includeExplanation) {
|
|
681
|
+
const themeSettingsSelectors = [];
|
|
682
|
+
if (options.includeExplanation !== 'scopeName') {
|
|
683
|
+
for (const setting of theme.settings) {
|
|
684
|
+
let selectors;
|
|
685
|
+
switch (typeof setting.scope) {
|
|
686
|
+
case 'string':
|
|
687
|
+
selectors = setting.scope.split(/,/).map(scope => scope.trim());
|
|
688
|
+
break;
|
|
689
|
+
case 'object':
|
|
690
|
+
selectors = setting.scope;
|
|
691
|
+
break;
|
|
692
|
+
default:
|
|
693
|
+
continue;
|
|
694
|
+
}
|
|
695
|
+
themeSettingsSelectors.push({
|
|
696
|
+
settings: setting,
|
|
697
|
+
selectors: selectors.map(selector => selector.split(/ /)),
|
|
698
|
+
});
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
701
|
token.explanation = [];
|
|
702
702
|
let offset = 0;
|
|
703
703
|
while (startIndex + offset < nextStartIndex) {
|
|
@@ -706,7 +706,9 @@ function _tokenizeWithTheme(code, grammar, theme, colorMap, options) {
|
|
|
706
706
|
offset += tokenWithScopesText.length;
|
|
707
707
|
token.explanation.push({
|
|
708
708
|
content: tokenWithScopesText,
|
|
709
|
-
scopes:
|
|
709
|
+
scopes: options.includeExplanation === 'scopeName'
|
|
710
|
+
? explainThemeScopesNameOnly(tokenWithScopes.scopes)
|
|
711
|
+
: explainThemeScopesFull(themeSettingsSelectors, tokenWithScopes.scopes),
|
|
710
712
|
});
|
|
711
713
|
tokensWithScopesIndex += 1;
|
|
712
714
|
}
|
|
@@ -722,14 +724,16 @@ function _tokenizeWithTheme(code, grammar, theme, colorMap, options) {
|
|
|
722
724
|
stateStack,
|
|
723
725
|
};
|
|
724
726
|
}
|
|
725
|
-
function
|
|
727
|
+
function explainThemeScopesNameOnly(scopes) {
|
|
728
|
+
return scopes.map(scope => ({ scopeName: scope }));
|
|
729
|
+
}
|
|
730
|
+
function explainThemeScopesFull(themeSelectors, scopes) {
|
|
726
731
|
const result = [];
|
|
727
732
|
for (let i = 0, len = scopes.length; i < len; i++) {
|
|
728
|
-
const parentScopes = scopes.slice(0, i);
|
|
729
733
|
const scope = scopes[i];
|
|
730
734
|
result[i] = {
|
|
731
735
|
scopeName: scope,
|
|
732
|
-
themeMatches: explainThemeScope(themeSelectors, scope,
|
|
736
|
+
themeMatches: explainThemeScope(themeSelectors, scope, scopes.slice(0, i)),
|
|
733
737
|
};
|
|
734
738
|
}
|
|
735
739
|
return result;
|