summaryrefslogtreecommitdiff
path: root/packages/integrations/mdx
diff options
context:
space:
mode:
Diffstat (limited to 'packages/integrations/mdx')
-rw-r--r--packages/integrations/mdx/README.md106
-rw-r--r--packages/integrations/mdx/package.json47
-rw-r--r--packages/integrations/mdx/src/index.ts39
-rw-r--r--packages/integrations/mdx/test/fixtures/mdx-component/src/components/Test.mdx3
-rw-r--r--packages/integrations/mdx/test/fixtures/mdx-component/src/pages/index.astro5
-rw-r--r--packages/integrations/mdx/test/fixtures/mdx-page/src/pages/index.mdx1
-rw-r--r--packages/integrations/mdx/test/mdx-component.test.js63
-rw-r--r--packages/integrations/mdx/test/mdx-page.test.js59
-rw-r--r--packages/integrations/mdx/tsconfig.json10
9 files changed, 333 insertions, 0 deletions
diff --git a/packages/integrations/mdx/README.md b/packages/integrations/mdx/README.md
new file mode 100644
index 000000000..0a909cc2f
--- /dev/null
+++ b/packages/integrations/mdx/README.md
@@ -0,0 +1,106 @@
+# @astrojs/mdx πŸ“
+
+This **[Astro integration][astro-integration]** enables the usage of [MDX](https://mdxjs.com/) components and allows you to create pages as `.mdx` files.
+
+- <strong>[Why MDX?](#why-mdx)</strong>
+- <strong>[Installation](#installation)</strong>
+- <strong>[Usage](#usage)</strong>
+- <strong>[Configuration](#configuration)</strong>
+- <strong>[Examples](#examples)</strong>
+- <strong>[Troubleshooting](#troubleshooting)</strong>
+- <strong>[Contributing](#contributing)</strong>
+- <strong>[Changelog](#changelog)</strong>
+
+## Why MDX?
+
+MDX is the defacto solution for embedding components, such as interactive charts or alerts, within Markdown content. If you have existing content authored in MDX, this integration makes migrating to Astro a breeze.
+
+**Want to learn more about MDX before using this integration?**
+Check out [β€œWhat is MDX?”](https://mdxjs.com/docs/what-is-mdx/), a deep-dive on the MDX format.
+
+## Installation
+
+<details>
+ <summary>Quick Install</summary>
+ <br/>
+
+The `astro add` command-line tool automates the installation for you. Run one of the following commands in a new terminal window. (If you aren't sure which package manager you're using, run the first command.) Then, follow the prompts, and type "y" in the terminal (meaning "yes") for each one.
+
+ ```sh
+ # Using NPM
+ npx astro add mdx
+ # Using Yarn
+ yarn astro add mdx
+ # Using PNPM
+ pnpx astro add mdx
+ ```
+
+Then, restart the dev server by typing `CTRL-C` and then `npm run astro dev` in the terminal window that was running Astro.
+
+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.
+</details>
+
+<details>
+ <summary>Manual Install</summary>
+ <br/>
+
+First, install the `@astrojs/mdx` package using your package manager. If you're using npm or aren't sure, run this in the terminal:
+
+ ```
+ npm install @astrojs/mdx
+ ```
+
+Then, apply this integration to your `astro.config.*` file using the `integrations` property:
+
+__astro.config.mjs__
+
+```js
+import { defineConfig } from 'astro/config';
+import mdx from '@astrojs/mdx';
+
+export default defineConfig({
+ // ...
+ integrations: [mdx()],
+});
+```
+
+Finally, restart the dev server.
+</details>
+
+## Usage
+
+To write your first MDX page in Astro, head to our [UI framework documentation][astro-ui-frameworks]. You'll explore:
+- πŸ“¦ how framework components are loaded,
+- πŸ’§ client-side hydration options, and
+- πŸͺ† opportunities to mix and nest frameworks together
+
+[**Client Directives**](https://docs.astro.build/en/reference/directives-reference/#client-directives) are still required in `.mdx` files.
+
+> **Note**: `.mdx` files adhere to strict JSX syntax rather than Astro's HTML-like syntax.
+
+Also check our [Astro Integration Documentation][astro-integration] for more on integrations.
+
+## Configuration
+
+There are currently no configuration options for the `@astrojs/mdx` integration. Please [open an issue](https://github.com/withastro/astro/issues/new/choose) if you have a compelling use case to share.
+
+## Examples
+
+- The [Astro MDX example](https://github.com/withastro/astro/tree/latest/examples/with-mdx) shows how to use MDX files in your Astro project.
+
+## Troubleshooting
+
+For help, check out the `#support-threads` channel on [Discord](https://astro.build/chat). Our friendly Support Squad members are here to help!
+
+You can also check our [Astro Integration Documentation][astro-integration] for more on integrations.
+
+## Contributing
+
+This package is maintained by Astro's Core team. You're welcome to submit an issue or PR!
+
+## Changelog
+
+See [CHANGELOG.md](CHANGELOG.md) for a history of changes to this integration.
+
+[astro-integration]: https://docs.astro.build/en/guides/integrations-guide/
+[astro-ui-frameworks]: https://docs.astro.build/en/core-concepts/framework-components/#using-framework-components
diff --git a/packages/integrations/mdx/package.json b/packages/integrations/mdx/package.json
new file mode 100644
index 000000000..7171c61f9
--- /dev/null
+++ b/packages/integrations/mdx/package.json
@@ -0,0 +1,47 @@
+{
+ "name": "@astrojs/mdx",
+ "description": "Use MDX within Astro",
+ "version": "0.0.1",
+ "type": "module",
+ "types": "./dist/index.d.ts",
+ "author": "withastro",
+ "license": "MIT",
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/withastro/astro.git",
+ "directory": "packages/integrations/mdx"
+ },
+ "keywords": [
+ "astro-component",
+ "renderer",
+ "mdx"
+ ],
+ "bugs": "https://github.com/withastro/astro/issues",
+ "homepage": "https://astro.build",
+ "exports": {
+ ".": "./dist/index.js",
+ "./package.json": "./package.json"
+ },
+ "scripts": {
+ "build": "astro-scripts build \"src/**/*.ts\" && tsc",
+ "build:ci": "astro-scripts build \"src/**/*.ts\"",
+ "dev": "astro-scripts dev \"src/**/*.ts\"",
+ "test": "mocha --exit --timeout 20000"
+ },
+ "dependencies": {
+ "@mdx-js/rollup": "^2.1.1"
+ },
+ "devDependencies": {
+ "@types/chai": "^4.3.1",
+ "@types/mocha": "^9.1.1",
+ "@types/yargs-parser": "^21.0.0",
+ "astro": "workspace:*",
+ "astro-scripts": "workspace:*",
+ "chai": "^4.3.6",
+ "mocha": "^9.2.2",
+ "linkedom": "^0.14.12"
+ },
+ "engines": {
+ "node": "^14.15.0 || >=16.0.0"
+ }
+}
diff --git a/packages/integrations/mdx/src/index.ts b/packages/integrations/mdx/src/index.ts
new file mode 100644
index 000000000..d07913ede
--- /dev/null
+++ b/packages/integrations/mdx/src/index.ts
@@ -0,0 +1,39 @@
+import type { AstroIntegration } from 'astro';
+import mdxPlugin from '@mdx-js/rollup';
+
+export default function mdx(): AstroIntegration {
+ return {
+ name: '@astrojs/mdx',
+ hooks: {
+ 'astro:config:setup': ({ updateConfig, addPageExtension, command }: any) => {
+ addPageExtension('.mdx');
+ updateConfig({
+ vite: {
+ plugins: [
+ {
+ enforce: 'pre',
+ ...mdxPlugin({
+ jsx: true,
+ jsxImportSource: 'astro',
+ // Note: disable `.md` support
+ format: 'mdx',
+ mdExtensions: []
+ })
+ },
+ command === 'dev' && {
+ name: '@astrojs/mdx',
+ transform(code: string, id: string) {
+ if (!id.endsWith('.mdx')) return;
+ // TODO: decline HMR updates until we have a stable approach
+ return `${code}\nif (import.meta.hot) {
+ import.meta.hot.decline();
+ }`
+ }
+ }
+ ]
+ }
+ })
+ }
+ }
+ }
+}
diff --git a/packages/integrations/mdx/test/fixtures/mdx-component/src/components/Test.mdx b/packages/integrations/mdx/test/fixtures/mdx-component/src/components/Test.mdx
new file mode 100644
index 000000000..1c6b33184
--- /dev/null
+++ b/packages/integrations/mdx/test/fixtures/mdx-component/src/components/Test.mdx
@@ -0,0 +1,3 @@
+# Hello component!
+
+<div id="foo">bar</div>
diff --git a/packages/integrations/mdx/test/fixtures/mdx-component/src/pages/index.astro b/packages/integrations/mdx/test/fixtures/mdx-component/src/pages/index.astro
new file mode 100644
index 000000000..ed5ae98a3
--- /dev/null
+++ b/packages/integrations/mdx/test/fixtures/mdx-component/src/pages/index.astro
@@ -0,0 +1,5 @@
+---
+import Test from '../components/Test.mdx';
+---
+
+<Test />
diff --git a/packages/integrations/mdx/test/fixtures/mdx-page/src/pages/index.mdx b/packages/integrations/mdx/test/fixtures/mdx-page/src/pages/index.mdx
new file mode 100644
index 000000000..195881e02
--- /dev/null
+++ b/packages/integrations/mdx/test/fixtures/mdx-page/src/pages/index.mdx
@@ -0,0 +1 @@
+# Hello page!
diff --git a/packages/integrations/mdx/test/mdx-component.test.js b/packages/integrations/mdx/test/mdx-component.test.js
new file mode 100644
index 000000000..462e86e7b
--- /dev/null
+++ b/packages/integrations/mdx/test/mdx-component.test.js
@@ -0,0 +1,63 @@
+import mdx from '@astrojs/mdx';
+
+import { expect } from 'chai';
+import { parseHTML } from 'linkedom'
+import { loadFixture } from '../../../astro/test/test-utils.js';
+
+describe('MDX Component', () => {
+ let fixture;
+
+ before(async () => {
+ fixture = await loadFixture({
+ root: new URL('./fixtures/mdx-component/', import.meta.url),
+ integrations: [
+ mdx()
+ ]
+ });
+ });
+
+ describe('build', () => {
+ before(async () => {
+ await fixture.build();
+ });
+
+
+ it('works', async () => {
+ const html = await fixture.readFile('/index.html');
+ const { document } = parseHTML(html);
+
+ const h1 = document.querySelector('h1');
+ const foo = document.querySelector('#foo');
+
+ expect(h1.textContent).to.equal('Hello component!');
+ expect(foo.textContent).to.equal('bar');
+ });
+ })
+
+ describe('dev', () => {
+ let devServer;
+
+ before(async () => {
+ devServer = await fixture.startDevServer();
+ });
+
+ after(async () => {
+ await devServer.stop();
+ });
+
+ it('works', async () => {
+ const res = await fixture.fetch('/');
+
+ expect(res.status).to.equal(200);
+
+ const html = await res.text();
+ const { document } = parseHTML(html);
+
+ const h1 = document.querySelector('h1');
+ const foo = document.querySelector('#foo');
+
+ expect(h1.textContent).to.equal('Hello component!');
+ expect(foo.textContent).to.equal('bar');
+ });
+ })
+})
diff --git a/packages/integrations/mdx/test/mdx-page.test.js b/packages/integrations/mdx/test/mdx-page.test.js
new file mode 100644
index 000000000..6e4ae79af
--- /dev/null
+++ b/packages/integrations/mdx/test/mdx-page.test.js
@@ -0,0 +1,59 @@
+import mdx from '@astrojs/mdx';
+
+import { expect } from 'chai';
+import { parseHTML } from 'linkedom'
+import { loadFixture } from '../../../astro/test/test-utils.js';
+
+describe('MDX Page', () => {
+ let fixture;
+
+ before(async () => {
+ fixture = await loadFixture({
+ root: new URL('./fixtures/mdx-page/', import.meta.url),
+ integrations: [
+ mdx()
+ ]
+ });
+ });
+
+ describe('build', () => {
+ before(async () => {
+ await fixture.build();
+ });
+
+
+ it('works', async () => {
+ const html = await fixture.readFile('/index.html');
+ const { document } = parseHTML(html);
+
+ const h1 = document.querySelector('h1');
+
+ expect(h1.textContent).to.equal('Hello page!');
+ });
+ })
+
+ describe('dev', () => {
+ let devServer;
+
+ before(async () => {
+ devServer = await fixture.startDevServer();
+ });
+
+ after(async () => {
+ await devServer.stop();
+ });
+
+ it('works', async () => {
+ const res = await fixture.fetch('/');
+
+ expect(res.status).to.equal(200);
+
+ const html = await res.text();
+ const { document } = parseHTML(html);
+
+ const h1 = document.querySelector('h1');
+
+ expect(h1.textContent).to.equal('Hello page!');
+ });
+ })
+})
diff --git a/packages/integrations/mdx/tsconfig.json b/packages/integrations/mdx/tsconfig.json
new file mode 100644
index 000000000..44baf375c
--- /dev/null
+++ b/packages/integrations/mdx/tsconfig.json
@@ -0,0 +1,10 @@
+{
+ "extends": "../../../tsconfig.base.json",
+ "include": ["src"],
+ "compilerOptions": {
+ "allowJs": true,
+ "module": "ES2020",
+ "outDir": "./dist",
+ "target": "ES2020"
+ }
+}