@astrojs/markdown-remark 7.0.0-beta.8 → 7.0.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/shiki.d.ts CHANGED
@@ -40,5 +40,5 @@ export interface ShikiHighlighterHighlightOptions {
40
40
  */
41
41
  meta?: string;
42
42
  }
43
- export declare function createShikiHighlighter({ langs, theme, themes, langAlias, }?: CreateShikiHighlighterOptions): Promise<ShikiHighlighter>;
43
+ export declare function createShikiHighlighter(options?: CreateShikiHighlighterOptions): Promise<ShikiHighlighter>;
44
44
  export type { ThemePresets };
package/dist/shiki.js CHANGED
@@ -9,8 +9,48 @@ const cssVariablesTheme = () => _cssVariablesTheme ?? (_cssVariablesTheme = crea
9
9
  variablePrefix: "--astro-code-"
10
10
  }));
11
11
  const cachedHighlighters = /* @__PURE__ */ new Map();
12
+ function clearShikiHighlighterCache() {
13
+ cachedHighlighters.clear();
14
+ }
15
+ function createShikiHighlighter(options) {
16
+ const key = getCacheKey(options);
17
+ let highlighterPromise = cachedHighlighters.get(key);
18
+ if (!highlighterPromise) {
19
+ highlighterPromise = createShikiHighlighterInternal(options);
20
+ cachedHighlighters.set(key, highlighterPromise);
21
+ }
22
+ return ensureLanguagesLoaded(highlighterPromise, options?.langs);
23
+ }
24
+ function getCacheKey(options) {
25
+ const keyCache = [];
26
+ const { theme, themes, langAlias } = options ?? {};
27
+ if (theme) {
28
+ keyCache.push(theme);
29
+ }
30
+ if (themes) {
31
+ keyCache.push(Object.entries(themes).sort());
32
+ }
33
+ if (langAlias) {
34
+ keyCache.push(Object.entries(langAlias).sort());
35
+ }
36
+ return keyCache.length > 0 ? JSON.stringify(keyCache) : "";
37
+ }
38
+ async function ensureLanguagesLoaded(promise, langs) {
39
+ const highlighter = await promise;
40
+ if (!langs) {
41
+ return highlighter;
42
+ }
43
+ const loadedLanguages = highlighter.getLoadedLanguages();
44
+ for (const lang of langs) {
45
+ if (typeof lang === "string" && (isSpecialLang(lang) || loadedLanguages.includes(lang))) {
46
+ continue;
47
+ }
48
+ await highlighter.loadLanguage(lang);
49
+ }
50
+ return highlighter;
51
+ }
12
52
  let shikiEngine = void 0;
13
- async function createShikiHighlighter({
53
+ async function createShikiHighlighterInternal({
14
54
  langs = [],
15
55
  theme = "github-dark",
16
56
  themes = {},
@@ -20,20 +60,12 @@ async function createShikiHighlighter({
20
60
  if (shikiEngine === void 0) {
21
61
  shikiEngine = await loadShikiEngine();
22
62
  }
23
- const highlighterOptions = {
63
+ const highlighter = await createHighlighter({
24
64
  langs: ["plaintext", ...langs],
25
65
  langAlias,
26
66
  themes: Object.values(themes).length ? Object.values(themes) : [theme],
27
67
  engine: shikiEngine
28
- };
29
- const key = JSON.stringify(highlighterOptions, Object.keys(highlighterOptions).sort());
30
- let highlighter;
31
- if (cachedHighlighters.has(key)) {
32
- highlighter = cachedHighlighters.get(key);
33
- } else {
34
- highlighter = await createHighlighter(highlighterOptions);
35
- cachedHighlighters.set(key, highlighter);
36
- }
68
+ });
37
69
  async function highlight(code, lang = "plaintext", options, to) {
38
70
  const resolvedLang = langAlias[lang] ?? lang;
39
71
  const loadedLanguages = highlighter.getLoadedLanguages();
@@ -55,7 +87,7 @@ async function createShikiHighlighter({
55
87
  lang,
56
88
  // NOTE: while we can spread `options.attributes` here so that Shiki can auto-serialize this as rendered
57
89
  // attributes on the top-level tag, it's not clear whether it is fine to pass all attributes as meta, as
58
- // they're technically not meta, nor parsed from Shiki's `parseMetaString` API.
90
+ // they're technically neither meta nor parsed from Shiki's `parseMetaString` API.
59
91
  meta: options?.meta ? { __raw: options?.meta } : void 0,
60
92
  transformers: [
61
93
  {
@@ -113,6 +145,12 @@ async function createShikiHighlighter({
113
145
  },
114
146
  codeToHtml(code, lang, options = {}) {
115
147
  return highlight(code, lang, options, "html");
148
+ },
149
+ loadLanguage(...newLangs) {
150
+ return highlighter.loadLanguage(...newLangs);
151
+ },
152
+ getLoadedLanguages() {
153
+ return highlighter.getLoadedLanguages();
116
154
  }
117
155
  };
118
156
  }
@@ -120,5 +158,6 @@ function normalizePropAsString(value) {
120
158
  return Array.isArray(value) ? value.join(" ") : value;
121
159
  }
122
160
  export {
161
+ clearShikiHighlighterCache,
123
162
  createShikiHighlighter
124
163
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrojs/markdown-remark",
3
- "version": "7.0.0-beta.8",
3
+ "version": "7.0.0",
4
4
  "type": "module",
5
5
  "author": "withastro",
6
6
  "license": "MIT",
@@ -41,15 +41,15 @@
41
41
  "remark-parse": "^11.0.0",
42
42
  "remark-rehype": "^11.1.2",
43
43
  "remark-smartypants": "^3.0.2",
44
- "shiki": "^3.22.0",
44
+ "shiki": "^4.0.0",
45
45
  "smol-toml": "^1.6.0",
46
46
  "unified": "^11.0.5",
47
47
  "unist-util-remove-position": "^5.0.0",
48
48
  "unist-util-visit": "^5.1.0",
49
49
  "unist-util-visit-parents": "^6.0.2",
50
50
  "vfile": "^6.0.3",
51
- "@astrojs/internal-helpers": "0.8.0-beta.1",
52
- "@astrojs/prism": "4.0.0-beta.2"
51
+ "@astrojs/internal-helpers": "0.8.0",
52
+ "@astrojs/prism": "4.0.0"
53
53
  },
54
54
  "devDependencies": {
55
55
  "@types/estree": "^1.0.8",