@astrojs/markdown-remark 3.2.1 → 3.3.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.
@@ -1,40 +1,39 @@
1
- import { getHighlighter } from "shiki";
1
+ import { bundledLanguages, getHighlighter } from "shikiji";
2
2
  import { visit } from "unist-util-visit";
3
+ const ASTRO_COLOR_REPLACEMENTS = {
4
+ "#000001": "var(--astro-code-color-text)",
5
+ "#000002": "var(--astro-code-color-background)",
6
+ "#000004": "var(--astro-code-token-constant)",
7
+ "#000005": "var(--astro-code-token-string)",
8
+ "#000006": "var(--astro-code-token-comment)",
9
+ "#000007": "var(--astro-code-token-keyword)",
10
+ "#000008": "var(--astro-code-token-parameter)",
11
+ "#000009": "var(--astro-code-token-function)",
12
+ "#000010": "var(--astro-code-token-string-expression)",
13
+ "#000011": "var(--astro-code-token-punctuation)",
14
+ "#000012": "var(--astro-code-token-link)"
15
+ };
16
+ const COLOR_REPLACEMENT_REGEX = new RegExp(
17
+ `(${Object.keys(ASTRO_COLOR_REPLACEMENTS).join("|")})`,
18
+ "g"
19
+ );
3
20
  const highlighterCacheAsync = /* @__PURE__ */ new Map();
4
21
  function remarkShiki({
5
22
  langs = [],
6
23
  theme = "github-dark",
7
24
  wrap = false
8
25
  } = {}) {
9
- const cacheID = typeof theme === "string" ? theme : theme.name;
10
- let highlighterAsync = highlighterCacheAsync.get(cacheID);
26
+ const cacheId = (typeof theme === "string" ? theme : theme.name ?? "") + langs.map((l) => l.name ?? l.id).join(",");
27
+ let highlighterAsync = highlighterCacheAsync.get(cacheId);
11
28
  if (!highlighterAsync) {
12
- highlighterAsync = getHighlighter({ theme }).then((hl) => {
13
- hl.setColorReplacements({
14
- "#000001": "var(--astro-code-color-text)",
15
- "#000002": "var(--astro-code-color-background)",
16
- "#000004": "var(--astro-code-token-constant)",
17
- "#000005": "var(--astro-code-token-string)",
18
- "#000006": "var(--astro-code-token-comment)",
19
- "#000007": "var(--astro-code-token-keyword)",
20
- "#000008": "var(--astro-code-token-parameter)",
21
- "#000009": "var(--astro-code-token-function)",
22
- "#000010": "var(--astro-code-token-string-expression)",
23
- "#000011": "var(--astro-code-token-punctuation)",
24
- "#000012": "var(--astro-code-token-link)"
25
- });
26
- return hl;
29
+ highlighterAsync = getHighlighter({
30
+ langs: langs.length ? langs : Object.keys(bundledLanguages),
31
+ themes: [theme]
27
32
  });
28
- highlighterCacheAsync.set(cacheID, highlighterAsync);
33
+ highlighterCacheAsync.set(cacheId, highlighterAsync);
29
34
  }
30
- let highlighter;
31
35
  return async (tree) => {
32
- if (!highlighter) {
33
- highlighter = await highlighterAsync;
34
- for (const lang of langs) {
35
- await highlighter.loadLanguage(lang);
36
- }
37
- }
36
+ const highlighter = await highlighterAsync;
38
37
  visit(tree, "code", (node) => {
39
38
  let lang;
40
39
  if (typeof node.lang === "string") {
@@ -48,7 +47,7 @@ function remarkShiki({
48
47
  } else {
49
48
  lang = "plaintext";
50
49
  }
51
- let html = highlighter.codeToHtml(node.value, { lang });
50
+ let html = highlighter.codeToHtml(node.value, { lang, theme });
52
51
  html = html.replace(/<pre class="(.*?)shiki(.*?)"/, `<pre class="$1astro-code$2"`);
53
52
  if (node.lang === "diff") {
54
53
  html = html.replace(
@@ -64,12 +63,19 @@ function remarkShiki({
64
63
  'style="$1; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;"'
65
64
  );
66
65
  }
66
+ const themeName = typeof theme === "string" ? theme : theme.name;
67
+ if (themeName === "css-variables") {
68
+ html = html.replace(/style="(.*?)"/g, (m) => replaceCssVariables(m));
69
+ }
67
70
  node.type = "html";
68
71
  node.value = html;
69
72
  node.children = [];
70
73
  });
71
74
  };
72
75
  }
76
+ function replaceCssVariables(str) {
77
+ return str.replace(COLOR_REPLACEMENT_REGEX, (match) => ASTRO_COLOR_REPLACEMENTS[match] || match);
78
+ }
73
79
  export {
74
80
  remarkShiki
75
81
  };
package/dist/types.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import type * as hast from 'hast';
2
2
  import type * as mdast from 'mdast';
3
3
  import type { one as Handler, all as Handlers, Options as RemarkRehypeOptions } from 'remark-rehype';
4
- import type { ILanguageRegistration, IThemeRegistration, Theme } from 'shiki';
4
+ import type { BuiltinTheme, LanguageRegistration, ThemeRegistration, ThemeRegistrationRaw } from 'shikiji';
5
5
  import type * as unified from 'unified';
6
6
  import type { VFile } from 'vfile';
7
7
  export type { Node } from 'unist';
@@ -17,8 +17,8 @@ export type RemarkRehype = Omit<RemarkRehypeOptions, 'handlers' | 'unknownHandle
17
17
  handler?: typeof Handler;
18
18
  };
19
19
  export interface ShikiConfig {
20
- langs?: ILanguageRegistration[];
21
- theme?: Theme | IThemeRegistration;
20
+ langs?: LanguageRegistration[];
21
+ theme?: BuiltinTheme | ThemeRegistration | ThemeRegistrationRaw;
22
22
  wrap?: boolean | null;
23
23
  }
24
24
  export interface AstroMarkdownOptions {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrojs/markdown-remark",
3
- "version": "3.2.1",
3
+ "version": "3.3.0",
4
4
  "type": "module",
5
5
  "author": "withastro",
6
6
  "license": "MIT",
@@ -20,7 +20,7 @@
20
20
  "dist"
21
21
  ],
22
22
  "peerDependencies": {
23
- "astro": "^3.2.3"
23
+ "astro": "^3.3.0"
24
24
  },
25
25
  "dependencies": {
26
26
  "@astrojs/prism": "^3.0.0",
@@ -33,7 +33,7 @@
33
33
  "remark-parse": "^10.0.2",
34
34
  "remark-rehype": "^10.1.0",
35
35
  "remark-smartypants": "^2.0.0",
36
- "shiki": "^0.14.3",
36
+ "shikiji": "^0.6.8",
37
37
  "unified": "^10.1.2",
38
38
  "unist-util-visit": "^4.1.2",
39
39
  "vfile": "^5.3.7"