@astrojs/mdx 0.19.7 → 1.0.0-beta.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/README.md CHANGED
@@ -42,9 +42,8 @@ npm install @astrojs/mdx
42
42
 
43
43
  Then, apply this integration to your `astro.config.*` file using the `integrations` property:
44
44
 
45
- __`astro.config.mjs`__
46
-
47
- ```js ins={2} "mdx()"
45
+ ```js ins={3} "mdx()"
46
+ // astro.config.mjs
48
47
  import { defineConfig } from 'astro/config';
49
48
  import mdx from '@astrojs/mdx';
50
49
 
@@ -59,14 +58,16 @@ export default defineConfig({
59
58
  [VS Code](https://code.visualstudio.com/) supports Markdown by default. However, for MDX editor support, you may wish to add the following setting in your VSCode config. This ensures authoring MDX files provides a Markdown-like editor experience.
60
59
 
61
60
  ```json title=".vscode/settings.json"
62
- "files.associations": {
61
+ {
62
+ "files.associations": {
63
63
  "*.mdx": "markdown"
64
+ }
64
65
  }
65
66
  ```
66
67
 
67
68
  ## Usage
68
69
 
69
- With the Astro MDX integration, you can [add MDX pages to your project](https://docs.astro.build/en/guides/markdown-content/#markdown-and-mdx-pages) by adding `.mdx` files within your `src/pages/` directory. You can also [import `.mdx` files](https://docs.astro.build/en/guides/markdown-content/#importing-markdown) into `.astro` files.
70
+ With the Astro MDX integration, you can [add MDX pages to your project](https://docs.astro.build/en/guides/markdown-content/#markdown-and-mdx-pages) by adding `.mdx` files within your `src/pages/` directory. You can also [import `.mdx` files](https://docs.astro.build/en/guides/markdown-content/#importing-markdown) into `.astro` files.
70
71
 
71
72
  Astro's MDX integration adds extra features to standard MDX, including Markdown-style frontmatter. This allows you to use most of Astro's built-in Markdown features like a [special frontmatter `layout` property](https://docs.astro.build/en/guides/markdown-content/#frontmatter-layout) and a [property for marking a page as a draft](https://docs.astro.build/en/guides/markdown-content/#draft-pages).
72
73
 
@@ -93,9 +94,8 @@ All [`markdown` configuration options](https://docs.astro.build/en/reference/con
93
94
  There is no separate MDX configuration for [including pages marked as draft in the build](https://docs.astro.build/en/reference/configuration-reference/#markdowndrafts). This Markdown setting will be respected by both Markdown and MDX files and cannot be overridden for MDX files specifically.
94
95
  :::
95
96
 
96
- __`astro.config.mjs`__
97
-
98
97
  ```js
98
+ // astro.config.mjs
99
99
  import { defineConfig } from 'astro/config';
100
100
  import mdx from '@astrojs/mdx';
101
101
  import remarkToc from 'remark-toc';
@@ -110,9 +110,9 @@ export default defineConfig({
110
110
  rehypePlugins: [rehypeMinifyHtml],
111
111
  remarkRehype: { footnoteLabel: 'Footnotes' },
112
112
  gfm: false,
113
- })
114
- ]
115
- })
113
+ }),
114
+ ],
115
+ });
116
116
  ```
117
117
 
118
118
  :::caution
@@ -130,9 +130,8 @@ MDX will extend [your project's existing Markdown configuration](https://docs.as
130
130
 
131
131
  For example, say you need to disable GitHub-Flavored Markdown and apply a different set of remark plugins for MDX files. You can apply these options like so, with `extendMarkdownConfig` enabled by default:
132
132
 
133
- __`astro.config.mjs`__
134
-
135
133
  ```js
134
+ // astro.config.mjs
136
135
  import { defineConfig } from 'astro/config';
137
136
  import mdx from '@astrojs/mdx';
138
137
 
@@ -151,16 +150,15 @@ export default defineConfig({
151
150
  remarkPlugins: [remarkPlugin2],
152
151
  // `gfm` overridden to `false`
153
152
  gfm: false,
154
- })
155
- ]
153
+ }),
154
+ ],
156
155
  });
157
156
  ```
158
157
 
159
158
  You may also need to disable `markdown` config extension in MDX. For this, set `extendMarkdownConfig` to `false`:
160
159
 
161
- __`astro.config.mjs`__
162
-
163
160
  ```js
161
+ // astro.config.mjs
164
162
  import { defineConfig } from 'astro/config';
165
163
  import mdx from '@astrojs/mdx';
166
164
 
@@ -173,8 +171,8 @@ export default defineConfig({
173
171
  // Markdown config now ignored
174
172
  extendMarkdownConfig: false,
175
173
  // No `remarkPlugins` applied
176
- })
177
- ]
174
+ }),
175
+ ],
178
176
  });
179
177
  ```
180
178
 
@@ -192,9 +190,8 @@ This is an optional configuration setting to optimize the MDX output for faster
192
190
 
193
191
  This is disabled by default. To enable MDX optimization, add the following to your MDX integration configuration:
194
192
 
195
- __`astro.config.mjs`__
196
-
197
193
  ```js
194
+ // astro.config.mjs
198
195
  import { defineConfig } from 'astro/config';
199
196
  import mdx from '@astrojs/mdx';
200
197
 
@@ -202,8 +199,8 @@ export default defineConfig({
202
199
  integrations: [
203
200
  mdx({
204
201
  optimize: true,
205
- })
206
- ]
202
+ }),
203
+ ],
207
204
  });
208
205
  ```
209
206
 
@@ -213,7 +210,7 @@ export default defineConfig({
213
210
 
214
211
  An optional property of `optimize` to prevent the MDX optimizer from handling any [custom components passed to imported MDX content via the components prop](https://docs.astro.build/en/guides/markdown-content/#custom-components-with-imported-mdx).
215
212
 
216
- You will need to exclude these components from optimization as the optimizer eagerly converts content into a static string, which will break custom components that needs to be dynamically rendered.
213
+ You will need to exclude these components from optimization as the optimizer eagerly converts content into a static string, which will break custom components that needs to be dynamically rendered.
217
214
 
218
215
  For example, the intended MDX output of the following is `<Heading>...</Heading>` in place of every `"<h1>...</h1>"`:
219
216
 
@@ -223,14 +220,13 @@ import { Content, components } from '../content.mdx';
223
220
  import Heading from '../Heading.astro';
224
221
  ---
225
222
 
226
- <Content components={{...components, h1: Heading }} />
223
+ <Content components={{ ...components, h1: Heading }} />
227
224
  ```
228
225
 
229
226
  To configure optimization for this using the `customComponentNames` property, specify an array of HTML element names that should be treated as custom components:
230
227
 
231
- __`astro.config.mjs`__
232
-
233
228
  ```js
229
+ // astro.config.mjs
234
230
  import { defineConfig } from 'astro/config';
235
231
  import mdx from '@astrojs/mdx';
236
232
 
@@ -242,8 +238,8 @@ export default defineConfig({
242
238
  // These will be treated as custom components
243
239
  customComponentNames: ['h1'],
244
240
  },
245
- })
246
- ]
241
+ }),
242
+ ],
247
243
  });
248
244
  ```
249
245
 
@@ -251,7 +247,7 @@ Note that if your MDX file [configures custom components using `export const com
251
247
 
252
248
  ## Examples
253
249
 
254
- * The [Astro MDX starter template](https://github.com/withastro/astro/tree/latest/examples/with-mdx) shows how to use MDX files in your Astro project.
250
+ - The [Astro MDX starter template](https://github.com/withastro/astro/tree/latest/examples/with-mdx) shows how to use MDX files in your Astro project.
255
251
 
256
252
  ## Troubleshooting
257
253
 
@@ -268,5 +264,4 @@ This package is maintained by Astro's Core team. You're welcome to submit an iss
268
264
  See [CHANGELOG.md](https://github.com/withastro/astro/tree/main/packages/integrations/mdx/CHANGELOG.md) for a history of changes to this integration.
269
265
 
270
266
  [astro-integration]: https://docs.astro.build/en/guides/integrations-guide/
271
-
272
267
  [astro-ui-frameworks]: https://docs.astro.build/en/core-concepts/framework-components/#using-framework-components
package/dist/index.js CHANGED
@@ -63,11 +63,21 @@ function mdx(partialMdxOptions = {}) {
63
63
  enforce: "pre",
64
64
  configResolved(resolved) {
65
65
  importMetaEnv = { ...importMetaEnv, ...resolved.env };
66
+ const jsxPluginIndex = resolved.plugins.findIndex((p) => p.name === "astro:jsx");
67
+ if (jsxPluginIndex !== -1) {
68
+ const myPluginIndex = resolved.plugins.findIndex(
69
+ (p) => p.name === "@mdx-js/rollup"
70
+ );
71
+ if (myPluginIndex !== -1) {
72
+ const myPlugin = resolved.plugins[myPluginIndex];
73
+ resolved.plugins.splice(myPluginIndex, 1);
74
+ resolved.plugins.splice(jsxPluginIndex, 0, myPlugin);
75
+ }
76
+ }
66
77
  },
67
78
  // Override transform to alter code before MDX compilation
68
79
  // ex. inject layouts
69
80
  async transform(_, id) {
70
- var _a;
71
81
  if (!id.endsWith(".mdx"))
72
82
  return;
73
83
  const { fileId } = getFileInfo(id, config);
@@ -85,7 +95,7 @@ function mdx(partialMdxOptions = {}) {
85
95
  ...mdxPluginOpts.recmaPlugins ?? [],
86
96
  () => recmaInjectImportMetaEnvPlugin({ importMetaEnv })
87
97
  ],
88
- SourceMapGenerator: ((_a = config.vite.build) == null ? void 0 : _a.sourcemap) ? SourceMapGenerator : void 0
98
+ SourceMapGenerator: config.vite.build?.sourcemap ? SourceMapGenerator : void 0
89
99
  });
90
100
  return {
91
101
  code: escapeViteEnvReferences(String(compiled.value)),
@@ -117,15 +127,18 @@ export const url = ${JSON.stringify(fileUrl)};`;
117
127
  export const file = ${JSON.stringify(fileId)};`;
118
128
  }
119
129
  if (!moduleExports.find(({ n }) => n === "Content")) {
130
+ const hasComponents = moduleExports.find(({ n }) => n === "components");
120
131
  code = code.replace("export default MDXContent;", "");
121
132
  code += `
122
133
  export const Content = (props = {}) => MDXContent({
123
134
  ...props,
124
- components: { Fragment, ...props.components },
135
+ components: { Fragment${hasComponents ? ", ...components" : ""}, ...props.components },
125
136
  });
126
137
  export default Content;`;
127
138
  }
128
139
  code += `
140
+ Content[Symbol.for('mdx-component')] = true`;
141
+ code += `
129
142
  Content[Symbol.for('astro.needsHeadRendering')] = !Boolean(frontmatter.layout);`;
130
143
  code += `
131
144
  Content.moduleId = ${JSON.stringify(id)};`;
@@ -2,8 +2,7 @@ import { visit } from "unist-util-visit";
2
2
  function rehypeMetaString() {
3
3
  return function(tree) {
4
4
  visit(tree, (node) => {
5
- var _a;
6
- if (node.type === "element" && node.tagName === "code" && ((_a = node.data) == null ? void 0 : _a.meta)) {
5
+ if (node.type === "element" && node.tagName === "code" && node.data?.meta) {
7
6
  node.properties ??= {};
8
7
  node.properties.metastring = node.data.meta;
9
8
  }
@@ -3,14 +3,13 @@ import { toHtml } from "hast-util-to-html";
3
3
  const exportConstComponentsRe = /export\s+const\s+components\s*=/;
4
4
  function rehypeOptimizeStatic(options) {
5
5
  return (tree) => {
6
- var _a, _b, _c, _d, _e, _f;
7
- const customComponentNames = new Set(options == null ? void 0 : options.customComponentNames);
6
+ const customComponentNames = new Set(options?.customComponentNames);
8
7
  for (const child of tree.children) {
9
8
  if (child.type === "mdxjsEsm" && exportConstComponentsRe.test(child.value)) {
10
- const objectPropertyNodes = (_d = (_c = (_b = (_a = child.data.estree.body[0]) == null ? void 0 : _a.declarations) == null ? void 0 : _b[0]) == null ? void 0 : _c.init) == null ? void 0 : _d.properties;
9
+ const objectPropertyNodes = child.data.estree.body[0]?.declarations?.[0]?.init?.properties;
11
10
  if (objectPropertyNodes) {
12
11
  for (const objectPropertyNode of objectPropertyNodes) {
13
- const componentName = ((_e = objectPropertyNode.key) == null ? void 0 : _e.name) ?? ((_f = objectPropertyNode.key) == null ? void 0 : _f.value);
12
+ const componentName = objectPropertyNode.key?.name ?? objectPropertyNode.key?.value;
14
13
  if (componentName) {
15
14
  customComponentNames.add(componentName);
16
15
  }
@@ -7,8 +7,7 @@ function remarkImageToComponent() {
7
7
  const importsStatements = [];
8
8
  const importedImages = /* @__PURE__ */ new Map();
9
9
  visit(tree, "image", (node, index, parent) => {
10
- var _a;
11
- if ((_a = file.data.imagePaths) == null ? void 0 : _a.has(node.url)) {
10
+ if (file.data.imagePaths?.has(node.url)) {
12
11
  let importName = importedImages.get(node.url);
13
12
  if (!importName) {
14
13
  importName = `__${importedImages.size}_${node.url.replace(/\W/g, "_")}__`;
package/dist/utils.js CHANGED
@@ -18,7 +18,7 @@ function getFileInfo(id, config) {
18
18
  const isPage = fileId.includes("/pages/");
19
19
  if (isPage) {
20
20
  fileUrl = fileId.replace(/^.*?\/pages\//, sitePathname).replace(/(\/index)?\.mdx$/, "");
21
- } else if (url && url.pathname.startsWith(config.root.pathname)) {
21
+ } else if (url?.pathname.startsWith(config.root.pathname)) {
22
22
  fileUrl = url.pathname.slice(config.root.pathname.length);
23
23
  } else {
24
24
  fileUrl = fileId;
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": "0.19.7",
4
+ "version": "1.0.0-beta.0",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",
7
7
  "author": "withastro",
@@ -27,13 +27,11 @@
27
27
  "template"
28
28
  ],
29
29
  "dependencies": {
30
- "@astrojs/markdown-remark": "^2.2.1",
31
- "@astrojs/prism": "^2.1.2",
32
30
  "@mdx-js/mdx": "^2.3.0",
33
- "acorn": "^8.8.0",
34
- "es-module-lexer": "^1.1.1",
35
- "estree-util-visit": "^1.2.0",
36
- "github-slugger": "^1.4.0",
31
+ "acorn": "^8.9.0",
32
+ "es-module-lexer": "^1.3.0",
33
+ "estree-util-visit": "^1.2.1",
34
+ "github-slugger": "^1.5.0",
37
35
  "gray-matter": "^4.0.3",
38
36
  "hast-util-to-html": "^8.0.4",
39
37
  "kleur": "^4.1.4",
@@ -43,35 +41,40 @@
43
41
  "remark-smartypants": "^2.0.0",
44
42
  "shiki": "^0.14.1",
45
43
  "source-map": "^0.7.4",
46
- "unist-util-visit": "^4.1.0",
47
- "vfile": "^5.3.2"
44
+ "unist-util-visit": "^4.1.2",
45
+ "vfile": "^5.3.7",
46
+ "@astrojs/markdown-remark": "3.0.0-beta.0",
47
+ "@astrojs/prism": "3.0.0-beta.0"
48
+ },
49
+ "peerDependencies": {
50
+ "astro": "^3.0.0-beta.0"
48
51
  },
49
52
  "devDependencies": {
50
- "@types/chai": "^4.3.1",
51
- "@types/estree": "^1.0.0",
53
+ "@types/chai": "^4.3.5",
54
+ "@types/estree": "^1.0.1",
52
55
  "@types/github-slugger": "^1.3.0",
53
- "@types/mdast": "^3.0.10",
56
+ "@types/mdast": "^3.0.11",
54
57
  "@types/mocha": "^9.1.1",
55
58
  "@types/yargs-parser": "^21.0.0",
56
- "chai": "^4.3.6",
57
- "cheerio": "^1.0.0-rc.11",
58
- "linkedom": "^0.14.12",
59
- "mdast-util-mdx": "^2.0.0",
60
- "mdast-util-to-string": "^3.1.0",
59
+ "chai": "^4.3.7",
60
+ "cheerio": "1.0.0-rc.12",
61
+ "linkedom": "^0.14.26",
62
+ "mdast-util-mdx": "^2.0.1",
63
+ "mdast-util-to-string": "^3.2.0",
61
64
  "mocha": "^9.2.2",
62
65
  "reading-time": "^1.5.0",
63
66
  "rehype-mathjax": "^4.0.2",
64
67
  "rehype-pretty-code": "^0.4.0",
65
68
  "remark-math": "^5.1.1",
66
69
  "remark-rehype": "^10.1.0",
67
- "remark-shiki-twoslash": "^3.1.0",
70
+ "remark-shiki-twoslash": "^3.1.3",
68
71
  "remark-toc": "^8.0.1",
69
- "vite": "^4.3.1",
70
- "astro": "2.6.1",
72
+ "vite": "^4.4.6",
73
+ "astro": "3.0.0-beta.0",
71
74
  "astro-scripts": "0.0.14"
72
75
  },
73
76
  "engines": {
74
- "node": ">=16.12.0"
77
+ "node": ">=18.14.1"
75
78
  },
76
79
  "scripts": {
77
80
  "build": "astro-scripts build \"src/**/*.ts\" && tsc",