@shikijs/core 1.11.1 → 1.12.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 +7 -5
- package/dist/index.d.mts +3 -1
- package/dist/index.mjs +23 -2
- package/package.json +1 -1
package/dist/chunk-tokens.d.mts
CHANGED
|
@@ -516,9 +516,13 @@ interface BundledThemeInfo {
|
|
|
516
516
|
* It's used to highlight code snippets that are part of the target language.
|
|
517
517
|
*/
|
|
518
518
|
declare class GrammarState {
|
|
519
|
-
private _stack;
|
|
520
|
-
lang: string;
|
|
521
|
-
theme: string;
|
|
519
|
+
private readonly _stack;
|
|
520
|
+
readonly lang: string;
|
|
521
|
+
readonly theme: string;
|
|
522
|
+
/**
|
|
523
|
+
* Static method to create a initial grammar state.
|
|
524
|
+
*/
|
|
525
|
+
static initial(lang: string, theme: string): GrammarState;
|
|
522
526
|
constructor(_stack: StateStack, lang: string, theme: string);
|
|
523
527
|
get scopes(): string[];
|
|
524
528
|
toJSON(): {
|
|
@@ -622,8 +626,6 @@ interface DecorationItem {
|
|
|
622
626
|
start: OffsetOrPosition;
|
|
623
627
|
/**
|
|
624
628
|
* End offset or position of the decoration.
|
|
625
|
-
*
|
|
626
|
-
* If the
|
|
627
629
|
*/
|
|
628
630
|
end: OffsetOrPosition;
|
|
629
631
|
/**
|
package/dist/index.d.mts
CHANGED
|
@@ -81,7 +81,7 @@ declare function createSingletonShorthands<L extends string, T extends string>(c
|
|
|
81
81
|
|
|
82
82
|
declare function toArray<T>(x: MaybeArray<T>): T[];
|
|
83
83
|
/**
|
|
84
|
-
*
|
|
84
|
+
* Split a string into lines, each line preserves the line ending.
|
|
85
85
|
*/
|
|
86
86
|
declare function splitLines(code: string, preserveEnding?: boolean): [string, number][];
|
|
87
87
|
/**
|
|
@@ -133,6 +133,8 @@ declare function getTokenStyleObject(token: TokenStyles): Record<string, string>
|
|
|
133
133
|
declare function stringifyTokenStyle(token: Record<string, string>): string;
|
|
134
134
|
/**
|
|
135
135
|
* Creates a converter between index and position in a code block.
|
|
136
|
+
*
|
|
137
|
+
* Overflow/underflow are unchecked.
|
|
136
138
|
*/
|
|
137
139
|
declare function createPositionConverter(code: string): {
|
|
138
140
|
lines: string[];
|
package/dist/index.mjs
CHANGED
|
@@ -5,7 +5,7 @@ function toArray(x) {
|
|
|
5
5
|
return Array.isArray(x) ? x : [x];
|
|
6
6
|
}
|
|
7
7
|
/**
|
|
8
|
-
*
|
|
8
|
+
* Split a string into lines, each line preserves the line ending.
|
|
9
9
|
*/
|
|
10
10
|
function splitLines(code, preserveEnding = false) {
|
|
11
11
|
const parts = code.split(/(\r?\n)/g);
|
|
@@ -158,10 +158,18 @@ function stringifyTokenStyle(token) {
|
|
|
158
158
|
}
|
|
159
159
|
/**
|
|
160
160
|
* Creates a converter between index and position in a code block.
|
|
161
|
+
*
|
|
162
|
+
* Overflow/underflow are unchecked.
|
|
161
163
|
*/
|
|
162
164
|
function createPositionConverter(code) {
|
|
163
165
|
const lines = splitLines(code, true).map(([line]) => line);
|
|
164
166
|
function indexToPos(index) {
|
|
167
|
+
if (index === code.length) {
|
|
168
|
+
return {
|
|
169
|
+
line: lines.length - 1,
|
|
170
|
+
character: lines[lines.length - 1].length,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
165
173
|
let character = index;
|
|
166
174
|
let line = 0;
|
|
167
175
|
for (const lineText of lines) {
|
|
@@ -533,6 +541,12 @@ class GrammarState {
|
|
|
533
541
|
_stack;
|
|
534
542
|
lang;
|
|
535
543
|
theme;
|
|
544
|
+
/**
|
|
545
|
+
* Static method to create a initial grammar state.
|
|
546
|
+
*/
|
|
547
|
+
static initial(lang, theme) {
|
|
548
|
+
return new GrammarState(INITIAL, lang, theme);
|
|
549
|
+
}
|
|
536
550
|
constructor(_stack, lang, theme) {
|
|
537
551
|
this._stack = _stack;
|
|
538
552
|
this.lang = lang;
|
|
@@ -866,7 +880,7 @@ function codeToTokens(internal, code, options) {
|
|
|
866
880
|
rootStyle = defaultColor ? undefined : [fg, bg].join(';');
|
|
867
881
|
}
|
|
868
882
|
else if ('theme' in options) {
|
|
869
|
-
const colorReplacements = resolveColorReplacements(options.theme, options
|
|
883
|
+
const colorReplacements = resolveColorReplacements(options.theme, options);
|
|
870
884
|
tokens = codeToTokensBase(internal, code, options);
|
|
871
885
|
const _theme = internal.getTheme(options.theme);
|
|
872
886
|
bg = applyColorReplacements(_theme.bg, colorReplacements);
|
|
@@ -926,12 +940,19 @@ function transformerDecorations() {
|
|
|
926
940
|
const converter = createPositionConverter(shiki.source);
|
|
927
941
|
function normalizePosition(p) {
|
|
928
942
|
if (typeof p === 'number') {
|
|
943
|
+
if (p < 0 || p > shiki.source.length)
|
|
944
|
+
throw new ShikiError(`Invalid decoration offset: ${p}. Code length: ${shiki.source.length}`);
|
|
929
945
|
return {
|
|
930
946
|
...converter.indexToPos(p),
|
|
931
947
|
offset: p,
|
|
932
948
|
};
|
|
933
949
|
}
|
|
934
950
|
else {
|
|
951
|
+
const line = converter.lines[p.line];
|
|
952
|
+
if (line === undefined)
|
|
953
|
+
throw new ShikiError(`Invalid decoration position ${JSON.stringify(p)}. Lines length: ${converter.lines.length}`);
|
|
954
|
+
if (p.character < 0 || p.character > line.length)
|
|
955
|
+
throw new ShikiError(`Invalid decoration position ${JSON.stringify(p)}. Line ${p.line} length: ${line.length}`);
|
|
935
956
|
return {
|
|
936
957
|
...p,
|
|
937
958
|
offset: converter.posToIndex(p.line, p.character),
|