@astrojs/mdx 0.8.2 → 0.8.3

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 (64) hide show
  1. package/.turbo/turbo-build.log +5 -5
  2. package/CHANGELOG.md +11 -0
  3. package/dist/astro-data-utils.js +35 -0
  4. package/dist/index.js +1 -14
  5. package/package.json +3 -3
  6. package/src/astro-data-utils.ts +36 -0
  7. package/src/index.ts +1 -10
  8. package/test/fixtures/mdx-frontmatter-injection/node_modules/.bin/astro +2 -2
  9. package/test/fixtures/mdx-frontmatter-injection/src/layouts/Base.astro +17 -0
  10. package/test/fixtures/mdx-frontmatter-injection/src/pages/page-1.mdx +4 -0
  11. package/test/fixtures/mdx-frontmatter-injection/src/pages/page-2.mdx +4 -0
  12. package/test/fixtures/mdx-page/node_modules/.bin/astro +2 -2
  13. package/test/fixtures/mdx-plus-react/node_modules/.bin/astro +2 -2
  14. package/test/mdx-frontmatter-injection.test.js +12 -0
  15. package/test/fixtures/mdx-component/dist/index.html +0 -3
  16. package/test/fixtures/mdx-component/node_modules/.vite/deps_temp/package.json +0 -1
  17. package/test/fixtures/mdx-custom-frontmatter-name/dist/glob.json +0 -1
  18. package/test/fixtures/mdx-custom-frontmatter-name/dist/index.html +0 -1
  19. package/test/fixtures/mdx-custom-frontmatter-name/node_modules/.vite/deps_temp/package.json +0 -1
  20. package/test/fixtures/mdx-frontmatter/dist/glob.json +0 -1
  21. package/test/fixtures/mdx-frontmatter/dist/index.html +0 -17
  22. package/test/fixtures/mdx-frontmatter/dist/with-headings/index.html +0 -18
  23. package/test/fixtures/mdx-frontmatter/node_modules/.vite/deps_temp/package.json +0 -1
  24. package/test/fixtures/mdx-get-headings/dist/pages.json +0 -1
  25. package/test/fixtures/mdx-get-headings/dist/test/index.html +0 -5
  26. package/test/fixtures/mdx-get-headings/dist/test-with-jsx-expressions/index.html +0 -3
  27. package/test/fixtures/mdx-get-headings/node_modules/.vite/deps_temp/package.json +0 -1
  28. package/test/fixtures/mdx-get-static-paths/dist/one/index.html +0 -13
  29. package/test/fixtures/mdx-page/dist/index.html +0 -1
  30. package/test/fixtures/mdx-page/node_modules/.vite/deps_temp/package.json +0 -1
  31. package/test/fixtures/mdx-plus-react/dist/client.0d691cb3.js +0 -32
  32. package/test/fixtures/mdx-plus-react/dist/index.html +0 -8
  33. package/test/fixtures/mdx-plus-react/node_modules/.vite/deps_temp/@astrojs_react_client__js.js +0 -95
  34. package/test/fixtures/mdx-plus-react/node_modules/.vite/deps_temp/@astrojs_react_client__js.js.map +0 -7
  35. package/test/fixtures/mdx-plus-react/node_modules/.vite/deps_temp/_metadata.json +0 -47
  36. package/test/fixtures/mdx-plus-react/node_modules/.vite/deps_temp/chunk-CDYRCHBX.js +0 -7480
  37. package/test/fixtures/mdx-plus-react/node_modules/.vite/deps_temp/chunk-CDYRCHBX.js.map +0 -7
  38. package/test/fixtures/mdx-plus-react/node_modules/.vite/deps_temp/chunk-RRXZ3AUC.js +0 -326
  39. package/test/fixtures/mdx-plus-react/node_modules/.vite/deps_temp/chunk-RRXZ3AUC.js.map +0 -7
  40. package/test/fixtures/mdx-plus-react/node_modules/.vite/deps_temp/chunk-TWLJ45QX.js +0 -27
  41. package/test/fixtures/mdx-plus-react/node_modules/.vite/deps_temp/chunk-TWLJ45QX.js.map +0 -7
  42. package/test/fixtures/mdx-plus-react/node_modules/.vite/deps_temp/package.json +0 -1
  43. package/test/fixtures/mdx-plus-react/node_modules/.vite/deps_temp/react-dom.js +0 -12
  44. package/test/fixtures/mdx-plus-react/node_modules/.vite/deps_temp/react-dom.js.map +0 -7
  45. package/test/fixtures/mdx-plus-react/node_modules/.vite/deps_temp/react.js +0 -11
  46. package/test/fixtures/mdx-plus-react/node_modules/.vite/deps_temp/react.js.map +0 -7
  47. package/test/fixtures/mdx-plus-react/node_modules/.vite/deps_temp/react_jsx-dev-runtime.js +0 -41
  48. package/test/fixtures/mdx-plus-react/node_modules/.vite/deps_temp/react_jsx-dev-runtime.js.map +0 -7
  49. package/test/fixtures/mdx-plus-react/node_modules/.vite/deps_temp/react_jsx-runtime.js +0 -62
  50. package/test/fixtures/mdx-plus-react/node_modules/.vite/deps_temp/react_jsx-runtime.js.map +0 -7
  51. package/test/fixtures/mdx-rehype-plugins/dist/reading-time.json +0 -1
  52. package/test/fixtures/mdx-rehype-plugins/dist/space-ipsum/index.html +0 -13
  53. package/test/fixtures/mdx-rehype-plugins/node_modules/.vite/deps_temp/package.json +0 -1
  54. package/test/fixtures/mdx-remark-plugins/dist/with-gfm/index.html +0 -2
  55. package/test/fixtures/mdx-remark-plugins/dist/with-toc/index.html +0 -22
  56. package/test/fixtures/mdx-remark-plugins/node_modules/.vite/deps_temp/package.json +0 -1
  57. package/test/fixtures/mdx-syntax-hightlighting/dist/index.html +0 -6
  58. package/test/fixtures/mdx-syntax-hightlighting/node_modules/.vite/deps_temp/package.json +0 -1
  59. package/test/fixtures/mdx-url-export/dist/pages.json +0 -1
  60. package/test/fixtures/mdx-url-export/dist/test-1/index.html +0 -1
  61. package/test/fixtures/mdx-url-export/dist/test-2/index.html +0 -1
  62. package/test/fixtures/mdx-url-export/dist/with-url-override/index.html +0 -1
  63. package/test/fixtures/mdx-url-export/node_modules/.vite/deps_temp/package.json +0 -1
  64. package/test/fixtures/node_modules/.vite/deps_temp/package.json +0 -1
@@ -1,5 +1,5 @@
1
- @astrojs/mdx:build: cache hit, replaying output 3036c715efc9fce9
2
- @astrojs/mdx:build: 
3
- @astrojs/mdx:build: > @astrojs/mdx@0.8.2 build /Users/matthew/dev/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 67cdf445a0fed367
2
+ @astrojs/mdx:build: 
3
+ @astrojs/mdx:build: > @astrojs/mdx@0.8.3 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,16 @@
1
1
  # @astrojs/mdx
2
2
 
3
+ ## 0.8.3
4
+
5
+ ### Patch Changes
6
+
7
+ - [#4248](https://github.com/withastro/astro/pull/4248) [`869d00935`](https://github.com/withastro/astro/commit/869d0093596b709cfcc1a1a95ee631b48d6d1c26) Thanks [@svemat01](https://github.com/svemat01)! - Load builtin rehype plugins before user plugins instead of after
8
+
9
+ * [#4255](https://github.com/withastro/astro/pull/4255) [`411612808`](https://github.com/withastro/astro/commit/4116128082121ee276d51cb245bf8095be4728a1) Thanks [@bholmesdev](https://github.com/bholmesdev)! - Pass injected frontmatter from remark and rehype plugins to layouts
10
+
11
+ * Updated dependencies [[`1f0dd31d9`](https://github.com/withastro/astro/commit/1f0dd31d9239b5e3dca99c88d021e7a9a3e2054d)]:
12
+ - @astrojs/prism@1.0.1
13
+
3
14
  ## 0.8.2
4
15
 
5
16
  ### Patch Changes
@@ -14,6 +14,41 @@ function rehypeApplyFrontmatterExport(pageFrontmatter) {
14
14
  const exportNodes = [
15
15
  jsToTreeNode(`export const ${EXPORT_NAME} = ${JSON.stringify(frontmatter)};`)
16
16
  ];
17
+ if (frontmatter.layout) {
18
+ exportNodes.unshift(
19
+ jsToTreeNode(
20
+ `import { jsx as layoutJsx } from 'astro/jsx-runtime';
21
+ import Layout from ${JSON.stringify(frontmatter.layout)};
22
+
23
+ export default function ({ children }) {
24
+ const { layout, ...content } = frontmatter;
25
+ content.astro = {};
26
+ Object.defineProperty(content.astro, 'headings', {
27
+ get() {
28
+ throw new Error('The "astro" property is no longer supported! To access "headings" from your layout, try using "Astro.props.headings."')
29
+ }
30
+ });
31
+ Object.defineProperty(content.astro, 'html', {
32
+ get() {
33
+ throw new Error('The "astro" property is no longer supported! To access "html" from your layout, try using "Astro.props.compiledContent()."')
34
+ }
35
+ });
36
+ Object.defineProperty(content.astro, 'source', {
37
+ get() {
38
+ throw new Error('The "astro" property is no longer supported! To access "source" from your layout, try using "Astro.props.rawContent()."')
39
+ }
40
+ });
41
+ return layoutJsx(Layout, {
42
+ content,
43
+ frontmatter: content,
44
+ headings: getHeadings(),
45
+ 'server:root': true,
46
+ children,
47
+ });
48
+ };`
49
+ )
50
+ );
51
+ }
17
52
  tree.children = exportNodes.concat(tree.children);
18
53
  };
19
54
  }
package/dist/index.js CHANGED
@@ -65,20 +65,7 @@ function mdx(mdxOptions = {}) {
65
65
  async transform(code, id) {
66
66
  if (!id.endsWith("mdx"))
67
67
  return;
68
- let { data: frontmatter, content: pageContent } = parseFrontmatter(code, id);
69
- if (frontmatter.layout) {
70
- const { layout, ...contentProp } = frontmatter;
71
- pageContent += `
72
-
73
- export default async function({ children }) {
74
- const Layout = (await import(${JSON.stringify(
75
- frontmatter.layout
76
- )})).default;
77
- const frontmatter=${JSON.stringify(
78
- contentProp
79
- )};
80
- return <Layout frontmatter={frontmatter} content={frontmatter} headings={getHeadings()}>{children}</Layout> }`;
81
- }
68
+ const { data: frontmatter, content: pageContent } = parseFrontmatter(code, id);
82
69
  const compiled = await mdxCompile(new VFile({ value: pageContent, path: id }), {
83
70
  ...mdxPluginOpts,
84
71
  rehypePlugins: [
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.8.2",
4
+ "version": "0.8.3",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",
7
7
  "author": "withastro",
@@ -23,7 +23,7 @@
23
23
  "./package.json": "./package.json"
24
24
  },
25
25
  "dependencies": {
26
- "@astrojs/prism": "^1.0.0",
26
+ "@astrojs/prism": "^1.0.1",
27
27
  "@mdx-js/mdx": "^2.1.2",
28
28
  "@mdx-js/rollup": "^2.1.1",
29
29
  "acorn": "^8.8.0",
@@ -43,7 +43,7 @@
43
43
  "@types/chai": "^4.3.1",
44
44
  "@types/mocha": "^9.1.1",
45
45
  "@types/yargs-parser": "^21.0.0",
46
- "astro": "1.0.2",
46
+ "astro": "1.0.3",
47
47
  "astro-scripts": "0.0.7",
48
48
  "chai": "^4.3.6",
49
49
  "linkedom": "^0.14.12",
@@ -19,6 +19,42 @@ export function rehypeApplyFrontmatterExport(pageFrontmatter: Record<string, any
19
19
  const exportNodes = [
20
20
  jsToTreeNode(`export const ${EXPORT_NAME} = ${JSON.stringify(frontmatter)};`),
21
21
  ];
22
+ if (frontmatter.layout) {
23
+ exportNodes.unshift(
24
+ jsToTreeNode(
25
+ /** @see 'vite-plugin-markdown' for layout props reference */
26
+ `import { jsx as layoutJsx } from 'astro/jsx-runtime';
27
+ import Layout from ${JSON.stringify(frontmatter.layout)};
28
+
29
+ export default function ({ children }) {
30
+ const { layout, ...content } = frontmatter;
31
+ content.astro = {};
32
+ Object.defineProperty(content.astro, 'headings', {
33
+ get() {
34
+ throw new Error('The "astro" property is no longer supported! To access "headings" from your layout, try using "Astro.props.headings."')
35
+ }
36
+ });
37
+ Object.defineProperty(content.astro, 'html', {
38
+ get() {
39
+ throw new Error('The "astro" property is no longer supported! To access "html" from your layout, try using "Astro.props.compiledContent()."')
40
+ }
41
+ });
42
+ Object.defineProperty(content.astro, 'source', {
43
+ get() {
44
+ throw new Error('The "astro" property is no longer supported! To access "source" from your layout, try using "Astro.props.rawContent()."')
45
+ }
46
+ });
47
+ return layoutJsx(Layout, {
48
+ content,
49
+ frontmatter: content,
50
+ headings: getHeadings(),
51
+ 'server:root': true,
52
+ children,
53
+ });
54
+ };`
55
+ )
56
+ );
57
+ }
22
58
  tree.children = exportNodes.concat(tree.children);
23
59
  };
24
60
  }
package/src/index.ts CHANGED
@@ -98,16 +98,7 @@ export default function mdx(mdxOptions: MdxOptions = {}): AstroIntegration {
98
98
  async transform(code, id) {
99
99
  if (!id.endsWith('mdx')) return;
100
100
 
101
- let { data: frontmatter, content: pageContent } = parseFrontmatter(code, id);
102
- if (frontmatter.layout) {
103
- const { layout, ...contentProp } = frontmatter;
104
- pageContent += `\n\nexport default async function({ children }) {\nconst Layout = (await import(${JSON.stringify(
105
- frontmatter.layout
106
- )})).default;\nconst frontmatter=${JSON.stringify(
107
- contentProp
108
- )};\nreturn <Layout frontmatter={frontmatter} content={frontmatter} headings={getHeadings()}>{children}</Layout> }`;
109
- }
110
-
101
+ const { data: frontmatter, content: pageContent } = parseFrontmatter(code, id);
111
102
  const compiled = await mdxCompile(new VFile({ value: pageContent, path: id }), {
112
103
  ...mdxPluginOpts,
113
104
  rehypePlugins: [
@@ -6,9 +6,9 @@ case `uname` in
6
6
  esac
7
7
 
8
8
  if [ -z "$NODE_PATH" ]; then
9
- export NODE_PATH="/Users/matthew/dev/astro/node_modules/.pnpm/node_modules"
9
+ export NODE_PATH="/home/runner/work/astro/astro/node_modules/.pnpm/node_modules"
10
10
  else
11
- export NODE_PATH="$NODE_PATH:/Users/matthew/dev/astro/node_modules/.pnpm/node_modules"
11
+ export NODE_PATH="$NODE_PATH:/home/runner/work/astro/astro/node_modules/.pnpm/node_modules"
12
12
  fi
13
13
  if [ -x "$basedir/node" ]; then
14
14
  exec "$basedir/node" "$basedir/../../../../../../../astro/astro.js" "$@"
@@ -0,0 +1,17 @@
1
+ ---
2
+ const defaults = { title: 'Frontmatter not passed to layout!' }
3
+ const { frontmatter = defaults, content = defaults } = Astro.props;
4
+ ---
5
+
6
+ <!DOCTYPE html>
7
+ <html lang="en">
8
+ <head>
9
+ <meta charset="UTF-8">
10
+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
11
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
12
+ <title>{frontmatter.title}</title>
13
+ </head>
14
+ <body>
15
+ <slot />
16
+ </body>
17
+ </html>
@@ -1,3 +1,7 @@
1
+ ---
2
+ layout: '../layouts/Base.astro'
3
+ ---
4
+
1
5
  # Page 1
2
6
 
3
7
  Look at that!
@@ -1,3 +1,7 @@
1
+ ---
2
+ layout: '../layouts/Base.astro'
3
+ ---
4
+
1
5
  # Page 2
2
6
 
3
7
  ## Table of contents
@@ -6,9 +6,9 @@ case `uname` in
6
6
  esac
7
7
 
8
8
  if [ -z "$NODE_PATH" ]; then
9
- export NODE_PATH="/Users/matthew/dev/astro/node_modules/.pnpm/node_modules"
9
+ export NODE_PATH="/home/runner/work/astro/astro/node_modules/.pnpm/node_modules"
10
10
  else
11
- export NODE_PATH="$NODE_PATH:/Users/matthew/dev/astro/node_modules/.pnpm/node_modules"
11
+ export NODE_PATH="$NODE_PATH:/home/runner/work/astro/astro/node_modules/.pnpm/node_modules"
12
12
  fi
13
13
  if [ -x "$basedir/node" ]; then
14
14
  exec "$basedir/node" "$basedir/../../../../../../../astro/astro.js" "$@"
@@ -6,9 +6,9 @@ case `uname` in
6
6
  esac
7
7
 
8
8
  if [ -z "$NODE_PATH" ]; then
9
- export NODE_PATH="/Users/matthew/dev/astro/node_modules/.pnpm/node_modules"
9
+ export NODE_PATH="/home/runner/work/astro/astro/node_modules/.pnpm/node_modules"
10
10
  else
11
- export NODE_PATH="$NODE_PATH:/Users/matthew/dev/astro/node_modules/.pnpm/node_modules"
11
+ export NODE_PATH="$NODE_PATH:/home/runner/work/astro/astro/node_modules/.pnpm/node_modules"
12
12
  fi
13
13
  if [ -x "$basedir/node" ]; then
14
14
  exec "$basedir/node" "$basedir/../../../../../../../astro/astro.js" "$@"
@@ -1,4 +1,5 @@
1
1
  import { expect } from 'chai';
2
+ import { parseHTML } from 'linkedom';
2
3
  import { loadFixture } from '../../../astro/test/test-utils.js';
3
4
 
4
5
  const FIXTURE_ROOT = new URL('./fixtures/mdx-frontmatter-injection/', import.meta.url);
@@ -41,4 +42,15 @@ describe('MDX frontmatter injection', () => {
41
42
  expect(titles).to.contain('Overridden title');
42
43
  expect(readingTimes).to.contain('1000 min read');
43
44
  });
45
+
46
+ it('passes injected frontmatter to layouts', async () => {
47
+ const html1 = await fixture.readFile('/page-1/index.html');
48
+ const html2 = await fixture.readFile('/page-2/index.html');
49
+
50
+ const title1 = parseHTML(html1).document.querySelector('title');
51
+ const title2 = parseHTML(html2).document.querySelector('title');
52
+
53
+ expect(title1.innerHTML).to.equal('Page 1');
54
+ expect(title2.innerHTML).to.equal('Page 2');
55
+ });
44
56
  });
@@ -1,3 +0,0 @@
1
- <!DOCTYPE html>
2
- <h1 id="hello-component">Hello component!</h1>
3
- <div id="foo">bar</div>
@@ -1 +0,0 @@
1
- {"titles":["Using YAML frontmatter"]}
@@ -1 +0,0 @@
1
- <!DOCTYPE html><h1 id="customfrontmatterillthrowifidontexist">Oh no, that&#39;s scary!</h1>
@@ -1 +0,0 @@
1
- {"titles":["Using YAML frontmatter",null]}
@@ -1,17 +0,0 @@
1
- <!DOCTYPE html><html lang="en">
2
-
3
- <head>
4
- <meta charset="UTF-8">
5
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
6
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
- </head>
8
-
9
- <body>
10
- <p data-content-title>Using YAML frontmatter</p>
11
- <p data-frontmatter-title>Using YAML frontmatter</p>
12
- <p data-layout-rendered>Layout rendered!</p>
13
- <ul data-headings>
14
-
15
- </ul>
16
- Oh no, that&#39;s scary!
17
- </body></html>
@@ -1,18 +0,0 @@
1
- <!DOCTYPE html><html lang="en">
2
-
3
- <head>
4
- <meta charset="UTF-8">
5
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
6
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
- </head>
8
-
9
- <body>
10
- <p data-content-title></p>
11
- <p data-frontmatter-title></p>
12
- <p data-layout-rendered>Layout rendered!</p>
13
- <ul data-headings>
14
- <li>section-1</li><li>section-2</li>
15
- </ul>
16
- <h2 id="section-1">Section 1</h2>
17
- <h2 id="section-2">Section 2</h2>
18
- </body></html>
@@ -1 +0,0 @@
1
- {"headingsByPage":{"./test-with-jsx-expressions.mdx":[{"depth":1,"slug":"heading-test-with-jsx-expressions","text":"Heading test with JSX expressions"},{"depth":2,"slug":"h2title","text":"h2Title"},{"depth":3,"slug":"h3title","text":"h3Title"}],"./test.mdx":[{"depth":1,"slug":"heading-test","text":"Heading test"},{"depth":2,"slug":"section-1","text":"Section 1"},{"depth":3,"slug":"subsection-1","text":"Subsection 1"},{"depth":3,"slug":"subsection-2","text":"Subsection 2"},{"depth":2,"slug":"section-2","text":"Section 2"}]}}
@@ -1,5 +0,0 @@
1
- <!DOCTYPE html><h1 id="heading-test">Heading test</h1>
2
- <h2 id="section-1">Section 1</h2>
3
- <h3 id="subsection-1">Subsection 1</h3>
4
- <h3 id="subsection-2">Subsection 2</h3>
5
- <h2 id="section-2">Section 2</h2>
@@ -1,3 +0,0 @@
1
- <!DOCTYPE html><h1 id="heading-test-with-jsx-expressions">Heading test with JSX expressions</h1>
2
- <h2 id="h2title">Section 1</h2>
3
- <h3 id="h3title">Subsection 1</h3>
@@ -1,13 +0,0 @@
1
- <!DOCTYPE html>
2
- <html>
3
- <head>
4
- <title>Page</title>
5
- </head>
6
- <body>
7
- <p>First mdx file</p>
8
-
9
- <div id="one">hello</div>
10
- <div id="url">/src/content/1.mdx</div>
11
- <div id="file">/Users/matthew/dev/astro/packages/integrations/mdx/test/fixtures/mdx-get-static-paths/src/content/1.mdx</div>
12
- </body>
13
- </html>
@@ -1 +0,0 @@
1
- <!DOCTYPE html><h1 id="hello-page">Hello page!</h1>
@@ -1 +0,0 @@
1
- {"type":"module"}