@shikijs/core 1.14.1 → 1.15.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/index.mjs CHANGED
@@ -978,27 +978,6 @@ function transformerDecorations() {
978
978
  }
979
979
  return map.get(shiki.meta);
980
980
  }
981
- function verifyIntersections(items) {
982
- for (let i = 0; i < items.length; i++) {
983
- const foo = items[i];
984
- if (foo.start.offset > foo.end.offset)
985
- throw new ShikiError(`Invalid decoration range: ${JSON.stringify(foo.start)} - ${JSON.stringify(foo.end)}`);
986
- for (let j = i + 1; j < items.length; j++) {
987
- const bar = items[j];
988
- const isFooHasBarStart = foo.start.offset < bar.start.offset && bar.start.offset < foo.end.offset;
989
- const isFooHasBarEnd = foo.start.offset < bar.end.offset && bar.end.offset < foo.end.offset;
990
- const isBarHasFooStart = bar.start.offset < foo.start.offset && foo.start.offset < bar.end.offset;
991
- const isBarHasFooEnd = bar.start.offset < foo.end.offset && foo.end.offset < bar.end.offset;
992
- if (isFooHasBarStart || isFooHasBarEnd || isBarHasFooStart || isBarHasFooEnd) {
993
- if (isFooHasBarEnd && isFooHasBarEnd)
994
- continue; // nested
995
- if (isBarHasFooStart && isBarHasFooEnd)
996
- continue; // nested
997
- throw new ShikiError(`Decorations ${JSON.stringify(foo.start)} and ${JSON.stringify(bar.start)} intersect.`);
998
- }
999
- }
1000
- }
1001
- }
1002
981
  return {
1003
982
  name: 'shiki:decorations',
1004
983
  tokens(tokens) {
@@ -1021,13 +1000,6 @@ function transformerDecorations() {
1021
1000
  let text = '';
1022
1001
  let startIndex = -1;
1023
1002
  let endIndex = -1;
1024
- function stringify(el) {
1025
- if (el.type === 'text')
1026
- return el.value;
1027
- if (el.type === 'element')
1028
- return el.children.map(stringify).join('');
1029
- return '';
1030
- }
1031
1003
  if (start === 0)
1032
1004
  startIndex = 0;
1033
1005
  if (end === 0)
@@ -1036,7 +1008,7 @@ function transformerDecorations() {
1036
1008
  endIndex = lineEl.children.length;
1037
1009
  if (startIndex === -1 || endIndex === -1) {
1038
1010
  for (let i = 0; i < lineEl.children.length; i++) {
1039
- text += stringify(lineEl.children[i]);
1011
+ text += stringify$2(lineEl.children[i]);
1040
1012
  if (startIndex === -1 && text.length === start)
1041
1013
  startIndex = i + 1;
1042
1014
  if (endIndex === -1 && text.length === end)
@@ -1104,6 +1076,34 @@ function transformerDecorations() {
1104
1076
  },
1105
1077
  };
1106
1078
  }
1079
+ function verifyIntersections(items) {
1080
+ for (let i = 0; i < items.length; i++) {
1081
+ const foo = items[i];
1082
+ if (foo.start.offset > foo.end.offset)
1083
+ throw new ShikiError(`Invalid decoration range: ${JSON.stringify(foo.start)} - ${JSON.stringify(foo.end)}`);
1084
+ for (let j = i + 1; j < items.length; j++) {
1085
+ const bar = items[j];
1086
+ const isFooHasBarStart = foo.start.offset < bar.start.offset && bar.start.offset < foo.end.offset;
1087
+ const isFooHasBarEnd = foo.start.offset < bar.end.offset && bar.end.offset < foo.end.offset;
1088
+ const isBarHasFooStart = bar.start.offset < foo.start.offset && foo.start.offset < bar.end.offset;
1089
+ const isBarHasFooEnd = bar.start.offset < foo.end.offset && foo.end.offset < bar.end.offset;
1090
+ if (isFooHasBarStart || isFooHasBarEnd || isBarHasFooStart || isBarHasFooEnd) {
1091
+ if (isFooHasBarEnd && isFooHasBarEnd)
1092
+ continue; // nested
1093
+ if (isBarHasFooStart && isBarHasFooEnd)
1094
+ continue; // nested
1095
+ throw new ShikiError(`Decorations ${JSON.stringify(foo.start)} and ${JSON.stringify(bar.start)} intersect.`);
1096
+ }
1097
+ }
1098
+ }
1099
+ }
1100
+ function stringify$2(el) {
1101
+ if (el.type === 'text')
1102
+ return el.value;
1103
+ if (el.type === 'element')
1104
+ return el.children.map(stringify$2).join('');
1105
+ return '';
1106
+ }
1107
1107
 
1108
1108
  const builtInTransformers = [
1109
1109
  /* @__PURE__ */ transformerDecorations(),
@@ -3383,10 +3383,8 @@ function stringifyEntities(value, options) {
3383
3383
  }
3384
3384
 
3385
3385
  /**
3386
- * @typedef {import('hast').Comment} Comment
3387
- * @typedef {import('hast').Parents} Parents
3388
- *
3389
- * @typedef {import('../index.js').State} State
3386
+ * @import {Comment, Parents} from 'hast'
3387
+ * @import {State} from '../index.js'
3390
3388
  */
3391
3389
 
3392
3390
 
@@ -3437,13 +3435,10 @@ function comment(node, _1, _2, state) {
3437
3435
  }
3438
3436
 
3439
3437
  /**
3440
- * @typedef {import('hast').Doctype} Doctype
3441
- * @typedef {import('hast').Parents} Parents
3442
- *
3443
- * @typedef {import('../index.js').State} State
3438
+ * @import {Doctype, Parents} from 'hast'
3439
+ * @import {State} from '../index.js'
3444
3440
  */
3445
3441
 
3446
-
3447
3442
  /**
3448
3443
  * Serialize a doctype.
3449
3444
  *
@@ -3587,8 +3582,7 @@ function empty(value) {
3587
3582
  }
3588
3583
 
3589
3584
  /**
3590
- * @typedef {import('hast').Parents} Parents
3591
- * @typedef {import('hast').RootContent} RootContent
3585
+ * @import {Parents, RootContent} from 'hast'
3592
3586
  */
3593
3587
 
3594
3588
 
@@ -3638,8 +3632,7 @@ function siblings(increment) {
3638
3632
  }
3639
3633
 
3640
3634
  /**
3641
- * @typedef {import('hast').Element} Element
3642
- * @typedef {import('hast').Parents} Parents
3635
+ * @import {Element, Parents} from 'hast'
3643
3636
  */
3644
3637
 
3645
3638
  /**
@@ -3684,8 +3677,7 @@ function omission(handlers) {
3684
3677
  }
3685
3678
 
3686
3679
  /**
3687
- * @typedef {import('hast').Element} Element
3688
- * @typedef {import('hast').Parents} Parents
3680
+ * @import {Element, Parents} from 'hast'
3689
3681
  */
3690
3682
 
3691
3683
 
@@ -4041,8 +4033,7 @@ function cells(_, index, parent) {
4041
4033
  }
4042
4034
 
4043
4035
  /**
4044
- * @typedef {import('hast').Element} Element
4045
- * @typedef {import('hast').Parents} Parents
4036
+ * @import {Element, Parents} from 'hast'
4046
4037
  */
4047
4038
 
4048
4039
 
@@ -4185,11 +4176,8 @@ function tbody(node, index, parent) {
4185
4176
  }
4186
4177
 
4187
4178
  /**
4188
- * @typedef {import('hast').Element} Element
4189
- * @typedef {import('hast').Parents} Parents
4190
- * @typedef {import('hast').Properties} Properties
4191
- *
4192
- * @typedef {import('../index.js').State} State
4179
+ * @import {Element, Parents, Properties} from 'hast'
4180
+ * @import {State} from '../index.js'
4193
4181
  */
4194
4182
 
4195
4183
 
@@ -4447,12 +4435,9 @@ function serializeAttribute(state, key, value) {
4447
4435
  }
4448
4436
 
4449
4437
  /**
4450
- * @typedef {import('hast').Parents} Parents
4451
- * @typedef {import('hast').Text} Text
4452
- *
4453
- * @typedef {import('mdast-util-to-hast').Raw} Raw
4454
- *
4455
- * @typedef {import('../index.js').State} State
4438
+ * @import {Parents, Text} from 'hast'
4439
+ * @import {Raw} from 'mdast-util-to-hast'
4440
+ * @import {State} from '../index.js'
4456
4441
  */
4457
4442
 
4458
4443
 
@@ -4488,11 +4473,9 @@ function text(node, _, parent, state) {
4488
4473
  }
4489
4474
 
4490
4475
  /**
4491
- * @typedef {import('hast').Parents} Parents
4492
- *
4493
- * @typedef {import('mdast-util-to-hast').Raw} Raw
4494
- *
4495
- * @typedef {import('../index.js').State} State
4476
+ * @import {Parents} from 'hast'
4477
+ * @import {Raw} from 'mdast-util-to-hast'
4478
+ * @import {State} from '../index.js'
4496
4479
  */
4497
4480
 
4498
4481
 
@@ -4517,13 +4500,10 @@ function raw(node, index, parent, state) {
4517
4500
  }
4518
4501
 
4519
4502
  /**
4520
- * @typedef {import('hast').Parents} Parents
4521
- * @typedef {import('hast').Root} Root
4522
- *
4523
- * @typedef {import('../index.js').State} State
4503
+ * @import {Parents, Root} from 'hast'
4504
+ * @import {State} from '../index.js'
4524
4505
  */
4525
4506
 
4526
-
4527
4507
  /**
4528
4508
  * Serialize a root.
4529
4509
  *
@@ -4543,10 +4523,8 @@ function root(node, _1, _2, state) {
4543
4523
  }
4544
4524
 
4545
4525
  /**
4546
- * @typedef {import('hast').Nodes} Nodes
4547
- * @typedef {import('hast').Parents} Parents
4548
- *
4549
- * @typedef {import('../index.js').State} State
4526
+ * @import {Nodes, Parents} from 'hast'
4527
+ * @import {State} from '../index.js'
4550
4528
  */
4551
4529
 
4552
4530
 
@@ -4586,13 +4564,9 @@ function unknown(node_) {
4586
4564
  }
4587
4565
 
4588
4566
  /**
4589
- * @typedef {import('hast').Nodes} Nodes
4590
- * @typedef {import('hast').Parents} Parents
4591
- * @typedef {import('hast').RootContent} RootContent
4592
- *
4593
- * @typedef {import('property-information').Schema} Schema
4594
- *
4595
- * @typedef {import('stringify-entities').Options} StringifyEntitiesOptions
4567
+ * @import {Nodes, Parents, RootContent} from 'hast'
4568
+ * @import {Schema} from 'property-information'
4569
+ * @import {Options as StringifyEntitiesOptions} from 'stringify-entities'
4596
4570
  */
4597
4571
 
4598
4572
 
@@ -4716,590 +4690,126 @@ function codeToHtml(internal, code, options) {
4716
4690
  return result;
4717
4691
  }
4718
4692
 
4719
- async function main(init) {
4720
- let wasmMemory;
4721
- let buffer;
4722
- const binding = {};
4723
- function updateGlobalBufferAndViews(buf) {
4724
- buffer = buf;
4725
- binding.HEAPU8 = new Uint8Array(buf);
4726
- binding.HEAPU32 = new Uint32Array(buf);
4727
- }
4728
- function _emscripten_get_now() {
4729
- return typeof performance !== 'undefined' ? performance.now() : Date.now();
4693
+ /**
4694
+ * https://github.com/microsoft/vscode/blob/f7f05dee53fb33fe023db2e06e30a89d3094488f/src/vs/platform/theme/common/colorRegistry.ts#L258-L268
4695
+ */
4696
+ const VSCODE_FALLBACK_EDITOR_FG = { light: '#333333', dark: '#bbbbbb' };
4697
+ const VSCODE_FALLBACK_EDITOR_BG = { light: '#fffffe', dark: '#1e1e1e' };
4698
+ const RESOLVED_KEY = '__shiki_resolved';
4699
+ /**
4700
+ * Normalize a textmate theme to shiki theme
4701
+ */
4702
+ function normalizeTheme(rawTheme) {
4703
+ // @ts-expect-error private field
4704
+ if (rawTheme?.[RESOLVED_KEY])
4705
+ return rawTheme;
4706
+ const theme = {
4707
+ ...rawTheme,
4708
+ };
4709
+ // Fallback settings
4710
+ if (theme.tokenColors && !theme.settings) {
4711
+ theme.settings = theme.tokenColors;
4712
+ delete theme.tokenColors;
4730
4713
  }
4731
- function _emscripten_memcpy_big(dest, src, num) {
4732
- binding.HEAPU8.copyWithin(dest, src, src + num);
4714
+ theme.type ||= 'dark';
4715
+ theme.colorReplacements = { ...theme.colorReplacements };
4716
+ theme.settings ||= [];
4717
+ // Guess fg/bg colors
4718
+ let { bg, fg } = theme;
4719
+ if (!bg || !fg) {
4720
+ /**
4721
+ * First try:
4722
+ * Theme might contain a global `tokenColor` without `name` or `scope`
4723
+ * Used as default value for foreground/background
4724
+ */
4725
+ const globalSetting = theme.settings
4726
+ ? theme.settings.find((s) => !s.name && !s.scope)
4727
+ : undefined;
4728
+ if (globalSetting?.settings?.foreground)
4729
+ fg = globalSetting.settings.foreground;
4730
+ if (globalSetting?.settings?.background)
4731
+ bg = globalSetting.settings.background;
4732
+ /**
4733
+ * Second try:
4734
+ * If there's no global `tokenColor` without `name` or `scope`
4735
+ * Use `editor.foreground` and `editor.background`
4736
+ */
4737
+ if (!fg && theme?.colors?.['editor.foreground'])
4738
+ fg = theme.colors['editor.foreground'];
4739
+ if (!bg && theme?.colors?.['editor.background'])
4740
+ bg = theme.colors['editor.background'];
4741
+ /**
4742
+ * Last try:
4743
+ * If there's no fg/bg color specified in theme, use default
4744
+ */
4745
+ if (!fg)
4746
+ fg = theme.type === 'light' ? VSCODE_FALLBACK_EDITOR_FG.light : VSCODE_FALLBACK_EDITOR_FG.dark;
4747
+ if (!bg)
4748
+ bg = theme.type === 'light' ? VSCODE_FALLBACK_EDITOR_BG.light : VSCODE_FALLBACK_EDITOR_BG.dark;
4749
+ theme.fg = fg;
4750
+ theme.bg = bg;
4733
4751
  }
4734
- function getHeapMax() {
4735
- return 2147483648;
4752
+ // Push a no-scope setting with fallback colors
4753
+ if (!(theme.settings[0] && theme.settings[0].settings && !theme.settings[0].scope)) {
4754
+ theme.settings.unshift({
4755
+ settings: {
4756
+ foreground: theme.fg,
4757
+ background: theme.bg,
4758
+ },
4759
+ });
4736
4760
  }
4737
- function emscripten_realloc_buffer(size) {
4738
- try {
4739
- wasmMemory.grow((size - buffer.byteLength + 65535) >>> 16);
4740
- updateGlobalBufferAndViews(wasmMemory.buffer);
4741
- return 1;
4742
- }
4743
- catch { }
4761
+ // Push non-hex colors to color replacements, as `vscode-textmate` doesn't support them
4762
+ let replacementCount = 0;
4763
+ const replacementMap = new Map();
4764
+ function getReplacementColor(value) {
4765
+ if (replacementMap.has(value))
4766
+ return replacementMap.get(value);
4767
+ replacementCount += 1;
4768
+ const hex = `#${replacementCount.toString(16).padStart(8, '0').toLowerCase()}`;
4769
+ if (theme.colorReplacements?.[`#${hex}`]) // already exists
4770
+ return getReplacementColor(value);
4771
+ replacementMap.set(value, hex);
4772
+ return hex;
4744
4773
  }
4745
- function _emscripten_resize_heap(requestedSize) {
4746
- const oldSize = binding.HEAPU8.length;
4747
- requestedSize = requestedSize >>> 0;
4748
- const maxHeapSize = getHeapMax();
4749
- if (requestedSize > maxHeapSize)
4750
- return false;
4751
- const alignUp = (x, multiple) => x + ((multiple - (x % multiple)) % multiple);
4752
- for (let cutDown = 1; cutDown <= 4; cutDown *= 2) {
4753
- let overGrownHeapSize = oldSize * (1 + 0.2 / cutDown);
4754
- overGrownHeapSize = Math.min(overGrownHeapSize, requestedSize + 100663296);
4755
- const newSize = Math.min(maxHeapSize, alignUp(Math.max(requestedSize, overGrownHeapSize), 65536));
4756
- const replacement = emscripten_realloc_buffer(newSize);
4757
- if (replacement)
4758
- return true;
4774
+ theme.settings = theme.settings.map((setting) => {
4775
+ const replaceFg = setting.settings?.foreground && !setting.settings.foreground.startsWith('#');
4776
+ const replaceBg = setting.settings?.background && !setting.settings.background.startsWith('#');
4777
+ if (!replaceFg && !replaceBg)
4778
+ return setting;
4779
+ const clone = {
4780
+ ...setting,
4781
+ settings: {
4782
+ ...setting.settings,
4783
+ },
4784
+ };
4785
+ if (replaceFg) {
4786
+ const replacement = getReplacementColor(setting.settings.foreground);
4787
+ theme.colorReplacements[replacement] = setting.settings.foreground;
4788
+ clone.settings.foreground = replacement;
4759
4789
  }
4760
- return false;
4761
- }
4762
- const UTF8Decoder = typeof TextDecoder != 'undefined' ? new TextDecoder('utf8') : undefined;
4763
- function UTF8ArrayToString(heapOrArray, idx, maxBytesToRead = 1024) {
4764
- const endIdx = idx + maxBytesToRead;
4765
- let endPtr = idx;
4766
- while (heapOrArray[endPtr] && !(endPtr >= endIdx))
4767
- ++endPtr;
4768
- if (endPtr - idx > 16 && heapOrArray.buffer && UTF8Decoder) {
4769
- return UTF8Decoder.decode(heapOrArray.subarray(idx, endPtr));
4790
+ if (replaceBg) {
4791
+ const replacement = getReplacementColor(setting.settings.background);
4792
+ theme.colorReplacements[replacement] = setting.settings.background;
4793
+ clone.settings.background = replacement;
4770
4794
  }
4771
- let str = '';
4772
- while (idx < endPtr) {
4773
- let u0 = heapOrArray[idx++];
4774
- if (!(u0 & 128)) {
4775
- str += String.fromCharCode(u0);
4776
- continue;
4777
- }
4778
- const u1 = heapOrArray[idx++] & 63;
4779
- if ((u0 & 224) === 192) {
4780
- str += String.fromCharCode(((u0 & 31) << 6) | u1);
4781
- continue;
4782
- }
4783
- const u2 = heapOrArray[idx++] & 63;
4784
- if ((u0 & 240) === 224) {
4785
- u0 = ((u0 & 15) << 12) | (u1 << 6) | u2;
4786
- }
4787
- else {
4788
- u0 = ((u0 & 7) << 18)
4789
- | (u1 << 12)
4790
- | (u2 << 6)
4791
- | (heapOrArray[idx++] & 63);
4792
- }
4793
- if (u0 < 65536) {
4794
- str += String.fromCharCode(u0);
4795
- }
4796
- else {
4797
- const ch = u0 - 65536;
4798
- str += String.fromCharCode(55296 | (ch >> 10), 56320 | (ch & 1023));
4795
+ return clone;
4796
+ });
4797
+ for (const key of Object.keys(theme.colors || {})) {
4798
+ // Only patch for known keys
4799
+ if (key === 'editor.foreground' || key === 'editor.background' || key.startsWith('terminal.ansi')) {
4800
+ if (!theme.colors[key]?.startsWith('#')) {
4801
+ const replacement = getReplacementColor(theme.colors[key]);
4802
+ theme.colorReplacements[replacement] = theme.colors[key];
4803
+ theme.colors[key] = replacement;
4799
4804
  }
4800
4805
  }
4801
- return str;
4802
- }
4803
- function UTF8ToString(ptr, maxBytesToRead) {
4804
- return ptr ? UTF8ArrayToString(binding.HEAPU8, ptr, maxBytesToRead) : '';
4805
- }
4806
- const asmLibraryArg = {
4807
- emscripten_get_now: _emscripten_get_now,
4808
- emscripten_memcpy_big: _emscripten_memcpy_big,
4809
- emscripten_resize_heap: _emscripten_resize_heap,
4810
- fd_write: () => 0,
4811
- };
4812
- async function createWasm() {
4813
- const info = {
4814
- env: asmLibraryArg,
4815
- wasi_snapshot_preview1: asmLibraryArg,
4816
- };
4817
- const exports = await init(info);
4818
- wasmMemory = exports.memory;
4819
- updateGlobalBufferAndViews(wasmMemory.buffer);
4820
- Object.assign(binding, exports);
4821
- binding.UTF8ToString = UTF8ToString;
4822
4806
  }
4823
- await createWasm();
4824
- return binding;
4825
- }
4826
-
4827
- /* ---------------------------------------------------------
4828
- * Copyright (C) Microsoft Corporation. All rights reserved.
4829
- *-------------------------------------------------------- */
4830
- let onigBinding = null;
4831
- let defaultDebugCall = false;
4832
- function throwLastOnigError(onigBinding) {
4833
- throw new ShikiError(onigBinding.UTF8ToString(onigBinding.getLastOnigError()));
4834
- }
4835
- class UtfString {
4836
- static _utf8ByteLength(str) {
4837
- let result = 0;
4838
- for (let i = 0, len = str.length; i < len; i++) {
4839
- const charCode = str.charCodeAt(i);
4840
- let codepoint = charCode;
4841
- let wasSurrogatePair = false;
4842
- if (charCode >= 0xD800 && charCode <= 0xDBFF) {
4843
- // Hit a high surrogate, try to look for a matching low surrogate
4844
- if (i + 1 < len) {
4845
- const nextCharCode = str.charCodeAt(i + 1);
4846
- if (nextCharCode >= 0xDC00 && nextCharCode <= 0xDFFF) {
4847
- // Found the matching low surrogate
4848
- codepoint = (((charCode - 0xD800) << 10) + 0x10000) | (nextCharCode - 0xDC00);
4849
- wasSurrogatePair = true;
4850
- }
4851
- }
4852
- }
4853
- if (codepoint <= 0x7F)
4854
- result += 1;
4855
- else if (codepoint <= 0x7FF)
4856
- result += 2;
4857
- else if (codepoint <= 0xFFFF)
4858
- result += 3;
4859
- else
4860
- result += 4;
4861
- if (wasSurrogatePair)
4862
- i++;
4863
- }
4864
- return result;
4865
- }
4866
- utf16Length;
4867
- utf8Length;
4868
- utf16Value;
4869
- utf8Value;
4870
- utf16OffsetToUtf8;
4871
- utf8OffsetToUtf16;
4872
- constructor(str) {
4873
- const utf16Length = str.length;
4874
- const utf8Length = UtfString._utf8ByteLength(str);
4875
- const computeIndicesMapping = (utf8Length !== utf16Length);
4876
- const utf16OffsetToUtf8 = computeIndicesMapping ? new Uint32Array(utf16Length + 1) : null;
4877
- if (computeIndicesMapping)
4878
- utf16OffsetToUtf8[utf16Length] = utf8Length;
4879
- const utf8OffsetToUtf16 = computeIndicesMapping ? new Uint32Array(utf8Length + 1) : null;
4880
- if (computeIndicesMapping)
4881
- utf8OffsetToUtf16[utf8Length] = utf16Length;
4882
- const utf8Value = new Uint8Array(utf8Length);
4883
- let i8 = 0;
4884
- for (let i16 = 0; i16 < utf16Length; i16++) {
4885
- const charCode = str.charCodeAt(i16);
4886
- let codePoint = charCode;
4887
- let wasSurrogatePair = false;
4888
- if (charCode >= 0xD800 && charCode <= 0xDBFF) {
4889
- // Hit a high surrogate, try to look for a matching low surrogate
4890
- if (i16 + 1 < utf16Length) {
4891
- const nextCharCode = str.charCodeAt(i16 + 1);
4892
- if (nextCharCode >= 0xDC00 && nextCharCode <= 0xDFFF) {
4893
- // Found the matching low surrogate
4894
- codePoint = (((charCode - 0xD800) << 10) + 0x10000) | (nextCharCode - 0xDC00);
4895
- wasSurrogatePair = true;
4896
- }
4897
- }
4898
- }
4899
- if (computeIndicesMapping) {
4900
- utf16OffsetToUtf8[i16] = i8;
4901
- if (wasSurrogatePair)
4902
- utf16OffsetToUtf8[i16 + 1] = i8;
4903
- if (codePoint <= 0x7F) {
4904
- utf8OffsetToUtf16[i8 + 0] = i16;
4905
- }
4906
- else if (codePoint <= 0x7FF) {
4907
- utf8OffsetToUtf16[i8 + 0] = i16;
4908
- utf8OffsetToUtf16[i8 + 1] = i16;
4909
- }
4910
- else if (codePoint <= 0xFFFF) {
4911
- utf8OffsetToUtf16[i8 + 0] = i16;
4912
- utf8OffsetToUtf16[i8 + 1] = i16;
4913
- utf8OffsetToUtf16[i8 + 2] = i16;
4914
- }
4915
- else {
4916
- utf8OffsetToUtf16[i8 + 0] = i16;
4917
- utf8OffsetToUtf16[i8 + 1] = i16;
4918
- utf8OffsetToUtf16[i8 + 2] = i16;
4919
- utf8OffsetToUtf16[i8 + 3] = i16;
4920
- }
4921
- }
4922
- if (codePoint <= 0x7F) {
4923
- utf8Value[i8++] = codePoint;
4924
- }
4925
- else if (codePoint <= 0x7FF) {
4926
- utf8Value[i8++] = 0b11000000 | ((codePoint & 0b00000000000000000000011111000000) >>> 6);
4927
- utf8Value[i8++] = 0b10000000 | ((codePoint & 0b00000000000000000000000000111111) >>> 0);
4928
- }
4929
- else if (codePoint <= 0xFFFF) {
4930
- utf8Value[i8++] = 0b11100000 | ((codePoint & 0b00000000000000001111000000000000) >>> 12);
4931
- utf8Value[i8++] = 0b10000000 | ((codePoint & 0b00000000000000000000111111000000) >>> 6);
4932
- utf8Value[i8++] = 0b10000000 | ((codePoint & 0b00000000000000000000000000111111) >>> 0);
4933
- }
4934
- else {
4935
- utf8Value[i8++] = 0b11110000 | ((codePoint & 0b00000000000111000000000000000000) >>> 18);
4936
- utf8Value[i8++] = 0b10000000 | ((codePoint & 0b00000000000000111111000000000000) >>> 12);
4937
- utf8Value[i8++] = 0b10000000 | ((codePoint & 0b00000000000000000000111111000000) >>> 6);
4938
- utf8Value[i8++] = 0b10000000 | ((codePoint & 0b00000000000000000000000000111111) >>> 0);
4939
- }
4940
- if (wasSurrogatePair)
4941
- i16++;
4942
- }
4943
- this.utf16Length = utf16Length;
4944
- this.utf8Length = utf8Length;
4945
- this.utf16Value = str;
4946
- this.utf8Value = utf8Value;
4947
- this.utf16OffsetToUtf8 = utf16OffsetToUtf8;
4948
- this.utf8OffsetToUtf16 = utf8OffsetToUtf16;
4949
- }
4950
- createString(onigBinding) {
4951
- const result = onigBinding.omalloc(this.utf8Length);
4952
- onigBinding.HEAPU8.set(this.utf8Value, result);
4953
- return result;
4954
- }
4955
- }
4956
- class OnigString {
4957
- static LAST_ID = 0;
4958
- static _sharedPtr = 0; // a pointer to a string of 10000 bytes
4959
- static _sharedPtrInUse = false;
4960
- id = (++OnigString.LAST_ID);
4961
- _onigBinding;
4962
- content;
4963
- utf16Length;
4964
- utf8Length;
4965
- utf16OffsetToUtf8;
4966
- utf8OffsetToUtf16;
4967
- ptr;
4968
- constructor(str) {
4969
- if (!onigBinding)
4970
- throw new ShikiError('Must invoke loadWasm first.');
4971
- this._onigBinding = onigBinding;
4972
- this.content = str;
4973
- const utfString = new UtfString(str);
4974
- this.utf16Length = utfString.utf16Length;
4975
- this.utf8Length = utfString.utf8Length;
4976
- this.utf16OffsetToUtf8 = utfString.utf16OffsetToUtf8;
4977
- this.utf8OffsetToUtf16 = utfString.utf8OffsetToUtf16;
4978
- if (this.utf8Length < 10000 && !OnigString._sharedPtrInUse) {
4979
- if (!OnigString._sharedPtr)
4980
- OnigString._sharedPtr = onigBinding.omalloc(10000);
4981
- OnigString._sharedPtrInUse = true;
4982
- onigBinding.HEAPU8.set(utfString.utf8Value, OnigString._sharedPtr);
4983
- this.ptr = OnigString._sharedPtr;
4984
- }
4985
- else {
4986
- this.ptr = utfString.createString(onigBinding);
4987
- }
4988
- }
4989
- convertUtf8OffsetToUtf16(utf8Offset) {
4990
- if (this.utf8OffsetToUtf16) {
4991
- if (utf8Offset < 0)
4992
- return 0;
4993
- if (utf8Offset > this.utf8Length)
4994
- return this.utf16Length;
4995
- return this.utf8OffsetToUtf16[utf8Offset];
4996
- }
4997
- return utf8Offset;
4998
- }
4999
- convertUtf16OffsetToUtf8(utf16Offset) {
5000
- if (this.utf16OffsetToUtf8) {
5001
- if (utf16Offset < 0)
5002
- return 0;
5003
- if (utf16Offset > this.utf16Length)
5004
- return this.utf8Length;
5005
- return this.utf16OffsetToUtf8[utf16Offset];
5006
- }
5007
- return utf16Offset;
5008
- }
5009
- dispose() {
5010
- if (this.ptr === OnigString._sharedPtr)
5011
- OnigString._sharedPtrInUse = false;
5012
- else
5013
- this._onigBinding.ofree(this.ptr);
5014
- }
5015
- }
5016
- class OnigScanner {
5017
- _onigBinding;
5018
- _ptr;
5019
- constructor(patterns) {
5020
- if (!onigBinding)
5021
- throw new ShikiError('Must invoke loadWasm first.');
5022
- const strPtrsArr = [];
5023
- const strLenArr = [];
5024
- for (let i = 0, len = patterns.length; i < len; i++) {
5025
- const utfString = new UtfString(patterns[i]);
5026
- strPtrsArr[i] = utfString.createString(onigBinding);
5027
- strLenArr[i] = utfString.utf8Length;
5028
- }
5029
- const strPtrsPtr = onigBinding.omalloc(4 * patterns.length);
5030
- onigBinding.HEAPU32.set(strPtrsArr, strPtrsPtr / 4);
5031
- const strLenPtr = onigBinding.omalloc(4 * patterns.length);
5032
- onigBinding.HEAPU32.set(strLenArr, strLenPtr / 4);
5033
- const scannerPtr = onigBinding.createOnigScanner(strPtrsPtr, strLenPtr, patterns.length);
5034
- for (let i = 0, len = patterns.length; i < len; i++)
5035
- onigBinding.ofree(strPtrsArr[i]);
5036
- onigBinding.ofree(strLenPtr);
5037
- onigBinding.ofree(strPtrsPtr);
5038
- if (scannerPtr === 0)
5039
- throwLastOnigError(onigBinding);
5040
- this._onigBinding = onigBinding;
5041
- this._ptr = scannerPtr;
5042
- }
5043
- dispose() {
5044
- this._onigBinding.freeOnigScanner(this._ptr);
5045
- }
5046
- findNextMatchSync(string, startPosition, arg) {
5047
- let debugCall = defaultDebugCall;
5048
- let options = 0 /* FindOption.None */;
5049
- if (typeof arg === 'number') {
5050
- if (arg & 8 /* FindOption.DebugCall */)
5051
- debugCall = true;
5052
- options = arg;
5053
- }
5054
- else if (typeof arg === 'boolean') {
5055
- debugCall = arg;
5056
- }
5057
- if (typeof string === 'string') {
5058
- string = new OnigString(string);
5059
- const result = this._findNextMatchSync(string, startPosition, debugCall, options);
5060
- string.dispose();
5061
- return result;
5062
- }
5063
- return this._findNextMatchSync(string, startPosition, debugCall, options);
5064
- }
5065
- _findNextMatchSync(string, startPosition, debugCall, options) {
5066
- const onigBinding = this._onigBinding;
5067
- let resultPtr;
5068
- if (debugCall)
5069
- resultPtr = onigBinding.findNextOnigScannerMatchDbg(this._ptr, string.id, string.ptr, string.utf8Length, string.convertUtf16OffsetToUtf8(startPosition), options);
5070
- else
5071
- resultPtr = onigBinding.findNextOnigScannerMatch(this._ptr, string.id, string.ptr, string.utf8Length, string.convertUtf16OffsetToUtf8(startPosition), options);
5072
- if (resultPtr === 0) {
5073
- // no match
5074
- return null;
5075
- }
5076
- const HEAPU32 = onigBinding.HEAPU32;
5077
- let offset = resultPtr / 4; // byte offset -> uint32 offset
5078
- const index = HEAPU32[offset++];
5079
- const count = HEAPU32[offset++];
5080
- const captureIndices = [];
5081
- for (let i = 0; i < count; i++) {
5082
- const beg = string.convertUtf8OffsetToUtf16(HEAPU32[offset++]);
5083
- const end = string.convertUtf8OffsetToUtf16(HEAPU32[offset++]);
5084
- captureIndices[i] = {
5085
- start: beg,
5086
- end,
5087
- length: end - beg,
5088
- };
5089
- }
5090
- return {
5091
- index,
5092
- captureIndices,
5093
- };
5094
- }
5095
- }
5096
- function isInstantiatorOptionsObject(dataOrOptions) {
5097
- return (typeof dataOrOptions.instantiator === 'function');
5098
- }
5099
- function isInstantiatorModule(dataOrOptions) {
5100
- return (typeof dataOrOptions.default === 'function');
5101
- }
5102
- function isDataOptionsObject(dataOrOptions) {
5103
- return (typeof dataOrOptions.data !== 'undefined');
5104
- }
5105
- function isResponse(dataOrOptions) {
5106
- return (typeof Response !== 'undefined' && dataOrOptions instanceof Response);
5107
- }
5108
- function isArrayBuffer(data) {
5109
- return (typeof ArrayBuffer !== 'undefined' && (data instanceof ArrayBuffer || ArrayBuffer.isView(data)))
5110
- // eslint-disable-next-line node/prefer-global/buffer
5111
- || (typeof Buffer !== 'undefined' && Buffer.isBuffer?.(data))
5112
- || (typeof SharedArrayBuffer !== 'undefined' && data instanceof SharedArrayBuffer)
5113
- || (typeof Uint32Array !== 'undefined' && data instanceof Uint32Array);
5114
- }
5115
- let initPromise;
5116
- function loadWasm(options) {
5117
- if (initPromise)
5118
- return initPromise;
5119
- async function _load() {
5120
- onigBinding = await main(async (info) => {
5121
- let instance = options;
5122
- instance = await instance;
5123
- if (typeof instance === 'function')
5124
- instance = await instance(info);
5125
- if (typeof instance === 'function')
5126
- instance = await instance(info);
5127
- if (isInstantiatorOptionsObject(instance)) {
5128
- instance = await instance.instantiator(info);
5129
- }
5130
- else if (isInstantiatorModule(instance)) {
5131
- instance = await instance.default(info);
5132
- }
5133
- else {
5134
- if (isDataOptionsObject(instance))
5135
- instance = instance.data;
5136
- if (isResponse(instance)) {
5137
- if (typeof WebAssembly.instantiateStreaming === 'function')
5138
- instance = await _makeResponseStreamingLoader(instance)(info);
5139
- else
5140
- instance = await _makeResponseNonStreamingLoader(instance)(info);
5141
- }
5142
- else if (isArrayBuffer(instance)) {
5143
- instance = await _makeArrayBufferLoader(instance)(info);
5144
- }
5145
- // import("shiki/onig.wasm") returns `{ default: WebAssembly.Module }` on cloudflare workers
5146
- // https://developers.cloudflare.com/workers/wrangler/bundling/
5147
- else if (instance instanceof WebAssembly.Module) {
5148
- instance = await _makeArrayBufferLoader(instance)(info);
5149
- }
5150
- else if ('default' in instance && instance.default instanceof WebAssembly.Module) {
5151
- instance = await _makeArrayBufferLoader(instance.default)(info);
5152
- }
5153
- }
5154
- if ('instance' in instance)
5155
- instance = instance.instance;
5156
- if ('exports' in instance)
5157
- instance = instance.exports;
5158
- return instance;
5159
- });
5160
- }
5161
- initPromise = _load();
5162
- return initPromise;
5163
- }
5164
- function _makeArrayBufferLoader(data) {
5165
- return importObject => WebAssembly.instantiate(data, importObject);
5166
- }
5167
- function _makeResponseStreamingLoader(data) {
5168
- return importObject => WebAssembly.instantiateStreaming(data, importObject);
5169
- }
5170
- function _makeResponseNonStreamingLoader(data) {
5171
- return async (importObject) => {
5172
- const arrayBuffer = await data.arrayBuffer();
5173
- return WebAssembly.instantiate(arrayBuffer, importObject);
5174
- };
5175
- }
5176
- function createOnigString(str) {
5177
- return new OnigString(str);
5178
- }
5179
- function createOnigScanner(patterns) {
5180
- return new OnigScanner(patterns);
5181
- }
5182
-
5183
- /**
5184
- * https://github.com/microsoft/vscode/blob/f7f05dee53fb33fe023db2e06e30a89d3094488f/src/vs/platform/theme/common/colorRegistry.ts#L258-L268
5185
- */
5186
- const VSCODE_FALLBACK_EDITOR_FG = { light: '#333333', dark: '#bbbbbb' };
5187
- const VSCODE_FALLBACK_EDITOR_BG = { light: '#fffffe', dark: '#1e1e1e' };
5188
- const RESOLVED_KEY = '__shiki_resolved';
5189
- /**
5190
- * Normalize a textmate theme to shiki theme
5191
- */
5192
- function normalizeTheme(rawTheme) {
5193
- // @ts-expect-error private field
5194
- if (rawTheme?.[RESOLVED_KEY])
5195
- return rawTheme;
5196
- const theme = {
5197
- ...rawTheme,
5198
- };
5199
- // Fallback settings
5200
- if (theme.tokenColors && !theme.settings) {
5201
- theme.settings = theme.tokenColors;
5202
- delete theme.tokenColors;
5203
- }
5204
- theme.type ||= 'dark';
5205
- theme.colorReplacements = { ...theme.colorReplacements };
5206
- theme.settings ||= [];
5207
- // Guess fg/bg colors
5208
- let { bg, fg } = theme;
5209
- if (!bg || !fg) {
5210
- /**
5211
- * First try:
5212
- * Theme might contain a global `tokenColor` without `name` or `scope`
5213
- * Used as default value for foreground/background
5214
- */
5215
- const globalSetting = theme.settings
5216
- ? theme.settings.find((s) => !s.name && !s.scope)
5217
- : undefined;
5218
- if (globalSetting?.settings?.foreground)
5219
- fg = globalSetting.settings.foreground;
5220
- if (globalSetting?.settings?.background)
5221
- bg = globalSetting.settings.background;
5222
- /**
5223
- * Second try:
5224
- * If there's no global `tokenColor` without `name` or `scope`
5225
- * Use `editor.foreground` and `editor.background`
5226
- */
5227
- if (!fg && theme?.colors?.['editor.foreground'])
5228
- fg = theme.colors['editor.foreground'];
5229
- if (!bg && theme?.colors?.['editor.background'])
5230
- bg = theme.colors['editor.background'];
5231
- /**
5232
- * Last try:
5233
- * If there's no fg/bg color specified in theme, use default
5234
- */
5235
- if (!fg)
5236
- fg = theme.type === 'light' ? VSCODE_FALLBACK_EDITOR_FG.light : VSCODE_FALLBACK_EDITOR_FG.dark;
5237
- if (!bg)
5238
- bg = theme.type === 'light' ? VSCODE_FALLBACK_EDITOR_BG.light : VSCODE_FALLBACK_EDITOR_BG.dark;
5239
- theme.fg = fg;
5240
- theme.bg = bg;
5241
- }
5242
- // Push a no-scope setting with fallback colors
5243
- if (!(theme.settings[0] && theme.settings[0].settings && !theme.settings[0].scope)) {
5244
- theme.settings.unshift({
5245
- settings: {
5246
- foreground: theme.fg,
5247
- background: theme.bg,
5248
- },
5249
- });
5250
- }
5251
- // Push non-hex colors to color replacements, as `vscode-textmate` doesn't support them
5252
- let replacementCount = 0;
5253
- const replacementMap = new Map();
5254
- function getReplacementColor(value) {
5255
- if (replacementMap.has(value))
5256
- return replacementMap.get(value);
5257
- replacementCount += 1;
5258
- const hex = `#${replacementCount.toString(16).padStart(8, '0').toLowerCase()}`;
5259
- if (theme.colorReplacements?.[`#${hex}`]) // already exists
5260
- return getReplacementColor(value);
5261
- replacementMap.set(value, hex);
5262
- return hex;
5263
- }
5264
- theme.settings = theme.settings.map((setting) => {
5265
- const replaceFg = setting.settings?.foreground && !setting.settings.foreground.startsWith('#');
5266
- const replaceBg = setting.settings?.background && !setting.settings.background.startsWith('#');
5267
- if (!replaceFg && !replaceBg)
5268
- return setting;
5269
- const clone = {
5270
- ...setting,
5271
- settings: {
5272
- ...setting.settings,
5273
- },
5274
- };
5275
- if (replaceFg) {
5276
- const replacement = getReplacementColor(setting.settings.foreground);
5277
- theme.colorReplacements[replacement] = setting.settings.foreground;
5278
- clone.settings.foreground = replacement;
5279
- }
5280
- if (replaceBg) {
5281
- const replacement = getReplacementColor(setting.settings.background);
5282
- theme.colorReplacements[replacement] = setting.settings.background;
5283
- clone.settings.background = replacement;
5284
- }
5285
- return clone;
5286
- });
5287
- for (const key of Object.keys(theme.colors || {})) {
5288
- // Only patch for known keys
5289
- if (key === 'editor.foreground' || key === 'editor.background' || key.startsWith('terminal.ansi')) {
5290
- if (!theme.colors[key]?.startsWith('#')) {
5291
- const replacement = getReplacementColor(theme.colors[key]);
5292
- theme.colorReplacements[replacement] = theme.colors[key];
5293
- theme.colors[key] = replacement;
5294
- }
5295
- }
5296
- }
5297
- Object.defineProperty(theme, RESOLVED_KEY, {
5298
- enumerable: false,
5299
- writable: false,
5300
- value: true,
5301
- });
5302
- return theme;
4807
+ Object.defineProperty(theme, RESOLVED_KEY, {
4808
+ enumerable: false,
4809
+ writable: false,
4810
+ value: true,
4811
+ });
4812
+ return theme;
5303
4813
  }
5304
4814
 
5305
4815
  class Registry extends Registry$1 {
@@ -5367,133 +4877,614 @@ class Registry extends Registry$1 {
5367
4877
  resolved.add(name);
5368
4878
  }
5369
4879
  }
5370
- return this._resolvedGrammars.get(name);
4880
+ return this._resolvedGrammars.get(name);
4881
+ }
4882
+ async loadLanguage(lang) {
4883
+ if (this.getGrammar(lang.name))
4884
+ return;
4885
+ const embeddedLazilyBy = new Set([...this._langMap.values()]
4886
+ .filter(i => i.embeddedLangsLazy?.includes(lang.name)));
4887
+ this._resolver.addLanguage(lang);
4888
+ const grammarConfig = {
4889
+ balancedBracketSelectors: lang.balancedBracketSelectors || ['*'],
4890
+ unbalancedBracketSelectors: lang.unbalancedBracketSelectors || [],
4891
+ };
4892
+ // @ts-expect-error Private members, set this to override the previous grammar (that can be a stub)
4893
+ this._syncRegistry._rawGrammars.set(lang.scopeName, lang);
4894
+ const g = await this.loadGrammarWithConfiguration(lang.scopeName, 1, grammarConfig);
4895
+ g.name = lang.name;
4896
+ this._resolvedGrammars.set(lang.name, g);
4897
+ if (lang.aliases) {
4898
+ lang.aliases.forEach((alias) => {
4899
+ this._alias[alias] = lang.name;
4900
+ });
4901
+ }
4902
+ // Reset cache
4903
+ this._loadedLanguagesCache = null;
4904
+ // If there is a language that embeds this language lazily, we need to reload it
4905
+ if (embeddedLazilyBy.size) {
4906
+ for (const e of embeddedLazilyBy) {
4907
+ this._resolvedGrammars.delete(e.name);
4908
+ // Reset cache
4909
+ this._loadedLanguagesCache = null;
4910
+ // @ts-expect-error clear cache
4911
+ this._syncRegistry?._injectionGrammars?.delete(e.scopeName);
4912
+ // @ts-expect-error clear cache
4913
+ this._syncRegistry?._grammars?.delete(e.scopeName);
4914
+ await this.loadLanguage(this._langMap.get(e.name));
4915
+ }
4916
+ }
4917
+ }
4918
+ async init() {
4919
+ this._themes.map(t => this.loadTheme(t));
4920
+ await this.loadLanguages(this._langs);
4921
+ }
4922
+ dispose() {
4923
+ super.dispose();
4924
+ this._resolvedThemes.clear();
4925
+ this._resolvedGrammars.clear();
4926
+ this._langMap.clear();
4927
+ this._langGraph.clear();
4928
+ this._loadedThemesCache = null;
4929
+ }
4930
+ async loadLanguages(langs) {
4931
+ for (const lang of langs)
4932
+ this.resolveEmbeddedLanguages(lang);
4933
+ const langsGraphArray = Array.from(this._langGraph.entries());
4934
+ const missingLangs = langsGraphArray.filter(([_, lang]) => !lang);
4935
+ if (missingLangs.length) {
4936
+ const dependents = langsGraphArray
4937
+ .filter(([_, lang]) => lang && lang.embeddedLangs?.some(l => missingLangs.map(([name]) => name).includes(l)))
4938
+ .filter(lang => !missingLangs.includes(lang));
4939
+ throw new ShikiError(`Missing languages ${missingLangs.map(([name]) => `\`${name}\``).join(', ')}, required by ${dependents.map(([name]) => `\`${name}\``).join(', ')}`);
4940
+ }
4941
+ for (const [_, lang] of langsGraphArray)
4942
+ this._resolver.addLanguage(lang);
4943
+ for (const [_, lang] of langsGraphArray)
4944
+ await this.loadLanguage(lang);
4945
+ }
4946
+ getLoadedLanguages() {
4947
+ if (!this._loadedLanguagesCache) {
4948
+ this._loadedLanguagesCache = [
4949
+ ...new Set([...this._resolvedGrammars.keys(), ...Object.keys(this._alias)]),
4950
+ ];
4951
+ }
4952
+ return this._loadedLanguagesCache;
4953
+ }
4954
+ resolveEmbeddedLanguages(lang) {
4955
+ this._langMap.set(lang.name, lang);
4956
+ this._langGraph.set(lang.name, lang);
4957
+ if (lang.embeddedLangs) {
4958
+ for (const embeddedLang of lang.embeddedLangs)
4959
+ this._langGraph.set(embeddedLang, this._langMap.get(embeddedLang));
4960
+ }
4961
+ }
4962
+ }
4963
+
4964
+ class Resolver {
4965
+ _langs = new Map();
4966
+ _scopeToLang = new Map();
4967
+ _injections = new Map();
4968
+ _onigLibPromise;
4969
+ constructor(onigLibPromise, langs) {
4970
+ this._onigLibPromise = onigLibPromise
4971
+ .then(engine => ({
4972
+ createOnigScanner: patterns => engine.createScanner(patterns),
4973
+ createOnigString: s => engine.createString(s),
4974
+ }));
4975
+ langs.forEach(i => this.addLanguage(i));
4976
+ }
4977
+ get onigLib() {
4978
+ return this._onigLibPromise;
4979
+ }
4980
+ getLangRegistration(langIdOrAlias) {
4981
+ return this._langs.get(langIdOrAlias);
4982
+ }
4983
+ async loadGrammar(scopeName) {
4984
+ return this._scopeToLang.get(scopeName);
4985
+ }
4986
+ addLanguage(l) {
4987
+ this._langs.set(l.name, l);
4988
+ if (l.aliases) {
4989
+ l.aliases.forEach((a) => {
4990
+ this._langs.set(a, l);
4991
+ });
4992
+ }
4993
+ this._scopeToLang.set(l.scopeName, l);
4994
+ if (l.injectTo) {
4995
+ l.injectTo.forEach((i) => {
4996
+ if (!this._injections.get(i))
4997
+ this._injections.set(i, []);
4998
+ this._injections.get(i).push(l.scopeName);
4999
+ });
5000
+ }
5001
+ }
5002
+ getInjections(scopeName) {
5003
+ const scopeParts = scopeName.split('.');
5004
+ let injections = [];
5005
+ for (let i = 1; i <= scopeParts.length; i++) {
5006
+ const subScopeName = scopeParts.slice(0, i).join('.');
5007
+ injections = [...injections, ...(this._injections.get(subScopeName) || [])];
5008
+ }
5009
+ return injections;
5010
+ }
5011
+ }
5012
+
5013
+ function getHeapMax() {
5014
+ return 2147483648;
5015
+ }
5016
+ function _emscripten_get_now() {
5017
+ return typeof performance !== 'undefined' ? performance.now() : Date.now();
5018
+ }
5019
+ const alignUp = (x, multiple) => x + ((multiple - (x % multiple)) % multiple);
5020
+ async function main(init) {
5021
+ let wasmMemory;
5022
+ let buffer;
5023
+ const binding = {};
5024
+ function updateGlobalBufferAndViews(buf) {
5025
+ buffer = buf;
5026
+ binding.HEAPU8 = new Uint8Array(buf);
5027
+ binding.HEAPU32 = new Uint32Array(buf);
5028
+ }
5029
+ function _emscripten_memcpy_big(dest, src, num) {
5030
+ binding.HEAPU8.copyWithin(dest, src, src + num);
5031
+ }
5032
+ function emscripten_realloc_buffer(size) {
5033
+ try {
5034
+ wasmMemory.grow((size - buffer.byteLength + 65535) >>> 16);
5035
+ updateGlobalBufferAndViews(wasmMemory.buffer);
5036
+ return 1;
5037
+ }
5038
+ catch { }
5039
+ }
5040
+ function _emscripten_resize_heap(requestedSize) {
5041
+ const oldSize = binding.HEAPU8.length;
5042
+ requestedSize = requestedSize >>> 0;
5043
+ const maxHeapSize = getHeapMax();
5044
+ if (requestedSize > maxHeapSize)
5045
+ return false;
5046
+ for (let cutDown = 1; cutDown <= 4; cutDown *= 2) {
5047
+ let overGrownHeapSize = oldSize * (1 + 0.2 / cutDown);
5048
+ overGrownHeapSize = Math.min(overGrownHeapSize, requestedSize + 100663296);
5049
+ const newSize = Math.min(maxHeapSize, alignUp(Math.max(requestedSize, overGrownHeapSize), 65536));
5050
+ const replacement = emscripten_realloc_buffer(newSize);
5051
+ if (replacement)
5052
+ return true;
5053
+ }
5054
+ return false;
5055
+ }
5056
+ const UTF8Decoder = typeof TextDecoder != 'undefined' ? new TextDecoder('utf8') : undefined;
5057
+ function UTF8ArrayToString(heapOrArray, idx, maxBytesToRead = 1024) {
5058
+ const endIdx = idx + maxBytesToRead;
5059
+ let endPtr = idx;
5060
+ while (heapOrArray[endPtr] && !(endPtr >= endIdx))
5061
+ ++endPtr;
5062
+ if (endPtr - idx > 16 && heapOrArray.buffer && UTF8Decoder) {
5063
+ return UTF8Decoder.decode(heapOrArray.subarray(idx, endPtr));
5064
+ }
5065
+ let str = '';
5066
+ while (idx < endPtr) {
5067
+ let u0 = heapOrArray[idx++];
5068
+ if (!(u0 & 128)) {
5069
+ str += String.fromCharCode(u0);
5070
+ continue;
5071
+ }
5072
+ const u1 = heapOrArray[idx++] & 63;
5073
+ if ((u0 & 224) === 192) {
5074
+ str += String.fromCharCode(((u0 & 31) << 6) | u1);
5075
+ continue;
5076
+ }
5077
+ const u2 = heapOrArray[idx++] & 63;
5078
+ if ((u0 & 240) === 224) {
5079
+ u0 = ((u0 & 15) << 12) | (u1 << 6) | u2;
5080
+ }
5081
+ else {
5082
+ u0 = ((u0 & 7) << 18)
5083
+ | (u1 << 12)
5084
+ | (u2 << 6)
5085
+ | (heapOrArray[idx++] & 63);
5086
+ }
5087
+ if (u0 < 65536) {
5088
+ str += String.fromCharCode(u0);
5089
+ }
5090
+ else {
5091
+ const ch = u0 - 65536;
5092
+ str += String.fromCharCode(55296 | (ch >> 10), 56320 | (ch & 1023));
5093
+ }
5094
+ }
5095
+ return str;
5096
+ }
5097
+ function UTF8ToString(ptr, maxBytesToRead) {
5098
+ return ptr ? UTF8ArrayToString(binding.HEAPU8, ptr, maxBytesToRead) : '';
5099
+ }
5100
+ const asmLibraryArg = {
5101
+ emscripten_get_now: _emscripten_get_now,
5102
+ emscripten_memcpy_big: _emscripten_memcpy_big,
5103
+ emscripten_resize_heap: _emscripten_resize_heap,
5104
+ fd_write: () => 0,
5105
+ };
5106
+ async function createWasm() {
5107
+ const info = {
5108
+ env: asmLibraryArg,
5109
+ wasi_snapshot_preview1: asmLibraryArg,
5110
+ };
5111
+ const exports = await init(info);
5112
+ wasmMemory = exports.memory;
5113
+ updateGlobalBufferAndViews(wasmMemory.buffer);
5114
+ Object.assign(binding, exports);
5115
+ binding.UTF8ToString = UTF8ToString;
5116
+ }
5117
+ await createWasm();
5118
+ return binding;
5119
+ }
5120
+
5121
+ /* ---------------------------------------------------------
5122
+ * Copyright (C) Microsoft Corporation. All rights reserved.
5123
+ *-------------------------------------------------------- */
5124
+ let onigBinding = null;
5125
+ // let defaultDebugCall = false
5126
+ function throwLastOnigError(onigBinding) {
5127
+ throw new ShikiError(onigBinding.UTF8ToString(onigBinding.getLastOnigError()));
5128
+ }
5129
+ class UtfString {
5130
+ static _utf8ByteLength(str) {
5131
+ let result = 0;
5132
+ for (let i = 0, len = str.length; i < len; i++) {
5133
+ const charCode = str.charCodeAt(i);
5134
+ let codepoint = charCode;
5135
+ let wasSurrogatePair = false;
5136
+ if (charCode >= 0xD800 && charCode <= 0xDBFF) {
5137
+ // Hit a high surrogate, try to look for a matching low surrogate
5138
+ if (i + 1 < len) {
5139
+ const nextCharCode = str.charCodeAt(i + 1);
5140
+ if (nextCharCode >= 0xDC00 && nextCharCode <= 0xDFFF) {
5141
+ // Found the matching low surrogate
5142
+ codepoint = (((charCode - 0xD800) << 10) + 0x10000) | (nextCharCode - 0xDC00);
5143
+ wasSurrogatePair = true;
5144
+ }
5145
+ }
5146
+ }
5147
+ if (codepoint <= 0x7F)
5148
+ result += 1;
5149
+ else if (codepoint <= 0x7FF)
5150
+ result += 2;
5151
+ else if (codepoint <= 0xFFFF)
5152
+ result += 3;
5153
+ else
5154
+ result += 4;
5155
+ if (wasSurrogatePair)
5156
+ i++;
5157
+ }
5158
+ return result;
5371
5159
  }
5372
- async loadLanguage(lang) {
5373
- if (this.getGrammar(lang.name))
5374
- return;
5375
- const embeddedLazilyBy = new Set([...this._langMap.values()]
5376
- .filter(i => i.embeddedLangsLazy?.includes(lang.name)));
5377
- this._resolver.addLanguage(lang);
5378
- const grammarConfig = {
5379
- balancedBracketSelectors: lang.balancedBracketSelectors || ['*'],
5380
- unbalancedBracketSelectors: lang.unbalancedBracketSelectors || [],
5381
- };
5382
- // @ts-expect-error Private members, set this to override the previous grammar (that can be a stub)
5383
- this._syncRegistry._rawGrammars.set(lang.scopeName, lang);
5384
- const g = await this.loadGrammarWithConfiguration(lang.scopeName, 1, grammarConfig);
5385
- g.name = lang.name;
5386
- this._resolvedGrammars.set(lang.name, g);
5387
- if (lang.aliases) {
5388
- lang.aliases.forEach((alias) => {
5389
- this._alias[alias] = lang.name;
5390
- });
5391
- }
5392
- // Reset cache
5393
- this._loadedLanguagesCache = null;
5394
- // If there is a language that embeds this language lazily, we need to reload it
5395
- if (embeddedLazilyBy.size) {
5396
- for (const e of embeddedLazilyBy) {
5397
- this._resolvedGrammars.delete(e.name);
5398
- // Reset cache
5399
- this._loadedLanguagesCache = null;
5400
- // @ts-expect-error clear cache
5401
- this._syncRegistry?._injectionGrammars?.delete(e.scopeName);
5402
- // @ts-expect-error clear cache
5403
- this._syncRegistry?._grammars?.delete(e.scopeName);
5404
- await this.loadLanguage(this._langMap.get(e.name));
5160
+ utf16Length;
5161
+ utf8Length;
5162
+ utf16Value;
5163
+ utf8Value;
5164
+ utf16OffsetToUtf8;
5165
+ utf8OffsetToUtf16;
5166
+ constructor(str) {
5167
+ const utf16Length = str.length;
5168
+ const utf8Length = UtfString._utf8ByteLength(str);
5169
+ const computeIndicesMapping = (utf8Length !== utf16Length);
5170
+ const utf16OffsetToUtf8 = computeIndicesMapping ? new Uint32Array(utf16Length + 1) : null;
5171
+ if (computeIndicesMapping)
5172
+ utf16OffsetToUtf8[utf16Length] = utf8Length;
5173
+ const utf8OffsetToUtf16 = computeIndicesMapping ? new Uint32Array(utf8Length + 1) : null;
5174
+ if (computeIndicesMapping)
5175
+ utf8OffsetToUtf16[utf8Length] = utf16Length;
5176
+ const utf8Value = new Uint8Array(utf8Length);
5177
+ let i8 = 0;
5178
+ for (let i16 = 0; i16 < utf16Length; i16++) {
5179
+ const charCode = str.charCodeAt(i16);
5180
+ let codePoint = charCode;
5181
+ let wasSurrogatePair = false;
5182
+ if (charCode >= 0xD800 && charCode <= 0xDBFF) {
5183
+ // Hit a high surrogate, try to look for a matching low surrogate
5184
+ if (i16 + 1 < utf16Length) {
5185
+ const nextCharCode = str.charCodeAt(i16 + 1);
5186
+ if (nextCharCode >= 0xDC00 && nextCharCode <= 0xDFFF) {
5187
+ // Found the matching low surrogate
5188
+ codePoint = (((charCode - 0xD800) << 10) + 0x10000) | (nextCharCode - 0xDC00);
5189
+ wasSurrogatePair = true;
5190
+ }
5191
+ }
5192
+ }
5193
+ if (computeIndicesMapping) {
5194
+ utf16OffsetToUtf8[i16] = i8;
5195
+ if (wasSurrogatePair)
5196
+ utf16OffsetToUtf8[i16 + 1] = i8;
5197
+ if (codePoint <= 0x7F) {
5198
+ utf8OffsetToUtf16[i8 + 0] = i16;
5199
+ }
5200
+ else if (codePoint <= 0x7FF) {
5201
+ utf8OffsetToUtf16[i8 + 0] = i16;
5202
+ utf8OffsetToUtf16[i8 + 1] = i16;
5203
+ }
5204
+ else if (codePoint <= 0xFFFF) {
5205
+ utf8OffsetToUtf16[i8 + 0] = i16;
5206
+ utf8OffsetToUtf16[i8 + 1] = i16;
5207
+ utf8OffsetToUtf16[i8 + 2] = i16;
5208
+ }
5209
+ else {
5210
+ utf8OffsetToUtf16[i8 + 0] = i16;
5211
+ utf8OffsetToUtf16[i8 + 1] = i16;
5212
+ utf8OffsetToUtf16[i8 + 2] = i16;
5213
+ utf8OffsetToUtf16[i8 + 3] = i16;
5214
+ }
5215
+ }
5216
+ if (codePoint <= 0x7F) {
5217
+ utf8Value[i8++] = codePoint;
5218
+ }
5219
+ else if (codePoint <= 0x7FF) {
5220
+ utf8Value[i8++] = 0b11000000 | ((codePoint & 0b00000000000000000000011111000000) >>> 6);
5221
+ utf8Value[i8++] = 0b10000000 | ((codePoint & 0b00000000000000000000000000111111) >>> 0);
5222
+ }
5223
+ else if (codePoint <= 0xFFFF) {
5224
+ utf8Value[i8++] = 0b11100000 | ((codePoint & 0b00000000000000001111000000000000) >>> 12);
5225
+ utf8Value[i8++] = 0b10000000 | ((codePoint & 0b00000000000000000000111111000000) >>> 6);
5226
+ utf8Value[i8++] = 0b10000000 | ((codePoint & 0b00000000000000000000000000111111) >>> 0);
5227
+ }
5228
+ else {
5229
+ utf8Value[i8++] = 0b11110000 | ((codePoint & 0b00000000000111000000000000000000) >>> 18);
5230
+ utf8Value[i8++] = 0b10000000 | ((codePoint & 0b00000000000000111111000000000000) >>> 12);
5231
+ utf8Value[i8++] = 0b10000000 | ((codePoint & 0b00000000000000000000111111000000) >>> 6);
5232
+ utf8Value[i8++] = 0b10000000 | ((codePoint & 0b00000000000000000000000000111111) >>> 0);
5405
5233
  }
5234
+ if (wasSurrogatePair)
5235
+ i16++;
5406
5236
  }
5237
+ this.utf16Length = utf16Length;
5238
+ this.utf8Length = utf8Length;
5239
+ this.utf16Value = str;
5240
+ this.utf8Value = utf8Value;
5241
+ this.utf16OffsetToUtf8 = utf16OffsetToUtf8;
5242
+ this.utf8OffsetToUtf16 = utf8OffsetToUtf16;
5407
5243
  }
5408
- async init() {
5409
- this._themes.map(t => this.loadTheme(t));
5410
- await this.loadLanguages(this._langs);
5411
- }
5412
- dispose() {
5413
- super.dispose();
5414
- this._resolvedThemes.clear();
5415
- this._resolvedGrammars.clear();
5416
- this._langMap.clear();
5417
- this._langGraph.clear();
5418
- this._loadedThemesCache = null;
5244
+ createString(onigBinding) {
5245
+ const result = onigBinding.omalloc(this.utf8Length);
5246
+ onigBinding.HEAPU8.set(this.utf8Value, result);
5247
+ return result;
5419
5248
  }
5420
- async loadLanguages(langs) {
5421
- for (const lang of langs)
5422
- this.resolveEmbeddedLanguages(lang);
5423
- const langsGraphArray = Array.from(this._langGraph.entries());
5424
- const missingLangs = langsGraphArray.filter(([_, lang]) => !lang);
5425
- if (missingLangs.length) {
5426
- const dependents = langsGraphArray
5427
- .filter(([_, lang]) => lang && lang.embeddedLangs?.some(l => missingLangs.map(([name]) => name).includes(l)))
5428
- .filter(lang => !missingLangs.includes(lang));
5429
- throw new ShikiError(`Missing languages ${missingLangs.map(([name]) => `\`${name}\``).join(', ')}, required by ${dependents.map(([name]) => `\`${name}\``).join(', ')}`);
5249
+ }
5250
+ class OnigString {
5251
+ static LAST_ID = 0;
5252
+ static _sharedPtr = 0; // a pointer to a string of 10000 bytes
5253
+ static _sharedPtrInUse = false;
5254
+ id = (++OnigString.LAST_ID);
5255
+ _onigBinding;
5256
+ content;
5257
+ utf16Length;
5258
+ utf8Length;
5259
+ utf16OffsetToUtf8;
5260
+ utf8OffsetToUtf16;
5261
+ ptr;
5262
+ constructor(str) {
5263
+ if (!onigBinding)
5264
+ throw new ShikiError('Must invoke loadWasm first.');
5265
+ this._onigBinding = onigBinding;
5266
+ this.content = str;
5267
+ const utfString = new UtfString(str);
5268
+ this.utf16Length = utfString.utf16Length;
5269
+ this.utf8Length = utfString.utf8Length;
5270
+ this.utf16OffsetToUtf8 = utfString.utf16OffsetToUtf8;
5271
+ this.utf8OffsetToUtf16 = utfString.utf8OffsetToUtf16;
5272
+ if (this.utf8Length < 10000 && !OnigString._sharedPtrInUse) {
5273
+ if (!OnigString._sharedPtr)
5274
+ OnigString._sharedPtr = onigBinding.omalloc(10000);
5275
+ OnigString._sharedPtrInUse = true;
5276
+ onigBinding.HEAPU8.set(utfString.utf8Value, OnigString._sharedPtr);
5277
+ this.ptr = OnigString._sharedPtr;
5430
5278
  }
5431
- for (const [_, lang] of langsGraphArray)
5432
- this._resolver.addLanguage(lang);
5433
- for (const [_, lang] of langsGraphArray)
5434
- await this.loadLanguage(lang);
5435
- }
5436
- getLoadedLanguages() {
5437
- if (!this._loadedLanguagesCache) {
5438
- this._loadedLanguagesCache = [
5439
- ...new Set([...this._resolvedGrammars.keys(), ...Object.keys(this._alias)]),
5440
- ];
5279
+ else {
5280
+ this.ptr = utfString.createString(onigBinding);
5441
5281
  }
5442
- return this._loadedLanguagesCache;
5443
5282
  }
5444
- resolveEmbeddedLanguages(lang) {
5445
- this._langMap.set(lang.name, lang);
5446
- this._langGraph.set(lang.name, lang);
5447
- if (lang.embeddedLangs) {
5448
- for (const embeddedLang of lang.embeddedLangs)
5449
- this._langGraph.set(embeddedLang, this._langMap.get(embeddedLang));
5283
+ convertUtf8OffsetToUtf16(utf8Offset) {
5284
+ if (this.utf8OffsetToUtf16) {
5285
+ if (utf8Offset < 0)
5286
+ return 0;
5287
+ if (utf8Offset > this.utf8Length)
5288
+ return this.utf16Length;
5289
+ return this.utf8OffsetToUtf16[utf8Offset];
5450
5290
  }
5291
+ return utf8Offset;
5451
5292
  }
5452
- }
5453
-
5454
- class Resolver {
5455
- _langs = new Map();
5456
- _scopeToLang = new Map();
5457
- _injections = new Map();
5458
- _onigLibPromise;
5459
- constructor(onigLibPromise, langs) {
5460
- this._onigLibPromise = onigLibPromise;
5461
- langs.forEach(i => this.addLanguage(i));
5293
+ convertUtf16OffsetToUtf8(utf16Offset) {
5294
+ if (this.utf16OffsetToUtf8) {
5295
+ if (utf16Offset < 0)
5296
+ return 0;
5297
+ if (utf16Offset > this.utf16Length)
5298
+ return this.utf8Length;
5299
+ return this.utf16OffsetToUtf8[utf16Offset];
5300
+ }
5301
+ return utf16Offset;
5462
5302
  }
5463
- get onigLib() {
5464
- return this._onigLibPromise;
5303
+ dispose() {
5304
+ if (this.ptr === OnigString._sharedPtr)
5305
+ OnigString._sharedPtrInUse = false;
5306
+ else
5307
+ this._onigBinding.ofree(this.ptr);
5465
5308
  }
5466
- getLangRegistration(langIdOrAlias) {
5467
- return this._langs.get(langIdOrAlias);
5309
+ }
5310
+ class OnigScanner {
5311
+ _onigBinding;
5312
+ _ptr;
5313
+ constructor(patterns) {
5314
+ if (!onigBinding)
5315
+ throw new ShikiError('Must invoke loadWasm first.');
5316
+ const strPtrsArr = [];
5317
+ const strLenArr = [];
5318
+ for (let i = 0, len = patterns.length; i < len; i++) {
5319
+ const utfString = new UtfString(patterns[i]);
5320
+ strPtrsArr[i] = utfString.createString(onigBinding);
5321
+ strLenArr[i] = utfString.utf8Length;
5322
+ }
5323
+ const strPtrsPtr = onigBinding.omalloc(4 * patterns.length);
5324
+ onigBinding.HEAPU32.set(strPtrsArr, strPtrsPtr / 4);
5325
+ const strLenPtr = onigBinding.omalloc(4 * patterns.length);
5326
+ onigBinding.HEAPU32.set(strLenArr, strLenPtr / 4);
5327
+ const scannerPtr = onigBinding.createOnigScanner(strPtrsPtr, strLenPtr, patterns.length);
5328
+ for (let i = 0, len = patterns.length; i < len; i++)
5329
+ onigBinding.ofree(strPtrsArr[i]);
5330
+ onigBinding.ofree(strLenPtr);
5331
+ onigBinding.ofree(strPtrsPtr);
5332
+ if (scannerPtr === 0)
5333
+ throwLastOnigError(onigBinding);
5334
+ this._onigBinding = onigBinding;
5335
+ this._ptr = scannerPtr;
5468
5336
  }
5469
- async loadGrammar(scopeName) {
5470
- return this._scopeToLang.get(scopeName);
5337
+ dispose() {
5338
+ this._onigBinding.freeOnigScanner(this._ptr);
5471
5339
  }
5472
- addLanguage(l) {
5473
- this._langs.set(l.name, l);
5474
- if (l.aliases) {
5475
- l.aliases.forEach((a) => {
5476
- this._langs.set(a, l);
5477
- });
5340
+ findNextMatchSync(string, startPosition, arg) {
5341
+ // let debugCall = defaultDebugCall
5342
+ let options = 0 /* FindOption.None */;
5343
+ if (typeof arg === 'number') {
5344
+ // if (arg & FindOption.DebugCall)
5345
+ // debugCall = true
5346
+ options = arg;
5478
5347
  }
5479
- this._scopeToLang.set(l.scopeName, l);
5480
- if (l.injectTo) {
5481
- l.injectTo.forEach((i) => {
5482
- if (!this._injections.get(i))
5483
- this._injections.set(i, []);
5484
- this._injections.get(i).push(l.scopeName);
5485
- });
5348
+ if (typeof string === 'string') {
5349
+ string = new OnigString(string);
5350
+ const result = this._findNextMatchSync(string, startPosition, false, options);
5351
+ string.dispose();
5352
+ return result;
5486
5353
  }
5354
+ return this._findNextMatchSync(string, startPosition, false, options);
5487
5355
  }
5488
- getInjections(scopeName) {
5489
- const scopeParts = scopeName.split('.');
5490
- let injections = [];
5491
- for (let i = 1; i <= scopeParts.length; i++) {
5492
- const subScopeName = scopeParts.slice(0, i).join('.');
5493
- injections = [...injections, ...(this._injections.get(subScopeName) || [])];
5356
+ _findNextMatchSync(string, startPosition, debugCall, options) {
5357
+ const onigBinding = this._onigBinding;
5358
+ // let resultPtr: Pointer
5359
+ // if (debugCall)
5360
+ // resultPtr = onigBinding.findNextOnigScannerMatchDbg(this._ptr, string.id, string.ptr, string.utf8Length, string.convertUtf16OffsetToUtf8(startPosition), options)
5361
+ // else
5362
+ const resultPtr = onigBinding.findNextOnigScannerMatch(this._ptr, string.id, string.ptr, string.utf8Length, string.convertUtf16OffsetToUtf8(startPosition), options);
5363
+ if (resultPtr === 0) {
5364
+ // no match
5365
+ return null;
5494
5366
  }
5495
- return injections;
5367
+ const HEAPU32 = onigBinding.HEAPU32;
5368
+ let offset = resultPtr / 4; // byte offset -> uint32 offset
5369
+ const index = HEAPU32[offset++];
5370
+ const count = HEAPU32[offset++];
5371
+ const captureIndices = [];
5372
+ for (let i = 0; i < count; i++) {
5373
+ const beg = string.convertUtf8OffsetToUtf16(HEAPU32[offset++]);
5374
+ const end = string.convertUtf8OffsetToUtf16(HEAPU32[offset++]);
5375
+ captureIndices[i] = {
5376
+ start: beg,
5377
+ end,
5378
+ length: end - beg,
5379
+ };
5380
+ }
5381
+ return {
5382
+ index,
5383
+ captureIndices,
5384
+ };
5385
+ }
5386
+ }
5387
+ function isInstantiatorOptionsObject(dataOrOptions) {
5388
+ return (typeof dataOrOptions.instantiator === 'function');
5389
+ }
5390
+ function isInstantiatorModule(dataOrOptions) {
5391
+ return (typeof dataOrOptions.default === 'function');
5392
+ }
5393
+ function isDataOptionsObject(dataOrOptions) {
5394
+ return (typeof dataOrOptions.data !== 'undefined');
5395
+ }
5396
+ function isResponse(dataOrOptions) {
5397
+ return (typeof Response !== 'undefined' && dataOrOptions instanceof Response);
5398
+ }
5399
+ function isArrayBuffer(data) {
5400
+ return (typeof ArrayBuffer !== 'undefined' && (data instanceof ArrayBuffer || ArrayBuffer.isView(data)))
5401
+ // eslint-disable-next-line node/prefer-global/buffer
5402
+ || (typeof Buffer !== 'undefined' && Buffer.isBuffer?.(data))
5403
+ || (typeof SharedArrayBuffer !== 'undefined' && data instanceof SharedArrayBuffer)
5404
+ || (typeof Uint32Array !== 'undefined' && data instanceof Uint32Array);
5405
+ }
5406
+ let initPromise;
5407
+ function loadWasm(options) {
5408
+ if (initPromise)
5409
+ return initPromise;
5410
+ async function _load() {
5411
+ onigBinding = await main(async (info) => {
5412
+ let instance = options;
5413
+ instance = await instance;
5414
+ if (typeof instance === 'function')
5415
+ instance = await instance(info);
5416
+ if (typeof instance === 'function')
5417
+ instance = await instance(info);
5418
+ if (isInstantiatorOptionsObject(instance)) {
5419
+ instance = await instance.instantiator(info);
5420
+ }
5421
+ else if (isInstantiatorModule(instance)) {
5422
+ instance = await instance.default(info);
5423
+ }
5424
+ else {
5425
+ if (isDataOptionsObject(instance))
5426
+ instance = instance.data;
5427
+ if (isResponse(instance)) {
5428
+ if (typeof WebAssembly.instantiateStreaming === 'function')
5429
+ instance = await _makeResponseStreamingLoader(instance)(info);
5430
+ else
5431
+ instance = await _makeResponseNonStreamingLoader(instance)(info);
5432
+ }
5433
+ else if (isArrayBuffer(instance)) {
5434
+ instance = await _makeArrayBufferLoader(instance)(info);
5435
+ }
5436
+ // import("shiki/onig.wasm") returns `{ default: WebAssembly.Module }` on cloudflare workers
5437
+ // https://developers.cloudflare.com/workers/wrangler/bundling/
5438
+ else if (instance instanceof WebAssembly.Module) {
5439
+ instance = await _makeArrayBufferLoader(instance)(info);
5440
+ }
5441
+ else if ('default' in instance && instance.default instanceof WebAssembly.Module) {
5442
+ instance = await _makeArrayBufferLoader(instance.default)(info);
5443
+ }
5444
+ }
5445
+ if ('instance' in instance)
5446
+ instance = instance.instance;
5447
+ if ('exports' in instance)
5448
+ instance = instance.exports;
5449
+ return instance;
5450
+ });
5496
5451
  }
5452
+ initPromise = _load();
5453
+ return initPromise;
5454
+ }
5455
+ function _makeArrayBufferLoader(data) {
5456
+ return importObject => WebAssembly.instantiate(data, importObject);
5457
+ }
5458
+ function _makeResponseStreamingLoader(data) {
5459
+ return importObject => WebAssembly.instantiateStreaming(data, importObject);
5460
+ }
5461
+ function _makeResponseNonStreamingLoader(data) {
5462
+ return async (importObject) => {
5463
+ const arrayBuffer = await data.arrayBuffer();
5464
+ return WebAssembly.instantiate(arrayBuffer, importObject);
5465
+ };
5466
+ }
5467
+ // export function createOnigString(str: string) {
5468
+ // return new OnigString(str)
5469
+ // }
5470
+ // export function createOnigScanner(patterns: string[]) {
5471
+ // return new OnigScanner(patterns)
5472
+ // }
5473
+ // export function setDefaultDebugCall(_defaultDebugCall: boolean): void {
5474
+ // defaultDebugCall = _defaultDebugCall
5475
+ // }
5476
+
5477
+ async function createWasmOnigEngine(options) {
5478
+ if (options)
5479
+ await loadWasm(options);
5480
+ return {
5481
+ createScanner(patterns) {
5482
+ return new OnigScanner(patterns);
5483
+ },
5484
+ createString(s) {
5485
+ return new OnigString(s);
5486
+ },
5487
+ };
5497
5488
  }
5498
5489
 
5499
5490
  let _defaultWasmLoader;
@@ -5521,20 +5512,11 @@ async function createShikiInternal(options = {}) {
5521
5512
  .filter(l => !isSpecialLang(l))
5522
5513
  .map(async (lang) => await normalizeGetter(lang).then(r => Array.isArray(r) ? r : [r])))).flat()));
5523
5514
  }
5524
- const wasmLoader = options.loadWasm || _defaultWasmLoader;
5525
5515
  const [themes, langs,] = await Promise.all([
5526
5516
  Promise.all((options.themes || []).map(normalizeGetter)).then(r => r.map(normalizeTheme)),
5527
5517
  resolveLangs(options.langs || []),
5528
- wasmLoader ? loadWasm(wasmLoader) : undefined,
5529
5518
  ]);
5530
- const resolver = new Resolver(Promise.resolve({
5531
- createOnigScanner(patterns) {
5532
- return createOnigScanner(patterns);
5533
- },
5534
- createOnigString(s) {
5535
- return createOnigString(s);
5536
- },
5537
- }), langs);
5519
+ const resolver = new Resolver(Promise.resolve(options.engine || createWasmOnigEngine(options.loadWasm || _defaultWasmLoader)), langs);
5538
5520
  const _registry = new Registry(resolver, themes, langs, options.langAlias);
5539
5521
  await _registry.init();
5540
5522
  let _lastTheme;
@@ -5792,4 +5774,421 @@ function createSingletonShorthands(createHighlighter) {
5792
5774
  };
5793
5775
  }
5794
5776
 
5795
- export { FontStyle, ShikiError, addClassToHast, applyColorReplacements, codeToHast, codeToHtml, codeToTokens, codeToTokensBase, codeToTokensWithThemes, createHighlighterCore, createPositionConverter, createShikiInternal, createSingletonShorthands, createdBundledHighlighter, getHighlighterCore, getShikiInternal, getSingletonHighlighterCore, getTokenStyleObject, toHtml as hastToHtml, isNoneTheme, isPlainLang, isSpecialLang, isSpecialTheme, loadWasm, makeSingletonHighlighter, makeSingletonHighlighterCore, normalizeTheme, resolveColorReplacements, setDefaultWasmLoader, splitLines, splitToken, splitTokens, stringifyTokenStyle, toArray, tokenizeAnsiWithTheme, tokenizeWithTheme, tokensToHast, transformerDecorations };
5777
+ var __defProp = Object.defineProperty;
5778
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
5779
+ var __publicField = (obj, key, value) => {
5780
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5781
+ return value;
5782
+ };
5783
+ class RegExpConversionError extends SyntaxError {
5784
+ constructor(message, options) {
5785
+ super(message, { cause: options.cause });
5786
+ __publicField(this, "patternOriginal");
5787
+ __publicField(this, "patternConverted");
5788
+ __publicField(this, "cursorPosition");
5789
+ this.patternOriginal = options.pattern;
5790
+ this.patternConverted = options.converted;
5791
+ this.cursorPosition = options.cursor;
5792
+ }
5793
+ }
5794
+
5795
+ const TABLE_POSIX = {
5796
+ alnum: "0-9A-Za-z",
5797
+ alpha: "A-Za-z",
5798
+ ascii: "\0-\x7F",
5799
+ blank: " \\t",
5800
+ cntrl: "\0-\x7F",
5801
+ digit: "\\d",
5802
+ graph: "!-~",
5803
+ lower: "a-z",
5804
+ print: " -~",
5805
+ punct: "!-/:-@[-`{-~",
5806
+ space: "\\s",
5807
+ upper: "A-Z",
5808
+ xdigit: "0-9A-Fa-f",
5809
+ word: "\\w"
5810
+ };
5811
+ const KNOWN_FLAGS = /* @__PURE__ */ new Set("gimsuyx");
5812
+ function syntaxLowering(input, options = {}) {
5813
+ const {
5814
+ preserveFlags = false,
5815
+ removePossessiveQuantifier = false,
5816
+ removeAtomicGroup = false
5817
+ } = options;
5818
+ let output = "";
5819
+ const flags = /* @__PURE__ */ new Set();
5820
+ const stack = [];
5821
+ const freeSpacingLocal = [];
5822
+ let freeSpacingGlobal = false;
5823
+ let i = 0;
5824
+ try {
5825
+ while (i < input.length) {
5826
+ const char = input[i];
5827
+ while (freeSpacingLocal.length && freeSpacingLocal[0] > stack.length) {
5828
+ freeSpacingLocal.shift();
5829
+ }
5830
+ const head = stack[0];
5831
+ const wsEscape = freeSpacingGlobal || freeSpacingLocal.length;
5832
+ if (char === "\\") {
5833
+ if (input[i + 1] === "h") {
5834
+ if (head === "[") {
5835
+ output += " \\t";
5836
+ } else {
5837
+ output += "[ \\t]";
5838
+ }
5839
+ i += 2;
5840
+ continue;
5841
+ }
5842
+ output += char + input[i + 1];
5843
+ i += 2;
5844
+ continue;
5845
+ }
5846
+ if (char === "#" && wsEscape && input[i - 1].match(/\s/) && head !== "[") {
5847
+ for (let j = i + 1; j <= input.length; j++) {
5848
+ if (input[j] === "\n" || j === input.length) {
5849
+ i = j;
5850
+ break;
5851
+ }
5852
+ }
5853
+ continue;
5854
+ }
5855
+ if (char === "(" && head !== "[") {
5856
+ if (input[i + 1] === "?") {
5857
+ if (input[i + 2] === "#") {
5858
+ for (let j = i + 3; j < input.length; j++) {
5859
+ if (input[j] === ")" && input[j - 1] !== "\\") {
5860
+ i = j + 1;
5861
+ break;
5862
+ }
5863
+ }
5864
+ continue;
5865
+ }
5866
+ if (removeAtomicGroup && input[i + 2] === ">") {
5867
+ output += "(?:";
5868
+ i += 3;
5869
+ continue;
5870
+ }
5871
+ if (KNOWN_FLAGS.has(input[i + 2])) {
5872
+ let end = i + 3;
5873
+ for (; end < input.length; end++) {
5874
+ if (!KNOWN_FLAGS.has(input[end]))
5875
+ break;
5876
+ }
5877
+ const flagStr = input.slice(i + 2, end);
5878
+ const hasX = flagStr.includes("x") && flagStr[0] !== "-";
5879
+ let remainFlags = [...flagStr].filter((x) => x !== "x").join("");
5880
+ if (!preserveFlags) {
5881
+ if (remainFlags[0] === "-") {
5882
+ remainFlags = remainFlags.slice(1);
5883
+ for (const flag of flagStr) {
5884
+ flags.delete(flag);
5885
+ }
5886
+ } else {
5887
+ for (const flag of remainFlags) {
5888
+ flags.add(flag);
5889
+ }
5890
+ }
5891
+ remainFlags = "";
5892
+ }
5893
+ if (input[end] === ")") {
5894
+ i = end + 1;
5895
+ if (hasX) {
5896
+ freeSpacingGlobal = true;
5897
+ }
5898
+ if (remainFlags.length) {
5899
+ output += `(?${remainFlags})`;
5900
+ }
5901
+ continue;
5902
+ } else if (input[end] === ":") {
5903
+ i = end + 1;
5904
+ stack.unshift(char);
5905
+ if (hasX) {
5906
+ freeSpacingLocal.unshift(stack.length);
5907
+ }
5908
+ output += `(?${remainFlags}:`;
5909
+ continue;
5910
+ }
5911
+ }
5912
+ stack.unshift(char);
5913
+ output += char + input[i + 1] + input[i + 2];
5914
+ i += 3;
5915
+ } else {
5916
+ stack.unshift(char);
5917
+ output += char;
5918
+ i += 1;
5919
+ }
5920
+ continue;
5921
+ }
5922
+ if (char === ")" && head !== "[") {
5923
+ if (head === "(")
5924
+ stack.shift();
5925
+ output += char;
5926
+ i += 1;
5927
+ continue;
5928
+ }
5929
+ if (char === "[") {
5930
+ if (input[i + 1] === ":") {
5931
+ let name = "";
5932
+ let negated = false;
5933
+ if (input[i + 2] === "^") {
5934
+ negated = true;
5935
+ i += 1;
5936
+ }
5937
+ for (let j = i + 2; j < input.length; j++) {
5938
+ if (input[j] === ":") {
5939
+ i = j + 2;
5940
+ break;
5941
+ }
5942
+ if (!input[j].match(/[a-z]/i)) {
5943
+ name = "";
5944
+ break;
5945
+ }
5946
+ name += input[j];
5947
+ }
5948
+ if (name) {
5949
+ let resolved = TABLE_POSIX[name];
5950
+ if (!resolved) {
5951
+ throw new RegExpConversionError(
5952
+ `Unknown posix class "${name}"`,
5953
+ { pattern: input, converted: output, cursor: i }
5954
+ );
5955
+ }
5956
+ if (negated)
5957
+ resolved = `^${resolved}`;
5958
+ if (head === "[")
5959
+ output += resolved;
5960
+ else
5961
+ output += `[${resolved}]`;
5962
+ continue;
5963
+ }
5964
+ }
5965
+ if (head !== "[") {
5966
+ stack.unshift(char);
5967
+ }
5968
+ output += char;
5969
+ i += 1;
5970
+ continue;
5971
+ }
5972
+ if (char === "]") {
5973
+ if (head === "[")
5974
+ stack.shift();
5975
+ output += char;
5976
+ i += 1;
5977
+ continue;
5978
+ }
5979
+ if (removePossessiveQuantifier && char === "+" && head !== "[") {
5980
+ if ("?+}*".includes(input[i - 1])) {
5981
+ i += 1;
5982
+ continue;
5983
+ }
5984
+ }
5985
+ if (!(wsEscape && head !== "[" && char.match(/\s/))) {
5986
+ output += char;
5987
+ }
5988
+ i += 1;
5989
+ }
5990
+ } catch (e) {
5991
+ if (e instanceof RegExpConversionError)
5992
+ throw e;
5993
+ throw new RegExpConversionError(
5994
+ `Error lowering regex at position ${i}`,
5995
+ { pattern: input, converted: output, cursor: i, cause: e }
5996
+ );
5997
+ }
5998
+ return {
5999
+ pattern: output,
6000
+ flags: [...flags].join("")
6001
+ };
6002
+ }
6003
+
6004
+ function construct(pattern, options = {}) {
6005
+ const {
6006
+ original = pattern,
6007
+ flags = ["g"]
6008
+ } = options;
6009
+ if (pattern.startsWith("(?x)") || pattern.includes("(?x:")) {
6010
+ throw new RegExpConversionError(
6011
+ "RegExp Free-spacing mode `(?x)` is not supported, you might want to run syntaxLowering() first",
6012
+ { pattern: original, converted: pattern }
6013
+ );
6014
+ }
6015
+ if (pattern.match(/\[:\w+:\]/)) {
6016
+ throw new RegExpConversionError(
6017
+ "POSIX character classes are not supported, you might want to run syntaxLowering() first",
6018
+ { pattern: original, converted: pattern }
6019
+ );
6020
+ }
6021
+ const flagSet = new Set(Array.isArray(flags) ? flags : flags.split(""));
6022
+ pattern = pattern.replace(/\\A/g, "^").replace(/\\x\{([^}]*)\}/g, (m, hex) => `\\u${hex.padStart(4, "0")}`).replace(/\(\?(-)?(\w+):/g, (_, neg, flagStr) => {
6023
+ if (neg) {
6024
+ for (const flag of flagStr)
6025
+ flagSet.delete(flag);
6026
+ } else {
6027
+ for (const flag of flagStr)
6028
+ flagSet.add(flag);
6029
+ }
6030
+ return "(?:";
6031
+ }).replace(/\(\?(-)?(\w+)\)/g, (_, neg, flags2) => {
6032
+ if (neg) {
6033
+ for (const flag of flags2)
6034
+ flagSet.delete(flag);
6035
+ } else {
6036
+ for (const flag of flags2)
6037
+ flagSet.add(flag);
6038
+ }
6039
+ return "";
6040
+ });
6041
+ if (flagSet.has("x")) {
6042
+ throw new RegExpConversionError(
6043
+ "JavaScript does not support extended mode (x flag), you might want to run syntaxLowering() first",
6044
+ { pattern: original, converted: pattern }
6045
+ );
6046
+ }
6047
+ try {
6048
+ return new RegExp(pattern, [...flagSet].join(""));
6049
+ } catch (e) {
6050
+ throw new RegExpConversionError(
6051
+ `Failed to construct RegExp`,
6052
+ {
6053
+ pattern: original,
6054
+ converted: pattern,
6055
+ cause: e
6056
+ }
6057
+ );
6058
+ }
6059
+ }
6060
+
6061
+ function onigurumaToRegexp(pattern, options = {}) {
6062
+ const {
6063
+ pattern: converted,
6064
+ flags
6065
+ } = syntaxLowering(pattern, {
6066
+ removePossessiveQuantifier: true,
6067
+ removeAtomicGroup: true,
6068
+ ...options
6069
+ });
6070
+ return construct(converted, {
6071
+ original: pattern,
6072
+ flags,
6073
+ ...options
6074
+ });
6075
+ }
6076
+
6077
+ const MAX = 4294967295;
6078
+ class JavaScriptScanner {
6079
+ patterns;
6080
+ cache;
6081
+ forgiving;
6082
+ regexps;
6083
+ constructor(patterns, cache, forgiving) {
6084
+ this.patterns = patterns;
6085
+ this.cache = cache;
6086
+ this.forgiving = forgiving;
6087
+ this.regexps = patterns.map((p) => {
6088
+ const cached = cache?.get(p);
6089
+ if (cached) {
6090
+ if (cached instanceof RegExp) {
6091
+ return cached;
6092
+ }
6093
+ if (forgiving)
6094
+ return null;
6095
+ throw cached;
6096
+ }
6097
+ try {
6098
+ const regex = onigurumaToRegexp(p
6099
+ // YAML specific handling; TODO: move to tm-grammars
6100
+ .replaceAll('[^\\s[-?:,\\[\\]{}#&*!|>\'"%@`]]', '[^\\s\\-?:,\\[\\]{}#&*!|>\'"%@`]'), { flags: 'dg' });
6101
+ cache?.set(p, regex);
6102
+ return regex;
6103
+ }
6104
+ catch (e) {
6105
+ cache?.set(p, e);
6106
+ if (forgiving)
6107
+ return null;
6108
+ // console.error({ ...e })
6109
+ throw e;
6110
+ }
6111
+ });
6112
+ }
6113
+ findNextMatchSync(string, startPosition) {
6114
+ const str = typeof string === 'string'
6115
+ ? string
6116
+ : string.content;
6117
+ const pending = [];
6118
+ function toResult(index, match) {
6119
+ return {
6120
+ index,
6121
+ captureIndices: match.indices.map((indice) => {
6122
+ if (indice == null) {
6123
+ return {
6124
+ end: MAX,
6125
+ start: MAX,
6126
+ length: 0,
6127
+ };
6128
+ }
6129
+ return {
6130
+ start: indice[0],
6131
+ length: indice[1] - indice[0],
6132
+ end: indice[1],
6133
+ };
6134
+ }),
6135
+ };
6136
+ }
6137
+ for (let i = 0; i < this.regexps.length; i++) {
6138
+ const regexp = this.regexps[i];
6139
+ if (!regexp)
6140
+ continue;
6141
+ try {
6142
+ regexp.lastIndex = startPosition;
6143
+ const match = regexp.exec(str);
6144
+ if (!match)
6145
+ continue;
6146
+ // If the match is at the start position, return it immediately
6147
+ if (match.index === startPosition) {
6148
+ return toResult(i, match);
6149
+ }
6150
+ // Otherwise, store it for later
6151
+ pending.push([i, match]);
6152
+ }
6153
+ catch (e) {
6154
+ if (this.forgiving)
6155
+ continue;
6156
+ throw e;
6157
+ }
6158
+ }
6159
+ // Find the closest match to the start position
6160
+ if (pending.length) {
6161
+ const minIndex = Math.min(...pending.map(m => m[1].index));
6162
+ for (const [i, match] of pending) {
6163
+ if (match.index === minIndex) {
6164
+ return toResult(i, match);
6165
+ }
6166
+ }
6167
+ }
6168
+ return null;
6169
+ }
6170
+ }
6171
+ /**
6172
+ * Use the modern JavaScript RegExp engine to implement the OnigScanner.
6173
+ *
6174
+ * As Oniguruma regex is more powerful than JavaScript regex, some patterns may not be supported.
6175
+ * Errors will be thrown when parsing TextMate grammars with unsupported patterns.
6176
+ * Set `forgiving` to `true` to ignore these errors and skip the unsupported patterns.
6177
+ *
6178
+ * @experimental
6179
+ */
6180
+ function createJavaScriptRegexEngine(options = {}) {
6181
+ const { forgiving = false, cache = new Map(), } = options;
6182
+ return {
6183
+ createScanner(patterns) {
6184
+ return new JavaScriptScanner(patterns, cache, forgiving);
6185
+ },
6186
+ createString(s) {
6187
+ return {
6188
+ content: s,
6189
+ };
6190
+ },
6191
+ };
6192
+ }
6193
+
6194
+ export { FontStyle, ShikiError, addClassToHast, applyColorReplacements, codeToHast, codeToHtml, codeToTokens, codeToTokensBase, codeToTokensWithThemes, createHighlighterCore, createJavaScriptRegexEngine, createPositionConverter, createShikiInternal, createSingletonShorthands, createWasmOnigEngine, createdBundledHighlighter, getHighlighterCore, getShikiInternal, getSingletonHighlighterCore, getTokenStyleObject, toHtml as hastToHtml, isNoneTheme, isPlainLang, isSpecialLang, isSpecialTheme, loadWasm, makeSingletonHighlighter, makeSingletonHighlighterCore, normalizeTheme, resolveColorReplacements, setDefaultWasmLoader, splitLines, splitToken, splitTokens, stringifyTokenStyle, toArray, tokenizeAnsiWithTheme, tokenizeWithTheme, tokensToHast, transformerDecorations };