@astrojs/mdx 0.0.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/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # @astrojs/mdx
2
+
3
+ ## 0.0.2
4
+
5
+ ### Patch Changes
6
+
7
+ - [#3706](https://github.com/withastro/astro/pull/3706) [`032ad1c0`](https://github.com/withastro/astro/commit/032ad1c047a62dd663067cc562537d16f2872aa7) Thanks [@natemoo-re](https://github.com/natemoo-re)! - Initial release! πŸŽ‰
package/LICENSE ADDED
@@ -0,0 +1,61 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Fred K. Schott
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
23
+
24
+ """
25
+ This license applies to parts of the `packages/create-astro` and `packages/astro` subdirectories originating from the https://github.com/sveltejs/kit repository:
26
+
27
+ Copyright (c) 2020 [these people](https://github.com/sveltejs/kit/graphs/contributors)
28
+
29
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
30
+
31
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
32
+
33
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34
+ """
35
+
36
+
37
+ """
38
+ This license applies to parts of the `packages/create-astro` and `packages/astro` subdirectories originating from the https://github.com/vitejs/vite repository:
39
+
40
+ MIT License
41
+
42
+ Copyright (c) 2019-present, Yuxi (Evan) You and Vite contributors
43
+
44
+ Permission is hereby granted, free of charge, to any person obtaining a copy
45
+ of this software and associated documentation files (the "Software"), to deal
46
+ in the Software without restriction, including without limitation the rights
47
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
48
+ copies of the Software, and to permit persons to whom the Software is
49
+ furnished to do so, subject to the following conditions:
50
+
51
+ The above copyright notice and this permission notice shall be included in all
52
+ copies or substantial portions of the Software.
53
+
54
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
55
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
56
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
57
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
58
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
59
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
60
+ SOFTWARE.
61
+ """
package/README.md ADDED
@@ -0,0 +1,106 @@
1
+ # @astrojs/mdx πŸ“
2
+
3
+ This **[Astro integration][astro-integration]** enables the usage of [MDX](https://mdxjs.com/) components and allows you to create pages as `.mdx` files.
4
+
5
+ - <strong>[Why MDX?](#why-mdx)</strong>
6
+ - <strong>[Installation](#installation)</strong>
7
+ - <strong>[Usage](#usage)</strong>
8
+ - <strong>[Configuration](#configuration)</strong>
9
+ - <strong>[Examples](#examples)</strong>
10
+ - <strong>[Troubleshooting](#troubleshooting)</strong>
11
+ - <strong>[Contributing](#contributing)</strong>
12
+ - <strong>[Changelog](#changelog)</strong>
13
+
14
+ ## Why MDX?
15
+
16
+ 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.
17
+
18
+ **Want to learn more about MDX before using this integration?**
19
+ Check out [β€œWhat is MDX?”](https://mdxjs.com/docs/what-is-mdx/), a deep-dive on the MDX format.
20
+
21
+ ## Installation
22
+
23
+ <details>
24
+ <summary>Quick Install</summary>
25
+ <br/>
26
+
27
+ 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.
28
+
29
+ ```sh
30
+ # Using NPM
31
+ npx astro add mdx
32
+ # Using Yarn
33
+ yarn astro add mdx
34
+ # Using PNPM
35
+ pnpx astro add mdx
36
+ ```
37
+
38
+ Then, restart the dev server by typing `CTRL-C` and then `npm run astro dev` in the terminal window that was running Astro.
39
+
40
+ 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.
41
+ </details>
42
+
43
+ <details>
44
+ <summary>Manual Install</summary>
45
+ <br/>
46
+
47
+ First, install the `@astrojs/mdx` package using your package manager. If you're using npm or aren't sure, run this in the terminal:
48
+
49
+ ```
50
+ npm install @astrojs/mdx
51
+ ```
52
+
53
+ Then, apply this integration to your `astro.config.*` file using the `integrations` property:
54
+
55
+ __astro.config.mjs__
56
+
57
+ ```js
58
+ import { defineConfig } from 'astro/config';
59
+ import mdx from '@astrojs/mdx';
60
+
61
+ export default defineConfig({
62
+ // ...
63
+ integrations: [mdx()],
64
+ });
65
+ ```
66
+
67
+ Finally, restart the dev server.
68
+ </details>
69
+
70
+ ## Usage
71
+
72
+ To write your first MDX page in Astro, head to our [UI framework documentation][astro-ui-frameworks]. You'll explore:
73
+ - πŸ“¦ how framework components are loaded,
74
+ - πŸ’§ client-side hydration options, and
75
+ - πŸͺ† opportunities to mix and nest frameworks together
76
+
77
+ [**Client Directives**](https://docs.astro.build/en/reference/directives-reference/#client-directives) are still required in `.mdx` files.
78
+
79
+ > **Note**: `.mdx` files adhere to strict JSX syntax rather than Astro's HTML-like syntax.
80
+
81
+ Also check our [Astro Integration Documentation][astro-integration] for more on integrations.
82
+
83
+ ## Configuration
84
+
85
+ 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.
86
+
87
+ ## Examples
88
+
89
+ - The [Astro MDX example](https://github.com/withastro/astro/tree/latest/examples/with-mdx) shows how to use MDX files in your Astro project.
90
+
91
+ ## Troubleshooting
92
+
93
+ For help, check out the `#support-threads` channel on [Discord](https://astro.build/chat). Our friendly Support Squad members are here to help!
94
+
95
+ You can also check our [Astro Integration Documentation][astro-integration] for more on integrations.
96
+
97
+ ## Contributing
98
+
99
+ This package is maintained by Astro's Core team. You're welcome to submit an issue or PR!
100
+
101
+ ## Changelog
102
+
103
+ See [CHANGELOG.md](CHANGELOG.md) for a history of changes to this integration.
104
+
105
+ [astro-integration]: https://docs.astro.build/en/guides/integrations-guide/
106
+ [astro-ui-frameworks]: https://docs.astro.build/en/core-concepts/framework-components/#using-framework-components
@@ -0,0 +1,2 @@
1
+ import type { AstroIntegration } from 'astro';
2
+ export default function mdx(): AstroIntegration;
package/dist/index.js ADDED
@@ -0,0 +1,40 @@
1
+ import mdxPlugin from "@mdx-js/rollup";
2
+ function mdx() {
3
+ return {
4
+ name: "@astrojs/mdx",
5
+ hooks: {
6
+ "astro:config:setup": ({ updateConfig, addPageExtension, command }) => {
7
+ addPageExtension(".mdx");
8
+ updateConfig({
9
+ vite: {
10
+ plugins: [
11
+ {
12
+ enforce: "pre",
13
+ ...mdxPlugin({
14
+ jsx: true,
15
+ jsxImportSource: "astro",
16
+ format: "mdx",
17
+ mdExtensions: []
18
+ })
19
+ },
20
+ command === "dev" && {
21
+ name: "@astrojs/mdx",
22
+ transform(code, id) {
23
+ if (!id.endsWith(".mdx"))
24
+ return;
25
+ return `${code}
26
+ if (import.meta.hot) {
27
+ import.meta.hot.decline();
28
+ }`;
29
+ }
30
+ }
31
+ ]
32
+ }
33
+ });
34
+ }
35
+ }
36
+ };
37
+ }
38
+ export {
39
+ mdx as default
40
+ };
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@astrojs/mdx",
3
+ "description": "Use MDX within Astro",
4
+ "version": "0.0.2",
5
+ "type": "module",
6
+ "types": "./dist/index.d.ts",
7
+ "author": "withastro",
8
+ "license": "MIT",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/withastro/astro.git",
12
+ "directory": "packages/integrations/mdx"
13
+ },
14
+ "keywords": [
15
+ "astro-component",
16
+ "renderer",
17
+ "mdx"
18
+ ],
19
+ "bugs": "https://github.com/withastro/astro/issues",
20
+ "homepage": "https://astro.build",
21
+ "exports": {
22
+ ".": "./dist/index.js",
23
+ "./package.json": "./package.json"
24
+ },
25
+ "dependencies": {
26
+ "@mdx-js/rollup": "^2.1.1"
27
+ },
28
+ "devDependencies": {
29
+ "@types/chai": "^4.3.1",
30
+ "@types/mocha": "^9.1.1",
31
+ "@types/yargs-parser": "^21.0.0",
32
+ "astro": "1.0.0-beta.60",
33
+ "astro-scripts": "0.0.6",
34
+ "chai": "^4.3.6",
35
+ "mocha": "^9.2.2",
36
+ "linkedom": "^0.14.12"
37
+ },
38
+ "engines": {
39
+ "node": "^14.15.0 || >=16.0.0"
40
+ },
41
+ "scripts": {
42
+ "build": "astro-scripts build \"src/**/*.ts\" && tsc",
43
+ "build:ci": "astro-scripts build \"src/**/*.ts\"",
44
+ "dev": "astro-scripts dev \"src/**/*.ts\"",
45
+ "test": "mocha --exit --timeout 20000"
46
+ }
47
+ }
package/src/index.ts ADDED
@@ -0,0 +1,39 @@
1
+ import mdxPlugin from '@mdx-js/rollup';
2
+ import type { AstroIntegration } from 'astro';
3
+
4
+ export default function mdx(): AstroIntegration {
5
+ return {
6
+ name: '@astrojs/mdx',
7
+ hooks: {
8
+ 'astro:config:setup': ({ updateConfig, addPageExtension, command }: any) => {
9
+ addPageExtension('.mdx');
10
+ updateConfig({
11
+ vite: {
12
+ plugins: [
13
+ {
14
+ enforce: 'pre',
15
+ ...mdxPlugin({
16
+ jsx: true,
17
+ jsxImportSource: 'astro',
18
+ // Note: disable `.md` support
19
+ format: 'mdx',
20
+ mdExtensions: [],
21
+ }),
22
+ },
23
+ command === 'dev' && {
24
+ name: '@astrojs/mdx',
25
+ transform(code: string, id: string) {
26
+ if (!id.endsWith('.mdx')) return;
27
+ // TODO: decline HMR updates until we have a stable approach
28
+ return `${code}\nif (import.meta.hot) {
29
+ import.meta.hot.decline();
30
+ }`;
31
+ },
32
+ },
33
+ ],
34
+ },
35
+ });
36
+ },
37
+ },
38
+ };
39
+ }
@@ -0,0 +1,3 @@
1
+ # Hello component!
2
+
3
+ <div id="foo">bar</div>
@@ -0,0 +1,5 @@
1
+ ---
2
+ import Test from '../components/Test.mdx';
3
+ ---
4
+
5
+ <Test />
@@ -0,0 +1 @@
1
+ # Hello page!
@@ -0,0 +1,60 @@
1
+ import mdx from '@astrojs/mdx';
2
+
3
+ import { expect } from 'chai';
4
+ import { parseHTML } from 'linkedom';
5
+ import { loadFixture } from '../../../astro/test/test-utils.js';
6
+
7
+ describe('MDX Component', () => {
8
+ let fixture;
9
+
10
+ before(async () => {
11
+ fixture = await loadFixture({
12
+ root: new URL('./fixtures/mdx-component/', import.meta.url),
13
+ integrations: [mdx()],
14
+ });
15
+ });
16
+
17
+ describe('build', () => {
18
+ before(async () => {
19
+ await fixture.build();
20
+ });
21
+
22
+ it('works', async () => {
23
+ const html = await fixture.readFile('/index.html');
24
+ const { document } = parseHTML(html);
25
+
26
+ const h1 = document.querySelector('h1');
27
+ const foo = document.querySelector('#foo');
28
+
29
+ expect(h1.textContent).to.equal('Hello component!');
30
+ expect(foo.textContent).to.equal('bar');
31
+ });
32
+ });
33
+
34
+ describe('dev', () => {
35
+ let devServer;
36
+
37
+ before(async () => {
38
+ devServer = await fixture.startDevServer();
39
+ });
40
+
41
+ after(async () => {
42
+ await devServer.stop();
43
+ });
44
+
45
+ it('works', async () => {
46
+ const res = await fixture.fetch('/');
47
+
48
+ expect(res.status).to.equal(200);
49
+
50
+ const html = await res.text();
51
+ const { document } = parseHTML(html);
52
+
53
+ const h1 = document.querySelector('h1');
54
+ const foo = document.querySelector('#foo');
55
+
56
+ expect(h1.textContent).to.equal('Hello component!');
57
+ expect(foo.textContent).to.equal('bar');
58
+ });
59
+ });
60
+ });
@@ -0,0 +1,56 @@
1
+ import mdx from '@astrojs/mdx';
2
+
3
+ import { expect } from 'chai';
4
+ import { parseHTML } from 'linkedom';
5
+ import { loadFixture } from '../../../astro/test/test-utils.js';
6
+
7
+ describe('MDX Page', () => {
8
+ let fixture;
9
+
10
+ before(async () => {
11
+ fixture = await loadFixture({
12
+ root: new URL('./fixtures/mdx-page/', import.meta.url),
13
+ integrations: [mdx()],
14
+ });
15
+ });
16
+
17
+ describe('build', () => {
18
+ before(async () => {
19
+ await fixture.build();
20
+ });
21
+
22
+ it('works', async () => {
23
+ const html = await fixture.readFile('/index.html');
24
+ const { document } = parseHTML(html);
25
+
26
+ const h1 = document.querySelector('h1');
27
+
28
+ expect(h1.textContent).to.equal('Hello page!');
29
+ });
30
+ });
31
+
32
+ describe('dev', () => {
33
+ let devServer;
34
+
35
+ before(async () => {
36
+ devServer = await fixture.startDevServer();
37
+ });
38
+
39
+ after(async () => {
40
+ await devServer.stop();
41
+ });
42
+
43
+ it('works', async () => {
44
+ const res = await fixture.fetch('/');
45
+
46
+ expect(res.status).to.equal(200);
47
+
48
+ const html = await res.text();
49
+ const { document } = parseHTML(html);
50
+
51
+ const h1 = document.querySelector('h1');
52
+
53
+ expect(h1.textContent).to.equal('Hello page!');
54
+ });
55
+ });
56
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "../../../tsconfig.base.json",
3
+ "include": ["src"],
4
+ "compilerOptions": {
5
+ "allowJs": true,
6
+ "module": "ES2020",
7
+ "outDir": "./dist",
8
+ "target": "ES2020"
9
+ }
10
+ }