astro 1.6.8 → 1.6.9

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.
@@ -56,7 +56,10 @@ const highlighter = await getHighlighter({
56
56
  // Load custom lang if passed an object, otherwise load the default
57
57
  langs: typeof lang !== 'string' ? [lang] : undefined,
58
58
  });
59
- const _html = highlighter.codeToHtml(code, { lang: typeof lang === 'string' ? lang : lang.id });
59
+ const _html = highlighter.codeToHtml(code, {
60
+ lang: typeof lang === 'string' ? lang : lang.id,
61
+ theme,
62
+ });
60
63
  const html = repairShikiTheme(_html);
61
64
  ---
62
65
 
@@ -1,8 +1,13 @@
1
1
  import { getHighlighter as getShikiHighlighter } from 'shiki';
2
+ import { themes } from './shiki-themes.js';
3
+ import { languages } from './shiki-languages.js';
2
4
 
3
5
  // Caches Promise<Highligher> for reuse when the same theme and langs are provided
4
6
  const _resolvedHighlighters = new Map();
5
7
 
8
+ /** @type {Promise<any>} */
9
+ let _allLanguages;
10
+
6
11
  function stringify(opts) {
7
12
  // Always sort keys before stringifying to make sure objects match regardless of parameter ordering
8
13
  return JSON.stringify(opts, Object.keys(opts).sort());
@@ -12,16 +17,36 @@ function stringify(opts) {
12
17
  * @param {import('shiki').HighlighterOptions} opts
13
18
  * @returns {Promise<import('shiki').Highlighter>}
14
19
  */
15
- export function getHighlighter(opts) {
16
- const key = stringify(opts);
20
+ async function resolveHighlighter(opts) {
21
+ const resolvedThemes = [];
22
+ if (opts.theme && opts.theme in themes) {
23
+ resolvedThemes.push(await themes[opts.theme]());
24
+ }
17
25
 
18
- // Highlighter has already been requested, reuse the same instance
19
- if (_resolvedHighlighters.has(key)) {
20
- return _resolvedHighlighters.get(key);
26
+ let resolvedLanguages;
27
+ if (opts.langs) {
28
+ resolvedLanguages = opts.langs;
29
+ } else {
30
+ if (!_allLanguages) {
31
+ _allLanguages = (await Promise.all(Object.values(languages).map((fn) => fn()))).filter(
32
+ Boolean
33
+ );
34
+ }
35
+ resolvedLanguages = await _allLanguages;
21
36
  }
22
37
 
38
+ /** @type {import('shiki').HighlighterOptions} */
39
+ const highlighterOptions = {
40
+ ...opts,
41
+ themes: resolvedThemes,
42
+ langs: resolvedLanguages,
43
+ };
44
+
45
+ // Do not pass through the theme as that will attempt to load it, even if it's included in themes
46
+ delete highlighterOptions['theme'];
47
+
23
48
  // Start the async getHighlighter call and cache the Promise
24
- const highlighter = getShikiHighlighter(opts).then((hl) => {
49
+ const highlighter = getShikiHighlighter(highlighterOptions).then((hl) => {
25
50
  hl.setColorReplacements({
26
51
  '#000001': 'var(--astro-code-color-text)',
27
52
  '#000002': 'var(--astro-code-color-background)',
@@ -37,6 +62,23 @@ export function getHighlighter(opts) {
37
62
  });
38
63
  return hl;
39
64
  });
65
+
66
+ return highlighter;
67
+ }
68
+
69
+ /**
70
+ * @param {import('shiki').HighlighterOptions} opts
71
+ * @returns {Promise<import('shiki').Highlighter>}
72
+ */
73
+ export function getHighlighter(opts) {
74
+ const key = stringify(opts);
75
+
76
+ // Highlighter has already been requested, reuse the same instance
77
+ if (_resolvedHighlighters.has(key)) {
78
+ return _resolvedHighlighters.get(key);
79
+ }
80
+
81
+ const highlighter = resolveHighlighter(opts);
40
82
  _resolvedHighlighters.set(key, highlighter);
41
83
 
42
84
  return highlighter;