@shikijs/core 3.17.1 → 3.19.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/index.mjs +49 -29
  2. package/package.json +9 -2
package/dist/index.mjs CHANGED
@@ -889,15 +889,15 @@ function dimColor(color) {
889
889
 
890
890
  function codeToTokensBase(internal, code, options = {}) {
891
891
  const {
892
- lang = "text",
893
892
  theme: themeName = internal.getLoadedThemes()[0]
894
893
  } = options;
894
+ const lang = internal.resolveLangAlias(options.lang || "text");
895
895
  if (isPlainLang(lang) || isNoneTheme(themeName))
896
896
  return splitLines(code).map((line) => [{ content: line[0], offset: line[1] }]);
897
897
  const { theme, colorMap } = internal.setTheme(themeName);
898
898
  if (lang === "ansi")
899
899
  return tokenizeAnsiWithTheme(theme, code, options);
900
- const _grammar = internal.getLanguage(lang);
900
+ const _grammar = internal.getLanguage(options.lang || "text");
901
901
  if (options.grammarState) {
902
902
  if (options.grammarState.lang !== _grammar.name) {
903
903
  throw new ShikiError$1(`Grammar state language "${options.grammarState.lang}" does not match highlight language "${_grammar.name}"`);
@@ -1306,7 +1306,7 @@ function codeToHast(internal, code, options, transformerContext = {
1306
1306
  fg,
1307
1307
  bg,
1308
1308
  themeName,
1309
- rootStyle
1309
+ rootStyle: options.rootStyle === false ? false : options.rootStyle ?? rootStyle
1310
1310
  },
1311
1311
  contextSource,
1312
1312
  grammarState
@@ -1323,21 +1323,25 @@ function tokensToHast(tokens, options, transformerContext, grammarState = getLas
1323
1323
  structure = "classic",
1324
1324
  tabindex = "0"
1325
1325
  } = options;
1326
+ const properties = {
1327
+ class: `shiki ${options.themeName || ""}`
1328
+ };
1329
+ if (options.rootStyle !== false) {
1330
+ if (options.rootStyle != null)
1331
+ properties.style = options.rootStyle;
1332
+ else
1333
+ properties.style = `background-color:${options.bg};color:${options.fg}`;
1334
+ }
1335
+ if (tabindex !== false && tabindex != null)
1336
+ properties.tabindex = tabindex.toString();
1337
+ for (const [key, value] of Object.entries(options.meta || {})) {
1338
+ if (!key.startsWith("_"))
1339
+ properties[key] = value;
1340
+ }
1326
1341
  let preNode = {
1327
1342
  type: "element",
1328
1343
  tagName: "pre",
1329
- properties: {
1330
- class: `shiki ${options.themeName || ""}`,
1331
- style: options.rootStyle || `background-color:${options.bg};color:${options.fg}`,
1332
- ...tabindex !== false && tabindex != null ? {
1333
- tabindex: tabindex.toString()
1334
- } : {},
1335
- ...Object.fromEntries(
1336
- Array.from(
1337
- Object.entries(options.meta || {})
1338
- ).filter(([key]) => !key.startsWith("_"))
1339
- )
1340
- },
1344
+ properties,
1341
1345
  children: []
1342
1346
  };
1343
1347
  let codeNode = {
@@ -1474,12 +1478,12 @@ function mergeWhitespaceTokens(tokens) {
1474
1478
  return tokens.map((line) => {
1475
1479
  const newLine = [];
1476
1480
  let carryOnContent = "";
1477
- let firstOffset = 0;
1481
+ let firstOffset;
1478
1482
  line.forEach((token, idx) => {
1479
1483
  const isDecorated = token.fontStyle && (token.fontStyle & FontStyle.Underline || token.fontStyle & FontStyle.Strikethrough);
1480
1484
  const couldMerge = !isDecorated;
1481
1485
  if (couldMerge && token.content.match(/^\s+$/) && line[idx + 1]) {
1482
- if (!firstOffset)
1486
+ if (firstOffset === void 0)
1483
1487
  firstOffset = token.offset;
1484
1488
  carryOnContent += token.content;
1485
1489
  } else {
@@ -1499,7 +1503,7 @@ function mergeWhitespaceTokens(tokens) {
1499
1503
  token
1500
1504
  );
1501
1505
  }
1502
- firstOffset = 0;
1506
+ firstOffset = void 0;
1503
1507
  carryOnContent = "";
1504
1508
  } else {
1505
1509
  newLine.push(token);
@@ -1711,6 +1715,21 @@ class ShikiError extends Error {
1711
1715
  }
1712
1716
  }
1713
1717
 
1718
+ function resolveLangAlias(name, alias) {
1719
+ if (!alias)
1720
+ return name;
1721
+ if (alias[name]) {
1722
+ const resolved = /* @__PURE__ */ new Set([name]);
1723
+ while (alias[name]) {
1724
+ name = alias[name];
1725
+ if (resolved.has(name))
1726
+ throw new ShikiError(`Circular alias \`${Array.from(resolved).join(" -> ")} -> ${name}\``);
1727
+ resolved.add(name);
1728
+ }
1729
+ }
1730
+ return name;
1731
+ }
1732
+
1714
1733
  class Registry extends Registry$1 {
1715
1734
  constructor(_resolver, _themes, _langs, _alias = {}) {
1716
1735
  super(_resolver);
@@ -1761,15 +1780,7 @@ class Registry extends Registry$1 {
1761
1780
  this._syncRegistry.setTheme(textmateTheme);
1762
1781
  }
1763
1782
  getGrammar(name) {
1764
- if (this._alias[name]) {
1765
- const resolved = /* @__PURE__ */ new Set([name]);
1766
- while (this._alias[name]) {
1767
- name = this._alias[name];
1768
- if (resolved.has(name))
1769
- throw new ShikiError(`Circular alias \`${Array.from(resolved).join(" -> ")} -> ${name}\``);
1770
- resolved.add(name);
1771
- }
1772
- }
1783
+ name = resolveLangAlias(name, this._alias);
1773
1784
  return this._resolvedGrammars.get(name);
1774
1785
  }
1775
1786
  loadLanguage(lang) {
@@ -1817,7 +1828,12 @@ class Registry extends Registry$1 {
1817
1828
  const langsGraphArray = Array.from(this._langGraph.entries());
1818
1829
  const missingLangs = langsGraphArray.filter(([_, lang]) => !lang);
1819
1830
  if (missingLangs.length) {
1820
- const dependents = langsGraphArray.filter(([_, lang]) => lang && lang.embeddedLangs?.some((l) => missingLangs.map(([name]) => name).includes(l))).filter((lang) => !missingLangs.includes(lang));
1831
+ const dependents = langsGraphArray.filter(([_, lang]) => {
1832
+ if (!lang)
1833
+ return false;
1834
+ const embedded = lang.embeddedLanguages || lang.embeddedLangs;
1835
+ return embedded?.some((l) => missingLangs.map(([name]) => name).includes(l));
1836
+ }).filter((lang) => !missingLangs.includes(lang));
1821
1837
  throw new ShikiError(`Missing languages ${missingLangs.map(([name]) => `\`${name}\``).join(", ")}, required by ${dependents.map(([name]) => `\`${name}\``).join(", ")}`);
1822
1838
  }
1823
1839
  for (const [_, lang] of langsGraphArray)
@@ -1905,6 +1921,9 @@ function createShikiInternalSync(options) {
1905
1921
  const resolver = new Resolver(options.engine, langs);
1906
1922
  const _registry = new Registry(resolver, themes, langs, options.langAlias);
1907
1923
  let _lastTheme;
1924
+ function resolveLangAlias$1(name) {
1925
+ return resolveLangAlias(name, options.langAlias);
1926
+ }
1908
1927
  function getLanguage(name) {
1909
1928
  ensureNotDisposed();
1910
1929
  const _lang = _registry.getGrammar(typeof name === "string" ? name : name.name);
@@ -1976,6 +1995,7 @@ function createShikiInternalSync(options) {
1976
1995
  getLanguage,
1977
1996
  getLoadedThemes,
1978
1997
  getLoadedLanguages,
1998
+ resolveLangAlias: resolveLangAlias$1,
1979
1999
  loadLanguage,
1980
2000
  loadLanguageSync,
1981
2001
  loadTheme,
@@ -2066,9 +2086,9 @@ function createBundledHighlighter(options) {
2066
2086
  async function createHighlighter(options2) {
2067
2087
  function resolveLang(lang) {
2068
2088
  if (typeof lang === "string") {
2089
+ lang = options2.langAlias?.[lang] || lang;
2069
2090
  if (isSpecialLang(lang))
2070
2091
  return [];
2071
- lang = options2.langAlias?.[lang] || lang;
2072
2092
  const bundle = bundledLanguages[lang];
2073
2093
  if (!bundle)
2074
2094
  throw new ShikiError$1(`Language \`${lang}\` is not included in this bundle. You may want to load it from external source.`);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@shikijs/core",
3
3
  "type": "module",
4
- "version": "3.17.1",
4
+ "version": "3.19.0",
5
5
  "description": "Core of Shiki",
6
6
  "author": "Pine Wu <octref@gmail.com>; Anthony Fu <anthonyfu117@hotmail.com>",
7
7
  "license": "MIT",
@@ -32,6 +32,13 @@
32
32
  "main": "./dist/index.mjs",
33
33
  "module": "./dist/index.mjs",
34
34
  "types": "./dist/index.d.mts",
35
+ "typesVersions": {
36
+ "*": {
37
+ "types": [
38
+ "./dist/types.d.mts"
39
+ ]
40
+ }
41
+ },
35
42
  "files": [
36
43
  "dist"
37
44
  ],
@@ -39,7 +46,7 @@
39
46
  "@shikijs/vscode-textmate": "^10.0.2",
40
47
  "@types/hast": "^3.0.4",
41
48
  "hast-util-to-html": "^9.0.5",
42
- "@shikijs/types": "3.17.1"
49
+ "@shikijs/types": "3.19.0"
43
50
  },
44
51
  "scripts": {
45
52
  "build": "unbuild",