@astrojs/mdx 4.0.0-beta.2 → 4.0.0-beta.3

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
@@ -11,7 +11,7 @@ function getContainerRenderer() {
11
11
  };
12
12
  }
13
13
  function mdx(partialMdxOptions = {}) {
14
- let mdxOptions = {};
14
+ let vitePluginMdxOptions = {};
15
15
  return {
16
16
  name: "@astrojs/mdx",
17
17
  hooks: {
@@ -43,7 +43,7 @@ function mdx(partialMdxOptions = {}) {
43
43
  });
44
44
  updateConfig({
45
45
  vite: {
46
- plugins: [vitePluginMdx(mdxOptions), vitePluginMdxPostprocess(config)]
46
+ plugins: [vitePluginMdx(vitePluginMdxOptions), vitePluginMdxPostprocess(config)]
47
47
  }
48
48
  });
49
49
  },
@@ -56,8 +56,11 @@ function mdx(partialMdxOptions = {}) {
56
56
  logger
57
57
  )
58
58
  });
59
- Object.assign(mdxOptions, resolvedMdxOptions);
60
- mdxOptions = {};
59
+ Object.assign(vitePluginMdxOptions, {
60
+ mdxOptions: resolvedMdxOptions,
61
+ srcDir: config.srcDir
62
+ });
63
+ vitePluginMdxOptions = {};
61
64
  }
62
65
  }
63
66
  };
@@ -1,2 +1,10 @@
1
+ import type { Root } from 'hast';
1
2
  import type { VFile } from 'vfile';
2
- export declare function rehypeApplyFrontmatterExport(): (tree: any, vfile: VFile) => void;
3
+ declare module 'vfile' {
4
+ interface DataMap {
5
+ applyFrontmatterExport?: {
6
+ srcDir?: URL;
7
+ };
8
+ }
9
+ }
10
+ export declare function rehypeApplyFrontmatterExport(): (tree: Root, vfile: VFile) => void;
@@ -1,5 +1,8 @@
1
+ import path from "node:path";
2
+ import { fileURLToPath } from "node:url";
1
3
  import { isFrontmatterValid } from "@astrojs/markdown-remark";
2
4
  import { jsToTreeNode } from "./utils.js";
5
+ const exportConstPartialTrueRe = /export\s+const\s+partial\s*=\s*true/;
3
6
  function rehypeApplyFrontmatterExport() {
4
7
  return function(tree, vfile) {
5
8
  const frontmatter = vfile.data.astro?.frontmatter;
@@ -9,11 +12,11 @@ function rehypeApplyFrontmatterExport() {
9
12
  // TODO: find way to import error data from core
10
13
  '[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`.'
11
14
  );
12
- const exportNodes = [
15
+ const extraChildren = [
13
16
  jsToTreeNode(`export const frontmatter = ${JSON.stringify(frontmatter)};`)
14
17
  ];
15
18
  if (frontmatter.layout) {
16
- exportNodes.unshift(
19
+ extraChildren.unshift(
17
20
  jsToTreeNode(
18
21
  // NOTE: Use `__astro_*` import names to prevent conflicts with user code
19
22
  /** @see 'vite-plugin-markdown' for layout props reference */
@@ -36,10 +39,45 @@ export default function ({ children }) {
36
39
  };`
37
40
  )
38
41
  );
42
+ } else if (shouldAddCharset(tree, vfile)) {
43
+ extraChildren.unshift({
44
+ type: "mdxJsxFlowElement",
45
+ name: "meta",
46
+ attributes: [
47
+ {
48
+ type: "mdxJsxAttribute",
49
+ name: "charset",
50
+ value: "utf-8"
51
+ }
52
+ ],
53
+ children: []
54
+ });
39
55
  }
40
- tree.children = exportNodes.concat(tree.children);
56
+ tree.children = extraChildren.concat(tree.children);
41
57
  };
42
58
  }
59
+ function shouldAddCharset(tree, vfile) {
60
+ const srcDirUrl = vfile.data.applyFrontmatterExport?.srcDir;
61
+ if (!srcDirUrl) return false;
62
+ const hasConstPartialTrue = tree.children.some(
63
+ (node) => node.type === "mdxjsEsm" && exportConstPartialTrueRe.test(node.value)
64
+ );
65
+ if (hasConstPartialTrue) return false;
66
+ const pagesDir = path.join(fileURLToPath(srcDirUrl), "pages").replace(/\\/g, "/");
67
+ const filePath = vfile.path;
68
+ if (!filePath.startsWith(pagesDir)) return false;
69
+ const hasLeadingUnderscoreInPath = filePath.slice(pagesDir.length).replace(/\\/g, "/").split("/").some((part) => part.startsWith("_"));
70
+ if (hasLeadingUnderscoreInPath) return false;
71
+ for (const child of tree.children) {
72
+ if (child.type === "element") break;
73
+ if (child.type === "mdxJsxFlowElement") {
74
+ if (child.name == null) break;
75
+ if (child.name[0] === child.name[0].toLowerCase()) break;
76
+ return false;
77
+ }
78
+ }
79
+ return true;
80
+ }
43
81
  export {
44
82
  rehypeApplyFrontmatterExport
45
83
  };
@@ -1,3 +1,7 @@
1
1
  import type { Plugin } from 'vite';
2
2
  import type { MdxOptions } from './index.js';
3
- export declare function vitePluginMdx(mdxOptions: MdxOptions): Plugin;
3
+ export interface VitePluginMdxOptions {
4
+ mdxOptions: MdxOptions;
5
+ srcDir: URL;
6
+ }
7
+ export declare function vitePluginMdx(opts: VitePluginMdxOptions): Plugin;
@@ -2,7 +2,7 @@ import { getAstroMetadata } from "astro/jsx/rehype.js";
2
2
  import { VFile } from "vfile";
3
3
  import { createMdxProcessor } from "./plugins.js";
4
4
  import { safeParseFrontmatter } from "./utils.js";
5
- function vitePluginMdx(mdxOptions) {
5
+ function vitePluginMdx(opts) {
6
6
  let processor;
7
7
  let sourcemapEnabled;
8
8
  return {
@@ -36,11 +36,14 @@ function vitePluginMdx(mdxOptions) {
36
36
  data: {
37
37
  astro: {
38
38
  frontmatter
39
+ },
40
+ applyFrontmatterExport: {
41
+ srcDir: opts.srcDir
39
42
  }
40
43
  }
41
44
  });
42
45
  if (!processor) {
43
- processor = createMdxProcessor(mdxOptions, { sourcemap: sourcemapEnabled });
46
+ processor = createMdxProcessor(opts.mdxOptions, { sourcemap: sourcemapEnabled });
44
47
  }
45
48
  try {
46
49
  const compiled = await processor.process(vfile);
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-beta.2",
4
+ "version": "4.0.0-beta.3",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",
7
7
  "author": "withastro",
@@ -28,8 +28,8 @@
28
28
  "template"
29
29
  ],
30
30
  "dependencies": {
31
- "@mdx-js/mdx": "^3.0.1",
32
- "acorn": "^8.12.1",
31
+ "@mdx-js/mdx": "^3.1.0",
32
+ "acorn": "^8.14.0",
33
33
  "es-module-lexer": "^1.5.4",
34
34
  "estree-util-visit": "^2.0.0",
35
35
  "hast-util-to-html": "^9.0.3",
@@ -60,10 +60,10 @@
60
60
  "remark-rehype": "^11.1.1",
61
61
  "remark-shiki-twoslash": "^3.1.3",
62
62
  "remark-toc": "^9.0.0",
63
- "shiki": "^1.21.0",
63
+ "shiki": "^1.22.2",
64
64
  "unified": "^11.0.5",
65
- "vite": "^5.4.8",
66
- "astro": "5.0.0-beta.3",
65
+ "vite": "6.0.0-beta.2",
66
+ "astro": "5.0.0-beta.6",
67
67
  "astro-scripts": "0.0.14"
68
68
  },
69
69
  "engines": {
@@ -4,6 +4,7 @@ declare module 'astro:content' {
4
4
  Content: import('astro').MarkdownInstance<{}>['Content'];
5
5
  headings: import('astro').MarkdownHeading[];
6
6
  remarkPluginFrontmatter: Record<string, any>;
7
+ components: import('astro').MDXInstance<{}>['components'];
7
8
  }>;
8
9
  }
9
10
  }