mini-shiki 3.17.0 → 3.18.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.
Files changed (2) hide show
  1. package/dist/shiki.js +42 -16
  2. package/package.json +5 -5
package/dist/shiki.js CHANGED
@@ -41,6 +41,7 @@ function isSpecialTheme(theme) {
41
41
  return isNoneTheme(theme);
42
42
  }
43
43
  function splitLines(code, preserveEnding = false) {
44
+ if (code.length === 0) return [["", 0]];
44
45
  const parts = code.split(/(\r?\n)/g);
45
46
  let index = 0;
46
47
  const lines = [];
@@ -417,25 +418,41 @@ function tokenizeAnsiWithTheme(theme, fileContents, options) {
417
418
  }));
418
419
  }
419
420
  function dimColor(color) {
420
- const hexMatch = color.match(/#([0-9a-f]{3})([0-9a-f]{3})?([0-9a-f]{2})?/i);
421
- if (hexMatch) if (hexMatch[3]) {
422
- const alpha = Math.round(Number.parseInt(hexMatch[3], 16) / 2).toString(16).padStart(2, "0");
423
- return `#${hexMatch[1]}${hexMatch[2]}${alpha}`;
424
- } else if (hexMatch[2]) return `#${hexMatch[1]}${hexMatch[2]}80`;
425
- else return `#${Array.from(hexMatch[1]).map((x) => `${x}${x}`).join("")}80`;
421
+ const hexMatch = color.match(/#([0-9a-f]{3,8})/i);
422
+ if (hexMatch) {
423
+ const hex = hexMatch[1];
424
+ if (hex.length === 8) {
425
+ const alpha = Math.round(Number.parseInt(hex.slice(6, 8), 16) / 2).toString(16).padStart(2, "0");
426
+ return `#${hex.slice(0, 6)}${alpha}`;
427
+ } else if (hex.length === 6) return `#${hex}80`;
428
+ else if (hex.length === 4) {
429
+ const r = hex[0];
430
+ const g = hex[1];
431
+ const b = hex[2];
432
+ const a = hex[3];
433
+ const alpha = Math.round(Number.parseInt(`${a}${a}`, 16) / 2).toString(16).padStart(2, "0");
434
+ return `#${r}${r}${g}${g}${b}${b}${alpha}`;
435
+ } else if (hex.length === 3) {
436
+ const r = hex[0];
437
+ const g = hex[1];
438
+ const b = hex[2];
439
+ return `#${r}${r}${g}${g}${b}${b}80`;
440
+ }
441
+ }
426
442
  const cssVarMatch = color.match(/var\((--[\w-]+-ansi-[\w-]+)\)/);
427
443
  if (cssVarMatch) return `var(${cssVarMatch[1]}-dim)`;
428
444
  return color;
429
445
  }
430
446
  function codeToTokensBase(internal, code, options = {}) {
431
- const { lang = "text", theme: themeName = internal.getLoadedThemes()[0] } = options;
447
+ const { theme: themeName = internal.getLoadedThemes()[0] } = options;
448
+ const lang = internal.resolveLangAlias(options.lang || "text");
432
449
  if (isPlainLang(lang) || isNoneTheme(themeName)) return splitLines(code).map((line) => [{
433
450
  content: line[0],
434
451
  offset: line[1]
435
452
  }]);
436
453
  const { theme, colorMap } = internal.setTheme(themeName);
437
454
  if (lang === "ansi") return tokenizeAnsiWithTheme(theme, code, options);
438
- const _grammar = internal.getLanguage(lang);
455
+ const _grammar = internal.getLanguage(options.lang || "text");
439
456
  if (options.grammarState) {
440
457
  if (options.grammarState.lang !== _grammar.name) throw new ShikiError(`Grammar state language "${options.grammarState.lang}" does not match highlight language "${_grammar.name}"`);
441
458
  if (!options.grammarState.themes.includes(theme.name)) throw new ShikiError(`Grammar state themes "${options.grammarState.themes}" do not contain highlight theme "${theme.name}"`);
@@ -746,6 +763,18 @@ var ShikiError$1 = class extends Error {
746
763
  this.name = "ShikiError";
747
764
  }
748
765
  };
766
+ function resolveLangAlias(name, alias) {
767
+ if (!alias) return name;
768
+ if (alias[name]) {
769
+ const resolved = /* @__PURE__ */ new Set([name]);
770
+ while (alias[name]) {
771
+ name = alias[name];
772
+ if (resolved.has(name)) throw new ShikiError$1(`Circular alias \`${Array.from(resolved).join(" -> ")} -> ${name}\``);
773
+ resolved.add(name);
774
+ }
775
+ }
776
+ return name;
777
+ }
749
778
  var Registry$1 = class extends Registry {
750
779
  constructor(_resolver, _themes, _langs, _alias = {}) {
751
780
  super(_resolver);
@@ -788,14 +817,7 @@ var Registry$1 = class extends Registry {
788
817
  this._syncRegistry.setTheme(textmateTheme);
789
818
  }
790
819
  getGrammar(name) {
791
- if (this._alias[name]) {
792
- const resolved = /* @__PURE__ */ new Set([name]);
793
- while (this._alias[name]) {
794
- name = this._alias[name];
795
- if (resolved.has(name)) throw new ShikiError$1(`Circular alias \`${Array.from(resolved).join(" -> ")} -> ${name}\``);
796
- resolved.add(name);
797
- }
798
- }
820
+ name = resolveLangAlias(name, this._alias);
799
821
  return this._resolvedGrammars.get(name);
800
822
  }
801
823
  loadLanguage(lang) {
@@ -905,6 +927,9 @@ function createShikiInternalSync(options) {
905
927
  const resolver = new Resolver(options.engine, langs);
906
928
  const _registry = new Registry$1(resolver, themes, langs, options.langAlias);
907
929
  let _lastTheme;
930
+ function resolveLangAlias$1(name) {
931
+ return resolveLangAlias(name, options.langAlias);
932
+ }
908
933
  function getLanguage(name) {
909
934
  ensureNotDisposed();
910
935
  const _lang = _registry.getGrammar(typeof name === "string" ? name : name.name);
@@ -975,6 +1000,7 @@ function createShikiInternalSync(options) {
975
1000
  getLanguage,
976
1001
  getLoadedThemes,
977
1002
  getLoadedLanguages,
1003
+ resolveLangAlias: resolveLangAlias$1,
978
1004
  loadLanguage,
979
1005
  loadLanguageSync,
980
1006
  loadTheme,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mini-shiki",
3
- "version": "3.17.0",
3
+ "version": "3.18.0",
4
4
  "type": "module",
5
5
  "repository": "git+https://github.com/un-ts/mini-shiki.git",
6
6
  "homepage": "https://github.com/un-ts/mini-shiki#readme",
@@ -21,10 +21,10 @@
21
21
  "static"
22
22
  ],
23
23
  "dependencies": {
24
- "@shikijs/engine-oniguruma": "^3.17.0",
25
- "@shikijs/langs": "^3.17.0",
26
- "@shikijs/themes": "^3.17.0",
27
- "@shikijs/types": "^3.17.0",
24
+ "@shikijs/engine-oniguruma": "^3.18.0",
25
+ "@shikijs/langs": "^3.18.0",
26
+ "@shikijs/themes": "^3.18.0",
27
+ "@shikijs/types": "^3.18.0",
28
28
  "@shikijs/vscode-textmate": "^10.0.2"
29
29
  }
30
30
  }