@astrojs/mdx 0.15.0 → 0.15.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/.turbo/turbo-build.log +5 -5
- package/CHANGELOG.md +38 -25
- package/README.md +271 -35
- package/package.json +3 -3
- package/test/css-head-mdx.test.js +33 -0
- package/test/fixtures/css-head-mdx/src/components/HelloWorld.astro +11 -0
- package/test/fixtures/css-head-mdx/src/layouts/One.astro +15 -0
- package/test/fixtures/css-head-mdx/src/layouts/Three.astro +6 -0
- package/test/fixtures/css-head-mdx/src/layouts/Two.astro +6 -0
- package/test/fixtures/css-head-mdx/src/pages/indexOne.astro +10 -0
- package/test/fixtures/css-head-mdx/src/pages/indexThree.astro +10 -0
- package/test/fixtures/css-head-mdx/src/pages/indexTwo.astro +10 -0
- package/test/fixtures/css-head-mdx/src/pages/testOne.mdx +15 -0
- package/test/fixtures/css-head-mdx/src/pages/testThree.mdx +15 -0
- package/test/fixtures/css-head-mdx/src/pages/testTwo.mdx +15 -0
- package/test/fixtures/css-head-mdx/src/test.mdx +14 -0
- package/test/fixtures/mdx-get-headings/src/pages/test-with-frontmatter.mdx +45 -0
- package/test/mdx-get-headings.test.js +43 -0
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
[
|
|
2
|
-
[
|
|
3
|
-
[
|
|
4
|
-
[
|
|
5
|
-
[
|
|
1
|
+
[34m@astrojs/mdx:build: [0mcache hit, replaying output [2m4e171fa4df33399f[0m
|
|
2
|
+
[34m@astrojs/mdx:build: [0m
|
|
3
|
+
[34m@astrojs/mdx:build: [0m> @astrojs/mdx@0.15.2 build /home/runner/work/astro/astro/packages/integrations/mdx
|
|
4
|
+
[34m@astrojs/mdx:build: [0m> astro-scripts build "src/**/*.ts" && tsc
|
|
5
|
+
[34m@astrojs/mdx:build: [0m
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
# @astrojs/mdx
|
|
2
2
|
|
|
3
|
+
## 0.15.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#5478](https://github.com/withastro/astro/pull/5478) [`1c7eef308`](https://github.com/withastro/astro/commit/1c7eef308e808aa5ed4662b53e67ec8d1b814d1f) Thanks [@nemo0](https://github.com/nemo0)! - Update READMEs for consistency
|
|
8
|
+
|
|
9
|
+
## 0.15.1
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- [#5978](https://github.com/withastro/astro/pull/5978) [`7abb1e905`](https://github.com/withastro/astro/commit/7abb1e9056c4b4fd0abfced347df32a41cdfbf28) Thanks [@HiDeoo](https://github.com/HiDeoo)! - Fix MDX heading IDs generation when using a frontmatter reference
|
|
14
|
+
|
|
15
|
+
- Updated dependencies [[`7abb1e905`](https://github.com/withastro/astro/commit/7abb1e9056c4b4fd0abfced347df32a41cdfbf28)]:
|
|
16
|
+
- @astrojs/markdown-remark@2.0.1
|
|
17
|
+
|
|
3
18
|
## 0.15.0
|
|
4
19
|
|
|
5
20
|
### Minor Changes
|
|
@@ -8,46 +23,44 @@
|
|
|
8
23
|
|
|
9
24
|
- **Markdown**
|
|
10
25
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
26
|
+
- **Replace the `extendDefaultPlugins` option** with a `gfm` boolean and a `smartypants` boolean. These are enabled by default, and can be disabled to remove GitHub-Flavored Markdown and SmartyPants.
|
|
27
|
+
|
|
28
|
+
- Ensure GitHub-Flavored Markdown and SmartyPants are applied whether or not custom `remarkPlugins` or `rehypePlugins` are configured. If you want to apply custom plugins _and_ remove Astro's default plugins, manually set `gfm: false` and `smartypants: false` in your config.
|
|
14
29
|
|
|
15
30
|
- **Migrate `extendDefaultPlugins` to `gfm` and `smartypants`**
|
|
16
31
|
|
|
17
|
-
|
|
32
|
+
You may have disabled Astro's built-in plugins (GitHub-Flavored Markdown and Smartypants) with the `extendDefaultPlugins` option. This has now been split into 2 flags to disable each plugin individually:
|
|
18
33
|
|
|
19
|
-
|
|
20
|
-
|
|
34
|
+
- `markdown.gfm` to disable GitHub-Flavored Markdown
|
|
35
|
+
- `markdown.smartypants` to disable SmartyPants
|
|
21
36
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
37
|
+
```diff
|
|
38
|
+
// astro.config.mjs
|
|
39
|
+
import { defineConfig } from 'astro/config';
|
|
25
40
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
41
|
+
export default defineConfig({
|
|
42
|
+
markdown: {
|
|
43
|
+
- extendDefaultPlugins: false,
|
|
44
|
+
+ smartypants: false,
|
|
45
|
+
+ gfm: false,
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
```
|
|
34
49
|
|
|
35
|
-
|
|
50
|
+
Additionally, applying remark and rehype plugins **no longer disables** `gfm` and `smartypants`. You will need to opt-out manually by setting `gfm` and `smartypants` to `false`.
|
|
36
51
|
|
|
37
52
|
- **MDX**
|
|
38
53
|
|
|
39
|
-
|
|
54
|
+
- Support _all_ Markdown configuration options (except `drafts`) from your MDX integration config. This includes `syntaxHighlighting` and `shikiConfig` options to further customize the MDX renderer.
|
|
40
55
|
|
|
41
|
-
|
|
56
|
+
- Simplify `extendPlugins` to an `extendMarkdownConfig` option. MDX options will default to their equivalent in your Markdown config. By setting `extendMarkdownConfig` to false, you can "eject" to set your own syntax highlighting, plugins, and more.
|
|
42
57
|
|
|
43
58
|
- **Migrate MDX's `extendPlugins` to `extendMarkdownConfig`**
|
|
44
59
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
- `extendMarkdownConfig` (`true` by default) to toggle Markdown config inheritance. This replaces the `extendPlugins: 'markdown'` option.
|
|
48
|
-
- `gfm` (`true` by default) and `smartypants` (`true` by default) to toggle GitHub-Flavored Markdown and SmartyPants in MDX. This replaces the `extendPlugins: 'defaults'` option.
|
|
49
|
-
|
|
60
|
+
You may have used the `extendPlugins` option to manage plugin defaults in MDX. This has been replaced by 3 flags:
|
|
50
61
|
|
|
62
|
+
- `extendMarkdownConfig` (`true` by default) to toggle Markdown config inheritance. This replaces the `extendPlugins: 'markdown'` option.
|
|
63
|
+
- `gfm` (`true` by default) and `smartypants` (`true` by default) to toggle GitHub-Flavored Markdown and SmartyPants in MDX. This replaces the `extendPlugins: 'defaults'` option.
|
|
51
64
|
|
|
52
65
|
- [#5687](https://github.com/withastro/astro/pull/5687) [`e2019be6f`](https://github.com/withastro/astro/commit/e2019be6ffa46fa33d92cfd346f9ecbe51bb7144) Thanks [@bholmesdev](https://github.com/bholmesdev)! - Give remark and rehype plugins access to user frontmatter via frontmatter injection. This means `data.astro.frontmatter` is now the _complete_ Markdown or MDX document's frontmatter, rather than an empty object.
|
|
53
66
|
|
package/README.md
CHANGED
|
@@ -42,9 +42,9 @@ npm install @astrojs/mdx
|
|
|
42
42
|
|
|
43
43
|
Then, apply this integration to your `astro.config.*` file using the `integrations` property:
|
|
44
44
|
|
|
45
|
-
|
|
45
|
+
__`astro.config.mjs`__
|
|
46
46
|
|
|
47
|
-
```js
|
|
47
|
+
```js ins={2} "mdx()"
|
|
48
48
|
import { defineConfig } from 'astro/config';
|
|
49
49
|
import mdx from '@astrojs/mdx';
|
|
50
50
|
|
|
@@ -89,11 +89,12 @@ You can configure how your MDX is rendered with the following options:
|
|
|
89
89
|
All [`markdown` configuration options](https://docs.astro.build/en/reference/configuration-reference/#markdown-options) except `drafts` can be configured separately in the MDX integration. This includes remark and rehype plugins, syntax highlighting, and more. Options will default to those in your Markdown config ([see the `extendMarkdownConfig` option](#extendmarkdownconfig) to modify this).
|
|
90
90
|
|
|
91
91
|
:::note
|
|
92
|
-
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
|
|
92
|
+
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.
|
|
93
93
|
:::
|
|
94
94
|
|
|
95
|
-
|
|
96
|
-
|
|
95
|
+
__`astro.config.mjs`__
|
|
96
|
+
|
|
97
|
+
```js
|
|
97
98
|
import { defineConfig } from 'astro/config';
|
|
98
99
|
import mdx from '@astrojs/mdx';
|
|
99
100
|
import remarkToc from 'remark-toc';
|
|
@@ -102,19 +103,47 @@ import rehypeMinifyHtml from 'rehype-minify-html';
|
|
|
102
103
|
export default defineConfig({
|
|
103
104
|
integrations: [
|
|
104
105
|
mdx({
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
106
|
+
remarkPlugins: [exampleRemarkPlugin],
|
|
107
|
+
}),
|
|
108
|
+
],
|
|
109
|
+
});
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
…every MDX file will have `customProperty` in its frontmatter! See [our Markdown documentation](https://docs.astro.build/en/guides/markdown-content/#example-injecting-frontmatter) for more usage instructions and a [reading time plugin example](https://docs.astro.build/en/guides/markdown-content/#example-calculate-reading-time).
|
|
113
|
+
|
|
114
|
+
### Layouts
|
|
115
|
+
Layouts can be applied [in the same way as standard Astro Markdown](https://docs.astro.build/en/guides/markdown-content/#frontmatter-layout). You can add a `layout` to [your frontmatter](#frontmatter) like so:
|
|
116
|
+
|
|
117
|
+
```yaml
|
|
118
|
+
---
|
|
119
|
+
layout: '../layouts/BaseLayout.astro'
|
|
120
|
+
title: 'My Blog Post'
|
|
121
|
+
---
|
|
114
122
|
```
|
|
115
123
|
|
|
116
|
-
|
|
117
|
-
|
|
124
|
+
Then, you can retrieve all other frontmatter properties from your layout via the `frontmatter` property, and render your MDX using the default [`<slot />`](https://docs.astro.build/en/core-concepts/astro-components/#slots). See [layout props](#layout-props) for a complete list of props available.
|
|
125
|
+
|
|
126
|
+
```astro title="src/layouts/BaseLayout.astro"
|
|
127
|
+
---
|
|
128
|
+
const { frontmatter, url } = Astro.props;
|
|
129
|
+
---
|
|
130
|
+
<html>
|
|
131
|
+
<head>
|
|
132
|
+
<meta rel="canonical" href={new URL(url, Astro.site).pathname}>
|
|
133
|
+
<title>{frontmatter.title}</title>
|
|
134
|
+
</head>
|
|
135
|
+
<body>
|
|
136
|
+
<h1>{frontmatter.title}</h1>
|
|
137
|
+
<!-- Rendered MDX will be passed into the default slot. -->
|
|
138
|
+
<slot />
|
|
139
|
+
</body>
|
|
140
|
+
</html>
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
You can set a layout’s [`Props` type](/en/guides/typescript/#component-props) with the `MDXLayoutProps` helper.
|
|
144
|
+
|
|
145
|
+
:::note
|
|
146
|
+
`MDXLayoutProps` is the same as the `MarkdownLayoutProps` utility type with `rawContent()` and `compiledContent()` removed (since these are not available for `.mdx` files). Feel free to **use `MarkdownLayoutProps` instead** when sharing a layout across `.md` and `.mdx` files.
|
|
118
147
|
:::
|
|
119
148
|
|
|
120
149
|
📚 See the [Markdown Options reference](https://docs.astro.build/en/reference/configuration-reference/#markdown-options) for a complete list of options.
|
|
@@ -128,8 +157,80 @@ MDX will extend [your project's existing Markdown configuration](https://docs.as
|
|
|
128
157
|
|
|
129
158
|
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:
|
|
130
159
|
|
|
131
|
-
```
|
|
132
|
-
|
|
160
|
+
```html
|
|
161
|
+
<blockquote>
|
|
162
|
+
<p>A blockquote with <em>some</em> emphasis.</p>
|
|
163
|
+
</blockquote>
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
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.
|
|
167
|
+
|
|
168
|
+
```astro title="src/components/Blockquote.astro"
|
|
169
|
+
---
|
|
170
|
+
const props = Astro.props;
|
|
171
|
+
---
|
|
172
|
+
|
|
173
|
+
<blockquote {...props} class="bg-blue-50 p-4">
|
|
174
|
+
<span class="text-4xl text-blue-600 mb-2">“</span>
|
|
175
|
+
<slot />
|
|
176
|
+
</blockquote>
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
Then in the MDX file you import the component and export it to the `components` export.
|
|
180
|
+
|
|
181
|
+
```mdx title="src/pages/posts/post-1.mdx" {2}
|
|
182
|
+
import Blockquote from '../components/Blockquote.astro';
|
|
183
|
+
export const components = { blockquote: Blockquote };
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
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.
|
|
187
|
+
|
|
188
|
+
#### Custom components with imported `mdx`
|
|
189
|
+
|
|
190
|
+
When rendering imported MDX content, custom components can be passed via the `components` prop.
|
|
191
|
+
|
|
192
|
+
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:
|
|
193
|
+
|
|
194
|
+
```astro title="src/pages/page.astro" "components={{...components, h1: Heading }}"
|
|
195
|
+
---
|
|
196
|
+
import { Content, components } from '../content.mdx';
|
|
197
|
+
import Heading from '../Heading.astro';
|
|
198
|
+
---
|
|
199
|
+
|
|
200
|
+
<Content components={{...components, h1: Heading }} />
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### Syntax highlighting
|
|
204
|
+
|
|
205
|
+
The MDX integration respects [your project's `markdown.syntaxHighlight` configuration](https://docs.astro.build/en/guides/markdown-content/#syntax-highlighting).
|
|
206
|
+
|
|
207
|
+
We will highlight your code blocks with [Shiki](https://github.com/shikijs/shiki) by default. You can customize this highlighter using the `markdown.shikiConfig` option in your `astro.config`. For example, you can apply a different built-in theme like so:
|
|
208
|
+
|
|
209
|
+
__`astro.config.mjs`__
|
|
210
|
+
|
|
211
|
+
```js
|
|
212
|
+
import { defineConfig } from 'astro/config';
|
|
213
|
+
import mdx from '@astrojs/mdx';
|
|
214
|
+
|
|
215
|
+
export default defineConfig({
|
|
216
|
+
markdown: {
|
|
217
|
+
shikiConfig: {
|
|
218
|
+
theme: 'dracula',
|
|
219
|
+
},
|
|
220
|
+
},
|
|
221
|
+
integrations: [mdx()],
|
|
222
|
+
});
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
Visit [our Shiki configuration docs](https://docs.astro.build/en/guides/markdown-content/#shiki-configuration) for more on using Shiki with Astro.
|
|
226
|
+
|
|
227
|
+
#### Switch to Prism
|
|
228
|
+
|
|
229
|
+
You can also use the [Prism](https://prismjs.com/) syntax highlighter by setting `markdown.syntaxHighlight` to `'prism'` in your `astro.config` like so:
|
|
230
|
+
|
|
231
|
+
__`astro.config.mjs`__
|
|
232
|
+
|
|
233
|
+
```js
|
|
133
234
|
import { defineConfig } from 'astro/config';
|
|
134
235
|
import mdx from '@astrojs/mdx';
|
|
135
236
|
|
|
@@ -139,38 +240,149 @@ export default defineConfig({
|
|
|
139
240
|
remarkPlugins: [remarkPlugin1],
|
|
140
241
|
gfm: true,
|
|
141
242
|
},
|
|
243
|
+
integrations: [mdx()],
|
|
244
|
+
});
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
This applies a minimal Prism renderer with added support for `astro` code blocks. Visit [our "Prism configuration" docs](https://docs.astro.build/en/guides/markdown-content/#prism-configuration) for more on using Prism with Astro.
|
|
248
|
+
|
|
249
|
+
#### Switch to a custom syntax highlighter
|
|
250
|
+
|
|
251
|
+
You may want to apply your own syntax highlighter too. If your highlighter offers a remark or rehype plugin, you can flip off our syntax highlighting by setting `markdown.syntaxHighlight: false` and wiring up your plugin. For example, say you want to apply [Shiki Twoslash's remark plugin](https://www.npmjs.com/package/remark-shiki-twoslash):
|
|
252
|
+
|
|
253
|
+
__`astro.config.mjs`__
|
|
254
|
+
|
|
255
|
+
```js
|
|
256
|
+
import { defineConfig } from 'astro/config';
|
|
257
|
+
import mdx from '@astrojs/mdx';
|
|
258
|
+
import shikiTwoslash from 'remark-shiki-twoslash';
|
|
259
|
+
|
|
260
|
+
export default defineConfig({
|
|
261
|
+
markdown: {
|
|
262
|
+
syntaxHighlight: false,
|
|
263
|
+
},
|
|
142
264
|
integrations: [
|
|
143
265
|
mdx({
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
// Markdown `remarkPlugins` ignored,
|
|
147
|
-
// only `remarkPlugin2` applied.
|
|
148
|
-
remarkPlugins: [remarkPlugin2],
|
|
149
|
-
// `gfm` overridden to `false`
|
|
150
|
-
gfm: false,
|
|
266
|
+
remarkPlugins: [shikiTwoslash, { /* Shiki Twoslash config */ }],
|
|
151
267
|
})
|
|
152
|
-
]
|
|
268
|
+
],
|
|
269
|
+
});
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
## Configuration
|
|
273
|
+
|
|
274
|
+
### remarkPlugins
|
|
275
|
+
|
|
276
|
+
[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!
|
|
277
|
+
|
|
278
|
+
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).
|
|
279
|
+
|
|
280
|
+
__`astro.config.mjs`__
|
|
281
|
+
|
|
282
|
+
```js
|
|
283
|
+
import { defineConfig } from 'astro/config';
|
|
284
|
+
import mdx from '@astrojs/mdx';
|
|
285
|
+
import remarkToc from 'remark-toc';
|
|
286
|
+
|
|
287
|
+
export default defineConfig({
|
|
288
|
+
integrations: [mdx({
|
|
289
|
+
remarkPlugins: [remarkToc],
|
|
290
|
+
})],
|
|
291
|
+
});
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
### rehypePlugins
|
|
295
|
+
|
|
296
|
+
[Rehype plugins](https://github.com/rehypejs/rehype/blob/main/doc/plugins.md) allow you to transform the HTML that your Markdown generates. We encourage you to browse [awesome-rehype](https://github.com/rehypejs/awesome-rehype) for a full curated list of plugins!
|
|
297
|
+
|
|
298
|
+
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).
|
|
299
|
+
|
|
300
|
+
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).
|
|
301
|
+
|
|
302
|
+
__`astro.config.mjs`__
|
|
303
|
+
|
|
304
|
+
```js
|
|
305
|
+
import { defineConfig } from 'astro/config';
|
|
306
|
+
import mdx from '@astrojs/mdx';
|
|
307
|
+
import rehypeMinifyHtml from 'rehype-minify';
|
|
308
|
+
|
|
309
|
+
export default defineConfig({
|
|
310
|
+
integrations: [mdx({
|
|
311
|
+
rehypePlugins: [rehypeMinifyHtml],
|
|
312
|
+
})],
|
|
313
|
+
});
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
### extendPlugins
|
|
317
|
+
|
|
318
|
+
**Type:** `'markdown' | 'astroDefaults' | false`
|
|
319
|
+
|
|
320
|
+
**Default:** `'markdown'`
|
|
321
|
+
|
|
322
|
+
#### `markdown` (default)
|
|
323
|
+
|
|
324
|
+
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.
|
|
325
|
+
|
|
326
|
+
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:
|
|
327
|
+
|
|
328
|
+
__`astro.config.mjs`__
|
|
329
|
+
|
|
330
|
+
```js
|
|
331
|
+
import { defineConfig } from 'astro/config';
|
|
332
|
+
import mdx from '@astrojs/mdx';
|
|
333
|
+
import remarkToc from 'remark-toc';
|
|
334
|
+
import rehypeMinify from 'rehype-minify';
|
|
335
|
+
|
|
336
|
+
export default defineConfig({
|
|
337
|
+
markdown: {
|
|
338
|
+
// Applied to .md and .mdx files
|
|
339
|
+
remarkPlugins: [remarkToc],
|
|
340
|
+
},
|
|
341
|
+
integrations: [mdx({
|
|
342
|
+
// Applied to .mdx files only
|
|
343
|
+
rehypePlugins: [rehypeMinify],
|
|
344
|
+
})],
|
|
153
345
|
});
|
|
154
346
|
```
|
|
155
347
|
|
|
156
348
|
You may also need to disable `markdown` config extension in MDX. For this, set `extendMarkdownConfig` to `false`:
|
|
157
349
|
|
|
158
|
-
|
|
159
|
-
|
|
350
|
+
__`astro.config.mjs`__
|
|
351
|
+
|
|
352
|
+
```js "extendPlugins: 'astroDefaults'"
|
|
160
353
|
import { defineConfig } from 'astro/config';
|
|
161
354
|
import mdx from '@astrojs/mdx';
|
|
355
|
+
import remarkToc from 'remark-toc';
|
|
162
356
|
|
|
163
357
|
export default defineConfig({
|
|
164
358
|
markdown: {
|
|
165
359
|
remarkPlugins: [remarkPlugin1],
|
|
166
360
|
},
|
|
167
|
-
integrations: [
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
361
|
+
integrations: [mdx({
|
|
362
|
+
remarkPlugins: [remarkToc],
|
|
363
|
+
// Astro defaults applied
|
|
364
|
+
extendPlugins: 'astroDefaults',
|
|
365
|
+
})],
|
|
366
|
+
});
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
#### `false`
|
|
370
|
+
|
|
371
|
+
If you don't want to extend any plugins, set `extendPlugins` to `false`:
|
|
372
|
+
|
|
373
|
+
__`astro.config.mjs`__
|
|
374
|
+
|
|
375
|
+
```js "extendPlugins: false"
|
|
376
|
+
import { defineConfig } from 'astro/config';
|
|
377
|
+
import mdx from '@astrojs/mdx';
|
|
378
|
+
import remarkToc from 'remark-toc';
|
|
379
|
+
|
|
380
|
+
export default defineConfig({
|
|
381
|
+
integrations: [mdx({
|
|
382
|
+
remarkPlugins: [remarkToc],
|
|
383
|
+
// Astro defaults not applied
|
|
384
|
+
extendPlugins: false,
|
|
385
|
+
})],
|
|
174
386
|
});
|
|
175
387
|
```
|
|
176
388
|
|
|
@@ -180,6 +392,30 @@ These are plugins that modify the output [estree](https://github.com/estree/estr
|
|
|
180
392
|
|
|
181
393
|
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.
|
|
182
394
|
|
|
395
|
+
### remarkRehype
|
|
396
|
+
|
|
397
|
+
Markdown content is transformed into HTML through remark-rehype which has [a number of options](https://github.com/remarkjs/remark-rehype#options).
|
|
398
|
+
|
|
399
|
+
You can use remark-rehype options in your MDX integration config file like so:
|
|
400
|
+
|
|
401
|
+
__`astro.config.mjs`__
|
|
402
|
+
|
|
403
|
+
```js
|
|
404
|
+
import { defineConfig } from 'astro/config';
|
|
405
|
+
import mdx from '@astrojs/mdx';
|
|
406
|
+
|
|
407
|
+
export default defineConfig({
|
|
408
|
+
integrations: [mdx({
|
|
409
|
+
remarkRehype: {
|
|
410
|
+
footnoteLabel: 'Catatan kaki',
|
|
411
|
+
footnoteBackLabel: 'Kembali ke konten',
|
|
412
|
+
},
|
|
413
|
+
})],
|
|
414
|
+
});
|
|
415
|
+
```
|
|
416
|
+
|
|
417
|
+
This inherits the configuration of `markdown.remarkRehype`. This behavior can be changed by configuring `extendPlugins`.
|
|
418
|
+
|
|
183
419
|
## Examples
|
|
184
420
|
|
|
185
421
|
* 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.
|
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.15.
|
|
4
|
+
"version": "0.15.2",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
7
7
|
"author": "withastro",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"./package.json": "./package.json"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@astrojs/markdown-remark": "^2.0.
|
|
26
|
+
"@astrojs/markdown-remark": "^2.0.1",
|
|
27
27
|
"@astrojs/prism": "^2.0.0",
|
|
28
28
|
"@mdx-js/mdx": "^2.1.2",
|
|
29
29
|
"@mdx-js/rollup": "^2.1.1",
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
"@types/mdast": "^3.0.10",
|
|
49
49
|
"@types/mocha": "^9.1.1",
|
|
50
50
|
"@types/yargs-parser": "^21.0.0",
|
|
51
|
-
"astro": "2.0.
|
|
51
|
+
"astro": "2.0.4",
|
|
52
52
|
"astro-scripts": "0.0.10",
|
|
53
53
|
"chai": "^4.3.6",
|
|
54
54
|
"cheerio": "^1.0.0-rc.11",
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import mdx from '@astrojs/mdx';
|
|
2
|
+
|
|
3
|
+
import { expect } from 'chai';
|
|
4
|
+
import { parseHTML } from 'linkedom';
|
|
5
|
+
import { loadFixture } from '../../../astro/test/test-utils.js';
|
|
6
|
+
|
|
7
|
+
describe('Head injection w/ MDX', () => {
|
|
8
|
+
let fixture;
|
|
9
|
+
|
|
10
|
+
before(async () => {
|
|
11
|
+
fixture = await loadFixture({
|
|
12
|
+
root: new URL('./fixtures/css-head-mdx/', import.meta.url),
|
|
13
|
+
integrations: [mdx()],
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
describe('build', () => {
|
|
18
|
+
before(async () => {
|
|
19
|
+
await fixture.build();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('only injects contents into head', async () => {
|
|
23
|
+
const html = await fixture.readFile('/indexThree/index.html');
|
|
24
|
+
const { document } = parseHTML(html);
|
|
25
|
+
|
|
26
|
+
const links = document.querySelectorAll('link[rel=stylesheet]');
|
|
27
|
+
expect(links).to.have.a.lengthOf(1);
|
|
28
|
+
|
|
29
|
+
const scripts = document.querySelectorAll('script[type=module]');
|
|
30
|
+
expect(scripts).to.have.a.lengthOf(1);
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
---
|
|
2
|
+
---
|
|
3
|
+
|
|
4
|
+
<html lang="en">
|
|
5
|
+
<head>
|
|
6
|
+
<meta charset="utf-8" />
|
|
7
|
+
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
|
|
8
|
+
<meta name="viewport" content="width=device-width" />
|
|
9
|
+
<meta name="generator" content={Astro.generator} />
|
|
10
|
+
<title>Astro</title>
|
|
11
|
+
</head>
|
|
12
|
+
<body>
|
|
13
|
+
<slot />
|
|
14
|
+
</body>
|
|
15
|
+
</html>
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: The Frontmatter Title
|
|
3
|
+
keywords: [Keyword 1, Keyword 2, Keyword 3]
|
|
4
|
+
tags:
|
|
5
|
+
- Tag 1
|
|
6
|
+
- Tag 2
|
|
7
|
+
- Tag 3
|
|
8
|
+
items:
|
|
9
|
+
- value: Item 1
|
|
10
|
+
- value: Item 2
|
|
11
|
+
- value: Item 3
|
|
12
|
+
nested_items:
|
|
13
|
+
nested:
|
|
14
|
+
- value: Nested Item 1
|
|
15
|
+
- value: Nested Item 2
|
|
16
|
+
- value: Nested Item 3
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
# {frontmatter.title}
|
|
20
|
+
|
|
21
|
+
This ID should be the frontmatter title.
|
|
22
|
+
|
|
23
|
+
## frontmatter.title
|
|
24
|
+
|
|
25
|
+
The ID should not be the frontmatter title.
|
|
26
|
+
|
|
27
|
+
### {frontmatter.keywords[1]}
|
|
28
|
+
|
|
29
|
+
The ID should be the frontmatter keyword #2.
|
|
30
|
+
|
|
31
|
+
### {frontmatter.tags[0]}
|
|
32
|
+
|
|
33
|
+
The ID should be the frontmatter tag #1.
|
|
34
|
+
|
|
35
|
+
#### {frontmatter.items[1].value}
|
|
36
|
+
|
|
37
|
+
The ID should be the frontmatter item #2.
|
|
38
|
+
|
|
39
|
+
##### {frontmatter.nested_items.nested[2].value}
|
|
40
|
+
|
|
41
|
+
The ID should be the frontmatter nested item #3.
|
|
42
|
+
|
|
43
|
+
###### {frontmatter.unknown}
|
|
44
|
+
|
|
45
|
+
This ID should not reference the frontmatter.
|
|
@@ -149,3 +149,46 @@ describe('MDX heading IDs can be injected before user plugins', () => {
|
|
|
149
149
|
expect(h1?.id).to.equal('heading-test');
|
|
150
150
|
});
|
|
151
151
|
});
|
|
152
|
+
|
|
153
|
+
describe('MDX headings with frontmatter', () => {
|
|
154
|
+
let fixture;
|
|
155
|
+
|
|
156
|
+
before(async () => {
|
|
157
|
+
fixture = await loadFixture({
|
|
158
|
+
root: new URL('./fixtures/mdx-get-headings/', import.meta.url),
|
|
159
|
+
integrations: [mdx()],
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
await fixture.build();
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it('adds anchor IDs to headings', async () => {
|
|
166
|
+
const html = await fixture.readFile('/test-with-frontmatter/index.html');
|
|
167
|
+
const { document } = parseHTML(html);
|
|
168
|
+
|
|
169
|
+
const h3Ids = document.querySelectorAll('h3').map((el) => el?.id);
|
|
170
|
+
|
|
171
|
+
expect(document.querySelector('h1').id).to.equal('the-frontmatter-title');
|
|
172
|
+
expect(document.querySelector('h2').id).to.equal('frontmattertitle');
|
|
173
|
+
expect(h3Ids).to.contain('keyword-2');
|
|
174
|
+
expect(h3Ids).to.contain('tag-1');
|
|
175
|
+
expect(document.querySelector('h4').id).to.equal('item-2');
|
|
176
|
+
expect(document.querySelector('h5').id).to.equal('nested-item-3');
|
|
177
|
+
expect(document.querySelector('h6').id).to.equal('frontmatterunknown');
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it('generates correct getHeadings() export', async () => {
|
|
181
|
+
const { headingsByPage } = JSON.parse(await fixture.readFile('/pages.json'));
|
|
182
|
+
expect(JSON.stringify(headingsByPage['./test-with-frontmatter.mdx'])).to.equal(
|
|
183
|
+
JSON.stringify([
|
|
184
|
+
{ depth: 1, slug: 'the-frontmatter-title', text: 'The Frontmatter Title' },
|
|
185
|
+
{ depth: 2, slug: 'frontmattertitle', text: 'frontmatter.title' },
|
|
186
|
+
{ depth: 3, slug: 'keyword-2', text: 'Keyword 2' },
|
|
187
|
+
{ depth: 3, slug: 'tag-1', text: 'Tag 1' },
|
|
188
|
+
{ depth: 4, slug: 'item-2', text: 'Item 2' },
|
|
189
|
+
{ depth: 5, slug: 'nested-item-3', text: 'Nested Item 3' },
|
|
190
|
+
{ depth: 6, slug: 'frontmatterunknown', text: 'frontmatter.unknown' },
|
|
191
|
+
])
|
|
192
|
+
);
|
|
193
|
+
});
|
|
194
|
+
});
|