@astrojs/mdx 0.11.4 → 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.
- package/.turbo/turbo-build.log +5 -5
- package/CHANGELOG.md +6 -0
- package/README.md +26 -12
- package/dist/index.d.ts +1 -0
- package/dist/index.js +5 -1
- package/package.json +4 -3
- package/src/index.ts +6 -1
- package/test/fixtures/mdx-plugins/src/pages/with-plugins.mdx +4 -0
- package/test/mdx-plugins.test.js +38 -0
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
[
|
|
2
|
-
[
|
|
3
|
-
[
|
|
4
|
-
[
|
|
5
|
-
[
|
|
1
|
+
[35m@astrojs/mdx:build: [0mcache hit, replaying output [2m2b17bb94a2cdc3f6[0m
|
|
2
|
+
[35m@astrojs/mdx:build: [0m
|
|
3
|
+
[35m@astrojs/mdx:build: [0m> @astrojs/mdx@0.11.5 build /home/runner/work/astro/astro/packages/integrations/mdx
|
|
4
|
+
[35m@astrojs/mdx:build: [0m> astro-scripts build "src/**/*.ts" && tsc
|
|
5
|
+
[35m@astrojs/mdx:build: [0m
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
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
|
+
|
|
3
9
|
## 0.11.4
|
|
4
10
|
|
|
5
11
|
### 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](#
|
|
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
|
-
|
|
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
|
|
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](#
|
|
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](#
|
|
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](#
|
|
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
package/dist/index.js
CHANGED
|
@@ -37,6 +37,7 @@ function mdx(mdxOptions = {}) {
|
|
|
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: [
|
|
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.
|
|
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.
|
|
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
|
*
|
|
@@ -64,6 +65,7 @@ export default function mdx(mdxOptions: MdxOptions = {}): AstroIntegration {
|
|
|
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: [
|
|
105
|
+
recmaPlugins: [
|
|
106
|
+
...(mdxPluginOpts.recmaPlugins ?? []),
|
|
107
|
+
() => recmaInjectImportMetaEnvPlugin({ importMetaEnv }),
|
|
108
|
+
],
|
|
104
109
|
});
|
|
105
110
|
|
|
106
111
|
return {
|
|
@@ -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>
|
package/test/mdx-plugins.test.js
CHANGED
|
@@ -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
|
+
}
|