@astrojs/mdx 4.0.0-alpha.2 → 4.0.0-beta.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/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() {
@@ -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(
@@ -19,7 +19,10 @@ const rehypeOptimizeStatic = (options) => {
19
19
  const elementStack = [];
20
20
  const elementMetadatas = /* @__PURE__ */ new WeakMap();
21
21
  const isNodeNonStatic = (node) => {
22
- return node.type.startsWith("mdx") || ignoreElementNames.has(node.tagName);
22
+ return node.type.startsWith("mdx") || // @ts-expect-error `node` should never have `type: 'root'`, but in some cases plugins may inject it as children,
23
+ // which MDX will render as a fragment instead (an MDX fragment is a `mdxJsxFlowElement` type).
24
+ node.type === "root" || // @ts-expect-error Access `.tagName` naively for perf
25
+ ignoreElementNames.has(node.tagName);
23
26
  };
24
27
  visit(tree, {
25
28
  // @ts-expect-error Force coerce node as hast node
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,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";
4
+ import { safeParseFrontmatter } from "./utils.js";
5
5
  function vitePluginMdx(mdxOptions) {
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,10 +29,9 @@ 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: {
@@ -43,9 +40,7 @@ function vitePluginMdx(mdxOptions) {
43
40
  }
44
41
  });
45
42
  if (!processor) {
46
- return this.error(
47
- "MDX processor is not initialized. This is an internal error. Please file an issue."
48
- );
43
+ processor = createMdxProcessor(mdxOptions, { sourcemap: sourcemapEnabled });
49
44
  }
50
45
  try {
51
46
  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-alpha.2",
4
+ "version": "4.0.0-beta.2",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",
7
7
  "author": "withastro",
@@ -32,8 +32,7 @@
32
32
  "acorn": "^8.12.1",
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-alpha.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.1",
63
+ "shiki": "^1.21.0",
65
64
  "unified": "^11.0.5",
66
- "vite": "^5.4.2",
67
- "astro": "5.0.0-alpha.3",
65
+ "vite": "^5.4.8",
66
+ "astro": "5.0.0-beta.3",
68
67
  "astro-scripts": "0.0.14"
69
68
  },
70
69
  "engines": {