@astrojs/markdown-remark 4.0.0 → 4.1.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/index.js CHANGED
@@ -30,7 +30,8 @@ const markdownConfigDefaults = {
30
30
  langs: [],
31
31
  theme: "github-dark",
32
32
  experimentalThemes: {},
33
- wrap: false
33
+ wrap: false,
34
+ transformers: []
34
35
  },
35
36
  remarkPlugins: [],
36
37
  rehypePlugins: [],
@@ -1,6 +1,7 @@
1
1
  import { visit } from "unist-util-visit";
2
2
  function rehypeImages() {
3
3
  return () => function(tree, file) {
4
+ const imageOccurrenceMap = /* @__PURE__ */ new Map();
4
5
  visit(tree, (node) => {
5
6
  if (node.type !== "element")
6
7
  return;
@@ -8,8 +9,13 @@ function rehypeImages() {
8
9
  return;
9
10
  if (node.properties?.src) {
10
11
  if (file.data.imagePaths?.has(node.properties.src)) {
11
- node.properties["__ASTRO_IMAGE_"] = node.properties.src;
12
- delete node.properties.src;
12
+ const { ...props } = node.properties;
13
+ const index = imageOccurrenceMap.get(node.properties.src) || 0;
14
+ imageOccurrenceMap.set(node.properties.src, index + 1);
15
+ node.properties["__ASTRO_IMAGE_"] = JSON.stringify({ ...props, index });
16
+ Object.keys(props).forEach((prop) => {
17
+ delete node.properties[prop];
18
+ });
13
19
  }
14
20
  }
15
21
  });
package/dist/shiki.d.ts CHANGED
@@ -4,4 +4,4 @@ export interface ShikiHighlighter {
4
4
  inline?: boolean;
5
5
  }): string;
6
6
  }
7
- export declare function createShikiHighlighter({ langs, theme, experimentalThemes, wrap, }?: ShikiConfig): Promise<ShikiHighlighter>;
7
+ export declare function createShikiHighlighter({ langs, theme, experimentalThemes, wrap, transformers, }?: ShikiConfig): Promise<ShikiHighlighter>;
package/dist/shiki.js CHANGED
@@ -1,29 +1,24 @@
1
- import { bundledLanguages, getHighlighter } from "shikiji";
1
+ import { bundledLanguages, createCssVariablesTheme, getHighlighter } from "shikiji";
2
2
  import { visit } from "unist-util-visit";
3
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)"
4
+ "--astro-code-foreground": "--astro-code-color-text",
5
+ "--astro-code-background": "--astro-code-color-background"
15
6
  };
16
7
  const COLOR_REPLACEMENT_REGEX = new RegExp(
17
8
  `(${Object.keys(ASTRO_COLOR_REPLACEMENTS).join("|")})`,
18
9
  "g"
19
10
  );
11
+ let _cssVariablesTheme;
12
+ const cssVariablesTheme = () => _cssVariablesTheme ?? (_cssVariablesTheme = createCssVariablesTheme({ variablePrefix: "--astro-code-" }));
20
13
  async function createShikiHighlighter({
21
14
  langs = [],
22
15
  theme = "github-dark",
23
16
  experimentalThemes = {},
24
- wrap = false
17
+ wrap = false,
18
+ transformers = []
25
19
  } = {}) {
26
20
  const themes = experimentalThemes;
21
+ theme = theme === "css-variables" ? cssVariablesTheme() : theme;
27
22
  const highlighter = await getHighlighter({
28
23
  langs: langs.length ? langs : Object.keys(bundledLanguages),
29
24
  themes: Object.values(themes).length ? Object.values(themes) : [theme]
@@ -40,57 +35,60 @@ async function createShikiHighlighter({
40
35
  return highlighter.codeToHtml(code, {
41
36
  ...themeOptions,
42
37
  lang,
43
- transforms: {
44
- pre(node) {
45
- if (inline) {
46
- node.tagName = "code";
47
- }
48
- const classValue = node.properties.class ?? "";
49
- const styleValue = node.properties.style ?? "";
50
- node.properties.class = classValue.replace(/shiki/g, "astro-code");
51
- if (wrap === false) {
52
- node.properties.style = styleValue + "; overflow-x: auto;";
53
- } else if (wrap === true) {
54
- node.properties.style = styleValue + "; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;";
55
- }
56
- },
57
- line(node) {
58
- if (lang === "diff") {
59
- const innerSpanNode = node.children[0];
60
- const innerSpanTextNode = innerSpanNode?.type === "element" && innerSpanNode.children?.[0];
61
- if (innerSpanTextNode && innerSpanTextNode.type === "text") {
62
- const start = innerSpanTextNode.value[0];
63
- if (start === "+" || start === "-") {
64
- innerSpanTextNode.value = innerSpanTextNode.value.slice(1);
65
- innerSpanNode.children.unshift({
66
- type: "element",
67
- tagName: "span",
68
- properties: { style: "user-select: none;" },
69
- children: [{ type: "text", value: start }]
70
- });
38
+ transformers: [
39
+ {
40
+ pre(node) {
41
+ if (inline) {
42
+ node.tagName = "code";
43
+ }
44
+ const classValue = node.properties.class ?? "";
45
+ const styleValue = node.properties.style ?? "";
46
+ node.properties.class = classValue.replace(/shiki/g, "astro-code");
47
+ if (wrap === false) {
48
+ node.properties.style = styleValue + "; overflow-x: auto;";
49
+ } else if (wrap === true) {
50
+ node.properties.style = styleValue + "; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;";
51
+ }
52
+ },
53
+ line(node) {
54
+ if (lang === "diff") {
55
+ const innerSpanNode = node.children[0];
56
+ const innerSpanTextNode = innerSpanNode?.type === "element" && innerSpanNode.children?.[0];
57
+ if (innerSpanTextNode && innerSpanTextNode.type === "text") {
58
+ const start = innerSpanTextNode.value[0];
59
+ if (start === "+" || start === "-") {
60
+ innerSpanTextNode.value = innerSpanTextNode.value.slice(1);
61
+ innerSpanNode.children.unshift({
62
+ type: "element",
63
+ tagName: "span",
64
+ properties: { style: "user-select: none;" },
65
+ children: [{ type: "text", value: start }]
66
+ });
67
+ }
71
68
  }
72
69
  }
70
+ },
71
+ code(node) {
72
+ if (inline) {
73
+ return node.children[0];
74
+ }
75
+ },
76
+ root(node) {
77
+ if (Object.values(experimentalThemes).length) {
78
+ return;
79
+ }
80
+ const themeName = typeof theme === "string" ? theme : theme.name;
81
+ if (themeName === "css-variables") {
82
+ visit(node, "element", (child) => {
83
+ if (child.properties?.style) {
84
+ child.properties.style = replaceCssVariables(child.properties.style);
85
+ }
86
+ });
87
+ }
73
88
  }
74
89
  },
75
- code(node) {
76
- if (inline) {
77
- return node.children[0];
78
- }
79
- },
80
- root(node) {
81
- if (Object.values(experimentalThemes).length) {
82
- return;
83
- }
84
- const themeName = typeof theme === "string" ? theme : theme.name;
85
- if (themeName === "css-variables") {
86
- visit(node, "element", (child) => {
87
- if (child.properties?.style) {
88
- child.properties.style = replaceCssVariables(child.properties.style);
89
- }
90
- });
91
- }
92
- }
93
- }
90
+ ...transformers
91
+ ]
94
92
  });
95
93
  }
96
94
  };
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 { Options as RemarkRehypeOptions } from 'remark-rehype';
4
- import type { BuiltinTheme, LanguageRegistration, ThemeRegistration, ThemeRegistrationRaw } from 'shikiji';
4
+ import type { BuiltinTheme, LanguageRegistration, ShikijiTransformer, 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';
@@ -13,11 +13,13 @@ export type RemarkPlugins = (string | [string, any] | RemarkPlugin | [RemarkPlug
13
13
  export type RehypePlugin<PluginParameters extends any[] = any[]> = unified.Plugin<PluginParameters, hast.Root>;
14
14
  export type RehypePlugins = (string | [string, any] | RehypePlugin | [RehypePlugin, any])[];
15
15
  export type RemarkRehype = RemarkRehypeOptions;
16
+ export type ThemePresets = BuiltinTheme | 'css-variables';
16
17
  export interface ShikiConfig {
17
18
  langs?: LanguageRegistration[];
18
- theme?: BuiltinTheme | ThemeRegistration | ThemeRegistrationRaw;
19
- experimentalThemes?: Record<string, BuiltinTheme | ThemeRegistration | ThemeRegistrationRaw>;
19
+ theme?: ThemePresets | ThemeRegistration | ThemeRegistrationRaw;
20
+ experimentalThemes?: Record<string, ThemePresets | ThemeRegistration | ThemeRegistrationRaw>;
20
21
  wrap?: boolean | null;
22
+ transformers?: ShikijiTransformer[];
21
23
  }
22
24
  export interface AstroMarkdownOptions {
23
25
  syntaxHighlight?: 'shiki' | 'prism' | false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrojs/markdown-remark",
3
- "version": "4.0.0",
3
+ "version": "4.1.0",
4
4
  "type": "module",
5
5
  "author": "withastro",
6
6
  "license": "MIT",
@@ -19,9 +19,6 @@
19
19
  "files": [
20
20
  "dist"
21
21
  ],
22
- "peerDependencies": {
23
- "astro": "^4.0.0-beta.0"
24
- },
25
22
  "dependencies": {
26
23
  "@astrojs/prism": "^3.0.0",
27
24
  "github-slugger": "^2.0.0",
@@ -33,7 +30,7 @@
33
30
  "remark-parse": "^11.0.0",
34
31
  "remark-rehype": "^11.0.0",
35
32
  "remark-smartypants": "^2.0.0",
36
- "shikiji": "^0.6.13",
33
+ "shikiji": "^0.9.18",
37
34
  "unified": "^11.0.4",
38
35
  "unist-util-visit": "^5.0.0",
39
36
  "vfile": "^6.0.1"