@astrojs/mdx 0.15.2 → 0.16.1
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 +2 -2
- package/CHANGELOG.md +16 -0
- package/README.md +27 -260
- package/dist/index.js +1 -0
- package/dist/plugins.js +6 -6
- package/package.json +4 -4
- package/src/index.ts +1 -0
- package/src/plugins.ts +9 -6
- package/test/css-head-mdx.test.js +54 -2
- package/test/fixtures/css-head-mdx/node_modules/.bin/astro +17 -0
- package/test/fixtures/css-head-mdx/package.json +10 -0
- package/test/fixtures/css-head-mdx/src/components/BaseHead.astro +11 -0
- package/test/fixtures/css-head-mdx/src/components/GenericComponent.astro +1 -0
- package/test/fixtures/css-head-mdx/src/components/MDXWrapper.astro +9 -0
- package/test/fixtures/css-head-mdx/src/components/P.astro +3 -0
- package/test/fixtures/css-head-mdx/src/components/SmallCaps.astro +3 -0
- package/test/fixtures/css-head-mdx/src/components/UsingMdx.astro +8 -0
- package/test/fixtures/css-head-mdx/src/components/WithHoistedScripts.astro +6 -0
- package/test/fixtures/css-head-mdx/src/content/blog/_styles.css +3 -0
- package/test/fixtures/css-head-mdx/src/content/blog/using-mdx.mdx +6 -0
- package/test/fixtures/css-head-mdx/src/content/posts/test.mdx +5 -0
- package/test/fixtures/css-head-mdx/src/content/posts/using-component.mdx +13 -0
- package/test/fixtures/css-head-mdx/src/layouts/ContentLayout.astro +18 -0
- package/test/fixtures/css-head-mdx/src/pages/DirectContentUsage.astro +17 -0
- package/test/fixtures/css-head-mdx/src/pages/noLayoutWithComponent.mdx +22 -0
- package/test/fixtures/css-head-mdx/src/pages/posts/[post].astro +18 -0
- package/test/fixtures/css-head-mdx/src/pages/remote.astro +17 -0
- package/test/fixtures/css-head-mdx/src/styles/global.css +3 -0
- package/test/mdx-plugins.test.js +35 -0
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
[34m@astrojs/mdx:build: [0mcache hit, replaying output [
|
|
1
|
+
[34m@astrojs/mdx:build: [0mcache hit, replaying output [2m14d153814201eafa[0m
|
|
2
2
|
[34m@astrojs/mdx:build: [0m
|
|
3
|
-
[34m@astrojs/mdx:build: [0m> @astrojs/mdx@0.
|
|
3
|
+
[34m@astrojs/mdx:build: [0m> @astrojs/mdx@0.16.1 build /home/runner/work/astro/astro/packages/integrations/mdx
|
|
4
4
|
[34m@astrojs/mdx:build: [0m> astro-scripts build "src/**/*.ts" && tsc
|
|
5
5
|
[34m@astrojs/mdx:build: [0m
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# @astrojs/mdx
|
|
2
2
|
|
|
3
|
+
## 0.16.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#6243](https://github.com/withastro/astro/pull/6243) [`4f6ecba4c`](https://github.com/withastro/astro/commit/4f6ecba4c1b35bacbbc0097854ee2b7b8c878e71) Thanks [@bluwy](https://github.com/bluwy)! - Support rehype plugins that inject namespaced attributes
|
|
8
|
+
|
|
9
|
+
## 0.16.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- [#6050](https://github.com/withastro/astro/pull/6050) [`2ab32b59e`](https://github.com/withastro/astro/commit/2ab32b59ef0a28d34757f2c2adb9cf2baa86855e) Thanks [@bholmesdev](https://github.com/bholmesdev)! - Fix: load syntax highlighters after MDX remark plugins. This keeps MDX consistent with Astro's markdown behavior.
|
|
14
|
+
|
|
15
|
+
### Patch Changes
|
|
16
|
+
|
|
17
|
+
- [#6062](https://github.com/withastro/astro/pull/6062) [`c6cf847bd`](https://github.com/withastro/astro/commit/c6cf847bd0b6bef3c51a5710fba5ca43b11e46f9) Thanks [@delucis](https://github.com/delucis)! - Update MDX README
|
|
18
|
+
|
|
3
19
|
## 0.15.2
|
|
4
20
|
|
|
5
21
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -103,47 +103,19 @@ import rehypeMinifyHtml from 'rehype-minify-html';
|
|
|
103
103
|
export default defineConfig({
|
|
104
104
|
integrations: [
|
|
105
105
|
mdx({
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
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
|
-
---
|
|
122
|
-
```
|
|
123
|
-
|
|
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>
|
|
106
|
+
syntaxHighlight: 'shiki',
|
|
107
|
+
shikiConfig: { theme: 'dracula' },
|
|
108
|
+
remarkPlugins: [remarkToc],
|
|
109
|
+
rehypePlugins: [rehypeMinifyHtml],
|
|
110
|
+
remarkRehype: { footnoteLabel: 'Footnotes' },
|
|
111
|
+
gfm: false,
|
|
112
|
+
})
|
|
113
|
+
]
|
|
114
|
+
})
|
|
141
115
|
```
|
|
142
116
|
|
|
143
|
-
|
|
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.
|
|
117
|
+
:::caution
|
|
118
|
+
MDX does not support passing remark and rehype plugins as a string. You should install, import, and apply the plugin function instead.
|
|
147
119
|
:::
|
|
148
120
|
|
|
149
121
|
📚 See the [Markdown Options reference](https://docs.astro.build/en/reference/configuration-reference/#markdown-options) for a complete list of options.
|
|
@@ -157,77 +129,6 @@ MDX will extend [your project's existing Markdown configuration](https://docs.as
|
|
|
157
129
|
|
|
158
130
|
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:
|
|
159
131
|
|
|
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
132
|
__`astro.config.mjs`__
|
|
232
133
|
|
|
233
134
|
```js
|
|
@@ -240,108 +141,17 @@ export default defineConfig({
|
|
|
240
141
|
remarkPlugins: [remarkPlugin1],
|
|
241
142
|
gfm: true,
|
|
242
143
|
},
|
|
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
|
-
},
|
|
264
144
|
integrations: [
|
|
265
145
|
mdx({
|
|
266
|
-
|
|
267
|
-
})
|
|
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`__
|
|
146
|
+
// `syntaxHighlight` inherited from Markdown
|
|
281
147
|
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
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
|
-
})],
|
|
148
|
+
// Markdown `remarkPlugins` ignored,
|
|
149
|
+
// only `remarkPlugin2` applied.
|
|
150
|
+
remarkPlugins: [remarkPlugin2],
|
|
151
|
+
// `gfm` overridden to `false`
|
|
152
|
+
gfm: false,
|
|
153
|
+
})
|
|
154
|
+
]
|
|
345
155
|
});
|
|
346
156
|
```
|
|
347
157
|
|
|
@@ -349,40 +159,21 @@ You may also need to disable `markdown` config extension in MDX. For this, set `
|
|
|
349
159
|
|
|
350
160
|
__`astro.config.mjs`__
|
|
351
161
|
|
|
352
|
-
```js
|
|
162
|
+
```js
|
|
353
163
|
import { defineConfig } from 'astro/config';
|
|
354
164
|
import mdx from '@astrojs/mdx';
|
|
355
|
-
import remarkToc from 'remark-toc';
|
|
356
165
|
|
|
357
166
|
export default defineConfig({
|
|
358
167
|
markdown: {
|
|
359
168
|
remarkPlugins: [remarkPlugin1],
|
|
360
169
|
},
|
|
361
|
-
integrations: [
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
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
|
-
})],
|
|
170
|
+
integrations: [
|
|
171
|
+
mdx({
|
|
172
|
+
// Markdown config now ignored
|
|
173
|
+
extendMarkdownConfig: false,
|
|
174
|
+
// No `remarkPlugins` applied
|
|
175
|
+
})
|
|
176
|
+
]
|
|
386
177
|
});
|
|
387
178
|
```
|
|
388
179
|
|
|
@@ -392,30 +183,6 @@ These are plugins that modify the output [estree](https://github.com/estree/estr
|
|
|
392
183
|
|
|
393
184
|
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.
|
|
394
185
|
|
|
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
|
-
|
|
419
186
|
## Examples
|
|
420
187
|
|
|
421
188
|
* 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/dist/index.js
CHANGED
|
@@ -48,6 +48,7 @@ function mdx(partialMdxOptions = {}) {
|
|
|
48
48
|
const { data: frontmatter, content: pageContent } = parseFrontmatter(code, id);
|
|
49
49
|
const compiled = await mdxCompile(new VFile({ value: pageContent, path: id }), {
|
|
50
50
|
...mdxPluginOpts,
|
|
51
|
+
elementAttributeNameCase: "html",
|
|
51
52
|
remarkPlugins: [
|
|
52
53
|
toRemarkInitializeAstroData({ userFrontmatter: frontmatter }),
|
|
53
54
|
...mdxPluginOpts.remarkPlugins ?? []
|
package/dist/plugins.js
CHANGED
|
@@ -98,12 +98,6 @@ function toRemarkContentRelImageError({ srcDir }) {
|
|
|
98
98
|
}
|
|
99
99
|
async function getRemarkPlugins(mdxOptions, config) {
|
|
100
100
|
let remarkPlugins = [];
|
|
101
|
-
if (mdxOptions.syntaxHighlight === "shiki") {
|
|
102
|
-
remarkPlugins.push([await remarkShiki(mdxOptions.shikiConfig)]);
|
|
103
|
-
}
|
|
104
|
-
if (mdxOptions.syntaxHighlight === "prism") {
|
|
105
|
-
remarkPlugins.push(remarkPrism);
|
|
106
|
-
}
|
|
107
101
|
if (mdxOptions.gfm) {
|
|
108
102
|
remarkPlugins.push(remarkGfm);
|
|
109
103
|
}
|
|
@@ -111,6 +105,12 @@ async function getRemarkPlugins(mdxOptions, config) {
|
|
|
111
105
|
remarkPlugins.push(remarkSmartypants);
|
|
112
106
|
}
|
|
113
107
|
remarkPlugins = [...remarkPlugins, ...ignoreStringPlugins(mdxOptions.remarkPlugins)];
|
|
108
|
+
if (mdxOptions.syntaxHighlight === "shiki") {
|
|
109
|
+
remarkPlugins.push([await remarkShiki(mdxOptions.shikiConfig)]);
|
|
110
|
+
}
|
|
111
|
+
if (mdxOptions.syntaxHighlight === "prism") {
|
|
112
|
+
remarkPlugins.push(remarkPrism);
|
|
113
|
+
}
|
|
114
114
|
remarkPlugins.push(toRemarkContentRelImageError(config));
|
|
115
115
|
return remarkPlugins;
|
|
116
116
|
}
|
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.
|
|
4
|
+
"version": "0.16.1",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
7
7
|
"author": "withastro",
|
|
@@ -25,8 +25,8 @@
|
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"@astrojs/markdown-remark": "^2.0.1",
|
|
27
27
|
"@astrojs/prism": "^2.0.0",
|
|
28
|
-
"@mdx-js/mdx": "^2.
|
|
29
|
-
"@mdx-js/rollup": "^2.
|
|
28
|
+
"@mdx-js/mdx": "^2.3.0",
|
|
29
|
+
"@mdx-js/rollup": "^2.3.0",
|
|
30
30
|
"acorn": "^8.8.0",
|
|
31
31
|
"es-module-lexer": "^0.10.5",
|
|
32
32
|
"estree-util-visit": "^1.2.0",
|
|
@@ -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.12",
|
|
52
52
|
"astro-scripts": "0.0.10",
|
|
53
53
|
"chai": "^4.3.6",
|
|
54
54
|
"cheerio": "^1.0.0-rc.11",
|
package/src/index.ts
CHANGED
|
@@ -74,6 +74,7 @@ export default function mdx(partialMdxOptions: Partial<MdxOptions> = {}): AstroI
|
|
|
74
74
|
const { data: frontmatter, content: pageContent } = parseFrontmatter(code, id);
|
|
75
75
|
const compiled = await mdxCompile(new VFile({ value: pageContent, path: id }), {
|
|
76
76
|
...mdxPluginOpts,
|
|
77
|
+
elementAttributeNameCase: 'html',
|
|
77
78
|
remarkPlugins: [
|
|
78
79
|
// Ensure `data.astro` is available to all remark plugins
|
|
79
80
|
toRemarkInitializeAstroData({ userFrontmatter: frontmatter }),
|
package/src/plugins.ts
CHANGED
|
@@ -129,12 +129,7 @@ export async function getRemarkPlugins(
|
|
|
129
129
|
config: AstroConfig
|
|
130
130
|
): Promise<MdxRollupPluginOptions['remarkPlugins']> {
|
|
131
131
|
let remarkPlugins: PluggableList = [];
|
|
132
|
-
|
|
133
|
-
remarkPlugins.push([await remarkShiki(mdxOptions.shikiConfig)]);
|
|
134
|
-
}
|
|
135
|
-
if (mdxOptions.syntaxHighlight === 'prism') {
|
|
136
|
-
remarkPlugins.push(remarkPrism);
|
|
137
|
-
}
|
|
132
|
+
|
|
138
133
|
if (mdxOptions.gfm) {
|
|
139
134
|
remarkPlugins.push(remarkGfm);
|
|
140
135
|
}
|
|
@@ -144,6 +139,14 @@ export async function getRemarkPlugins(
|
|
|
144
139
|
|
|
145
140
|
remarkPlugins = [...remarkPlugins, ...ignoreStringPlugins(mdxOptions.remarkPlugins)];
|
|
146
141
|
|
|
142
|
+
// Apply syntax highlighters after user plugins to match `markdown/remark` behavior
|
|
143
|
+
if (mdxOptions.syntaxHighlight === 'shiki') {
|
|
144
|
+
remarkPlugins.push([await remarkShiki(mdxOptions.shikiConfig)]);
|
|
145
|
+
}
|
|
146
|
+
if (mdxOptions.syntaxHighlight === 'prism') {
|
|
147
|
+
remarkPlugins.push(remarkPrism);
|
|
148
|
+
}
|
|
149
|
+
|
|
147
150
|
// Apply last in case user plugins resolve relative image paths
|
|
148
151
|
remarkPlugins.push(toRemarkContentRelImageError(config));
|
|
149
152
|
|
|
@@ -3,6 +3,7 @@ import mdx from '@astrojs/mdx';
|
|
|
3
3
|
import { expect } from 'chai';
|
|
4
4
|
import { parseHTML } from 'linkedom';
|
|
5
5
|
import { loadFixture } from '../../../astro/test/test-utils.js';
|
|
6
|
+
import * as cheerio from 'cheerio';
|
|
6
7
|
|
|
7
8
|
describe('Head injection w/ MDX', () => {
|
|
8
9
|
let fixture;
|
|
@@ -23,11 +24,62 @@ describe('Head injection w/ MDX', () => {
|
|
|
23
24
|
const html = await fixture.readFile('/indexThree/index.html');
|
|
24
25
|
const { document } = parseHTML(html);
|
|
25
26
|
|
|
26
|
-
const links = document.querySelectorAll('link[rel=stylesheet]');
|
|
27
|
+
const links = document.querySelectorAll('head link[rel=stylesheet]');
|
|
27
28
|
expect(links).to.have.a.lengthOf(1);
|
|
28
29
|
|
|
29
|
-
const scripts = document.querySelectorAll('script[type=module]');
|
|
30
|
+
const scripts = document.querySelectorAll('head script[type=module]');
|
|
30
31
|
expect(scripts).to.have.a.lengthOf(1);
|
|
31
32
|
});
|
|
33
|
+
|
|
34
|
+
it('injects into the head for content collections', async () => {
|
|
35
|
+
const html = await fixture.readFile('/posts/test/index.html');
|
|
36
|
+
const { document } = parseHTML(html);
|
|
37
|
+
|
|
38
|
+
const links = document.querySelectorAll('head link[rel=stylesheet]');
|
|
39
|
+
expect(links).to.have.a.lengthOf(1);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('injects content from a component using Content#render()', async () => {
|
|
43
|
+
const html = await fixture.readFile('/DirectContentUsage/index.html');
|
|
44
|
+
const { document } = parseHTML(html);
|
|
45
|
+
|
|
46
|
+
const links = document.querySelectorAll('head link[rel=stylesheet]');
|
|
47
|
+
expect(links).to.have.a.lengthOf(1);
|
|
48
|
+
|
|
49
|
+
const scripts = document.querySelectorAll('head script[type=module]');
|
|
50
|
+
expect(scripts).to.have.a.lengthOf(2);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('Using component using slots.render() API', async () => {
|
|
54
|
+
const html = await fixture.readFile('/remote/index.html');
|
|
55
|
+
const { document } = parseHTML(html);
|
|
56
|
+
|
|
57
|
+
const links = document.querySelectorAll('head link[rel=stylesheet]');
|
|
58
|
+
expect(links).to.have.a.lengthOf(1);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('Using component but no layout', async () => {
|
|
62
|
+
const html = await fixture.readFile('/noLayoutWithComponent/index.html');
|
|
63
|
+
// Using cheerio here because linkedom doesn't support head tag injection
|
|
64
|
+
const $ = cheerio.load(html);
|
|
65
|
+
|
|
66
|
+
const headLinks = $('head link[rel=stylesheet]');
|
|
67
|
+
expect(headLinks).to.have.a.lengthOf(1);
|
|
68
|
+
|
|
69
|
+
const bodyLinks = $('body link[rel=stylesheet]');
|
|
70
|
+
expect(bodyLinks).to.have.a.lengthOf(0);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('JSX component rendering Astro children within head buffering phase', async () => {
|
|
74
|
+
const html = await fixture.readFile('/posts/using-component/index.html');
|
|
75
|
+
// Using cheerio here because linkedom doesn't support head tag injection
|
|
76
|
+
const $ = cheerio.load(html);
|
|
77
|
+
|
|
78
|
+
const headLinks = $('head link[rel=stylesheet]');
|
|
79
|
+
expect(headLinks).to.have.a.lengthOf(1);
|
|
80
|
+
|
|
81
|
+
const bodyLinks = $('body link[rel=stylesheet]');
|
|
82
|
+
expect(bodyLinks).to.have.a.lengthOf(0);
|
|
83
|
+
});
|
|
32
84
|
});
|
|
33
85
|
});
|
|
@@ -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,11 @@
|
|
|
1
|
+
---
|
|
2
|
+
const { title } = Astro.props;
|
|
3
|
+
---
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width" />
|
|
6
|
+
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
|
|
7
|
+
<meta name="generator" content={Astro.generator} />
|
|
8
|
+
<title>{title}</title>
|
|
9
|
+
<style is:global>
|
|
10
|
+
@import "../styles/global.css";
|
|
11
|
+
</style>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<span>just a generic component</span>
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
---
|
|
2
|
+
import BaseHead from "../components/BaseHead.astro";
|
|
3
|
+
export interface Props {
|
|
4
|
+
title: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
const { title } = Astro.props;
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
<!DOCTYPE html>
|
|
11
|
+
<html lang="en">
|
|
12
|
+
<head>
|
|
13
|
+
<BaseHead title={title} />
|
|
14
|
+
</head>
|
|
15
|
+
<body>
|
|
16
|
+
<slot />
|
|
17
|
+
</body>
|
|
18
|
+
</html>
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
---
|
|
2
|
+
import UsingMdx from '../components/UsingMdx.astro'
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
<html lang="en">
|
|
6
|
+
<head>
|
|
7
|
+
<meta charset="utf-8" />
|
|
8
|
+
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
|
|
9
|
+
<meta name="viewport" content="width=device-width" />
|
|
10
|
+
<meta name="generator" content={Astro.generator} />
|
|
11
|
+
<title>Astro</title>
|
|
12
|
+
</head>
|
|
13
|
+
<body>
|
|
14
|
+
<h1>Astro</h1>
|
|
15
|
+
<UsingMdx />
|
|
16
|
+
</body>
|
|
17
|
+
</html>
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: 'Lorem'
|
|
3
|
+
description: 'Lorem ipsum dolor sit amet'
|
|
4
|
+
pubDate: 'Jul 02 2022'
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
import MyComponent from '../components/HelloWorld.astro';
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
## Lorem
|
|
11
|
+
|
|
12
|
+
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
|
|
13
|
+
|
|
14
|
+
## Lorem 2
|
|
15
|
+
|
|
16
|
+
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
|
|
17
|
+
|
|
18
|
+
<MyComponent />
|
|
19
|
+
|
|
20
|
+
## Lorem 3
|
|
21
|
+
|
|
22
|
+
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
---
|
|
2
|
+
import { getCollection } from 'astro:content';
|
|
3
|
+
import Layout from '../../layouts/ContentLayout.astro';
|
|
4
|
+
import SmallCaps from '../../components/SmallCaps.astro';
|
|
5
|
+
|
|
6
|
+
export async function getStaticPaths() {
|
|
7
|
+
const entries = await getCollection('posts');
|
|
8
|
+
return entries.map(entry => {
|
|
9
|
+
return {params: { post: entry.slug }, props: { entry },
|
|
10
|
+
}});
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const { entry } = Astro.props;
|
|
14
|
+
const { Content } = await entry.render();
|
|
15
|
+
---
|
|
16
|
+
<Layout title="">
|
|
17
|
+
<Content components={{ SmallCaps }} />
|
|
18
|
+
</Layout>
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
---
|
|
2
|
+
import '../styles/global.css'
|
|
3
|
+
import Layout from '../layouts/One.astro';
|
|
4
|
+
import Paragraph from '../components/P.astro';
|
|
5
|
+
import { Markdown } from 'astro-remote'
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
<Layout title="Welcome to Astro.">
|
|
9
|
+
<main>
|
|
10
|
+
<Markdown
|
|
11
|
+
components={{
|
|
12
|
+
p: Paragraph,
|
|
13
|
+
}}>
|
|
14
|
+
**Removing p component fixes the problem**
|
|
15
|
+
</Markdown>
|
|
16
|
+
</main>
|
|
17
|
+
</Layout>
|
package/test/mdx-plugins.test.js
CHANGED
|
@@ -63,6 +63,20 @@ describe('MDX plugins', () => {
|
|
|
63
63
|
expect(selectRehypeExample(document)).to.not.be.null;
|
|
64
64
|
});
|
|
65
65
|
|
|
66
|
+
it('supports custom rehype plugins with namespaced attributes', async () => {
|
|
67
|
+
const fixture = await buildFixture({
|
|
68
|
+
integrations: [
|
|
69
|
+
mdx({
|
|
70
|
+
rehypePlugins: [rehypeSvgPlugin],
|
|
71
|
+
}),
|
|
72
|
+
],
|
|
73
|
+
});
|
|
74
|
+
const html = await fixture.readFile(FILE);
|
|
75
|
+
const { document } = parseHTML(html);
|
|
76
|
+
|
|
77
|
+
expect(selectRehypeSvg(document)).to.not.be.null;
|
|
78
|
+
});
|
|
79
|
+
|
|
66
80
|
it('extends markdown config by default', async () => {
|
|
67
81
|
const fixture = await buildFixture({
|
|
68
82
|
markdown: {
|
|
@@ -207,6 +221,23 @@ function rehypeExamplePlugin() {
|
|
|
207
221
|
};
|
|
208
222
|
}
|
|
209
223
|
|
|
224
|
+
function rehypeSvgPlugin() {
|
|
225
|
+
return (tree) => {
|
|
226
|
+
tree.children.push({
|
|
227
|
+
type: 'element',
|
|
228
|
+
tagName: 'svg',
|
|
229
|
+
properties: { xmlns: 'http://www.w3.org/2000/svg' },
|
|
230
|
+
children: [
|
|
231
|
+
{
|
|
232
|
+
type: 'element',
|
|
233
|
+
tagName: 'use',
|
|
234
|
+
properties: { xLinkHref: '#icon' },
|
|
235
|
+
},
|
|
236
|
+
],
|
|
237
|
+
});
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
|
|
210
241
|
function recmaExamplePlugin() {
|
|
211
242
|
return (tree) => {
|
|
212
243
|
estreeVisit(tree, (node) => {
|
|
@@ -245,6 +276,10 @@ function selectRehypeExample(document) {
|
|
|
245
276
|
return document.querySelector('div[data-rehype-plugin-works]');
|
|
246
277
|
}
|
|
247
278
|
|
|
279
|
+
function selectRehypeSvg(document) {
|
|
280
|
+
return document.querySelector('svg > use[xlink\\:href]');
|
|
281
|
+
}
|
|
282
|
+
|
|
248
283
|
function selectRecmaExample(document) {
|
|
249
284
|
return document.querySelector('div[data-recma-plugin-works]');
|
|
250
285
|
}
|