@astrojs/mdx 0.11.3 → 0.11.5

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.
@@ -1,5 +1,5 @@
1
- @astrojs/mdx:build: cache hit, replaying output 64be6583f3d92349
2
- @astrojs/mdx:build: 
3
- @astrojs/mdx:build: > @astrojs/mdx@0.11.3 build /home/runner/work/astro/astro/packages/integrations/mdx
4
- @astrojs/mdx:build: > astro-scripts build "src/**/*.ts" && tsc
5
- @astrojs/mdx:build: 
1
+ @astrojs/mdx:build: cache hit, replaying output 2b17bb94a2cdc3f6
2
+ @astrojs/mdx:build: 
3
+ @astrojs/mdx:build: > @astrojs/mdx@0.11.5 build /home/runner/work/astro/astro/packages/integrations/mdx
4
+ @astrojs/mdx:build: > astro-scripts build "src/**/*.ts" && tsc
5
+ @astrojs/mdx:build: 
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @astrojs/mdx
2
2
 
3
+ ## 0.11.5
4
+
5
+ ### Patch Changes
6
+
7
+ - [#5146](https://github.com/withastro/astro/pull/5146) [`308e565ad`](https://github.com/withastro/astro/commit/308e565ad39957e3353d72ca5d3bbce1a1b45008) Thanks [@bholmesdev](https://github.com/bholmesdev)! - Support recmaPlugins config option
8
+
9
+ ## 0.11.4
10
+
11
+ ### Patch Changes
12
+
13
+ - [#4953](https://github.com/withastro/astro/pull/4953) [`a59731995`](https://github.com/withastro/astro/commit/a59731995b93ae69c21dc3adc5c8b482b466d12e) Thanks [@bluwy](https://github.com/bluwy)! - Log markdown hints with console.info
14
+
3
15
  ## 0.11.3
4
16
 
5
17
  ### Patch Changes
package/README.md CHANGED
@@ -71,11 +71,12 @@ Finally, restart the dev server.
71
71
 
72
72
  ## Usage
73
73
 
74
- 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.
74
+ 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.
75
75
 
76
76
  ### Components
77
77
 
78
78
  To use components in your MDX pages in Astro, head to our [UI framework documentation][astro-ui-frameworks]. You'll explore:
79
+
79
80
  - 📦 how framework components are loaded,
80
81
  - 💧 client-side hydration options, and
81
82
  - 🤝 opportunities to mix and nest frameworks together
@@ -160,7 +161,7 @@ const posts = await Astro.glob('./*.mdx');
160
161
 
161
162
  ### Inject frontmatter via remark or rehype plugins
162
163
 
163
- You may want to inject frontmatter properties across all of your MDX files. By using a [remark](#remarkPlugins) or [rehype](#remarkplugins) plugin, you can generate these properties based on a file’s contents.
164
+ You may want to inject frontmatter properties across all of your MDX files. By using a [remark](#remarkplugins) or [rehype](#rehypeplugins) plugin, you can generate these properties based on a file’s contents.
164
165
 
165
166
  You can append to the `data.astro.frontmatter` property from your plugin’s `file` argument like so:
166
167
 
@@ -260,6 +261,7 @@ const { frontmatter, url } = Astro.props;
260
261
  #### Layout props
261
262
 
262
263
  All [exported properties](#exported-properties) are available from `Astro.props` in your layout, **with two key differences:**
264
+
263
265
  - Heading information (i.e. `h1 -> h6` elements) is available via the `headings` array, rather than a `getHeadings()` function.
264
266
  - `file` and `url` are _also_ available as nested `frontmatter` properties (i.e. `frontmatter.url` and `frontmatter.file`). This is consistent with Astro's Markdown layout properties.
265
267
 
@@ -286,6 +288,7 @@ function fancyJsHelper() {
286
288
  Welcome to my new Astro blog, using MDX!
287
289
  </BaseLayout>
288
290
  ```
291
+
289
292
  Then, your values are available to you through `Astro.props` in your layout, and your MDX content will be injected into the page where your `<slot />` component is written:
290
293
 
291
294
  ```astro
@@ -319,13 +322,17 @@ will be converted into this HTML:
319
322
  But what if you want to specify your own markup for these blockquotes? In the above example, you could create a custom `<Blockquote />` component (in any language) that either has a `<slot />` component or accepts a `children` prop.
320
323
 
321
324
  ```astro title="src/components/Blockquote.astro"
322
- <blockquote class="bg-blue-50 p-4">
325
+ ---
326
+ const props = Astro.props;
327
+ ---
328
+
329
+ <blockquote {...props} class="bg-blue-50 p-4">
323
330
  <span class="text-4xl text-blue-600 mb-2">“</span>
324
331
  <slot />
325
332
  </blockquote>
326
333
  ```
327
334
 
328
- Then in the MDX file you import the component and export it to the `components` export.
335
+ Then in the MDX file you import the component and export it to the `components` export.
329
336
 
330
337
  ```mdx title="src/pages/posts/post-1.mdx" {2}
331
338
  import Blockquote from '../components/Blockquote.astro';
@@ -334,18 +341,19 @@ export const components = { blockquote: Blockquote };
334
341
 
335
342
  Now, writing the standard Markdown blockquote syntax (`>`) will use your custom `<Blockquote />` component instead. No need to use a component in Markdown, or write a remark/rehype plugin! Visit the [MDX website](https://mdxjs.com/table-of-components/) for a full list of HTML elements that can be overwritten as custom components.
336
343
 
337
-
338
344
  #### Custom components with imported `mdx`
339
345
 
340
- When rendering imported MDX content, custom components can also be passed via the `components` prop:
346
+ When rendering imported MDX content, custom components can be passed via the `components` prop.
347
+
348
+ Note: An MDX file's exported components will _not_ be used unless you manually import and pass them via the `components` property. See the example below:
341
349
 
342
- ```astro title="src/pages/page.astro" "components={{ h1: Heading }}"
350
+ ```astro title="src/pages/page.astro" "components={{...components, h1: Heading }}"
343
351
  ---
344
- import Content from '../content.mdx';
352
+ import { Content, components } from '../content.mdx';
345
353
  import Heading from '../Heading.astro';
346
354
  ---
347
355
 
348
- <Content components={{ h1: Heading }} />
356
+ <Content components={{...components, h1: Heading }} />
349
357
  ```
350
358
 
351
359
  ### Syntax highlighting
@@ -407,7 +415,7 @@ export default {
407
415
 
408
416
  [Remark plugins](https://github.com/remarkjs/remark/blob/main/doc/plugins.md) allow you to extend your Markdown with new capabilities. This includes [auto-generating a table of contents](https://github.com/remarkjs/remark-toc), [applying accessible emoji labels](https://github.com/florianeckerstorfer/remark-a11y-emoji), and more. We encourage you to browse [awesome-remark](https://github.com/remarkjs/awesome-remark) for a full curated list!
409
417
 
410
- This example applies the [`remark-toc`](https://github.com/remarkjs/remark-toc) plugin to `.mdx` files. To customize plugin inheritance from your Markdown config or Astro's defaults, [see the `extendPlugins` option](#extendPlugins).
418
+ This example applies the [`remark-toc`](https://github.com/remarkjs/remark-toc) plugin to `.mdx` files. To customize plugin inheritance from your Markdown config or Astro's defaults, [see the `extendPlugins` option](#extendplugins).
411
419
 
412
420
  ```js
413
421
  // astro.config.mjs
@@ -426,7 +434,7 @@ export default {
426
434
 
427
435
  We apply our own (non-removable) [`collect-headings`](https://github.com/withastro/astro/blob/main/packages/integrations/mdx/src/rehype-collect-headings.ts) plugin. This applies IDs to all headings (i.e. `h1 -> h6`) in your MDX files to [link to headings via anchor tags](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#linking_to_an_element_on_the_same_page).
428
436
 
429
- This example applies the [`rehype-minify`](https://github.com/rehypejs/rehype-minify) plugin to `.mdx` files. To customize plugin inheritance from your Markdown config or Astro's defaults, [see the `extendPlugins` option](#extendPlugins).
437
+ This example applies the [`rehype-minify`](https://github.com/rehypejs/rehype-minify) plugin to `.mdx` files. To customize plugin inheritance from your Markdown config or Astro's defaults, [see the `extendPlugins` option](#extendplugins).
430
438
 
431
439
  ```js
432
440
  // astro.config.mjs
@@ -447,7 +455,7 @@ export default {
447
455
 
448
456
  #### `markdown` (default)
449
457
 
450
- By default, Astro inherits all [remark](#remarkPlugins) and [rehype](#rehypePlugins) plugins from [the `markdown` option in your Astro config](https://docs.astro.build/en/guides/markdown-content/#markdown-plugins). This also respects the [`markdown.extendDefaultPlugins`](https://docs.astro.build/en/reference/configuration-reference/#markdownextenddefaultplugins) option to extend Astro's defaults. Any additional plugins you apply in your MDX config will be applied _after_ your configured Markdown plugins.
458
+ By default, Astro inherits all [remark](#remarkplugins) and [rehype](#rehypeplugins) plugins from [the `markdown` option in your Astro config](https://docs.astro.build/en/guides/markdown-content/#markdown-plugins). This also respects the [`markdown.extendDefaultPlugins`](https://docs.astro.build/en/reference/configuration-reference/#markdownextenddefaultplugins) option to extend Astro's defaults. Any additional plugins you apply in your MDX config will be applied _after_ your configured Markdown plugins.
451
459
 
452
460
  This example applies [`remark-toc`](https://github.com/remarkjs/remark-toc) to Markdown _and_ MDX, and [`rehype-minify`](https://github.com/rehypejs/rehype-minify) to MDX alone:
453
461
 
@@ -505,6 +513,12 @@ export default {
505
513
  }
506
514
  ```
507
515
 
516
+ ### recmaPlugins
517
+
518
+ These are plugins that modify the output [estree](https://github.com/estree/estree) directly. This is useful for modifying or injecting JavaScript variables in your MDX files.
519
+
520
+ We suggest [using AST Explorer](https://astexplorer.net/) to play with estree outputs, and trying [`estree-util-visit`](https://unifiedjs.com/explore/package/estree-util-visit/) for searching across JavaScript nodes.
521
+
508
522
  ## Examples
509
523
 
510
524
  - The [Astro MDX example](https://github.com/withastro/astro/tree/latest/examples/with-mdx) shows how to use MDX files in your Astro project.
package/dist/index.d.ts CHANGED
@@ -3,6 +3,7 @@ import type { AstroIntegration } from 'astro';
3
3
  export declare type MdxOptions = {
4
4
  remarkPlugins?: PluggableList;
5
5
  rehypePlugins?: PluggableList;
6
+ recmaPlugins?: PluggableList;
6
7
  /**
7
8
  * Choose which remark and rehype plugins to inherit, if any.
8
9
  *
package/dist/index.js CHANGED
@@ -24,19 +24,20 @@ function mdx(mdxOptions = {}) {
24
24
  handleExtendsNotSupported(mdxOptions.remarkPlugins);
25
25
  handleExtendsNotSupported(mdxOptions.rehypePlugins);
26
26
  if (mdxOptions.extendPlugins === "markdown" && (((_a = config.markdown.rehypePlugins) == null ? void 0 : _a.length) || ((_b = config.markdown.remarkPlugins) == null ? void 0 : _b.length))) {
27
- console.log(
27
+ console.info(
28
28
  blue(`[MDX] Now inheriting remark and rehype plugins from "markdown" config.`)
29
29
  );
30
- console.log(
30
+ console.info(
31
31
  `If you applied a plugin to both your Markdown and MDX configs, we suggest ${bold(
32
32
  "removing the duplicate MDX entry."
33
33
  )}`
34
34
  );
35
- console.log(`See "extendPlugins" option to configure this behavior.`);
35
+ console.info(`See "extendPlugins" option to configure this behavior.`);
36
36
  }
37
37
  const mdxPluginOpts = {
38
38
  remarkPlugins: await getRemarkPlugins(mdxOptions, config),
39
39
  rehypePlugins: getRehypePlugins(mdxOptions, config),
40
+ recmaPlugins: mdxOptions.recmaPlugins,
40
41
  jsx: true,
41
42
  jsxImportSource: "astro",
42
43
  format: "mdx",
@@ -66,7 +67,10 @@ function mdx(mdxOptions = {}) {
66
67
  ...mdxPluginOpts.rehypePlugins ?? [],
67
68
  () => rehypeApplyFrontmatterExport(frontmatter)
68
69
  ],
69
- recmaPlugins: [() => recmaInjectImportMetaEnvPlugin({ importMetaEnv })]
70
+ recmaPlugins: [
71
+ ...mdxPluginOpts.recmaPlugins ?? [],
72
+ () => recmaInjectImportMetaEnvPlugin({ importMetaEnv })
73
+ ]
70
74
  });
71
75
  return {
72
76
  code: escapeViteEnvReferences(String(compiled.value)),
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@astrojs/mdx",
3
3
  "description": "Use MDX within Astro",
4
- "version": "0.11.3",
4
+ "version": "0.11.5",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",
7
7
  "author": "withastro",
@@ -46,7 +46,7 @@
46
46
  "@types/github-slugger": "^1.3.0",
47
47
  "@types/mocha": "^9.1.1",
48
48
  "@types/yargs-parser": "^21.0.0",
49
- "astro": "1.4.0",
49
+ "astro": "1.5.3",
50
50
  "astro-scripts": "0.0.8",
51
51
  "chai": "^4.3.6",
52
52
  "cheerio": "^1.0.0-rc.11",
@@ -66,6 +66,7 @@
66
66
  "build": "astro-scripts build \"src/**/*.ts\" && tsc",
67
67
  "build:ci": "astro-scripts build \"src/**/*.ts\"",
68
68
  "dev": "astro-scripts dev \"src/**/*.ts\"",
69
- "test": "mocha --exit --timeout 20000"
69
+ "test": "mocha --exit --timeout 20000",
70
+ "test:match": "mocha --timeout 20000 -g"
70
71
  }
71
72
  }
package/src/index.ts CHANGED
@@ -24,6 +24,7 @@ const COMPILED_CONTENT_ERROR =
24
24
  export type MdxOptions = {
25
25
  remarkPlugins?: PluggableList;
26
26
  rehypePlugins?: PluggableList;
27
+ recmaPlugins?: PluggableList;
27
28
  /**
28
29
  * Choose which remark and rehype plugins to inherit, if any.
29
30
  *
@@ -50,20 +51,21 @@ export default function mdx(mdxOptions: MdxOptions = {}): AstroIntegration {
50
51
  mdxOptions.extendPlugins === 'markdown' &&
51
52
  (config.markdown.rehypePlugins?.length || config.markdown.remarkPlugins?.length)
52
53
  ) {
53
- console.log(
54
+ console.info(
54
55
  blue(`[MDX] Now inheriting remark and rehype plugins from "markdown" config.`)
55
56
  );
56
- console.log(
57
+ console.info(
57
58
  `If you applied a plugin to both your Markdown and MDX configs, we suggest ${bold(
58
59
  'removing the duplicate MDX entry.'
59
60
  )}`
60
61
  );
61
- console.log(`See "extendPlugins" option to configure this behavior.`);
62
+ console.info(`See "extendPlugins" option to configure this behavior.`);
62
63
  }
63
64
 
64
65
  const mdxPluginOpts: MdxRollupPluginOptions = {
65
66
  remarkPlugins: await getRemarkPlugins(mdxOptions, config),
66
67
  rehypePlugins: getRehypePlugins(mdxOptions, config),
68
+ recmaPlugins: mdxOptions.recmaPlugins,
67
69
  jsx: true,
68
70
  jsxImportSource: 'astro',
69
71
  // Note: disable `.md` support
@@ -100,7 +102,10 @@ export default function mdx(mdxOptions: MdxOptions = {}): AstroIntegration {
100
102
  ...(mdxPluginOpts.rehypePlugins ?? []),
101
103
  () => rehypeApplyFrontmatterExport(frontmatter),
102
104
  ],
103
- recmaPlugins: [() => recmaInjectImportMetaEnvPlugin({ importMetaEnv })],
105
+ recmaPlugins: [
106
+ ...(mdxPluginOpts.recmaPlugins ?? []),
107
+ () => recmaInjectImportMetaEnvPlugin({ importMetaEnv }),
108
+ ],
104
109
  });
105
110
 
106
111
  return {
@@ -0,0 +1,6 @@
1
+ import mdx from '@astrojs/mdx';
2
+ import preact from '@astrojs/preact';
3
+
4
+ export default {
5
+ integrations: [mdx(), preact()]
6
+ }
@@ -0,0 +1,17 @@
1
+ #!/bin/sh
2
+ basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
3
+
4
+ case `uname` in
5
+ *CYGWIN*) basedir=`cygpath -w "$basedir"`;;
6
+ esac
7
+
8
+ if [ -z "$NODE_PATH" ]; then
9
+ export NODE_PATH="/home/runner/work/astro/astro/node_modules/.pnpm/node_modules"
10
+ else
11
+ export NODE_PATH="$NODE_PATH:/home/runner/work/astro/astro/node_modules/.pnpm/node_modules"
12
+ fi
13
+ if [ -x "$basedir/node" ]; then
14
+ exec "$basedir/node" "$basedir/../../../../../../../astro/astro.js" "$@"
15
+ else
16
+ exec node "$basedir/../../../../../../../astro/astro.js" "$@"
17
+ fi
@@ -0,0 +1,10 @@
1
+ {
2
+ "name": "@test/mdx-infinite-loop",
3
+ "type": "module",
4
+ "dependencies": {
5
+ "@astrojs/mdx": "workspace:*",
6
+ "@astrojs/preact": "workspace:*",
7
+ "preact": "^10.7.3",
8
+ "astro": "workspace:*"
9
+ }
10
+ }
@@ -0,0 +1,3 @@
1
+ export default function () {
2
+ return 'Hello world'
3
+ }
@@ -0,0 +1,6 @@
1
+ import Test, { Missing } from '../components/Test';
2
+
3
+ # Hello page!
4
+
5
+ <Test />
6
+ <Missing />
@@ -0,0 +1,5 @@
1
+ ---
2
+ const files = await Astro.glob('./**/*.mdx')
3
+ ---
4
+
5
+ {files.map((file: any) => <file.Content />)}
@@ -1,3 +1,5 @@
1
+ export let recmaPluginWorking = false
2
+
1
3
  # TOC test
2
4
 
3
5
  ## Table of contents
@@ -17,3 +19,5 @@ Oh cool, more text!
17
19
  ## Section 2
18
20
 
19
21
  And section 2, with a hyperlink to check GFM is preserved: https://handle-me-gfm.com
22
+
23
+ <div data-recma-plugin-works={recmaPluginWorking}></div>
@@ -0,0 +1,30 @@
1
+ import mdx from '@astrojs/mdx';
2
+
3
+ import { expect } from 'chai';
4
+ import { loadFixture } from '../../../astro/test/test-utils.js';
5
+
6
+ describe('MDX Infinite Loop', () => {
7
+ let fixture;
8
+
9
+ before(async () => {
10
+ fixture = await loadFixture({
11
+ root: new URL('./fixtures/mdx-infinite-loop/', import.meta.url),
12
+ integrations: [mdx()],
13
+ });
14
+ });
15
+
16
+ describe('build', () => {
17
+ let err;
18
+ before(async () => {
19
+ try {
20
+ await fixture.build();
21
+ } catch (e) {
22
+ err = e;
23
+ }
24
+ });
25
+
26
+ it('does not hang forever if an error is thrown', async () => {
27
+ expect(!!err).to.be.true;
28
+ });
29
+ });
30
+ });
@@ -4,6 +4,7 @@ import { expect } from 'chai';
4
4
  import { parseHTML } from 'linkedom';
5
5
  import { loadFixture } from '../../../astro/test/test-utils.js';
6
6
  import remarkToc from 'remark-toc';
7
+ import { visit as estreeVisit } from 'estree-util-visit';
7
8
 
8
9
  const FIXTURE_ROOT = new URL('./fixtures/mdx-plugins/', import.meta.url);
9
10
  const FILE = '/with-plugins/index.html';
@@ -164,6 +165,21 @@ describe('MDX plugins', () => {
164
165
  expect(selectGfmLink(document)).to.be.null;
165
166
  expect(selectRemarkExample(document)).to.be.null;
166
167
  });
168
+
169
+ it('supports custom recma plugins', async () => {
170
+ const fixture = await buildFixture({
171
+ integrations: [
172
+ mdx({
173
+ recmaPlugins: [recmaExamplePlugin],
174
+ }),
175
+ ],
176
+ });
177
+
178
+ const html = await fixture.readFile(FILE);
179
+ const { document } = parseHTML(html);
180
+
181
+ expect(selectRecmaExample(document)).to.not.be.null;
182
+ });
167
183
  });
168
184
 
169
185
  async function buildFixture(config) {
@@ -194,6 +210,24 @@ function rehypeExamplePlugin() {
194
210
  };
195
211
  }
196
212
 
213
+ function recmaExamplePlugin() {
214
+ return (tree) => {
215
+ estreeVisit(tree, (node) => {
216
+ if (
217
+ node.type === 'VariableDeclarator' &&
218
+ node.id.name === 'recmaPluginWorking' &&
219
+ node.init?.type === 'Literal'
220
+ ) {
221
+ node.init = {
222
+ ...(node.init ?? {}),
223
+ value: true,
224
+ raw: 'true',
225
+ };
226
+ }
227
+ });
228
+ };
229
+ }
230
+
197
231
  function selectTocLink(document) {
198
232
  return document.querySelector('ul a[href="#section-1"]');
199
233
  }
@@ -209,3 +243,7 @@ function selectRemarkExample(document) {
209
243
  function selectRehypeExample(document) {
210
244
  return document.querySelector('div[data-rehype-plugin-works]');
211
245
  }
246
+
247
+ function selectRecmaExample(document) {
248
+ return document.querySelector('div[data-recma-plugin-works]');
249
+ }