@astrojs/mdx 0.2.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +5 -5
- package/CHANGELOG.md +28 -0
- package/README.md +247 -1
- package/dist/index.d.ts +17 -1
- package/dist/index.js +87 -10
- package/dist/remark-prism.d.ts +2 -0
- package/dist/remark-prism.js +49 -0
- package/dist/utils.d.ts +15 -0
- package/dist/utils.js +47 -0
- package/package.json +18 -4
- package/src/index.ts +122 -15
- package/src/remark-prism.ts +60 -0
- package/src/utils.ts +60 -0
- package/test/fixtures/mdx-custom-frontmatter-name/src/pages/glob.json.js +9 -0
- package/test/fixtures/mdx-custom-frontmatter-name/src/pages/index.mdx +6 -0
- package/test/fixtures/mdx-frontmatter/src/layouts/Base.astro +18 -0
- package/test/fixtures/mdx-frontmatter/src/pages/glob.json.js +9 -0
- package/test/fixtures/mdx-frontmatter/src/pages/index.mdx +7 -0
- package/test/fixtures/mdx-get-static-paths/src/content/1.mdx +5 -0
- package/test/fixtures/mdx-get-static-paths/src/pages/[slug].astro +34 -0
- package/test/fixtures/mdx-remark-plugins/src/pages/with-gfm.mdx +3 -0
- package/test/fixtures/mdx-remark-plugins/src/pages/with-toc.mdx +19 -0
- package/test/fixtures/mdx-syntax-hightlighting/src/pages/index.mdx +9 -0
- package/test/fixtures/mdx-url-export/src/pages/pages.json.js +9 -0
- package/test/fixtures/mdx-url-export/src/pages/test-1.mdx +1 -0
- package/test/fixtures/mdx-url-export/src/pages/test-2.mdx +1 -0
- package/test/fixtures/mdx-url-export/src/pages/with-url-override.mdx +3 -0
- package/test/mdx-frontmatter.test.js +76 -0
- package/test/mdx-get-static-paths.test.js +32 -0
- package/test/mdx-remark-plugins.test.js +62 -0
- package/test/mdx-syntax-highlighting.test.js +67 -0
- package/test/mdx-url-export.test.js +28 -0
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
[
|
|
2
|
-
[
|
|
3
|
-
[
|
|
4
|
-
[
|
|
5
|
-
[
|
|
1
|
+
[36m@astrojs/mdx:build: [0mcache hit, replaying output [2mc4afb89e8a455a28[0m
|
|
2
|
+
[36m@astrojs/mdx:build: [0m
|
|
3
|
+
[36m@astrojs/mdx:build: [0m> @astrojs/mdx@0.4.0 build /home/runner/work/astro/astro/packages/integrations/mdx
|
|
4
|
+
[36m@astrojs/mdx:build: [0m> astro-scripts build "src/**/*.ts" && tsc
|
|
5
|
+
[36m@astrojs/mdx:build: [0m
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,33 @@
|
|
|
1
1
|
# @astrojs/mdx
|
|
2
2
|
|
|
3
|
+
## 0.4.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#4088](https://github.com/withastro/astro/pull/4088) [`1743fe140`](https://github.com/withastro/astro/commit/1743fe140eb58d60e26cbd11a066bb60de046e0c) Thanks [@bholmesdev](https://github.com/bholmesdev)! - Support "layout" frontmatter property
|
|
8
|
+
|
|
9
|
+
## 0.3.1
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- [#4076](https://github.com/withastro/astro/pull/4076) [`6120a71e5`](https://github.com/withastro/astro/commit/6120a71e5425ad55a17ddac800d64a3f50273bce) Thanks [@matthewp](https://github.com/matthewp)! - Ensure file and url are always present in MDX for Astro.glob
|
|
14
|
+
|
|
15
|
+
## 0.3.0
|
|
16
|
+
|
|
17
|
+
### Minor Changes
|
|
18
|
+
|
|
19
|
+
- [#3977](https://github.com/withastro/astro/pull/3977) [`19433eb4a`](https://github.com/withastro/astro/commit/19433eb4a4441522f68492ca914ad2ab4f061343) Thanks [@bholmesdev](https://github.com/bholmesdev)! - Add remarkPlugins and rehypePlugins to config, with the same default plugins as our standard Markdown parser
|
|
20
|
+
|
|
21
|
+
* [#4002](https://github.com/withastro/astro/pull/4002) [`3b8a74452`](https://github.com/withastro/astro/commit/3b8a7445247221100462ba035f6778b43ea180e7) Thanks [@bholmesdev](https://github.com/bholmesdev)! - Support Prism and Shiki syntax highlighting based on project config
|
|
22
|
+
|
|
23
|
+
- [#3995](https://github.com/withastro/astro/pull/3995) [`b2b367c96`](https://github.com/withastro/astro/commit/b2b367c969493aaf21c974064beb241d05228066) Thanks [@bholmesdev](https://github.com/bholmesdev)! - Support YAML frontmatter in MDX files
|
|
24
|
+
|
|
25
|
+
### Patch Changes
|
|
26
|
+
|
|
27
|
+
- [#4050](https://github.com/withastro/astro/pull/4050) [`9ab66c4ba`](https://github.com/withastro/astro/commit/9ab66c4ba9bf2250990114c76b792f26d0694365) Thanks [@FredKSchott](https://github.com/FredKSchott)! - Add support for injected "page-ssr" scripts
|
|
28
|
+
|
|
29
|
+
* [#3981](https://github.com/withastro/astro/pull/3981) [`61fec6304`](https://github.com/withastro/astro/commit/61fec63044e1585348e8405bee6fdf4dec635efa) Thanks [@bholmesdev](https://github.com/bholmesdev)! - Include page url in MDX glob result
|
|
30
|
+
|
|
3
31
|
## 0.2.1
|
|
4
32
|
|
|
5
33
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -78,9 +78,255 @@ To write your first MDX page in Astro, head to our [UI framework documentation][
|
|
|
78
78
|
|
|
79
79
|
Also check our [Astro Integration Documentation][astro-integration] for more on integrations.
|
|
80
80
|
|
|
81
|
+
### Variables
|
|
82
|
+
|
|
83
|
+
MDX supports `export` statements to add variables to your templates. These variables are accessible both from the template itself _and_ as named properties when importing the template somewhere else.
|
|
84
|
+
|
|
85
|
+
For instance, you can export a `title` field from an MDX page or component to use as a heading with `{JSX expressions}`:
|
|
86
|
+
|
|
87
|
+
```mdx
|
|
88
|
+
export const title = 'My first MDX post'
|
|
89
|
+
|
|
90
|
+
# {title}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
This `title` will be accessible from `import` and [glob](https://docs.astro.build/en/reference/api-reference/#astroglob) statements as well:
|
|
94
|
+
|
|
95
|
+
```astro
|
|
96
|
+
---
|
|
97
|
+
// src/pages/index.astro
|
|
98
|
+
const posts = await Astro.glob('./*.mdx');
|
|
99
|
+
---
|
|
100
|
+
|
|
101
|
+
{posts.map(post => <p>{post.title}</p>)}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
See [the official "how MDX works" guide](https://mdxjs.com/docs/using-mdx/#how-mdx-works) for more on MDX variables.
|
|
105
|
+
|
|
106
|
+
### Frontmatter
|
|
107
|
+
|
|
108
|
+
Astro also supports YAML-based frontmatter out-of-the-box using the [remark-mdx-frontmatter](https://github.com/remcohaszing/remark-mdx-frontmatter) plugin. By default, all variables declared in a frontmatter fence (`---`) will be accessible via the `frontmatter` export. See the `frontmatterOptions` configuration to customize this behavior.
|
|
109
|
+
|
|
110
|
+
For example, we can add a `title` and `publishDate` to an MDX page or component like so:
|
|
111
|
+
|
|
112
|
+
```mdx
|
|
113
|
+
---
|
|
114
|
+
title: 'My first MDX post'
|
|
115
|
+
publishDate: '21 September 2022'
|
|
116
|
+
---
|
|
117
|
+
|
|
118
|
+
# {frontmatter.title}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
Now, this `title` and `publishDate` will be accessible from `import` and [glob](https://docs.astro.build/en/reference/api-reference/#astroglob) statements via the `frontmatter` property. This matches the behavior of [plain markdown in Astro](https://docs.astro.build/en/reference/api-reference/#markdown-files) as well!
|
|
122
|
+
|
|
123
|
+
```astro
|
|
124
|
+
---
|
|
125
|
+
// src/pages/index.astro
|
|
126
|
+
const posts = await Astro.glob('./*.mdx');
|
|
127
|
+
---
|
|
128
|
+
|
|
129
|
+
{posts.map(post => (
|
|
130
|
+
<Fragment>
|
|
131
|
+
<h2>{post.frontmatter.title}</h2>
|
|
132
|
+
<time>{post.frontmatter.publishDate}</time>
|
|
133
|
+
</Fragment>
|
|
134
|
+
))}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### Layouts
|
|
138
|
+
|
|
139
|
+
Layouts can be applied [in the same way as standard Astro Markdown](https://docs.astro.build/en/guides/markdown-content/#markdown-layouts). You can add a `layout` to [your frontmatter](#frontmatter) like so:
|
|
140
|
+
|
|
141
|
+
```yaml
|
|
142
|
+
---
|
|
143
|
+
layout: '../layouts/BaseLayout.astro'
|
|
144
|
+
title: 'My Blog Post'
|
|
145
|
+
---
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Then, you can retrieve all other frontmatter properties from your layout via the `content` property, and render your MDX using the default [`<slot />`](https://docs.astro.build/en/core-concepts/astro-components/#slots):
|
|
149
|
+
|
|
150
|
+
```astro
|
|
151
|
+
---
|
|
152
|
+
// src/layouts/BaseLayout.astro
|
|
153
|
+
const { content } = Astro.props;
|
|
154
|
+
---
|
|
155
|
+
<html>
|
|
156
|
+
<head>
|
|
157
|
+
<title>{content.title}</title>
|
|
158
|
+
</head>
|
|
159
|
+
<body>
|
|
160
|
+
<h1>{content.title}</h1>
|
|
161
|
+
<!-- Rendered MDX will be passed into the default slot. -->
|
|
162
|
+
<slot />
|
|
163
|
+
</body>
|
|
164
|
+
</html>
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
#### Importing layouts manually
|
|
168
|
+
|
|
169
|
+
You may need to pass information to your layouts that does not (or cannot) exist in your frontmatter. In this case, you can import and use a [`<Layout />` component](https://docs.astro.build/en/core-concepts/layouts/) like any other component:
|
|
170
|
+
|
|
171
|
+
```mdx
|
|
172
|
+
---
|
|
173
|
+
// src/pages/posts/first-post.mdx
|
|
174
|
+
|
|
175
|
+
title: 'My first MDX post'
|
|
176
|
+
publishDate: '21 September 2022'
|
|
177
|
+
---
|
|
178
|
+
import BaseLayout from '../layouts/BaseLayout.astro';
|
|
179
|
+
|
|
180
|
+
function fancyJsHelper() {
|
|
181
|
+
return "Try doing that with YAML!";
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
<BaseLayout title={frontmatter.title} fancyJsHelper={fancyJsHelper}>
|
|
185
|
+
Welcome to my new Astro blog, using MDX!
|
|
186
|
+
</BaseLayout>
|
|
187
|
+
```
|
|
188
|
+
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:
|
|
189
|
+
|
|
190
|
+
```astro
|
|
191
|
+
---
|
|
192
|
+
// src/layouts/BaseLayout.astro
|
|
193
|
+
const { title, fancyJsHelper } = Astro.props;
|
|
194
|
+
---
|
|
195
|
+
<!-- -->
|
|
196
|
+
<h1>{title}</h1>
|
|
197
|
+
<slot />
|
|
198
|
+
<p>{fancyJsHelper()}</p>
|
|
199
|
+
<!-- -->
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### Syntax highlighting
|
|
203
|
+
|
|
204
|
+
The MDX integration respects [your project's `markdown.syntaxHighlight` configuration](https://docs.astro.build/en/guides/markdown-content/#syntax-highlighting).
|
|
205
|
+
|
|
206
|
+
We will highlight your code blocks with [Shiki](https://github.com/shikijs/shiki) by default [using Shiki twoslash](https://shikijs.github.io/twoslash/). You can customize [this remark plugin](https://www.npmjs.com/package/remark-shiki-twoslash) using the `markdown.shikiConfig` option in your `astro.config`. For example, you can apply a different built-in theme like so:
|
|
207
|
+
|
|
208
|
+
```js
|
|
209
|
+
// astro.config.mjs
|
|
210
|
+
export default {
|
|
211
|
+
markdown: {
|
|
212
|
+
shikiConfig: {
|
|
213
|
+
theme: 'dracula',
|
|
214
|
+
},
|
|
215
|
+
},
|
|
216
|
+
integrations: [mdx()],
|
|
217
|
+
}
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
Visit [our Shiki configuration docs](https://docs.astro.build/en/guides/markdown-content/#shiki-configuration) for more on using Shiki with Astro.
|
|
221
|
+
|
|
222
|
+
#### Switch to Prism
|
|
223
|
+
|
|
224
|
+
You can also use the [Prism](https://prismjs.com/) syntax highlighter by setting `markdown.syntaxHighlight` to `'prism'` in your `astro.config` like so:
|
|
225
|
+
|
|
226
|
+
```js
|
|
227
|
+
// astro.config.mjs
|
|
228
|
+
export default {
|
|
229
|
+
markdown: {
|
|
230
|
+
syntaxHighlight: 'prism',
|
|
231
|
+
},
|
|
232
|
+
integrations: [mdx()],
|
|
233
|
+
}
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
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.
|
|
237
|
+
|
|
81
238
|
## Configuration
|
|
82
239
|
|
|
83
|
-
|
|
240
|
+
<details>
|
|
241
|
+
<summary><strong>remarkPlugins</strong></summary>
|
|
242
|
+
|
|
243
|
+
**Default plugins:** [remark-gfm](https://github.com/remarkjs/remark-gfm), [remark-smartypants](https://github.com/silvenon/remark-smartypants)
|
|
244
|
+
|
|
245
|
+
[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!
|
|
246
|
+
|
|
247
|
+
We apply [GitHub-flavored Markdown](https://github.com/remarkjs/remark-gfm) and [Smartypants](https://github.com/silvenon/remark-smartypants) by default. This brings some niceties like auto-generating clickable links from text (ex. `https://example.com`) and formatting quotes for readability. When applying your own plugins, you can choose to preserve or remove these defaults.
|
|
248
|
+
|
|
249
|
+
To apply plugins _while preserving_ Astro's default plugins, use a nested `extends` object like so:
|
|
250
|
+
|
|
251
|
+
```js
|
|
252
|
+
// astro.config.mjs
|
|
253
|
+
import remarkToc from 'remark-toc';
|
|
254
|
+
|
|
255
|
+
export default {
|
|
256
|
+
integrations: [mdx({
|
|
257
|
+
// apply remark-toc alongside GitHub-flavored markdown and Smartypants
|
|
258
|
+
remarkPlugins: { extends: [remarkToc] },
|
|
259
|
+
})],
|
|
260
|
+
}
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
To apply plugins _without_ Astro's defaults, you can apply a plain array:
|
|
264
|
+
|
|
265
|
+
```js
|
|
266
|
+
// astro.config.mjs
|
|
267
|
+
import remarkToc from 'remark-toc';
|
|
268
|
+
|
|
269
|
+
export default {
|
|
270
|
+
integrations: [mdx({
|
|
271
|
+
// apply remark-toc alone, removing other defaults
|
|
272
|
+
remarkPlugins: [remarkToc],
|
|
273
|
+
})],
|
|
274
|
+
}
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
</details>
|
|
278
|
+
|
|
279
|
+
<details>
|
|
280
|
+
<summary><strong>rehypePlugins</strong></summary>
|
|
281
|
+
|
|
282
|
+
**Default plugins:** none
|
|
283
|
+
|
|
284
|
+
[Rehype plugins](https://github.com/rehypejs/rehype/blob/main/doc/plugins.md) allow you to transform the HTML that your Markdown generates. We recommend checking the [Remark plugin](https://github.com/remarkjs/remark/blob/main/doc/plugins.md) catalog first _before_ considering rehype plugins, since most users want to transform their Markdown syntax instead. If HTML transforms are what you need, we encourage you to browse [awesome-rehype](https://github.com/rehypejs/awesome-rehype) for a full curated list of plugins!
|
|
285
|
+
|
|
286
|
+
To apply rehype plugins, use the `rehypePlugins` configuration option like so:
|
|
287
|
+
|
|
288
|
+
```js
|
|
289
|
+
// astro.config.mjs
|
|
290
|
+
import rehypeMinifyHtml from 'rehype-minify';
|
|
291
|
+
|
|
292
|
+
export default {
|
|
293
|
+
integrations: [mdx({
|
|
294
|
+
rehypePlugins: [rehypeMinifyHtml],
|
|
295
|
+
})],
|
|
296
|
+
}
|
|
297
|
+
```
|
|
298
|
+
</details>
|
|
299
|
+
|
|
300
|
+
<details>
|
|
301
|
+
<summary><strong>frontmatterOptions</strong></summary>
|
|
302
|
+
|
|
303
|
+
**Default:** `{ name: 'frontmatter' }`
|
|
304
|
+
|
|
305
|
+
We use [remark-mdx-frontmatter](https://github.com/remcohaszing/remark-mdx-frontmatter) to parse YAML-based frontmatter in your MDX files. If you want to override our default configuration or extend remark-mdx-frontmatter (ex. to [apply a custom frontmatter parser](https://github.com/remcohaszing/remark-mdx-frontmatter#parsers)), you can supply a `frontmatterOptions` configuration.
|
|
306
|
+
|
|
307
|
+
For example, say you want to access frontmatter as root-level variables without a nested `frontmatter` object. You can override the [`name` configuration option](https://github.com/remcohaszing/remark-mdx-frontmatter#name) like so:
|
|
308
|
+
|
|
309
|
+
```js
|
|
310
|
+
// astro.config.mjs
|
|
311
|
+
export default {
|
|
312
|
+
integrations: [mdx({
|
|
313
|
+
frontmatterOptions: {
|
|
314
|
+
name: '',
|
|
315
|
+
}
|
|
316
|
+
})],
|
|
317
|
+
}
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
```mdx
|
|
321
|
+
---
|
|
322
|
+
title: I'm just a variable now!
|
|
323
|
+
---
|
|
324
|
+
|
|
325
|
+
# {title}
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
See the [remark-mdx-frontmatter README](https://github.com/remcohaszing/remark-mdx-frontmatter#options) for a complete list of options.
|
|
329
|
+
</details>
|
|
84
330
|
|
|
85
331
|
## Examples
|
|
86
332
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,18 @@
|
|
|
1
|
+
import { Options as MdxRollupPluginOptions } from '@mdx-js/rollup';
|
|
1
2
|
import type { AstroIntegration } from 'astro';
|
|
2
|
-
|
|
3
|
+
import type { RemarkMdxFrontmatterOptions } from 'remark-mdx-frontmatter';
|
|
4
|
+
declare type WithExtends<T> = T | {
|
|
5
|
+
extends: T;
|
|
6
|
+
};
|
|
7
|
+
declare type MdxOptions = {
|
|
8
|
+
remarkPlugins?: WithExtends<MdxRollupPluginOptions['remarkPlugins']>;
|
|
9
|
+
rehypePlugins?: WithExtends<MdxRollupPluginOptions['rehypePlugins']>;
|
|
10
|
+
/**
|
|
11
|
+
* Configure the remark-mdx-frontmatter plugin
|
|
12
|
+
* @see https://github.com/remcohaszing/remark-mdx-frontmatter#options for a full list of options
|
|
13
|
+
* @default {{ name: 'frontmatter' }}
|
|
14
|
+
*/
|
|
15
|
+
frontmatterOptions?: RemarkMdxFrontmatterOptions;
|
|
16
|
+
};
|
|
17
|
+
export default function mdx(mdxOptions?: MdxOptions): AstroIntegration;
|
|
18
|
+
export {};
|
package/dist/index.js
CHANGED
|
@@ -1,31 +1,108 @@
|
|
|
1
|
+
import { nodeTypes } from "@mdx-js/mdx";
|
|
1
2
|
import mdxPlugin from "@mdx-js/rollup";
|
|
2
|
-
|
|
3
|
+
import { parse as parseESM } from "es-module-lexer";
|
|
4
|
+
import rehypeRaw from "rehype-raw";
|
|
5
|
+
import remarkFrontmatter from "remark-frontmatter";
|
|
6
|
+
import remarkGfm from "remark-gfm";
|
|
7
|
+
import remarkMdxFrontmatter from "remark-mdx-frontmatter";
|
|
8
|
+
import remarkShikiTwoslash from "remark-shiki-twoslash";
|
|
9
|
+
import remarkSmartypants from "remark-smartypants";
|
|
10
|
+
import remarkPrism from "./remark-prism.js";
|
|
11
|
+
import { getFileInfo, getFrontmatter } from "./utils.js";
|
|
12
|
+
const DEFAULT_REMARK_PLUGINS = [remarkGfm, remarkSmartypants];
|
|
13
|
+
function handleExtends(config, defaults = []) {
|
|
14
|
+
if (Array.isArray(config))
|
|
15
|
+
return config;
|
|
16
|
+
return [...defaults, ...(config == null ? void 0 : config.extends) ?? []];
|
|
17
|
+
}
|
|
18
|
+
function mdx(mdxOptions = {}) {
|
|
3
19
|
return {
|
|
4
20
|
name: "@astrojs/mdx",
|
|
5
21
|
hooks: {
|
|
6
|
-
"astro:config:setup": ({ updateConfig, addPageExtension, command }) => {
|
|
22
|
+
"astro:config:setup": ({ updateConfig, config, addPageExtension, command }) => {
|
|
7
23
|
addPageExtension(".mdx");
|
|
24
|
+
let remarkPlugins = handleExtends(mdxOptions.remarkPlugins, DEFAULT_REMARK_PLUGINS);
|
|
25
|
+
let rehypePlugins = handleExtends(mdxOptions.rehypePlugins);
|
|
26
|
+
if (config.markdown.syntaxHighlight === "shiki") {
|
|
27
|
+
remarkPlugins.push([
|
|
28
|
+
remarkShikiTwoslash.default,
|
|
29
|
+
config.markdown.shikiConfig
|
|
30
|
+
]);
|
|
31
|
+
rehypePlugins.push([rehypeRaw, { passThrough: nodeTypes }]);
|
|
32
|
+
}
|
|
33
|
+
if (config.markdown.syntaxHighlight === "prism") {
|
|
34
|
+
remarkPlugins.push(remarkPrism);
|
|
35
|
+
rehypePlugins.push([rehypeRaw, { passThrough: nodeTypes }]);
|
|
36
|
+
}
|
|
37
|
+
remarkPlugins.push(remarkFrontmatter);
|
|
38
|
+
remarkPlugins.push([
|
|
39
|
+
remarkMdxFrontmatter,
|
|
40
|
+
{
|
|
41
|
+
name: "frontmatter",
|
|
42
|
+
...mdxOptions.frontmatterOptions
|
|
43
|
+
}
|
|
44
|
+
]);
|
|
45
|
+
const configuredMdxPlugin = mdxPlugin({
|
|
46
|
+
remarkPlugins,
|
|
47
|
+
rehypePlugins,
|
|
48
|
+
jsx: true,
|
|
49
|
+
jsxImportSource: "astro",
|
|
50
|
+
format: "mdx",
|
|
51
|
+
mdExtensions: []
|
|
52
|
+
});
|
|
8
53
|
updateConfig({
|
|
9
54
|
vite: {
|
|
10
55
|
plugins: [
|
|
11
56
|
{
|
|
12
57
|
enforce: "pre",
|
|
13
|
-
...
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
58
|
+
...configuredMdxPlugin,
|
|
59
|
+
async transform(code, id) {
|
|
60
|
+
var _a, _b;
|
|
61
|
+
if (!id.endsWith(".mdx"))
|
|
62
|
+
return;
|
|
63
|
+
const mdxPluginTransform = (_a = configuredMdxPlugin.transform) == null ? void 0 : _a.bind(this);
|
|
64
|
+
if ((_b = mdxOptions.frontmatterOptions) == null ? void 0 : _b.parsers) {
|
|
65
|
+
return mdxPluginTransform == null ? void 0 : mdxPluginTransform(code, id);
|
|
66
|
+
}
|
|
67
|
+
const frontmatter = getFrontmatter(code, id);
|
|
68
|
+
if (frontmatter.layout) {
|
|
69
|
+
const { layout, ...content } = frontmatter;
|
|
70
|
+
code += `
|
|
71
|
+
export default async function({ children }) {
|
|
72
|
+
const Layout = (await import(${JSON.stringify(
|
|
73
|
+
frontmatter.layout
|
|
74
|
+
)})).default;
|
|
75
|
+
return <Layout content={${JSON.stringify(
|
|
76
|
+
content
|
|
77
|
+
)}}>{children}</Layout> }`;
|
|
78
|
+
}
|
|
79
|
+
return mdxPluginTransform == null ? void 0 : mdxPluginTransform(code, id);
|
|
80
|
+
}
|
|
19
81
|
},
|
|
20
|
-
|
|
82
|
+
{
|
|
21
83
|
name: "@astrojs/mdx",
|
|
22
84
|
transform(code, id) {
|
|
23
85
|
if (!id.endsWith(".mdx"))
|
|
24
86
|
return;
|
|
25
|
-
|
|
87
|
+
const [, moduleExports] = parseESM(code);
|
|
88
|
+
code += `
|
|
89
|
+
import "${"astro:scripts/page-ssr.js"}";`;
|
|
90
|
+
const { fileUrl, fileId } = getFileInfo(id, config);
|
|
91
|
+
if (!moduleExports.includes("url")) {
|
|
92
|
+
code += `
|
|
93
|
+
export const url = ${JSON.stringify(fileUrl)};`;
|
|
94
|
+
}
|
|
95
|
+
if (!moduleExports.includes("file")) {
|
|
96
|
+
code += `
|
|
97
|
+
export const file = ${JSON.stringify(fileId)};`;
|
|
98
|
+
}
|
|
99
|
+
if (command === "dev") {
|
|
100
|
+
code += `
|
|
26
101
|
if (import.meta.hot) {
|
|
27
102
|
import.meta.hot.decline();
|
|
28
103
|
}`;
|
|
104
|
+
}
|
|
105
|
+
return code;
|
|
29
106
|
}
|
|
30
107
|
}
|
|
31
108
|
]
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { addAstro } from "@astrojs/prism/internal";
|
|
2
|
+
import Prism from "prismjs";
|
|
3
|
+
import loadLanguages from "prismjs/components/index.js";
|
|
4
|
+
import { visit } from "unist-util-visit";
|
|
5
|
+
const languageMap = /* @__PURE__ */ new Map([["ts", "typescript"]]);
|
|
6
|
+
function runHighlighter(lang, code) {
|
|
7
|
+
let classLanguage = `language-${lang}`;
|
|
8
|
+
if (lang == null) {
|
|
9
|
+
lang = "plaintext";
|
|
10
|
+
}
|
|
11
|
+
const ensureLoaded = (language) => {
|
|
12
|
+
if (language && !Prism.languages[language]) {
|
|
13
|
+
loadLanguages([language]);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
if (languageMap.has(lang)) {
|
|
17
|
+
ensureLoaded(languageMap.get(lang));
|
|
18
|
+
} else if (lang === "astro") {
|
|
19
|
+
ensureLoaded("typescript");
|
|
20
|
+
addAstro(Prism);
|
|
21
|
+
} else {
|
|
22
|
+
ensureLoaded("markup-templating");
|
|
23
|
+
ensureLoaded(lang);
|
|
24
|
+
}
|
|
25
|
+
if (lang && !Prism.languages[lang]) {
|
|
26
|
+
console.warn(`Unable to load the language: ${lang}`);
|
|
27
|
+
}
|
|
28
|
+
const grammar = Prism.languages[lang];
|
|
29
|
+
let html = code;
|
|
30
|
+
if (grammar) {
|
|
31
|
+
html = Prism.highlight(code, grammar, lang);
|
|
32
|
+
}
|
|
33
|
+
return { classLanguage, html };
|
|
34
|
+
}
|
|
35
|
+
function remarkPrism() {
|
|
36
|
+
return (tree) => visit(tree, "code", (node) => {
|
|
37
|
+
let { lang, value } = node;
|
|
38
|
+
node.type = "html";
|
|
39
|
+
let { html, classLanguage } = runHighlighter(lang, value);
|
|
40
|
+
let classes = [classLanguage];
|
|
41
|
+
node.value = `<pre class="${classes.join(
|
|
42
|
+
" "
|
|
43
|
+
)}"><code class="${classLanguage}">${html}</code></pre>`;
|
|
44
|
+
return node;
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
export {
|
|
48
|
+
remarkPrism as default
|
|
49
|
+
};
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { AstroConfig } from 'astro';
|
|
2
|
+
interface FileInfo {
|
|
3
|
+
fileId: string;
|
|
4
|
+
fileUrl: string;
|
|
5
|
+
}
|
|
6
|
+
/** @see 'vite-plugin-utils' for source */
|
|
7
|
+
export declare function getFileInfo(id: string, config: AstroConfig): FileInfo;
|
|
8
|
+
/**
|
|
9
|
+
* Match YAML exception handling from Astro core errors
|
|
10
|
+
* @see 'astro/src/core/errors.ts'
|
|
11
|
+
*/
|
|
12
|
+
export declare function getFrontmatter(code: string, id: string): {
|
|
13
|
+
[key: string]: any;
|
|
14
|
+
};
|
|
15
|
+
export {};
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import matter from "gray-matter";
|
|
2
|
+
function appendForwardSlash(path) {
|
|
3
|
+
return path.endsWith("/") ? path : path + "/";
|
|
4
|
+
}
|
|
5
|
+
function getFileInfo(id, config) {
|
|
6
|
+
const sitePathname = appendForwardSlash(
|
|
7
|
+
config.site ? new URL(config.base, config.site).pathname : config.base
|
|
8
|
+
);
|
|
9
|
+
let url = void 0;
|
|
10
|
+
try {
|
|
11
|
+
url = new URL(`file://${id}`);
|
|
12
|
+
} catch {
|
|
13
|
+
}
|
|
14
|
+
const fileId = id.split("?")[0];
|
|
15
|
+
let fileUrl;
|
|
16
|
+
const isPage = fileId.includes("/pages/");
|
|
17
|
+
if (isPage) {
|
|
18
|
+
fileUrl = fileId.replace(/^.*?\/pages\//, sitePathname).replace(/(\/index)?\.mdx$/, "");
|
|
19
|
+
} else if (url && url.pathname.startsWith(config.root.pathname)) {
|
|
20
|
+
fileUrl = url.pathname.slice(config.root.pathname.length);
|
|
21
|
+
} else {
|
|
22
|
+
fileUrl = fileId;
|
|
23
|
+
}
|
|
24
|
+
if (fileUrl && config.trailingSlash === "always") {
|
|
25
|
+
fileUrl = appendForwardSlash(fileUrl);
|
|
26
|
+
}
|
|
27
|
+
return { fileId, fileUrl };
|
|
28
|
+
}
|
|
29
|
+
function getFrontmatter(code, id) {
|
|
30
|
+
try {
|
|
31
|
+
return matter(code).data;
|
|
32
|
+
} catch (e) {
|
|
33
|
+
if (e.name === "YAMLException") {
|
|
34
|
+
const err = e;
|
|
35
|
+
err.id = id;
|
|
36
|
+
err.loc = { file: e.id, line: e.mark.line + 1, column: e.mark.column };
|
|
37
|
+
err.message = e.reason;
|
|
38
|
+
throw err;
|
|
39
|
+
} else {
|
|
40
|
+
throw e;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export {
|
|
45
|
+
getFileInfo,
|
|
46
|
+
getFrontmatter
|
|
47
|
+
};
|
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.4.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
7
7
|
"author": "withastro",
|
|
@@ -24,17 +24,31 @@
|
|
|
24
24
|
"./package.json": "./package.json"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@
|
|
27
|
+
"@astrojs/prism": "^0.6.1",
|
|
28
|
+
"@mdx-js/mdx": "^2.1.2",
|
|
29
|
+
"@mdx-js/rollup": "^2.1.1",
|
|
30
|
+
"es-module-lexer": "^0.10.5",
|
|
31
|
+
"gray-matter": "^4.0.3",
|
|
32
|
+
"prismjs": "^1.28.0",
|
|
33
|
+
"rehype-raw": "^6.1.1",
|
|
34
|
+
"remark-frontmatter": "^4.0.1",
|
|
35
|
+
"remark-gfm": "^3.0.1",
|
|
36
|
+
"remark-mdx-frontmatter": "^2.0.2",
|
|
37
|
+
"remark-shiki-twoslash": "^3.1.0",
|
|
38
|
+
"remark-smartypants": "^2.0.0",
|
|
39
|
+
"shiki": "^0.10.1",
|
|
40
|
+
"unist-util-visit": "^4.1.0"
|
|
28
41
|
},
|
|
29
42
|
"devDependencies": {
|
|
30
43
|
"@types/chai": "^4.3.1",
|
|
31
44
|
"@types/mocha": "^9.1.1",
|
|
32
45
|
"@types/yargs-parser": "^21.0.0",
|
|
33
|
-
"astro": "1.0.0-
|
|
46
|
+
"astro": "1.0.0-rc.3",
|
|
34
47
|
"astro-scripts": "0.0.6",
|
|
35
48
|
"chai": "^4.3.6",
|
|
49
|
+
"linkedom": "^0.14.12",
|
|
36
50
|
"mocha": "^9.2.2",
|
|
37
|
-
"
|
|
51
|
+
"remark-toc": "^8.0.1"
|
|
38
52
|
},
|
|
39
53
|
"engines": {
|
|
40
54
|
"node": "^14.18.0 || >=16.12.0"
|