@astrojs/mdx 4.0.0-alpha.1 → 4.0.0-alpha.2

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/plugins.js CHANGED
@@ -62,7 +62,7 @@ function getRehypePlugins(mdxOptions) {
62
62
  rehypePlugins.push(rehypeHeadingIds, rehypeInjectHeadingsExport);
63
63
  }
64
64
  rehypePlugins.push(
65
- // Render info from `vfile.data.astro.data.frontmatter` as JS
65
+ // Render info from `vfile.data.astro.frontmatter` as JS
66
66
  rehypeApplyFrontmatterExport,
67
67
  // Analyze MDX nodes and attach to `vfile.data.__astroMetadata`
68
68
  rehypeAnalyzeAstroMetadata
@@ -1,40 +1,39 @@
1
- import { InvalidAstroDataError } from "@astrojs/markdown-remark";
2
- import { safelyGetAstroData } from "@astrojs/markdown-remark/dist/internal.js";
1
+ import { isFrontmatterValid } from "@astrojs/markdown-remark";
3
2
  import { jsToTreeNode } from "./utils.js";
4
3
  function rehypeApplyFrontmatterExport() {
5
4
  return function(tree, vfile) {
6
- const astroData = safelyGetAstroData(vfile.data);
7
- if (astroData instanceof InvalidAstroDataError)
5
+ const frontmatter = vfile.data.astro?.frontmatter;
6
+ if (!frontmatter || !isFrontmatterValid(frontmatter))
8
7
  throw new Error(
9
8
  // Copied from Astro core `errors-data`
10
9
  // TODO: find way to import error data from core
11
10
  '[MDX] A remark or rehype plugin attempted to inject invalid frontmatter. Ensure "astro.frontmatter" is set to a valid JSON object that is not `null` or `undefined`.'
12
11
  );
13
- const { frontmatter } = astroData;
14
12
  const exportNodes = [
15
13
  jsToTreeNode(`export const frontmatter = ${JSON.stringify(frontmatter)};`)
16
14
  ];
17
15
  if (frontmatter.layout) {
18
16
  exportNodes.unshift(
19
17
  jsToTreeNode(
18
+ // NOTE: Use `__astro_*` import names to prevent conflicts with user code
20
19
  /** @see 'vite-plugin-markdown' for layout props reference */
21
- `import { jsx as layoutJsx } from 'astro/jsx-runtime';
20
+ `import { jsx as __astro_layout_jsx__ } from 'astro/jsx-runtime';
21
+ import __astro_layout_component__ from ${JSON.stringify(frontmatter.layout)};
22
22
 
23
- export default async function ({ children }) {
24
- const Layout = (await import(${JSON.stringify(frontmatter.layout)})).default;
25
- const { layout, ...content } = frontmatter;
26
- content.file = file;
27
- content.url = url;
28
- return layoutJsx(Layout, {
29
- file,
30
- url,
31
- content,
32
- frontmatter: content,
33
- headings: getHeadings(),
34
- 'server:root': true,
35
- children,
36
- });
37
- };`
23
+ export default function ({ children }) {
24
+ const { layout, ...content } = frontmatter;
25
+ content.file = file;
26
+ content.url = url;
27
+ return __astro_layout_jsx__(__astro_layout_component__, {
28
+ file,
29
+ url,
30
+ content,
31
+ frontmatter: content,
32
+ headings: getHeadings(),
33
+ 'server:root': true,
34
+ children,
35
+ });
36
+ };`
38
37
  )
39
38
  );
40
39
  }
@@ -1,2 +1,2 @@
1
- import type { MarkdownVFile } from '@astrojs/markdown-remark';
2
- export declare function rehypeInjectHeadingsExport(): (tree: any, file: MarkdownVFile) => void;
1
+ import type { VFile } from 'vfile';
2
+ export declare function rehypeInjectHeadingsExport(): (tree: any, file: VFile) => void;
@@ -1,7 +1,7 @@
1
1
  import { jsToTreeNode } from "./utils.js";
2
2
  function rehypeInjectHeadingsExport() {
3
3
  return function(tree, file) {
4
- const headings = file.data.__astroHeadings || [];
4
+ const headings = file.data.astro?.headings ?? [];
5
5
  tree.children.unshift(
6
6
  jsToTreeNode(`export function getHeadings() { return ${JSON.stringify(headings)} }`)
7
7
  );
@@ -1,6 +1,6 @@
1
- import type { MarkdownVFile } from '@astrojs/markdown-remark';
2
1
  import type { Root } from 'hast';
2
+ import type { VFile } from 'vfile';
3
3
  export declare const ASTRO_IMAGE_ELEMENT = "astro-image";
4
4
  export declare const ASTRO_IMAGE_IMPORT = "__AstroImage__";
5
5
  export declare const USES_ASTRO_IMAGE_FLAG = "__usesAstroImage";
6
- export declare function rehypeImageToComponent(): (tree: Root, file: MarkdownVFile) => void;
6
+ export declare function rehypeImageToComponent(): (tree: Root, file: VFile) => void;
@@ -51,13 +51,13 @@ function getImageComponentAttributes(props) {
51
51
  }
52
52
  function rehypeImageToComponent() {
53
53
  return function(tree, file) {
54
- if (!file.data.imagePaths) return;
54
+ if (!file.data.astro?.imagePaths) return;
55
55
  const importsStatements = [];
56
56
  const importedImages = /* @__PURE__ */ new Map();
57
57
  visit(tree, "element", (node, index, parent) => {
58
- if (!file.data.imagePaths || node.tagName !== "img" || !node.properties.src) return;
58
+ if (!file.data.astro?.imagePaths || node.tagName !== "img" || !node.properties.src) return;
59
59
  const src = decodeURI(String(node.properties.src));
60
- if (!file.data.imagePaths.has(src)) return;
60
+ if (!file.data.astro.imagePaths?.includes(src)) return;
61
61
  let importName = importedImages.get(src);
62
62
  if (!importName) {
63
63
  importName = `__${importedImages.size}_${src.replace(/\W/g, "_")}__`;
@@ -1,4 +1,3 @@
1
- import { setVfileFrontmatter } from "@astrojs/markdown-remark";
2
1
  import { getAstroMetadata } from "astro/jsx/rehype.js";
3
2
  import { VFile } from "vfile";
4
3
  import { createMdxProcessor } from "./plugins.js";
@@ -34,8 +33,15 @@ function vitePluginMdx(mdxOptions) {
34
33
  if (!id.endsWith(".mdx")) return;
35
34
  const { data: frontmatter, content: pageContent, matter } = parseFrontmatter(code, id);
36
35
  const frontmatterLines = matter ? matter.match(/\n/g)?.join("") + "\n\n" : "";
37
- const vfile = new VFile({ value: frontmatterLines + pageContent, path: id });
38
- setVfileFrontmatter(vfile, frontmatter);
36
+ const vfile = new VFile({
37
+ value: frontmatterLines + pageContent,
38
+ path: id,
39
+ data: {
40
+ astro: {
41
+ frontmatter
42
+ }
43
+ }
44
+ });
39
45
  if (!processor) {
40
46
  return this.error(
41
47
  "MDX processor is not initialized. This is an internal error. Please file an issue."
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@astrojs/mdx",
3
3
  "description": "Add support for MDX pages in your Astro site",
4
- "version": "4.0.0-alpha.1",
4
+ "version": "4.0.0-alpha.2",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",
7
7
  "author": "withastro",
@@ -33,15 +33,15 @@
33
33
  "es-module-lexer": "^1.5.4",
34
34
  "estree-util-visit": "^2.0.0",
35
35
  "gray-matter": "^4.0.3",
36
- "hast-util-to-html": "^9.0.1",
36
+ "hast-util-to-html": "^9.0.2",
37
37
  "kleur": "^4.1.5",
38
38
  "rehype-raw": "^7.0.0",
39
39
  "remark-gfm": "^4.0.0",
40
40
  "remark-smartypants": "^3.0.2",
41
41
  "source-map": "^0.7.4",
42
42
  "unist-util-visit": "^5.0.0",
43
- "vfile": "^6.0.2",
44
- "@astrojs/markdown-remark": "6.0.0-alpha.0"
43
+ "vfile": "^6.0.3",
44
+ "@astrojs/markdown-remark": "6.0.0-alpha.1"
45
45
  },
46
46
  "peerDependencies": {
47
47
  "astro": "^5.0.0-alpha.0"
@@ -53,18 +53,18 @@
53
53
  "cheerio": "1.0.0",
54
54
  "linkedom": "^0.18.4",
55
55
  "mdast-util-mdx": "^3.0.0",
56
- "mdast-util-mdx-jsx": "^3.1.2",
56
+ "mdast-util-mdx-jsx": "^3.1.3",
57
57
  "mdast-util-to-string": "^4.0.0",
58
58
  "rehype-mathjax": "^6.0.0",
59
- "rehype-pretty-code": "^0.13.2",
59
+ "rehype-pretty-code": "^0.14.0",
60
60
  "remark-math": "^6.0.0",
61
61
  "remark-rehype": "^11.1.0",
62
62
  "remark-shiki-twoslash": "^3.1.3",
63
63
  "remark-toc": "^9.0.0",
64
- "shiki": "^1.14.1",
64
+ "shiki": "^1.16.1",
65
65
  "unified": "^11.0.5",
66
- "vite": "^5.4.1",
67
- "astro": "5.0.0-alpha.1",
66
+ "vite": "^5.4.2",
67
+ "astro": "5.0.0-alpha.3",
68
68
  "astro-scripts": "0.0.14"
69
69
  },
70
70
  "engines": {