@astrojs/mdx 0.16.0 → 0.16.1

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.
Files changed (26) hide show
  1. package/.turbo/turbo-build.log +5 -5
  2. package/CHANGELOG.md +6 -0
  3. package/dist/index.js +1 -0
  4. package/package.json +4 -4
  5. package/src/index.ts +1 -0
  6. package/test/css-head-mdx.test.js +54 -2
  7. package/test/fixtures/css-head-mdx/node_modules/.bin/astro +17 -0
  8. package/test/fixtures/css-head-mdx/package.json +10 -0
  9. package/test/fixtures/css-head-mdx/src/components/BaseHead.astro +11 -0
  10. package/test/fixtures/css-head-mdx/src/components/GenericComponent.astro +1 -0
  11. package/test/fixtures/css-head-mdx/src/components/MDXWrapper.astro +9 -0
  12. package/test/fixtures/css-head-mdx/src/components/P.astro +3 -0
  13. package/test/fixtures/css-head-mdx/src/components/SmallCaps.astro +3 -0
  14. package/test/fixtures/css-head-mdx/src/components/UsingMdx.astro +8 -0
  15. package/test/fixtures/css-head-mdx/src/components/WithHoistedScripts.astro +6 -0
  16. package/test/fixtures/css-head-mdx/src/content/blog/_styles.css +3 -0
  17. package/test/fixtures/css-head-mdx/src/content/blog/using-mdx.mdx +6 -0
  18. package/test/fixtures/css-head-mdx/src/content/posts/test.mdx +5 -0
  19. package/test/fixtures/css-head-mdx/src/content/posts/using-component.mdx +13 -0
  20. package/test/fixtures/css-head-mdx/src/layouts/ContentLayout.astro +18 -0
  21. package/test/fixtures/css-head-mdx/src/pages/DirectContentUsage.astro +17 -0
  22. package/test/fixtures/css-head-mdx/src/pages/noLayoutWithComponent.mdx +22 -0
  23. package/test/fixtures/css-head-mdx/src/pages/posts/[post].astro +18 -0
  24. package/test/fixtures/css-head-mdx/src/pages/remote.astro +17 -0
  25. package/test/fixtures/css-head-mdx/src/styles/global.css +3 -0
  26. package/test/mdx-plugins.test.js +35 -0
@@ -1,5 +1,5 @@
1
- @astrojs/mdx:build: cache hit, replaying output 2e9264d0ca51149a
2
- @astrojs/mdx:build: 
3
- @astrojs/mdx:build: > @astrojs/mdx@0.16.0 build /home/runner/work/astro/astro/packages/integrations/mdx
4
- @astrojs/mdx:build: > astro-scripts build "src/**/*.ts" && tsc
5
- @astrojs/mdx:build: 
1
+ @astrojs/mdx:build: cache hit, replaying output 14d153814201eafa
2
+ @astrojs/mdx:build: 
3
+ @astrojs/mdx:build: > @astrojs/mdx@0.16.1 build /home/runner/work/astro/astro/packages/integrations/mdx
4
+ @astrojs/mdx:build: > astro-scripts build "src/**/*.ts" && tsc
5
+ @astrojs/mdx:build: 
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @astrojs/mdx
2
2
 
3
+ ## 0.16.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [#6243](https://github.com/withastro/astro/pull/6243) [`4f6ecba4c`](https://github.com/withastro/astro/commit/4f6ecba4c1b35bacbbc0097854ee2b7b8c878e71) Thanks [@bluwy](https://github.com/bluwy)! - Support rehype plugins that inject namespaced attributes
8
+
3
9
  ## 0.16.0
4
10
 
5
11
  ### Minor Changes
package/dist/index.js CHANGED
@@ -48,6 +48,7 @@ function mdx(partialMdxOptions = {}) {
48
48
  const { data: frontmatter, content: pageContent } = parseFrontmatter(code, id);
49
49
  const compiled = await mdxCompile(new VFile({ value: pageContent, path: id }), {
50
50
  ...mdxPluginOpts,
51
+ elementAttributeNameCase: "html",
51
52
  remarkPlugins: [
52
53
  toRemarkInitializeAstroData({ userFrontmatter: frontmatter }),
53
54
  ...mdxPluginOpts.remarkPlugins ?? []
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.16.0",
4
+ "version": "0.16.1",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",
7
7
  "author": "withastro",
@@ -25,8 +25,8 @@
25
25
  "dependencies": {
26
26
  "@astrojs/markdown-remark": "^2.0.1",
27
27
  "@astrojs/prism": "^2.0.0",
28
- "@mdx-js/mdx": "^2.1.2",
29
- "@mdx-js/rollup": "^2.1.1",
28
+ "@mdx-js/mdx": "^2.3.0",
29
+ "@mdx-js/rollup": "^2.3.0",
30
30
  "acorn": "^8.8.0",
31
31
  "es-module-lexer": "^0.10.5",
32
32
  "estree-util-visit": "^1.2.0",
@@ -48,7 +48,7 @@
48
48
  "@types/mdast": "^3.0.10",
49
49
  "@types/mocha": "^9.1.1",
50
50
  "@types/yargs-parser": "^21.0.0",
51
- "astro": "2.0.5",
51
+ "astro": "2.0.12",
52
52
  "astro-scripts": "0.0.10",
53
53
  "chai": "^4.3.6",
54
54
  "cheerio": "^1.0.0-rc.11",
package/src/index.ts CHANGED
@@ -74,6 +74,7 @@ export default function mdx(partialMdxOptions: Partial<MdxOptions> = {}): AstroI
74
74
  const { data: frontmatter, content: pageContent } = parseFrontmatter(code, id);
75
75
  const compiled = await mdxCompile(new VFile({ value: pageContent, path: id }), {
76
76
  ...mdxPluginOpts,
77
+ elementAttributeNameCase: 'html',
77
78
  remarkPlugins: [
78
79
  // Ensure `data.astro` is available to all remark plugins
79
80
  toRemarkInitializeAstroData({ userFrontmatter: frontmatter }),
@@ -3,6 +3,7 @@ import mdx from '@astrojs/mdx';
3
3
  import { expect } from 'chai';
4
4
  import { parseHTML } from 'linkedom';
5
5
  import { loadFixture } from '../../../astro/test/test-utils.js';
6
+ import * as cheerio from 'cheerio';
6
7
 
7
8
  describe('Head injection w/ MDX', () => {
8
9
  let fixture;
@@ -23,11 +24,62 @@ describe('Head injection w/ MDX', () => {
23
24
  const html = await fixture.readFile('/indexThree/index.html');
24
25
  const { document } = parseHTML(html);
25
26
 
26
- const links = document.querySelectorAll('link[rel=stylesheet]');
27
+ const links = document.querySelectorAll('head link[rel=stylesheet]');
27
28
  expect(links).to.have.a.lengthOf(1);
28
29
 
29
- const scripts = document.querySelectorAll('script[type=module]');
30
+ const scripts = document.querySelectorAll('head script[type=module]');
30
31
  expect(scripts).to.have.a.lengthOf(1);
31
32
  });
33
+
34
+ it('injects into the head for content collections', async () => {
35
+ const html = await fixture.readFile('/posts/test/index.html');
36
+ const { document } = parseHTML(html);
37
+
38
+ const links = document.querySelectorAll('head link[rel=stylesheet]');
39
+ expect(links).to.have.a.lengthOf(1);
40
+ });
41
+
42
+ it('injects content from a component using Content#render()', async () => {
43
+ const html = await fixture.readFile('/DirectContentUsage/index.html');
44
+ const { document } = parseHTML(html);
45
+
46
+ const links = document.querySelectorAll('head link[rel=stylesheet]');
47
+ expect(links).to.have.a.lengthOf(1);
48
+
49
+ const scripts = document.querySelectorAll('head script[type=module]');
50
+ expect(scripts).to.have.a.lengthOf(2);
51
+ });
52
+
53
+ it('Using component using slots.render() API', async () => {
54
+ const html = await fixture.readFile('/remote/index.html');
55
+ const { document } = parseHTML(html);
56
+
57
+ const links = document.querySelectorAll('head link[rel=stylesheet]');
58
+ expect(links).to.have.a.lengthOf(1);
59
+ });
60
+
61
+ it('Using component but no layout', async () => {
62
+ const html = await fixture.readFile('/noLayoutWithComponent/index.html');
63
+ // Using cheerio here because linkedom doesn't support head tag injection
64
+ const $ = cheerio.load(html);
65
+
66
+ const headLinks = $('head link[rel=stylesheet]');
67
+ expect(headLinks).to.have.a.lengthOf(1);
68
+
69
+ const bodyLinks = $('body link[rel=stylesheet]');
70
+ expect(bodyLinks).to.have.a.lengthOf(0);
71
+ });
72
+
73
+ it('JSX component rendering Astro children within head buffering phase', async () => {
74
+ const html = await fixture.readFile('/posts/using-component/index.html');
75
+ // Using cheerio here because linkedom doesn't support head tag injection
76
+ const $ = cheerio.load(html);
77
+
78
+ const headLinks = $('head link[rel=stylesheet]');
79
+ expect(headLinks).to.have.a.lengthOf(1);
80
+
81
+ const bodyLinks = $('body link[rel=stylesheet]');
82
+ expect(bodyLinks).to.have.a.lengthOf(0);
83
+ });
32
84
  });
33
85
  });
@@ -0,0 +1,17 @@
1
+ #!/bin/sh
2
+ basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
3
+
4
+ case `uname` in
5
+ *CYGWIN*) basedir=`cygpath -w "$basedir"`;;
6
+ esac
7
+
8
+ if [ -z "$NODE_PATH" ]; then
9
+ export NODE_PATH="/home/runner/work/astro/astro/node_modules/.pnpm/node_modules"
10
+ else
11
+ export NODE_PATH="$NODE_PATH:/home/runner/work/astro/astro/node_modules/.pnpm/node_modules"
12
+ fi
13
+ if [ -x "$basedir/node" ]; then
14
+ exec "$basedir/node" "$basedir/../../../../../../../astro/astro.js" "$@"
15
+ else
16
+ exec node "$basedir/../../../../../../../astro/astro.js" "$@"
17
+ fi
@@ -0,0 +1,10 @@
1
+ {
2
+ "name": "@test/mdx-css-head-mdx",
3
+ "version": "0.0.0",
4
+ "private": true,
5
+ "dependencies": {
6
+ "astro": "workspace:*",
7
+ "@astrojs/mdx": "workspace:*",
8
+ "astro-remote": "0.2.3"
9
+ }
10
+ }
@@ -0,0 +1,11 @@
1
+ ---
2
+ const { title } = Astro.props;
3
+ ---
4
+ <meta charset="UTF-8" />
5
+ <meta name="viewport" content="width=device-width" />
6
+ <link rel="icon" type="image/svg+xml" href="/favicon.svg" />
7
+ <meta name="generator" content={Astro.generator} />
8
+ <title>{title}</title>
9
+ <style is:global>
10
+ @import "../styles/global.css";
11
+ </style>
@@ -0,0 +1 @@
1
+ <span>just a generic component</span>
@@ -0,0 +1,9 @@
1
+ ---
2
+ import Component from "./GenericComponent.astro";
3
+ ---
4
+
5
+ <div>
6
+ <slot name="title" />
7
+ <slot name="intro" class="inline" />
8
+ <Component />
9
+ </div>
@@ -0,0 +1,3 @@
1
+ <p>
2
+ <slot />
3
+ </p>
@@ -0,0 +1,3 @@
1
+ ---
2
+ ---
3
+ <span style={{fontVariant: "small-caps"}}><slot /></span>
@@ -0,0 +1,8 @@
1
+ ---
2
+ import { getEntryBySlug } from 'astro:content';
3
+
4
+ const launchWeek = await getEntryBySlug('blog', 'using-mdx');
5
+ const { Content } = await launchWeek.render();
6
+ ---
7
+
8
+ <Content />
@@ -0,0 +1,6 @@
1
+ ---
2
+ ---
3
+
4
+ <script>
5
+ console.log('hoisted')
6
+ </script>
@@ -0,0 +1,3 @@
1
+ body {
2
+ color: red !important;
3
+ }
@@ -0,0 +1,6 @@
1
+ import './_styles.css';
2
+ import WithHoistedScripts from '../../components/WithHoistedScripts.astro';
3
+
4
+ # Using mdx
5
+
6
+ <WithHoistedScripts />
@@ -0,0 +1,5 @@
1
+ ---
2
+ title: Testing
3
+ ---
4
+
5
+ <SmallCaps>A test file</SmallCaps>
@@ -0,0 +1,13 @@
1
+ ---
2
+ title: testing
3
+ ---
4
+ import MDXWrapper from "../../components/MDXWrapper.astro";
5
+
6
+ <MDXWrapper>
7
+ <h1 slot="title">
8
+ testing
9
+ </h1>
10
+ <div slot="intro">
11
+ Intro
12
+ </div>
13
+ </MDXWrapper>
@@ -0,0 +1,18 @@
1
+ ---
2
+ import BaseHead from "../components/BaseHead.astro";
3
+ export interface Props {
4
+ title: string;
5
+ }
6
+
7
+ const { title } = Astro.props;
8
+ ---
9
+
10
+ <!DOCTYPE html>
11
+ <html lang="en">
12
+ <head>
13
+ <BaseHead title={title} />
14
+ </head>
15
+ <body>
16
+ <slot />
17
+ </body>
18
+ </html>
@@ -0,0 +1,17 @@
1
+ ---
2
+ import UsingMdx from '../components/UsingMdx.astro'
3
+ ---
4
+
5
+ <html lang="en">
6
+ <head>
7
+ <meta charset="utf-8" />
8
+ <link rel="icon" type="image/svg+xml" href="/favicon.svg" />
9
+ <meta name="viewport" content="width=device-width" />
10
+ <meta name="generator" content={Astro.generator} />
11
+ <title>Astro</title>
12
+ </head>
13
+ <body>
14
+ <h1>Astro</h1>
15
+ <UsingMdx />
16
+ </body>
17
+ </html>
@@ -0,0 +1,22 @@
1
+ ---
2
+ title: 'Lorem'
3
+ description: 'Lorem ipsum dolor sit amet'
4
+ pubDate: 'Jul 02 2022'
5
+ ---
6
+
7
+ import MyComponent from '../components/HelloWorld.astro';
8
+
9
+
10
+ ## Lorem
11
+
12
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
13
+
14
+ ## Lorem 2
15
+
16
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
17
+
18
+ <MyComponent />
19
+
20
+ ## Lorem 3
21
+
22
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
@@ -0,0 +1,18 @@
1
+ ---
2
+ import { getCollection } from 'astro:content';
3
+ import Layout from '../../layouts/ContentLayout.astro';
4
+ import SmallCaps from '../../components/SmallCaps.astro';
5
+
6
+ export async function getStaticPaths() {
7
+ const entries = await getCollection('posts');
8
+ return entries.map(entry => {
9
+ return {params: { post: entry.slug }, props: { entry },
10
+ }});
11
+ }
12
+
13
+ const { entry } = Astro.props;
14
+ const { Content } = await entry.render();
15
+ ---
16
+ <Layout title="">
17
+ <Content components={{ SmallCaps }} />
18
+ </Layout>
@@ -0,0 +1,17 @@
1
+ ---
2
+ import '../styles/global.css'
3
+ import Layout from '../layouts/One.astro';
4
+ import Paragraph from '../components/P.astro';
5
+ import { Markdown } from 'astro-remote'
6
+ ---
7
+
8
+ <Layout title="Welcome to Astro.">
9
+ <main>
10
+ <Markdown
11
+ components={{
12
+ p: Paragraph,
13
+ }}>
14
+ **Removing p component fixes the problem**
15
+ </Markdown>
16
+ </main>
17
+ </Layout>
@@ -0,0 +1,3 @@
1
+ html {
2
+ font-weight: bolder;
3
+ }
@@ -63,6 +63,20 @@ describe('MDX plugins', () => {
63
63
  expect(selectRehypeExample(document)).to.not.be.null;
64
64
  });
65
65
 
66
+ it('supports custom rehype plugins with namespaced attributes', async () => {
67
+ const fixture = await buildFixture({
68
+ integrations: [
69
+ mdx({
70
+ rehypePlugins: [rehypeSvgPlugin],
71
+ }),
72
+ ],
73
+ });
74
+ const html = await fixture.readFile(FILE);
75
+ const { document } = parseHTML(html);
76
+
77
+ expect(selectRehypeSvg(document)).to.not.be.null;
78
+ });
79
+
66
80
  it('extends markdown config by default', async () => {
67
81
  const fixture = await buildFixture({
68
82
  markdown: {
@@ -207,6 +221,23 @@ function rehypeExamplePlugin() {
207
221
  };
208
222
  }
209
223
 
224
+ function rehypeSvgPlugin() {
225
+ return (tree) => {
226
+ tree.children.push({
227
+ type: 'element',
228
+ tagName: 'svg',
229
+ properties: { xmlns: 'http://www.w3.org/2000/svg' },
230
+ children: [
231
+ {
232
+ type: 'element',
233
+ tagName: 'use',
234
+ properties: { xLinkHref: '#icon' },
235
+ },
236
+ ],
237
+ });
238
+ };
239
+ }
240
+
210
241
  function recmaExamplePlugin() {
211
242
  return (tree) => {
212
243
  estreeVisit(tree, (node) => {
@@ -245,6 +276,10 @@ function selectRehypeExample(document) {
245
276
  return document.querySelector('div[data-rehype-plugin-works]');
246
277
  }
247
278
 
279
+ function selectRehypeSvg(document) {
280
+ return document.querySelector('svg > use[xlink\\:href]');
281
+ }
282
+
248
283
  function selectRecmaExample(document) {
249
284
  return document.querySelector('div[data-recma-plugin-works]');
250
285
  }