@astrojs/mdx 0.11.0 → 0.11.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 +2 -2
- package/CHANGELOG.md +14 -0
- package/README.md +59 -8
- package/dist/index.d.ts +13 -1
- package/dist/index.js +21 -9
- package/dist/plugins.d.ts +11 -0
- package/dist/plugins.js +214 -0
- package/dist/utils.d.ts +0 -16
- package/dist/utils.js +0 -83
- package/package.json +5 -3
- package/src/index.ts +41 -9
- package/src/plugins.ts +273 -0
- package/src/utils.ts +0 -111
- package/test/fixtures/mdx-vite-env-vars/astro.config.mjs +9 -0
- package/test/fixtures/mdx-vite-env-vars/node_modules/.bin/astro +17 -0
- package/test/fixtures/mdx-vite-env-vars/package.json +7 -0
- package/test/fixtures/mdx-vite-env-vars/src/pages/frontmatter.json.js +7 -0
- package/test/fixtures/mdx-vite-env-vars/src/pages/vite-env-vars.mdx +38 -0
- package/test/mdx-plugins.test.js +11 -0
- package/test/mdx-vite-env-vars.test.js +54 -0
- package/dist/astro-data-utils.d.ts +0 -9
- package/dist/astro-data-utils.js +0 -86
- package/src/astro-data-utils.ts +0 -103
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 [2mefbc0a3c2d5779f7[0m
|
|
2
2
|
[34m@astrojs/mdx:build: [0m
|
|
3
|
-
[34m@astrojs/mdx:build: [0m> @astrojs/mdx@0.11.
|
|
3
|
+
[34m@astrojs/mdx:build: [0m> @astrojs/mdx@0.11.2 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,19 @@
|
|
|
1
1
|
# @astrojs/mdx
|
|
2
2
|
|
|
3
|
+
## 0.11.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#4700](https://github.com/withastro/astro/pull/4700) [`e5f71142e`](https://github.com/withastro/astro/commit/e5f71142eb62bd72456e889dad5774347c3753f2) Thanks [@bholmesdev](https://github.com/bholmesdev)! - Document MDXLayoutProps utility type
|
|
8
|
+
|
|
9
|
+
- [#4858](https://github.com/withastro/astro/pull/4858) [`58a2dca22`](https://github.com/withastro/astro/commit/58a2dca2286cb14f6211cf51267c02447e78433a) Thanks [@bholmesdev](https://github.com/bholmesdev)! - Correctly parse import.meta.env in MDX files
|
|
10
|
+
|
|
11
|
+
## 0.11.1
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- [#4588](https://github.com/withastro/astro/pull/4588) [`db38f61b2`](https://github.com/withastro/astro/commit/db38f61b2b2dc55f03b28797d19b163b1940f1c8) Thanks [@bholmesdev](https://github.com/bholmesdev)! - Fix: Add GFM and Smartypants to MDX by default
|
|
16
|
+
|
|
3
17
|
## 0.11.0
|
|
4
18
|
|
|
5
19
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -26,16 +26,14 @@ The `astro add` command-line tool automates the installation for you. Run one of
|
|
|
26
26
|
|
|
27
27
|
```sh
|
|
28
28
|
# Using NPM
|
|
29
|
-
|
|
29
|
+
npx astro add mdx
|
|
30
30
|
# Using Yarn
|
|
31
31
|
yarn astro add mdx
|
|
32
32
|
# Using PNPM
|
|
33
33
|
pnpm astro add mdx
|
|
34
34
|
```
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
Because this command is new, it might not properly set things up. If that happens, [feel free to log an issue on our GitHub](https://github.com/withastro/astro/issues) and try the manual installation steps below.
|
|
36
|
+
If you run into any issues, [feel free to report them to us on GitHub](https://github.com/withastro/astro/issues) and try the manual installation steps below.
|
|
39
37
|
|
|
40
38
|
### Manual Install
|
|
41
39
|
|
|
@@ -61,6 +59,16 @@ export default defineConfig({
|
|
|
61
59
|
|
|
62
60
|
Finally, restart the dev server.
|
|
63
61
|
|
|
62
|
+
### Editor Integration
|
|
63
|
+
|
|
64
|
+
[VS Code](https://code.visualstudio.com/) supports Markdown by default. However, for MDX editor support, you may wish to add the following setting in your VSCode config. This ensures authoring MDX files provides a Markdown-like editor experience.
|
|
65
|
+
|
|
66
|
+
```json title=".vscode/settings.json"
|
|
67
|
+
"files.associations": {
|
|
68
|
+
"*.mdx": "markdown"
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
64
72
|
## Usage
|
|
65
73
|
|
|
66
74
|
You can [add MDX pages to your project](https://docs.astro.build/en/guides/markdown-content/#markdown-and-mdx-pages) by adding `.mdx` files within your `src/pages/` directory.
|
|
@@ -70,7 +78,7 @@ You can [add MDX pages to your project](https://docs.astro.build/en/guides/markd
|
|
|
70
78
|
To use components in your MDX pages in Astro, head to our [UI framework documentation][astro-ui-frameworks]. You'll explore:
|
|
71
79
|
- 📦 how framework components are loaded,
|
|
72
80
|
- 💧 client-side hydration options, and
|
|
73
|
-
-
|
|
81
|
+
- 🤝 opportunities to mix and nest frameworks together
|
|
74
82
|
|
|
75
83
|
[**Client Directives**](https://docs.astro.build/en/reference/directives-reference/#client-directives) are still required in `.mdx` files.
|
|
76
84
|
|
|
@@ -195,15 +203,16 @@ title: 'My Blog Post'
|
|
|
195
203
|
---
|
|
196
204
|
```
|
|
197
205
|
|
|
198
|
-
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)
|
|
206
|
+
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.
|
|
199
207
|
|
|
200
208
|
```astro
|
|
201
209
|
---
|
|
202
210
|
// src/layouts/BaseLayout.astro
|
|
203
|
-
const { frontmatter } = Astro.props;
|
|
211
|
+
const { frontmatter, url } = Astro.props;
|
|
204
212
|
---
|
|
205
213
|
<html>
|
|
206
214
|
<head>
|
|
215
|
+
<meta rel="canonical" href={new URL(url, Astro.site).pathname}>
|
|
207
216
|
<title>{frontmatter.title}</title>
|
|
208
217
|
</head>
|
|
209
218
|
<body>
|
|
@@ -214,6 +223,48 @@ const { frontmatter } = Astro.props;
|
|
|
214
223
|
</html>
|
|
215
224
|
```
|
|
216
225
|
|
|
226
|
+
You can set a layout’s [`Props` type](/en/guides/typescript/#component-props) with the `MDXLayoutProps` helper.
|
|
227
|
+
|
|
228
|
+
:::note
|
|
229
|
+
`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.
|
|
230
|
+
:::
|
|
231
|
+
|
|
232
|
+
```astro ins={2,4-9}
|
|
233
|
+
---
|
|
234
|
+
// src/layouts/BaseLayout.astro
|
|
235
|
+
import type { MDXLayoutProps } from 'astro';
|
|
236
|
+
|
|
237
|
+
type Props = MDXLayoutProps<{
|
|
238
|
+
// Define frontmatter props here
|
|
239
|
+
title: string;
|
|
240
|
+
author: string;
|
|
241
|
+
date: string;
|
|
242
|
+
}>;
|
|
243
|
+
|
|
244
|
+
// Now, `frontmatter`, `url`, and other MDX layout properties
|
|
245
|
+
// are accessible with type safety
|
|
246
|
+
const { frontmatter, url } = Astro.props;
|
|
247
|
+
---
|
|
248
|
+
<html>
|
|
249
|
+
<head>
|
|
250
|
+
<meta rel="canonical" href={new URL(url, Astro.site).pathname}>
|
|
251
|
+
<title>{frontmatter.title}</title>
|
|
252
|
+
</head>
|
|
253
|
+
<body>
|
|
254
|
+
<h1>{frontmatter.title}</h1>
|
|
255
|
+
<slot />
|
|
256
|
+
</body>
|
|
257
|
+
</html>
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
#### Layout props
|
|
261
|
+
|
|
262
|
+
All [exported properties](#exported-properties) are available from `Astro.props` in your layout, **with two key differences:**
|
|
263
|
+
- Heading information (i.e. `h1 -> h6` elements) is available via the `headings` array, rather than a `getHeadings()` function.
|
|
264
|
+
- `file` and `url` are _also_ available as nested `frontmatter` properties (i.e. `frontmatter.url` and `frontmatter.file`). This is consistent with Astro's Markdown layout properties.
|
|
265
|
+
|
|
266
|
+
Astro recommends using the `MDXLayoutProps` type (see previous section) to explore all available properties.
|
|
267
|
+
|
|
217
268
|
#### Importing layouts manually
|
|
218
269
|
|
|
219
270
|
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:
|
|
@@ -460,7 +511,7 @@ export default {
|
|
|
460
511
|
|
|
461
512
|
## Troubleshooting
|
|
462
513
|
|
|
463
|
-
For help, check out the `#support
|
|
514
|
+
For help, check out the `#support` channel on [Discord](https://astro.build/chat). Our friendly Support Squad members are here to help!
|
|
464
515
|
|
|
465
516
|
You can also check our [Astro Integration Documentation][astro-integration] for more on integrations.
|
|
466
517
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
import { PluggableList } from '@mdx-js/mdx/lib/core.js';
|
|
1
2
|
import type { AstroIntegration } from 'astro';
|
|
2
|
-
|
|
3
|
+
export declare type MdxOptions = {
|
|
4
|
+
remarkPlugins?: PluggableList;
|
|
5
|
+
rehypePlugins?: PluggableList;
|
|
6
|
+
/**
|
|
7
|
+
* Choose which remark and rehype plugins to inherit, if any.
|
|
8
|
+
*
|
|
9
|
+
* - "markdown" (default) - inherit your project’s markdown plugin config ([see Markdown docs](https://docs.astro.build/en/guides/markdown-content/#configuring-markdown))
|
|
10
|
+
* - "astroDefaults" - inherit Astro’s default plugins only ([see defaults](https://docs.astro.build/en/reference/configuration-reference/#markdownextenddefaultplugins))
|
|
11
|
+
* - false - do not inherit any plugins
|
|
12
|
+
*/
|
|
13
|
+
extendPlugins?: 'markdown' | 'astroDefaults' | false;
|
|
14
|
+
};
|
|
3
15
|
export default function mdx(mdxOptions?: MdxOptions): AstroIntegration;
|
package/dist/index.js
CHANGED
|
@@ -2,15 +2,15 @@ import { compile as mdxCompile } from "@mdx-js/mdx";
|
|
|
2
2
|
import mdxPlugin from "@mdx-js/rollup";
|
|
3
3
|
import { parse as parseESM } from "es-module-lexer";
|
|
4
4
|
import { blue, bold } from "kleur/colors";
|
|
5
|
+
import fs from "node:fs/promises";
|
|
5
6
|
import { VFile } from "vfile";
|
|
6
|
-
import { rehypeApplyFrontmatterExport } from "./astro-data-utils.js";
|
|
7
7
|
import {
|
|
8
|
-
getFileInfo,
|
|
9
8
|
getRehypePlugins,
|
|
10
9
|
getRemarkPlugins,
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
} from "./
|
|
10
|
+
recmaInjectImportMetaEnvPlugin,
|
|
11
|
+
rehypeApplyFrontmatterExport
|
|
12
|
+
} from "./plugins.js";
|
|
13
|
+
import { getFileInfo, handleExtendsNotSupported, parseFrontmatter } from "./utils.js";
|
|
14
14
|
const RAW_CONTENT_ERROR = "MDX does not support rawContent()! If you need to read the Markdown contents to calculate values (ex. reading time), we suggest injecting frontmatter via remark plugins. Learn more on our docs: https://docs.astro.build/en/guides/integrations-guide/mdx/#inject-frontmatter-via-remark-or-rehype-plugins";
|
|
15
15
|
const COMPILED_CONTENT_ERROR = "MDX does not support compiledContent()! If you need to read the HTML contents to calculate values (ex. reading time), we suggest injecting frontmatter via rehype plugins. Learn more on our docs: https://docs.astro.build/en/guides/integrations-guide/mdx/#inject-frontmatter-via-remark-or-rehype-plugins";
|
|
16
16
|
function mdx(mdxOptions = {}) {
|
|
@@ -42,25 +42,34 @@ function mdx(mdxOptions = {}) {
|
|
|
42
42
|
format: "mdx",
|
|
43
43
|
mdExtensions: []
|
|
44
44
|
};
|
|
45
|
+
let importMetaEnv = {
|
|
46
|
+
SITE: config.site
|
|
47
|
+
};
|
|
45
48
|
updateConfig({
|
|
46
49
|
vite: {
|
|
47
50
|
plugins: [
|
|
48
51
|
{
|
|
49
52
|
enforce: "pre",
|
|
50
53
|
...mdxPlugin(mdxPluginOpts),
|
|
51
|
-
|
|
54
|
+
configResolved(resolved) {
|
|
55
|
+
importMetaEnv = { ...importMetaEnv, ...resolved.env };
|
|
56
|
+
},
|
|
57
|
+
async transform(_, id) {
|
|
52
58
|
if (!id.endsWith("mdx"))
|
|
53
59
|
return;
|
|
60
|
+
const { fileId } = getFileInfo(id, config);
|
|
61
|
+
const code = await fs.readFile(fileId, "utf-8");
|
|
54
62
|
const { data: frontmatter, content: pageContent } = parseFrontmatter(code, id);
|
|
55
63
|
const compiled = await mdxCompile(new VFile({ value: pageContent, path: id }), {
|
|
56
64
|
...mdxPluginOpts,
|
|
57
65
|
rehypePlugins: [
|
|
58
66
|
...mdxPluginOpts.rehypePlugins ?? [],
|
|
59
67
|
() => rehypeApplyFrontmatterExport(frontmatter)
|
|
60
|
-
]
|
|
68
|
+
],
|
|
69
|
+
recmaPlugins: [() => recmaInjectImportMetaEnvPlugin({ importMetaEnv })]
|
|
61
70
|
});
|
|
62
71
|
return {
|
|
63
|
-
code: String(compiled.value),
|
|
72
|
+
code: escapeViteEnvReferences(String(compiled.value)),
|
|
64
73
|
map: compiled.map
|
|
65
74
|
};
|
|
66
75
|
}
|
|
@@ -104,7 +113,7 @@ if (import.meta.hot) {
|
|
|
104
113
|
import.meta.hot.decline();
|
|
105
114
|
}`;
|
|
106
115
|
}
|
|
107
|
-
return code;
|
|
116
|
+
return escapeViteEnvReferences(code);
|
|
108
117
|
}
|
|
109
118
|
}
|
|
110
119
|
]
|
|
@@ -114,6 +123,9 @@ if (import.meta.hot) {
|
|
|
114
123
|
}
|
|
115
124
|
};
|
|
116
125
|
}
|
|
126
|
+
function escapeViteEnvReferences(code) {
|
|
127
|
+
return code.replace(/import\.meta\.env/g, "import\\u002Emeta.env");
|
|
128
|
+
}
|
|
117
129
|
export {
|
|
118
130
|
mdx as default
|
|
119
131
|
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Options as MdxRollupPluginOptions } from '@mdx-js/rollup';
|
|
2
|
+
import type { AstroConfig } from 'astro';
|
|
3
|
+
import type { VFile } from 'vfile';
|
|
4
|
+
import { MdxOptions } from './index.js';
|
|
5
|
+
export declare function recmaInjectImportMetaEnvPlugin({ importMetaEnv, }: {
|
|
6
|
+
importMetaEnv: Record<string, any>;
|
|
7
|
+
}): (tree: any) => void;
|
|
8
|
+
export declare function remarkInitializeAstroData(): (tree: any, vfile: VFile) => void;
|
|
9
|
+
export declare function rehypeApplyFrontmatterExport(pageFrontmatter: Record<string, any>): (tree: any, vfile: VFile) => void;
|
|
10
|
+
export declare function getRemarkPlugins(mdxOptions: MdxOptions, config: AstroConfig): Promise<MdxRollupPluginOptions['remarkPlugins']>;
|
|
11
|
+
export declare function getRehypePlugins(mdxOptions: MdxOptions, config: AstroConfig): MdxRollupPluginOptions['rehypePlugins'];
|
package/dist/plugins.js
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { nodeTypes } from "@mdx-js/mdx";
|
|
2
|
+
import { visit as estreeVisit } from "estree-util-visit";
|
|
3
|
+
import { bold, yellow } from "kleur/colors";
|
|
4
|
+
import rehypeRaw from "rehype-raw";
|
|
5
|
+
import remarkGfm from "remark-gfm";
|
|
6
|
+
import remarkSmartypants from "remark-smartypants";
|
|
7
|
+
import rehypeCollectHeadings from "./rehype-collect-headings.js";
|
|
8
|
+
import remarkPrism from "./remark-prism.js";
|
|
9
|
+
import remarkShiki from "./remark-shiki.js";
|
|
10
|
+
import { jsToTreeNode } from "./utils.js";
|
|
11
|
+
function recmaInjectImportMetaEnvPlugin({
|
|
12
|
+
importMetaEnv
|
|
13
|
+
}) {
|
|
14
|
+
return (tree) => {
|
|
15
|
+
estreeVisit(tree, (node) => {
|
|
16
|
+
if (node.type === "MemberExpression") {
|
|
17
|
+
const envVarName = getImportMetaEnvVariableName(node);
|
|
18
|
+
if (typeof envVarName === "string") {
|
|
19
|
+
for (const key in node) {
|
|
20
|
+
delete node[key];
|
|
21
|
+
}
|
|
22
|
+
const envVarLiteral = {
|
|
23
|
+
type: "Literal",
|
|
24
|
+
value: importMetaEnv[envVarName],
|
|
25
|
+
raw: JSON.stringify(importMetaEnv[envVarName])
|
|
26
|
+
};
|
|
27
|
+
Object.assign(node, envVarLiteral);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function remarkInitializeAstroData() {
|
|
34
|
+
return function(tree, vfile) {
|
|
35
|
+
if (!vfile.data.astro) {
|
|
36
|
+
vfile.data.astro = { frontmatter: {} };
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
const EXPORT_NAME = "frontmatter";
|
|
41
|
+
function rehypeApplyFrontmatterExport(pageFrontmatter) {
|
|
42
|
+
return function(tree, vfile) {
|
|
43
|
+
const { frontmatter: injectedFrontmatter } = safelyGetAstroData(vfile.data);
|
|
44
|
+
const frontmatter = { ...injectedFrontmatter, ...pageFrontmatter };
|
|
45
|
+
const exportNodes = [
|
|
46
|
+
jsToTreeNode(`export const ${EXPORT_NAME} = ${JSON.stringify(frontmatter)};`)
|
|
47
|
+
];
|
|
48
|
+
if (frontmatter.layout) {
|
|
49
|
+
exportNodes.unshift(
|
|
50
|
+
jsToTreeNode(
|
|
51
|
+
`import { jsx as layoutJsx } from 'astro/jsx-runtime';
|
|
52
|
+
|
|
53
|
+
export default async function ({ children }) {
|
|
54
|
+
const Layout = (await import(${JSON.stringify(frontmatter.layout)})).default;
|
|
55
|
+
const { layout, ...content } = frontmatter;
|
|
56
|
+
content.file = file;
|
|
57
|
+
content.url = url;
|
|
58
|
+
content.astro = {};
|
|
59
|
+
Object.defineProperty(content.astro, 'headings', {
|
|
60
|
+
get() {
|
|
61
|
+
throw new Error('The "astro" property is no longer supported! To access "headings" from your layout, try using "Astro.props.headings."')
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
Object.defineProperty(content.astro, 'html', {
|
|
65
|
+
get() {
|
|
66
|
+
throw new Error('The "astro" property is no longer supported! To access "html" from your layout, try using "Astro.props.compiledContent()."')
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
Object.defineProperty(content.astro, 'source', {
|
|
70
|
+
get() {
|
|
71
|
+
throw new Error('The "astro" property is no longer supported! To access "source" from your layout, try using "Astro.props.rawContent()."')
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
return layoutJsx(Layout, {
|
|
75
|
+
file,
|
|
76
|
+
url,
|
|
77
|
+
content,
|
|
78
|
+
frontmatter: content,
|
|
79
|
+
headings: getHeadings(),
|
|
80
|
+
'server:root': true,
|
|
81
|
+
children,
|
|
82
|
+
});
|
|
83
|
+
};`
|
|
84
|
+
)
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
tree.children = exportNodes.concat(tree.children);
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
const DEFAULT_REMARK_PLUGINS = [remarkGfm, remarkSmartypants];
|
|
91
|
+
const DEFAULT_REHYPE_PLUGINS = [];
|
|
92
|
+
async function getRemarkPlugins(mdxOptions, config) {
|
|
93
|
+
let remarkPlugins = [
|
|
94
|
+
remarkInitializeAstroData
|
|
95
|
+
];
|
|
96
|
+
switch (mdxOptions.extendPlugins) {
|
|
97
|
+
case false:
|
|
98
|
+
break;
|
|
99
|
+
case "astroDefaults":
|
|
100
|
+
remarkPlugins = [...remarkPlugins, ...DEFAULT_REMARK_PLUGINS];
|
|
101
|
+
break;
|
|
102
|
+
default:
|
|
103
|
+
remarkPlugins = [
|
|
104
|
+
...remarkPlugins,
|
|
105
|
+
...markdownShouldExtendDefaultPlugins(config) ? DEFAULT_REMARK_PLUGINS : [],
|
|
106
|
+
...ignoreStringPlugins(config.markdown.remarkPlugins ?? [])
|
|
107
|
+
];
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
if (config.markdown.syntaxHighlight === "shiki") {
|
|
111
|
+
remarkPlugins.push([await remarkShiki(config.markdown.shikiConfig)]);
|
|
112
|
+
}
|
|
113
|
+
if (config.markdown.syntaxHighlight === "prism") {
|
|
114
|
+
remarkPlugins.push(remarkPrism);
|
|
115
|
+
}
|
|
116
|
+
remarkPlugins = [...remarkPlugins, ...mdxOptions.remarkPlugins ?? []];
|
|
117
|
+
return remarkPlugins;
|
|
118
|
+
}
|
|
119
|
+
function getRehypePlugins(mdxOptions, config) {
|
|
120
|
+
let rehypePlugins = [
|
|
121
|
+
rehypeCollectHeadings,
|
|
122
|
+
[rehypeRaw, { passThrough: nodeTypes }]
|
|
123
|
+
];
|
|
124
|
+
switch (mdxOptions.extendPlugins) {
|
|
125
|
+
case false:
|
|
126
|
+
break;
|
|
127
|
+
case "astroDefaults":
|
|
128
|
+
rehypePlugins = [...rehypePlugins, ...DEFAULT_REHYPE_PLUGINS];
|
|
129
|
+
break;
|
|
130
|
+
default:
|
|
131
|
+
rehypePlugins = [
|
|
132
|
+
...rehypePlugins,
|
|
133
|
+
...markdownShouldExtendDefaultPlugins(config) ? DEFAULT_REHYPE_PLUGINS : [],
|
|
134
|
+
...ignoreStringPlugins(config.markdown.rehypePlugins ?? [])
|
|
135
|
+
];
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
rehypePlugins = [...rehypePlugins, ...mdxOptions.rehypePlugins ?? []];
|
|
139
|
+
return rehypePlugins;
|
|
140
|
+
}
|
|
141
|
+
function markdownShouldExtendDefaultPlugins(config) {
|
|
142
|
+
return config.markdown.extendDefaultPlugins || config.markdown.remarkPlugins.length === 0 && config.markdown.rehypePlugins.length === 0;
|
|
143
|
+
}
|
|
144
|
+
function ignoreStringPlugins(plugins) {
|
|
145
|
+
let validPlugins = [];
|
|
146
|
+
let hasInvalidPlugin = false;
|
|
147
|
+
for (const plugin of plugins) {
|
|
148
|
+
if (typeof plugin === "string") {
|
|
149
|
+
console.warn(yellow(`[MDX] ${bold(plugin)} not applied.`));
|
|
150
|
+
hasInvalidPlugin = true;
|
|
151
|
+
} else if (Array.isArray(plugin) && typeof plugin[0] === "string") {
|
|
152
|
+
console.warn(yellow(`[MDX] ${bold(plugin[0])} not applied.`));
|
|
153
|
+
hasInvalidPlugin = true;
|
|
154
|
+
} else {
|
|
155
|
+
validPlugins.push(plugin);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
if (hasInvalidPlugin) {
|
|
159
|
+
console.warn(
|
|
160
|
+
`To inherit Markdown plugins in MDX, please use explicit imports in your config instead of "strings." See Markdown docs: https://docs.astro.build/en/guides/markdown-content/#markdown-plugins`
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
return validPlugins;
|
|
164
|
+
}
|
|
165
|
+
function isValidAstroData(obj) {
|
|
166
|
+
if (typeof obj === "object" && obj !== null && obj.hasOwnProperty("frontmatter")) {
|
|
167
|
+
const { frontmatter } = obj;
|
|
168
|
+
try {
|
|
169
|
+
JSON.stringify(frontmatter);
|
|
170
|
+
} catch {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
return typeof frontmatter === "object" && frontmatter !== null;
|
|
174
|
+
}
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
function safelyGetAstroData(vfileData) {
|
|
178
|
+
const { astro } = vfileData;
|
|
179
|
+
if (!astro)
|
|
180
|
+
return { frontmatter: {} };
|
|
181
|
+
if (!isValidAstroData(astro)) {
|
|
182
|
+
throw Error(
|
|
183
|
+
`[MDX] A remark or rehype plugin tried to add invalid frontmatter. Ensure "astro.frontmatter" is a JSON object!`
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
return astro;
|
|
187
|
+
}
|
|
188
|
+
function getImportMetaEnvVariableName(node) {
|
|
189
|
+
try {
|
|
190
|
+
if (node.object.type !== "MemberExpression" || node.property.type !== "Identifier")
|
|
191
|
+
return new Error();
|
|
192
|
+
const nestedExpression = node.object;
|
|
193
|
+
if (nestedExpression.property.type !== "Identifier" || nestedExpression.property.name !== "env")
|
|
194
|
+
return new Error();
|
|
195
|
+
const envExpression = nestedExpression.object;
|
|
196
|
+
if (envExpression.type !== "MetaProperty" || envExpression.property.type !== "Identifier" || envExpression.property.name !== "meta")
|
|
197
|
+
return new Error();
|
|
198
|
+
if (envExpression.meta.name !== "import")
|
|
199
|
+
return new Error();
|
|
200
|
+
return node.property.name;
|
|
201
|
+
} catch (e) {
|
|
202
|
+
if (e instanceof Error) {
|
|
203
|
+
return e;
|
|
204
|
+
}
|
|
205
|
+
return new Error("Unknown parsing error");
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
export {
|
|
209
|
+
getRehypePlugins,
|
|
210
|
+
getRemarkPlugins,
|
|
211
|
+
recmaInjectImportMetaEnvPlugin,
|
|
212
|
+
rehypeApplyFrontmatterExport,
|
|
213
|
+
remarkInitializeAstroData
|
|
214
|
+
};
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,21 +1,7 @@
|
|
|
1
|
-
import type { PluggableList } from '@mdx-js/mdx/lib/core.js';
|
|
2
|
-
import type { Options as MdxRollupPluginOptions } from '@mdx-js/rollup';
|
|
3
1
|
import type { Options as AcornOpts } from 'acorn';
|
|
4
2
|
import type { AstroConfig } from 'astro';
|
|
5
3
|
import matter from 'gray-matter';
|
|
6
4
|
import type { MdxjsEsm } from 'mdast-util-mdx';
|
|
7
|
-
export declare type MdxOptions = {
|
|
8
|
-
remarkPlugins?: PluggableList;
|
|
9
|
-
rehypePlugins?: PluggableList;
|
|
10
|
-
/**
|
|
11
|
-
* Choose which remark and rehype plugins to inherit, if any.
|
|
12
|
-
*
|
|
13
|
-
* - "markdown" (default) - inherit your project’s markdown plugin config ([see Markdown docs](https://docs.astro.build/en/guides/markdown-content/#configuring-markdown))
|
|
14
|
-
* - "astroDefaults" - inherit Astro’s default plugins only ([see defaults](https://docs.astro.build/en/reference/configuration-reference/#markdownextenddefaultplugins))
|
|
15
|
-
* - false - do not inherit any plugins
|
|
16
|
-
*/
|
|
17
|
-
extendPlugins?: 'markdown' | 'astroDefaults' | false;
|
|
18
|
-
};
|
|
19
5
|
interface FileInfo {
|
|
20
6
|
fileId: string;
|
|
21
7
|
fileUrl: string;
|
|
@@ -28,7 +14,5 @@ export declare function getFileInfo(id: string, config: AstroConfig): FileInfo;
|
|
|
28
14
|
*/
|
|
29
15
|
export declare function parseFrontmatter(code: string, id: string): matter.GrayMatterFile<string>;
|
|
30
16
|
export declare function jsToTreeNode(jsString: string, acornOpts?: AcornOpts): MdxjsEsm;
|
|
31
|
-
export declare function getRemarkPlugins(mdxOptions: MdxOptions, config: AstroConfig): Promise<MdxRollupPluginOptions['remarkPlugins']>;
|
|
32
|
-
export declare function getRehypePlugins(mdxOptions: MdxOptions, config: AstroConfig): MdxRollupPluginOptions['rehypePlugins'];
|
|
33
17
|
export declare function handleExtendsNotSupported(pluginConfig: any): void;
|
|
34
18
|
export {};
|
package/dist/utils.js
CHANGED
|
@@ -1,19 +1,8 @@
|
|
|
1
|
-
import { nodeTypes } from "@mdx-js/mdx";
|
|
2
1
|
import { parse } from "acorn";
|
|
3
2
|
import matter from "gray-matter";
|
|
4
|
-
import { bold, yellow } from "kleur/colors";
|
|
5
|
-
import rehypeRaw from "rehype-raw";
|
|
6
|
-
import remarkGfm from "remark-gfm";
|
|
7
|
-
import remarkSmartypants from "remark-smartypants";
|
|
8
|
-
import { remarkInitializeAstroData } from "./astro-data-utils.js";
|
|
9
|
-
import rehypeCollectHeadings from "./rehype-collect-headings.js";
|
|
10
|
-
import remarkPrism from "./remark-prism.js";
|
|
11
|
-
import remarkShiki from "./remark-shiki.js";
|
|
12
3
|
function appendForwardSlash(path) {
|
|
13
4
|
return path.endsWith("/") ? path : path + "/";
|
|
14
5
|
}
|
|
15
|
-
const DEFAULT_REMARK_PLUGINS = [remarkGfm, remarkSmartypants];
|
|
16
|
-
const DEFAULT_REHYPE_PLUGINS = [];
|
|
17
6
|
function getFileInfo(id, config) {
|
|
18
7
|
const sitePathname = appendForwardSlash(
|
|
19
8
|
config.site ? new URL(config.base, config.site).pathname : config.base
|
|
@@ -70,76 +59,6 @@ function jsToTreeNode(jsString, acornOpts = {
|
|
|
70
59
|
}
|
|
71
60
|
};
|
|
72
61
|
}
|
|
73
|
-
async function getRemarkPlugins(mdxOptions, config) {
|
|
74
|
-
let remarkPlugins = [
|
|
75
|
-
remarkInitializeAstroData
|
|
76
|
-
];
|
|
77
|
-
switch (mdxOptions.extendPlugins) {
|
|
78
|
-
case false:
|
|
79
|
-
break;
|
|
80
|
-
case "astroDefaults":
|
|
81
|
-
remarkPlugins = [...remarkPlugins, ...DEFAULT_REMARK_PLUGINS];
|
|
82
|
-
break;
|
|
83
|
-
default:
|
|
84
|
-
remarkPlugins = [
|
|
85
|
-
...remarkPlugins,
|
|
86
|
-
...config.markdown.extendDefaultPlugins ? DEFAULT_REMARK_PLUGINS : [],
|
|
87
|
-
...ignoreStringPlugins(config.markdown.remarkPlugins ?? [])
|
|
88
|
-
];
|
|
89
|
-
break;
|
|
90
|
-
}
|
|
91
|
-
if (config.markdown.syntaxHighlight === "shiki") {
|
|
92
|
-
remarkPlugins.push([await remarkShiki(config.markdown.shikiConfig)]);
|
|
93
|
-
}
|
|
94
|
-
if (config.markdown.syntaxHighlight === "prism") {
|
|
95
|
-
remarkPlugins.push(remarkPrism);
|
|
96
|
-
}
|
|
97
|
-
remarkPlugins = [...remarkPlugins, ...mdxOptions.remarkPlugins ?? []];
|
|
98
|
-
return remarkPlugins;
|
|
99
|
-
}
|
|
100
|
-
function getRehypePlugins(mdxOptions, config) {
|
|
101
|
-
let rehypePlugins = [
|
|
102
|
-
rehypeCollectHeadings,
|
|
103
|
-
[rehypeRaw, { passThrough: nodeTypes }]
|
|
104
|
-
];
|
|
105
|
-
switch (mdxOptions.extendPlugins) {
|
|
106
|
-
case false:
|
|
107
|
-
break;
|
|
108
|
-
case "astroDefaults":
|
|
109
|
-
rehypePlugins = [...rehypePlugins, ...DEFAULT_REHYPE_PLUGINS];
|
|
110
|
-
break;
|
|
111
|
-
default:
|
|
112
|
-
rehypePlugins = [
|
|
113
|
-
...rehypePlugins,
|
|
114
|
-
...config.markdown.extendDefaultPlugins ? DEFAULT_REHYPE_PLUGINS : [],
|
|
115
|
-
...ignoreStringPlugins(config.markdown.rehypePlugins ?? [])
|
|
116
|
-
];
|
|
117
|
-
break;
|
|
118
|
-
}
|
|
119
|
-
rehypePlugins = [...rehypePlugins, ...mdxOptions.rehypePlugins ?? []];
|
|
120
|
-
return rehypePlugins;
|
|
121
|
-
}
|
|
122
|
-
function ignoreStringPlugins(plugins) {
|
|
123
|
-
let validPlugins = [];
|
|
124
|
-
let hasInvalidPlugin = false;
|
|
125
|
-
for (const plugin of plugins) {
|
|
126
|
-
if (typeof plugin === "string") {
|
|
127
|
-
console.warn(yellow(`[MDX] ${bold(plugin)} not applied.`));
|
|
128
|
-
hasInvalidPlugin = true;
|
|
129
|
-
} else if (Array.isArray(plugin) && typeof plugin[0] === "string") {
|
|
130
|
-
console.warn(yellow(`[MDX] ${bold(plugin[0])} not applied.`));
|
|
131
|
-
hasInvalidPlugin = true;
|
|
132
|
-
} else {
|
|
133
|
-
validPlugins.push(plugin);
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
if (hasInvalidPlugin) {
|
|
137
|
-
console.warn(
|
|
138
|
-
`To inherit Markdown plugins in MDX, please use explicit imports in your config instead of "strings." See Markdown docs: https://docs.astro.build/en/guides/markdown-content/#markdown-plugins`
|
|
139
|
-
);
|
|
140
|
-
}
|
|
141
|
-
return validPlugins;
|
|
142
|
-
}
|
|
143
62
|
function handleExtendsNotSupported(pluginConfig) {
|
|
144
63
|
if (typeof pluginConfig === "object" && pluginConfig !== null && pluginConfig.hasOwnProperty("extends")) {
|
|
145
64
|
throw new Error(
|
|
@@ -149,8 +68,6 @@ function handleExtendsNotSupported(pluginConfig) {
|
|
|
149
68
|
}
|
|
150
69
|
export {
|
|
151
70
|
getFileInfo,
|
|
152
|
-
getRehypePlugins,
|
|
153
|
-
getRemarkPlugins,
|
|
154
71
|
handleExtendsNotSupported,
|
|
155
72
|
jsToTreeNode,
|
|
156
73
|
parseFrontmatter
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astrojs/mdx",
|
|
3
3
|
"description": "Use MDX within Astro",
|
|
4
|
-
"version": "0.11.
|
|
4
|
+
"version": "0.11.2",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
7
7
|
"author": "withastro",
|
|
@@ -28,6 +28,7 @@
|
|
|
28
28
|
"@mdx-js/rollup": "^2.1.1",
|
|
29
29
|
"acorn": "^8.8.0",
|
|
30
30
|
"es-module-lexer": "^0.10.5",
|
|
31
|
+
"estree-util-visit": "^1.2.0",
|
|
31
32
|
"github-slugger": "^1.4.0",
|
|
32
33
|
"gray-matter": "^4.0.3",
|
|
33
34
|
"kleur": "^4.1.4",
|
|
@@ -41,10 +42,11 @@
|
|
|
41
42
|
},
|
|
42
43
|
"devDependencies": {
|
|
43
44
|
"@types/chai": "^4.3.1",
|
|
45
|
+
"@types/estree": "^1.0.0",
|
|
44
46
|
"@types/mocha": "^9.1.1",
|
|
45
47
|
"@types/yargs-parser": "^21.0.0",
|
|
46
|
-
"astro": "1.1
|
|
47
|
-
"astro-scripts": "0.0.
|
|
48
|
+
"astro": "1.3.1",
|
|
49
|
+
"astro-scripts": "0.0.8",
|
|
48
50
|
"chai": "^4.3.6",
|
|
49
51
|
"linkedom": "^0.14.12",
|
|
50
52
|
"mdast-util-to-string": "^3.1.0",
|