@mistweaverco/mdsvex-shiki 1.0.15 → 1.0.16

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 (4) hide show
  1. package/index.cjs +49 -121
  2. package/index.d.ts +7 -1
  3. package/index.js +49 -121
  4. package/package.json +2 -2
package/index.cjs CHANGED
@@ -9727,34 +9727,6 @@ function createPositionConverter(code) {
9727
9727
  posToIndex
9728
9728
  };
9729
9729
  }
9730
- function guessEmbeddedLanguages(code, _lang, highlighter) {
9731
- const langs = /* @__PURE__ */ new Set;
9732
- for (const match of code.matchAll(/:?lang=["']([^"']+)["']/g)) {
9733
- const lang = match[1].toLowerCase().trim();
9734
- if (lang)
9735
- langs.add(lang);
9736
- }
9737
- for (const match of code.matchAll(/(?:```|~~~)([\w-]+)/g)) {
9738
- const lang = match[1].toLowerCase().trim();
9739
- if (lang)
9740
- langs.add(lang);
9741
- }
9742
- for (const match of code.matchAll(/\\begin\{([\w-]+)\}/g)) {
9743
- const lang = match[1].toLowerCase().trim();
9744
- if (lang)
9745
- langs.add(lang);
9746
- }
9747
- for (const match of code.matchAll(/<script\s+(?:type|lang)=["']([^"']+)["']/gi)) {
9748
- const fullType = match[1].toLowerCase().trim();
9749
- const lang = fullType.includes("/") ? fullType.split("/").pop() : fullType;
9750
- if (lang)
9751
- langs.add(lang);
9752
- }
9753
- if (!highlighter)
9754
- return Array.from(langs);
9755
- const bundle = highlighter.getBundledLanguages();
9756
- return Array.from(langs).filter((l) => l && bundle[l]);
9757
- }
9758
9730
  var DEFAULT_COLOR_LIGHT_DARK = "light-dark()";
9759
9731
  var COLOR_KEYS = ["color", "background-color"];
9760
9732
  function splitToken(token, offsets) {
@@ -11612,78 +11584,6 @@ function createBundledHighlighter(options) {
11612
11584
  }
11613
11585
  return createHighlighter;
11614
11586
  }
11615
- function makeSingletonHighlighter(createHighlighter) {
11616
- let _shiki;
11617
- async function getSingletonHighlighter(options = {}) {
11618
- if (!_shiki) {
11619
- _shiki = createHighlighter({
11620
- ...options,
11621
- themes: [],
11622
- langs: []
11623
- });
11624
- const s = await _shiki;
11625
- await Promise.all([
11626
- s.loadTheme(...options.themes || []),
11627
- s.loadLanguage(...options.langs || [])
11628
- ]);
11629
- return s;
11630
- } else {
11631
- const s = await _shiki;
11632
- await Promise.all([
11633
- s.loadTheme(...options.themes || []),
11634
- s.loadLanguage(...options.langs || [])
11635
- ]);
11636
- return s;
11637
- }
11638
- }
11639
- return getSingletonHighlighter;
11640
- }
11641
- function createSingletonShorthands(createHighlighter, config) {
11642
- const getSingletonHighlighter = makeSingletonHighlighter(createHighlighter);
11643
- async function get(code, options) {
11644
- const shiki = await getSingletonHighlighter({
11645
- langs: [options.lang],
11646
- themes: "theme" in options ? [options.theme] : Object.values(options.themes)
11647
- });
11648
- const langs = await config?.guessEmbeddedLanguages?.(code, options.lang, shiki);
11649
- if (langs) {
11650
- await shiki.loadLanguage(...langs);
11651
- }
11652
- return shiki;
11653
- }
11654
- return {
11655
- getSingletonHighlighter(options) {
11656
- return getSingletonHighlighter(options);
11657
- },
11658
- async codeToHtml(code, options) {
11659
- const shiki = await get(code, options);
11660
- return shiki.codeToHtml(code, options);
11661
- },
11662
- async codeToHast(code, options) {
11663
- const shiki = await get(code, options);
11664
- return shiki.codeToHast(code, options);
11665
- },
11666
- async codeToTokens(code, options) {
11667
- const shiki = await get(code, options);
11668
- return shiki.codeToTokens(code, options);
11669
- },
11670
- async codeToTokensBase(code, options) {
11671
- const shiki = await get(code, options);
11672
- return shiki.codeToTokensBase(code, options);
11673
- },
11674
- async codeToTokensWithThemes(code, options) {
11675
- const shiki = await get(code, options);
11676
- return shiki.codeToTokensWithThemes(code, options);
11677
- },
11678
- async getLastGrammarState(code, options) {
11679
- const shiki = await getSingletonHighlighter({
11680
- langs: [options.lang],
11681
- themes: [options.theme]
11682
- });
11683
- return shiki.getLastGrammarState(code, options);
11684
- }
11685
- };
11686
- }
11687
11587
  // node_modules/shiki/dist/langs.mjs
11688
11588
  var bundledLanguagesInfo = [
11689
11589
  {
@@ -13862,21 +13762,8 @@ var createHighlighter = /* @__PURE__ */ createBundledHighlighter({
13862
13762
  themes: bundledThemes,
13863
13763
  engine: () => createOnigurumaEngine(Promise.resolve().then(() => (init_wasm2(), exports_wasm2)))
13864
13764
  });
13865
- var {
13866
- codeToHtml: codeToHtml2,
13867
- codeToHast: codeToHast2,
13868
- codeToTokens: codeToTokens2,
13869
- codeToTokensBase: codeToTokensBase2,
13870
- codeToTokensWithThemes: codeToTokensWithThemes2,
13871
- getSingletonHighlighter,
13872
- getLastGrammarState: getLastGrammarState2
13873
- } = /* @__PURE__ */ createSingletonShorthands(createHighlighter, { guessEmbeddedLanguages });
13874
13765
  // src/index.ts
13875
13766
  var defaultShikiOptions = {
13876
- themes: {
13877
- dark: "catppuccin-mocha",
13878
- light: "catppuccin-mocha"
13879
- },
13880
13767
  cssVariablePrefix: "--shiki-",
13881
13768
  transformers: [
13882
13769
  transformerMetaHighlight(),
@@ -13887,22 +13774,63 @@ var defaultShikiOptions = {
13887
13774
  transformerNotationErrorLevel()
13888
13775
  ]
13889
13776
  };
13777
+ var defaultThemes = {
13778
+ dark: "catppuccin-mocha",
13779
+ light: "catppuccin-mocha"
13780
+ };
13781
+ var highlighterInstance = null;
13782
+ var highlighterPromise = null;
13783
+ var getHighlighterInstance = async (themes, langs = "all") => {
13784
+ if (highlighterInstance) {
13785
+ return highlighterInstance;
13786
+ }
13787
+ if (highlighterPromise) {
13788
+ return highlighterPromise;
13789
+ }
13790
+ const langsArray = langs === "all" ? Object.keys(bundledLanguages) : langs;
13791
+ let themesArray;
13792
+ if (Array.isArray(themes)) {
13793
+ themesArray = themes;
13794
+ } else if (typeof themes === "object" && "light" in themes && "dark" in themes) {
13795
+ themesArray = [themes.light, themes.dark];
13796
+ } else {
13797
+ themesArray = [themes];
13798
+ }
13799
+ const highlighterOptions = {
13800
+ themes: themesArray,
13801
+ langs: langsArray
13802
+ };
13803
+ highlighterPromise = createHighlighter(highlighterOptions);
13804
+ highlighterInstance = await highlighterPromise;
13805
+ return highlighterInstance;
13806
+ };
13890
13807
  var mdsvexShiki = async (config) => {
13808
+ const themes = config.shikiOptions?.themes || defaultThemes;
13809
+ const langs = config.shikiOptions?.langs || "all";
13891
13810
  const shikiOptions = {
13892
13811
  ...defaultShikiOptions,
13893
13812
  ...config.shikiOptions
13894
13813
  };
13814
+ delete shikiOptions.themes;
13815
+ delete shikiOptions.langs;
13816
+ await getHighlighterInstance(themes, langs);
13895
13817
  return async (code, lang245) => {
13896
13818
  lang245 = lang245 ?? "text";
13897
- shikiOptions.lang = lang245;
13898
- shikiOptions.transformers = [
13899
- ...defaultShikiOptions.transformers,
13900
- ...shikiOptions.transformers
13819
+ const highlighter = await getHighlighterInstance(themes, langs);
13820
+ const transformers = [
13821
+ ...defaultShikiOptions.transformers || [],
13822
+ ...shikiOptions.transformers || []
13901
13823
  ];
13902
- if (shikiOptions.transformers.find((t) => t.name === "transformerMdsvexWrapItUp") === undefined) {
13903
- shikiOptions.transformers.push(mdsvexWrapItUpTransformer(lang245, code, config.disableCopyButton));
13904
- }
13905
- return escapeHTML(await codeToHtml2(code, shikiOptions));
13824
+ if (!transformers.find((t) => t.name === "transformerMdsvexWrapItUp")) {
13825
+ transformers.push(mdsvexWrapItUpTransformer(lang245, code, config.disableCopyButton));
13826
+ }
13827
+ const html5 = highlighter.codeToHtml(code, {
13828
+ ...shikiOptions,
13829
+ lang: lang245,
13830
+ transformers,
13831
+ themes: typeof themes === "object" && !Array.isArray(themes) ? themes : undefined
13832
+ });
13833
+ return escapeHTML(html5);
13906
13834
  };
13907
13835
  };
13908
13836
  var src_default = mdsvexShiki;
package/index.d.ts CHANGED
@@ -19,7 +19,13 @@ export type HighlighterOptions = {
19
19
  displayTitle?: boolean;
20
20
  displayLanguage?: boolean;
21
21
  disableCopyButton?: boolean;
22
- shikiOptions?: Partial<CodeToHastOptions<BundledLanguage, BundledTheme>>;
22
+ shikiOptions?: Partial<CodeToHastOptions<BundledLanguage, BundledTheme>> & {
23
+ themes?: BundledTheme[] | {
24
+ light: BundledTheme;
25
+ dark: BundledTheme;
26
+ } | BundledTheme;
27
+ langs?: BundledLanguage[] | "all";
28
+ };
23
29
  };
24
30
  export declare const defaultShikiOptions: Partial<CodeToHastOptions<BundledLanguage, BundledTheme>>;
25
31
  export declare const mdsvexShiki: (config: HighlighterOptions) => Promise<(code: string, lang: string) => Promise<string>>;
package/index.js CHANGED
@@ -9700,34 +9700,6 @@ function createPositionConverter(code) {
9700
9700
  posToIndex
9701
9701
  };
9702
9702
  }
9703
- function guessEmbeddedLanguages(code, _lang, highlighter) {
9704
- const langs = /* @__PURE__ */ new Set;
9705
- for (const match of code.matchAll(/:?lang=["']([^"']+)["']/g)) {
9706
- const lang = match[1].toLowerCase().trim();
9707
- if (lang)
9708
- langs.add(lang);
9709
- }
9710
- for (const match of code.matchAll(/(?:```|~~~)([\w-]+)/g)) {
9711
- const lang = match[1].toLowerCase().trim();
9712
- if (lang)
9713
- langs.add(lang);
9714
- }
9715
- for (const match of code.matchAll(/\\begin\{([\w-]+)\}/g)) {
9716
- const lang = match[1].toLowerCase().trim();
9717
- if (lang)
9718
- langs.add(lang);
9719
- }
9720
- for (const match of code.matchAll(/<script\s+(?:type|lang)=["']([^"']+)["']/gi)) {
9721
- const fullType = match[1].toLowerCase().trim();
9722
- const lang = fullType.includes("/") ? fullType.split("/").pop() : fullType;
9723
- if (lang)
9724
- langs.add(lang);
9725
- }
9726
- if (!highlighter)
9727
- return Array.from(langs);
9728
- const bundle = highlighter.getBundledLanguages();
9729
- return Array.from(langs).filter((l) => l && bundle[l]);
9730
- }
9731
9703
  var DEFAULT_COLOR_LIGHT_DARK = "light-dark()";
9732
9704
  var COLOR_KEYS = ["color", "background-color"];
9733
9705
  function splitToken(token, offsets) {
@@ -11585,78 +11557,6 @@ function createBundledHighlighter(options) {
11585
11557
  }
11586
11558
  return createHighlighter;
11587
11559
  }
11588
- function makeSingletonHighlighter(createHighlighter) {
11589
- let _shiki;
11590
- async function getSingletonHighlighter(options = {}) {
11591
- if (!_shiki) {
11592
- _shiki = createHighlighter({
11593
- ...options,
11594
- themes: [],
11595
- langs: []
11596
- });
11597
- const s = await _shiki;
11598
- await Promise.all([
11599
- s.loadTheme(...options.themes || []),
11600
- s.loadLanguage(...options.langs || [])
11601
- ]);
11602
- return s;
11603
- } else {
11604
- const s = await _shiki;
11605
- await Promise.all([
11606
- s.loadTheme(...options.themes || []),
11607
- s.loadLanguage(...options.langs || [])
11608
- ]);
11609
- return s;
11610
- }
11611
- }
11612
- return getSingletonHighlighter;
11613
- }
11614
- function createSingletonShorthands(createHighlighter, config) {
11615
- const getSingletonHighlighter = makeSingletonHighlighter(createHighlighter);
11616
- async function get(code, options) {
11617
- const shiki = await getSingletonHighlighter({
11618
- langs: [options.lang],
11619
- themes: "theme" in options ? [options.theme] : Object.values(options.themes)
11620
- });
11621
- const langs = await config?.guessEmbeddedLanguages?.(code, options.lang, shiki);
11622
- if (langs) {
11623
- await shiki.loadLanguage(...langs);
11624
- }
11625
- return shiki;
11626
- }
11627
- return {
11628
- getSingletonHighlighter(options) {
11629
- return getSingletonHighlighter(options);
11630
- },
11631
- async codeToHtml(code, options) {
11632
- const shiki = await get(code, options);
11633
- return shiki.codeToHtml(code, options);
11634
- },
11635
- async codeToHast(code, options) {
11636
- const shiki = await get(code, options);
11637
- return shiki.codeToHast(code, options);
11638
- },
11639
- async codeToTokens(code, options) {
11640
- const shiki = await get(code, options);
11641
- return shiki.codeToTokens(code, options);
11642
- },
11643
- async codeToTokensBase(code, options) {
11644
- const shiki = await get(code, options);
11645
- return shiki.codeToTokensBase(code, options);
11646
- },
11647
- async codeToTokensWithThemes(code, options) {
11648
- const shiki = await get(code, options);
11649
- return shiki.codeToTokensWithThemes(code, options);
11650
- },
11651
- async getLastGrammarState(code, options) {
11652
- const shiki = await getSingletonHighlighter({
11653
- langs: [options.lang],
11654
- themes: [options.theme]
11655
- });
11656
- return shiki.getLastGrammarState(code, options);
11657
- }
11658
- };
11659
- }
11660
11560
  // node_modules/shiki/dist/langs.mjs
11661
11561
  var bundledLanguagesInfo = [
11662
11562
  {
@@ -13835,21 +13735,8 @@ var createHighlighter = /* @__PURE__ */ createBundledHighlighter({
13835
13735
  themes: bundledThemes,
13836
13736
  engine: () => createOnigurumaEngine(Promise.resolve().then(() => (init_wasm2(), exports_wasm2)))
13837
13737
  });
13838
- var {
13839
- codeToHtml: codeToHtml2,
13840
- codeToHast: codeToHast2,
13841
- codeToTokens: codeToTokens2,
13842
- codeToTokensBase: codeToTokensBase2,
13843
- codeToTokensWithThemes: codeToTokensWithThemes2,
13844
- getSingletonHighlighter,
13845
- getLastGrammarState: getLastGrammarState2
13846
- } = /* @__PURE__ */ createSingletonShorthands(createHighlighter, { guessEmbeddedLanguages });
13847
13738
  // src/index.ts
13848
13739
  var defaultShikiOptions = {
13849
- themes: {
13850
- dark: "catppuccin-mocha",
13851
- light: "catppuccin-mocha"
13852
- },
13853
13740
  cssVariablePrefix: "--shiki-",
13854
13741
  transformers: [
13855
13742
  transformerMetaHighlight(),
@@ -13860,22 +13747,63 @@ var defaultShikiOptions = {
13860
13747
  transformerNotationErrorLevel()
13861
13748
  ]
13862
13749
  };
13750
+ var defaultThemes = {
13751
+ dark: "catppuccin-mocha",
13752
+ light: "catppuccin-mocha"
13753
+ };
13754
+ var highlighterInstance = null;
13755
+ var highlighterPromise = null;
13756
+ var getHighlighterInstance = async (themes, langs = "all") => {
13757
+ if (highlighterInstance) {
13758
+ return highlighterInstance;
13759
+ }
13760
+ if (highlighterPromise) {
13761
+ return highlighterPromise;
13762
+ }
13763
+ const langsArray = langs === "all" ? Object.keys(bundledLanguages) : langs;
13764
+ let themesArray;
13765
+ if (Array.isArray(themes)) {
13766
+ themesArray = themes;
13767
+ } else if (typeof themes === "object" && "light" in themes && "dark" in themes) {
13768
+ themesArray = [themes.light, themes.dark];
13769
+ } else {
13770
+ themesArray = [themes];
13771
+ }
13772
+ const highlighterOptions = {
13773
+ themes: themesArray,
13774
+ langs: langsArray
13775
+ };
13776
+ highlighterPromise = createHighlighter(highlighterOptions);
13777
+ highlighterInstance = await highlighterPromise;
13778
+ return highlighterInstance;
13779
+ };
13863
13780
  var mdsvexShiki = async (config) => {
13781
+ const themes = config.shikiOptions?.themes || defaultThemes;
13782
+ const langs = config.shikiOptions?.langs || "all";
13864
13783
  const shikiOptions = {
13865
13784
  ...defaultShikiOptions,
13866
13785
  ...config.shikiOptions
13867
13786
  };
13787
+ delete shikiOptions.themes;
13788
+ delete shikiOptions.langs;
13789
+ await getHighlighterInstance(themes, langs);
13868
13790
  return async (code, lang245) => {
13869
13791
  lang245 = lang245 ?? "text";
13870
- shikiOptions.lang = lang245;
13871
- shikiOptions.transformers = [
13872
- ...defaultShikiOptions.transformers,
13873
- ...shikiOptions.transformers
13792
+ const highlighter = await getHighlighterInstance(themes, langs);
13793
+ const transformers = [
13794
+ ...defaultShikiOptions.transformers || [],
13795
+ ...shikiOptions.transformers || []
13874
13796
  ];
13875
- if (shikiOptions.transformers.find((t) => t.name === "transformerMdsvexWrapItUp") === undefined) {
13876
- shikiOptions.transformers.push(mdsvexWrapItUpTransformer(lang245, code, config.disableCopyButton));
13877
- }
13878
- return escapeHTML(await codeToHtml2(code, shikiOptions));
13797
+ if (!transformers.find((t) => t.name === "transformerMdsvexWrapItUp")) {
13798
+ transformers.push(mdsvexWrapItUpTransformer(lang245, code, config.disableCopyButton));
13799
+ }
13800
+ const html5 = highlighter.codeToHtml(code, {
13801
+ ...shikiOptions,
13802
+ lang: lang245,
13803
+ transformers,
13804
+ themes: typeof themes === "object" && !Array.isArray(themes) ? themes : undefined
13805
+ });
13806
+ return escapeHTML(html5);
13879
13807
  };
13880
13808
  };
13881
13809
  var src_default = mdsvexShiki;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mistweaverco/mdsvex-shiki",
3
- "version": "1.0.15",
3
+ "version": "1.0.16",
4
4
  "type": "module",
5
5
  "main": "./index.cjs",
6
6
  "module": "./index.js",
@@ -24,7 +24,7 @@
24
24
  "prepublishOnly": "bun run build",
25
25
  "clear": "rm -f *.js *.cjs *.d.ts",
26
26
  "build": "bun build.ts",
27
- "publish:npm": "npm publish --access public",
27
+ "publish:npm": "npm publish",
28
28
  "publish:github": "npm publish --registry https://npm.pkg.github.com --access public"
29
29
  },
30
30
  "keywords": [