@shikijs/core 1.1.2 → 1.1.4

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.
@@ -999,7 +999,7 @@ interface CodeOptionsMultipleThemes<Themes extends string = string> {
999
999
  }
1000
1000
  type CodeOptionsThemes<Themes extends string = string> = CodeOptionsSingleTheme<Themes> | CodeOptionsMultipleThemes<Themes>;
1001
1001
  type CodeToHastOptions<Languages extends string = string, Themes extends string = string> = CodeToHastOptionsCommon<Languages> & CodeOptionsThemes<Themes> & CodeOptionsMeta;
1002
- interface CodeToHastOptionsCommon<Languages extends string = string> extends TransformerOptions, DecorationOptions, Pick<TokenizeWithThemeOptions, 'colorReplacements'> {
1002
+ interface CodeToHastOptionsCommon<Languages extends string = string> extends TransformerOptions, DecorationOptions, Pick<TokenizeWithThemeOptions, 'colorReplacements' | 'tokenizeMaxLineLength' | 'tokenizeTimeLimit'> {
1003
1003
  lang: StringLiteralUnion<Languages | SpecialLanguage>;
1004
1004
  /**
1005
1005
  * Merge whitespace tokens to saving extra `<span>`.
@@ -1181,6 +1181,18 @@ interface TokenizeWithThemeOptions {
1181
1181
  * This will be merged with theme's `colorReplacements` if any.
1182
1182
  */
1183
1183
  colorReplacements?: Record<string, string>;
1184
+ /**
1185
+ * Lines above this length will not be tokenized for performance reasons.
1186
+ *
1187
+ * @default 0 (no limit)
1188
+ */
1189
+ tokenizeMaxLineLength?: number;
1190
+ /**
1191
+ * Time limit in milliseconds for tokenizing a single line.
1192
+ *
1193
+ * @default 500 (0.5s)
1194
+ */
1195
+ tokenizeTimeLimit?: number;
1184
1196
  }
1185
1197
  /**
1186
1198
  * Result of `codeToTokens`, an object with 2D array of tokens and meta info like background and foreground color.
package/dist/index.mjs CHANGED
@@ -524,6 +524,7 @@ function tokenizeWithTheme(code, grammar, theme, colorMap, options) {
524
524
  ...theme.colorReplacements,
525
525
  ...options?.colorReplacements,
526
526
  };
527
+ const { tokenizeMaxLineLength = 0, tokenizeTimeLimit = 500, } = options;
527
528
  const lines = splitLines(code);
528
529
  let ruleStack = INITIAL;
529
530
  let actual = [];
@@ -535,6 +536,17 @@ function tokenizeWithTheme(code, grammar, theme, colorMap, options) {
535
536
  final.push([]);
536
537
  continue;
537
538
  }
539
+ // Do not attempt to tokenize if the line length is longer than the `tokenizationMaxLineLength`
540
+ if (tokenizeMaxLineLength > 0 && line.length >= tokenizeMaxLineLength) {
541
+ actual = [];
542
+ final.push([{
543
+ content: line,
544
+ offset: lineOffset,
545
+ color: '',
546
+ fontStyle: 0,
547
+ }]);
548
+ continue;
549
+ }
538
550
  let resultWithScopes;
539
551
  let tokensWithScopes;
540
552
  let tokensWithScopesIndex;
@@ -543,7 +555,7 @@ function tokenizeWithTheme(code, grammar, theme, colorMap, options) {
543
555
  tokensWithScopes = resultWithScopes.tokens;
544
556
  tokensWithScopesIndex = 0;
545
557
  }
546
- const result = grammar.tokenizeLine2(line, ruleStack);
558
+ const result = grammar.tokenizeLine2(line, ruleStack, tokenizeTimeLimit);
547
559
  const tokensLength = result.tokens.length / 2;
548
560
  for (let j = 0; j < tokensLength; j++) {
549
561
  const startIndex = result.tokens[2 * j];
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@shikijs/core",
3
3
  "type": "module",
4
- "version": "1.1.2",
4
+ "version": "1.1.4",
5
5
  "description": "Core of Shiki",
6
6
  "author": "Pine Wu <octref@gmail.com>; Anthony Fu <anthonyfu117@hotmail.com>",
7
7
  "license": "MIT",