summaryrefslogtreecommitdiff
path: root/packages/integrations/mdx
diff options
context:
space:
mode:
Diffstat (limited to 'packages/integrations/mdx')
-rw-r--r--packages/integrations/mdx/CHANGELOG.md19
-rw-r--r--packages/integrations/mdx/package.json11
-rw-r--r--packages/integrations/mdx/src/index.ts34
-rw-r--r--packages/integrations/mdx/src/plugins.ts10
-rw-r--r--packages/integrations/mdx/src/remark-images-to-component.ts2
-rw-r--r--packages/integrations/mdx/test/fixtures/mdx-images/astro.config.ts3
-rw-r--r--packages/integrations/mdx/test/fixtures/mdx-images/tsconfig.json9
-rw-r--r--packages/integrations/mdx/tsconfig.json2
8 files changed, 69 insertions, 21 deletions
diff --git a/packages/integrations/mdx/CHANGELOG.md b/packages/integrations/mdx/CHANGELOG.md
index 531e8809f..ac853011d 100644
--- a/packages/integrations/mdx/CHANGELOG.md
+++ b/packages/integrations/mdx/CHANGELOG.md
@@ -1,5 +1,24 @@
# @astrojs/mdx
+## 1.0.0-beta.0
+
+### Minor Changes
+
+- [`1eae2e3f7`](https://github.com/withastro/astro/commit/1eae2e3f7d693c9dfe91c8ccfbe606d32bf2fb81) Thanks [@Princesseuh](https://github.com/Princesseuh)! - Remove support for Node 16. The lowest supported version by Astro and all integrations is now v18.14.1. As a reminder, Node 16 will be deprecated on the 11th September 2023.
+
+- [`dfc2d93e3`](https://github.com/withastro/astro/commit/dfc2d93e3c645995379358fabbdfa9aab99f43d8) Thanks [@bluwy](https://github.com/bluwy)! - Add `astro` as peer dependency
+
+### Patch Changes
+
+- [`dfc2d93e3`](https://github.com/withastro/astro/commit/dfc2d93e3c645995379358fabbdfa9aab99f43d8) Thanks [@bluwy](https://github.com/bluwy)! - Re-orders the MDX plugin to run before Astro's JSX plugin
+
+- [`631b9c410`](https://github.com/withastro/astro/commit/631b9c410d5d66fa384674027ba95d69ebb5063f) Thanks [@bluwy](https://github.com/bluwy)! - Handle `components` exports handling itself
+
+- Updated dependencies [[`1eae2e3f7`](https://github.com/withastro/astro/commit/1eae2e3f7d693c9dfe91c8ccfbe606d32bf2fb81), [`76ddef19c`](https://github.com/withastro/astro/commit/76ddef19ccab6e5f7d3a5740cd41acf10e334b38), [`9b4f70a62`](https://github.com/withastro/astro/commit/9b4f70a629f55e461759ba46f68af7097a2e9215), [`3fdf509b2`](https://github.com/withastro/astro/commit/3fdf509b2731a9b2f972d89291e57cf78d62c769), [`2f951cd40`](https://github.com/withastro/astro/commit/2f951cd403dfcc2c3ca6aae618ae3e1409516e32), [`c022a4217`](https://github.com/withastro/astro/commit/c022a4217a805d223c1494e9eda4e48bbf810388), [`67becaa58`](https://github.com/withastro/astro/commit/67becaa580b8f787df58de66b7008b7098f1209c), [`bc37331d8`](https://github.com/withastro/astro/commit/bc37331d8154e3e95a8df9131e4e014e78a7a9e7), [`dfc2d93e3`](https://github.com/withastro/astro/commit/dfc2d93e3c645995379358fabbdfa9aab99f43d8), [`3dc1ca2fa`](https://github.com/withastro/astro/commit/3dc1ca2fac8d9965cc5085a5d09e72ed87b4281a), [`1be84dfee`](https://github.com/withastro/astro/commit/1be84dfee3ce8e6f5cc624f99aec4e980f6fde37), [`35f01df79`](https://github.com/withastro/astro/commit/35f01df797d23315f2bee2fc3fd795adb0559c58), [`3fdf509b2`](https://github.com/withastro/astro/commit/3fdf509b2731a9b2f972d89291e57cf78d62c769), [`78de801f2`](https://github.com/withastro/astro/commit/78de801f21fd4ca1653950027d953bf08614566b), [`59d6e569f`](https://github.com/withastro/astro/commit/59d6e569f63e175c97e82e94aa7974febfb76f7c), [`7723c4cc9`](https://github.com/withastro/astro/commit/7723c4cc93298c2e6530e55da7afda048f22cf81), [`fb5cd6b56`](https://github.com/withastro/astro/commit/fb5cd6b56dc27a71366ed5e1ab8bfe9b8f96bac5), [`631b9c410`](https://github.com/withastro/astro/commit/631b9c410d5d66fa384674027ba95d69ebb5063f)]:
+ - @astrojs/prism@3.0.0-beta.0
+ - astro@3.0.0-beta.0
+ - @astrojs/markdown-remark@3.0.0-beta.0
+
## 0.19.7
### Patch Changes
diff --git a/packages/integrations/mdx/package.json b/packages/integrations/mdx/package.json
index 7e38cb1b6..69d1caba9 100644
--- a/packages/integrations/mdx/package.json
+++ b/packages/integrations/mdx/package.json
@@ -1,7 +1,7 @@
{
"name": "@astrojs/mdx",
"description": "Add support for MDX pages in your Astro site",
- "version": "0.19.7",
+ "version": "1.0.0-beta.0",
"type": "module",
"types": "./dist/index.d.ts",
"author": "withastro",
@@ -34,8 +34,8 @@
"test:match": "mocha --timeout 20000 -g"
},
"dependencies": {
- "@astrojs/markdown-remark": "^2.2.1",
- "@astrojs/prism": "^2.1.2",
+ "@astrojs/markdown-remark": "workspace:*",
+ "@astrojs/prism": "workspace:*",
"@mdx-js/mdx": "^2.3.0",
"acorn": "^8.9.0",
"es-module-lexer": "^1.3.0",
@@ -53,6 +53,9 @@
"unist-util-visit": "^4.1.2",
"vfile": "^5.3.7"
},
+ "peerDependencies": {
+ "astro": "workspace:^3.0.0-beta.2"
+ },
"devDependencies": {
"@types/chai": "^4.3.5",
"@types/estree": "^1.0.1",
@@ -78,6 +81,6 @@
"vite": "^4.4.6"
},
"engines": {
- "node": ">=16.12.0"
+ "node": ">=18.14.1"
}
}
diff --git a/packages/integrations/mdx/src/index.ts b/packages/integrations/mdx/src/index.ts
index 1d0320213..e9c6924c6 100644
--- a/packages/integrations/mdx/src/index.ts
+++ b/packages/integrations/mdx/src/index.ts
@@ -13,6 +13,7 @@ import type { Plugin as VitePlugin } from 'vite';
import { getRehypePlugins, getRemarkPlugins, recmaInjectImportMetaEnvPlugin } from './plugins.js';
import type { OptimizeOptions } from './rehype-optimize-static.js';
import { getFileInfo, ignoreStringPlugins, parseFrontmatter } from './utils.js';
+import astroJSXRenderer from 'astro/jsx/renderer.js';
export type MdxOptions = Omit<typeof markdownConfigDefaults, 'remarkPlugins' | 'rehypePlugins'> & {
extendMarkdownConfig: boolean;
@@ -37,9 +38,16 @@ export default function mdx(partialMdxOptions: Partial<MdxOptions> = {}): AstroI
name: '@astrojs/mdx',
hooks: {
'astro:config:setup': async (params) => {
- const { updateConfig, config, addPageExtension, addContentEntryType, command } =
- params as SetupHookParams;
+ const {
+ updateConfig,
+ config,
+ addPageExtension,
+ addContentEntryType,
+ command,
+ addRenderer,
+ } = params as SetupHookParams;
+ addRenderer(astroJSXRenderer);
addPageExtension('.mdx');
addContentEntryType({
extensions: ['.mdx'],
@@ -72,7 +80,7 @@ export default function mdx(partialMdxOptions: Partial<MdxOptions> = {}): AstroI
});
const mdxPluginOpts: CompileOptions = {
- remarkPlugins: await getRemarkPlugins(mdxOptions, config),
+ remarkPlugins: await getRemarkPlugins(mdxOptions),
rehypePlugins: getRehypePlugins(mdxOptions),
recmaPlugins: mdxOptions.recmaPlugins,
remarkRehypeOptions: mdxOptions.remarkRehype,
@@ -95,6 +103,21 @@ export default function mdx(partialMdxOptions: Partial<MdxOptions> = {}): AstroI
enforce: 'pre',
configResolved(resolved) {
importMetaEnv = { ...importMetaEnv, ...resolved.env };
+
+ // HACK: move ourselves before Astro's JSX plugin to transform things in the right order
+ const jsxPluginIndex = resolved.plugins.findIndex((p) => p.name === 'astro:jsx');
+ if (jsxPluginIndex !== -1) {
+ const myPluginIndex = resolved.plugins.findIndex(
+ (p) => p.name === '@mdx-js/rollup'
+ );
+ if (myPluginIndex !== -1) {
+ const myPlugin = resolved.plugins[myPluginIndex];
+ // @ts-ignore-error ignore readonly annotation
+ resolved.plugins.splice(myPluginIndex, 1);
+ // @ts-ignore-error ignore readonly annotation
+ resolved.plugins.splice(jsxPluginIndex, 0, myPlugin);
+ }
+ }
},
// Override transform to alter code before MDX compilation
// ex. inject layouts
@@ -156,11 +179,14 @@ export default function mdx(partialMdxOptions: Partial<MdxOptions> = {}): AstroI
code += `\nexport const file = ${JSON.stringify(fileId)};`;
}
if (!moduleExports.find(({ n }) => n === 'Content')) {
+ // If have `export const components`, pass that as props to `Content` as fallback
+ const hasComponents = moduleExports.find(({ n }) => n === 'components');
+
// Make `Content` the default export so we can wrap `MDXContent` and pass in `Fragment`
code = code.replace('export default MDXContent;', '');
code += `\nexport const Content = (props = {}) => MDXContent({
...props,
- components: { Fragment, ...props.components },
+ components: { Fragment${hasComponents ? ', ...components' : ''}, ...props.components },
});
export default Content;`;
}
diff --git a/packages/integrations/mdx/src/plugins.ts b/packages/integrations/mdx/src/plugins.ts
index ed03f4b2b..5d7b9b58c 100644
--- a/packages/integrations/mdx/src/plugins.ts
+++ b/packages/integrations/mdx/src/plugins.ts
@@ -5,7 +5,6 @@ import {
} from '@astrojs/markdown-remark/dist/internal.js';
import { nodeTypes } from '@mdx-js/mdx';
import type { PluggableList } from '@mdx-js/mdx/lib/core.js';
-import type { AstroConfig } from 'astro';
import type { Literal, MemberExpression } from 'estree';
import { visit as estreeVisit } from 'estree-util-visit';
import rehypeRaw from 'rehype-raw';
@@ -96,13 +95,8 @@ export function rehypeApplyFrontmatterExport() {
};
}
-export async function getRemarkPlugins(
- mdxOptions: MdxOptions,
- config: AstroConfig
-): Promise<PluggableList> {
- let remarkPlugins: PluggableList = [
- ...(config.experimental.assets ? [remarkCollectImages, remarkImageToComponent] : []),
- ];
+export async function getRemarkPlugins(mdxOptions: MdxOptions): Promise<PluggableList> {
+ let remarkPlugins: PluggableList = [remarkCollectImages, remarkImageToComponent];
if (!isPerformanceBenchmark) {
if (mdxOptions.gfm) {
diff --git a/packages/integrations/mdx/src/remark-images-to-component.ts b/packages/integrations/mdx/src/remark-images-to-component.ts
index 8a3166f49..bb9657f42 100644
--- a/packages/integrations/mdx/src/remark-images-to-component.ts
+++ b/packages/integrations/mdx/src/remark-images-to-component.ts
@@ -1,5 +1,5 @@
import type { MarkdownVFile } from '@astrojs/markdown-remark';
-import { type Image, type Parent } from 'mdast';
+import type { Image, Parent } from 'mdast';
import type { MdxJsxFlowElement, MdxjsEsm } from 'mdast-util-mdx';
import { visit } from 'unist-util-visit';
import { jsToTreeNode } from './utils.js';
diff --git a/packages/integrations/mdx/test/fixtures/mdx-images/astro.config.ts b/packages/integrations/mdx/test/fixtures/mdx-images/astro.config.ts
index 30ce31bc2..2e487f5a8 100644
--- a/packages/integrations/mdx/test/fixtures/mdx-images/astro.config.ts
+++ b/packages/integrations/mdx/test/fixtures/mdx-images/astro.config.ts
@@ -3,9 +3,6 @@ import { testImageService } from '../../../../../astro/test/test-image-service.j
export default {
integrations: [mdx()],
- experimental: {
- assets: true,
- },
image: {
service: testImageService(),
},
diff --git a/packages/integrations/mdx/test/fixtures/mdx-images/tsconfig.json b/packages/integrations/mdx/test/fixtures/mdx-images/tsconfig.json
new file mode 100644
index 000000000..b5bf6a715
--- /dev/null
+++ b/packages/integrations/mdx/test/fixtures/mdx-images/tsconfig.json
@@ -0,0 +1,9 @@
+{
+ "extends": "astro/tsconfigs/base",
+ "compilerOptions": {
+ "baseUrl": ".",
+ "paths": {
+ "~/assets/*": ["src/assets/*"]
+ },
+ }
+}
diff --git a/packages/integrations/mdx/tsconfig.json b/packages/integrations/mdx/tsconfig.json
index 64d4ef454..af1b43564 100644
--- a/packages/integrations/mdx/tsconfig.json
+++ b/packages/integrations/mdx/tsconfig.json
@@ -5,6 +5,6 @@
"allowJs": true,
"module": "ES2022",
"outDir": "./dist",
- "target": "ES2021"
+ "target": "ES2022"
}
}