@astrojs/mdx 4.0.0-beta.1 → 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
@@ -1,7 +1,7 @@
1
1
  import fs from "node:fs/promises";
2
2
  import { fileURLToPath } from "node:url";
3
3
  import { markdownConfigDefaults } from "@astrojs/markdown-remark";
4
- import { ignoreStringPlugins, parseFrontmatter } from "./utils.js";
4
+ import { ignoreStringPlugins, safeParseFrontmatter } from "./utils.js";
5
5
  import { vitePluginMdxPostprocess } from "./vite-plugin-mdx-postprocess.js";
6
6
  import { vitePluginMdx } from "./vite-plugin-mdx.js";
7
7
  function getContainerRenderer() {
@@ -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: {
@@ -25,12 +25,12 @@ function mdx(partialMdxOptions = {}) {
25
25
  addContentEntryType({
26
26
  extensions: [".mdx"],
27
27
  async getEntryInfo({ fileUrl, contents }) {
28
- const parsed = parseFrontmatter(contents, fileURLToPath(fileUrl));
28
+ const parsed = safeParseFrontmatter(contents, fileURLToPath(fileUrl));
29
29
  return {
30
- data: parsed.data,
31
- body: parsed.content,
32
- slug: parsed.data.slug,
33
- rawData: parsed.matter
30
+ data: parsed.frontmatter,
31
+ body: parsed.content.trim(),
32
+ slug: parsed.frontmatter.slug,
33
+ rawData: parsed.rawFrontmatter
34
34
  };
35
35
  },
36
36
  contentModuleTypes: await fs.readFile(
@@ -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
  };
package/dist/utils.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import type { Options as AcornOpts } from 'acorn';
2
2
  import type { AstroConfig, AstroIntegrationLogger } from 'astro';
3
- import matter from 'gray-matter';
4
3
  import type { MdxjsEsm } from 'mdast-util-mdx';
5
4
  import type { PluggableList } from 'unified';
6
5
  export interface FileInfo {
@@ -13,6 +12,6 @@ export declare function getFileInfo(id: string, config: AstroConfig): FileInfo;
13
12
  * Match YAML exception handling from Astro core errors
14
13
  * @see 'astro/src/core/errors.ts'
15
14
  */
16
- export declare function parseFrontmatter(code: string, id: string): matter.GrayMatterFile<string>;
15
+ export declare function safeParseFrontmatter(code: string, id: string): import("@astrojs/markdown-remark").ParseFrontmatterResult;
17
16
  export declare function jsToTreeNode(jsString: string, acornOpts?: AcornOpts): MdxjsEsm;
18
17
  export declare function ignoreStringPlugins(plugins: any[], logger: AstroIntegrationLogger): PluggableList;
package/dist/utils.js CHANGED
@@ -1,5 +1,5 @@
1
+ import { parseFrontmatter } from "@astrojs/markdown-remark";
1
2
  import { parse } from "acorn";
2
- import matter from "gray-matter";
3
3
  import { bold } from "kleur/colors";
4
4
  function appendForwardSlash(path) {
5
5
  return path.endsWith("/") ? path : path + "/";
@@ -28,9 +28,9 @@ function getFileInfo(id, config) {
28
28
  }
29
29
  return { fileId, fileUrl };
30
30
  }
31
- function parseFrontmatter(code, id) {
31
+ function safeParseFrontmatter(code, id) {
32
32
  try {
33
- return matter(code);
33
+ return parseFrontmatter(code, { frontmatter: "empty-with-spaces" });
34
34
  } catch (e) {
35
35
  if (e.name === "YAMLException") {
36
36
  const err = e;
@@ -85,5 +85,5 @@ export {
85
85
  getFileInfo,
86
86
  ignoreStringPlugins,
87
87
  jsToTreeNode,
88
- parseFrontmatter
88
+ safeParseFrontmatter
89
89
  };
@@ -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;
@@ -1,9 +1,10 @@
1
1
  import { getAstroMetadata } from "astro/jsx/rehype.js";
2
2
  import { VFile } from "vfile";
3
3
  import { createMdxProcessor } from "./plugins.js";
4
- import { parseFrontmatter } from "./utils.js";
5
- function vitePluginMdx(mdxOptions) {
4
+ import { safeParseFrontmatter } from "./utils.js";
5
+ function vitePluginMdx(opts) {
6
6
  let processor;
7
+ let sourcemapEnabled;
7
8
  return {
8
9
  name: "@mdx-js/rollup",
9
10
  enforce: "pre",
@@ -11,10 +12,7 @@ function vitePluginMdx(mdxOptions) {
11
12
  processor = void 0;
12
13
  },
13
14
  configResolved(resolved) {
14
- if (Object.keys(mdxOptions).length === 0) return;
15
- processor = createMdxProcessor(mdxOptions, {
16
- sourcemap: !!resolved.build.sourcemap
17
- });
15
+ sourcemapEnabled = !!resolved.build.sourcemap;
18
16
  const jsxPluginIndex = resolved.plugins.findIndex((p) => p.name === "astro:jsx");
19
17
  if (jsxPluginIndex !== -1) {
20
18
  resolved.plugins.splice(jsxPluginIndex, 1);
@@ -31,21 +29,21 @@ function vitePluginMdx(mdxOptions) {
31
29
  // ex. inject layouts
32
30
  async transform(code, id) {
33
31
  if (!id.endsWith(".mdx")) return;
34
- const { data: frontmatter, content: pageContent, matter } = parseFrontmatter(code, id);
35
- const frontmatterLines = matter ? matter.match(/\n/g)?.join("") + "\n\n" : "";
32
+ const { frontmatter, content } = safeParseFrontmatter(code, id);
36
33
  const vfile = new VFile({
37
- value: frontmatterLines + pageContent,
34
+ value: content,
38
35
  path: id,
39
36
  data: {
40
37
  astro: {
41
38
  frontmatter
39
+ },
40
+ applyFrontmatterExport: {
41
+ srcDir: opts.srcDir
42
42
  }
43
43
  }
44
44
  });
45
45
  if (!processor) {
46
- return this.error(
47
- "MDX processor is not initialized. This is an internal error. Please file an issue."
48
- );
46
+ processor = createMdxProcessor(opts.mdxOptions, { sourcemap: sourcemapEnabled });
49
47
  }
50
48
  try {
51
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.1",
4
+ "version": "4.0.0-beta.3",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",
7
7
  "author": "withastro",
@@ -28,12 +28,11 @@
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
- "gray-matter": "^4.0.3",
36
- "hast-util-to-html": "^9.0.2",
35
+ "hast-util-to-html": "^9.0.3",
37
36
  "kleur": "^4.1.5",
38
37
  "rehype-raw": "^7.0.0",
39
38
  "remark-gfm": "^4.0.0",
@@ -41,30 +40,30 @@
41
40
  "source-map": "^0.7.4",
42
41
  "unist-util-visit": "^5.0.0",
43
42
  "vfile": "^6.0.3",
44
- "@astrojs/markdown-remark": "6.0.0-beta.1"
43
+ "@astrojs/markdown-remark": "6.0.0-beta.2"
45
44
  },
46
45
  "peerDependencies": {
47
46
  "astro": "^5.0.0-alpha.0"
48
47
  },
49
48
  "devDependencies": {
50
- "@types/estree": "^1.0.5",
49
+ "@types/estree": "^1.0.6",
51
50
  "@types/hast": "^3.0.4",
52
51
  "@types/mdast": "^4.0.4",
53
52
  "cheerio": "1.0.0",
54
- "linkedom": "^0.18.4",
53
+ "linkedom": "^0.18.5",
55
54
  "mdast-util-mdx": "^3.0.0",
56
55
  "mdast-util-mdx-jsx": "^3.1.3",
57
56
  "mdast-util-to-string": "^4.0.0",
58
57
  "rehype-mathjax": "^6.0.0",
59
58
  "rehype-pretty-code": "^0.14.0",
60
59
  "remark-math": "^6.0.0",
61
- "remark-rehype": "^11.1.0",
60
+ "remark-rehype": "^11.1.1",
62
61
  "remark-shiki-twoslash": "^3.1.3",
63
62
  "remark-toc": "^9.0.0",
64
- "shiki": "^1.16.2",
63
+ "shiki": "^1.22.2",
65
64
  "unified": "^11.0.5",
66
- "vite": "^5.4.3",
67
- "astro": "5.0.0-beta.1",
65
+ "vite": "6.0.0-beta.2",
66
+ "astro": "5.0.0-beta.6",
68
67
  "astro-scripts": "0.0.14"
69
68
  },
70
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
  }