@astrojs/mdx 0.18.1 → 0.18.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.
@@ -1,5 +1,5 @@
1
- @astrojs/mdx:build: cache hit, replaying output 6863c2b0a0d736f6
2
- @astrojs/mdx:build: 
3
- @astrojs/mdx:build: > @astrojs/mdx@0.18.1 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 603ca93f0d424089
2
+ @astrojs/mdx:build: 
3
+ @astrojs/mdx:build: > @astrojs/mdx@0.18.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,20 @@
1
1
  # @astrojs/mdx
2
2
 
3
+ ## 0.18.3
4
+
5
+ ### Patch Changes
6
+
7
+ - [#6779](https://github.com/withastro/astro/pull/6779) [`a98f6f418`](https://github.com/withastro/astro/commit/a98f6f418c92261a06ef79624a8c86e288c21eab) Thanks [@matthewp](https://github.com/matthewp)! - Prevent body head content injection in MDX when using layout
8
+
9
+ ## 0.18.2
10
+
11
+ ### Patch Changes
12
+
13
+ - [#6552](https://github.com/withastro/astro/pull/6552) [`392ba3e4d`](https://github.com/withastro/astro/commit/392ba3e4d55f73ce9194bd94a2243f1aa62af079) Thanks [@bluwy](https://github.com/bluwy)! - Fix integration return type
14
+
15
+ - Updated dependencies [[`90e5f87d0`](https://github.com/withastro/astro/commit/90e5f87d03215a833bb6ac91f9548670a25ce659), [`f5fddafc2`](https://github.com/withastro/astro/commit/f5fddafc248bb1ef57b7349bfecc25539ae2b5ea)]:
16
+ - @astrojs/markdown-remark@2.1.1
17
+
3
18
  ## 0.18.1
4
19
 
5
20
  ### Patch Changes
package/dist/index.d.ts CHANGED
@@ -1,22 +1,12 @@
1
1
  import { markdownConfigDefaults } from '@astrojs/markdown-remark';
2
2
  import type { PluggableList } from '@mdx-js/mdx/lib/core.js';
3
- import type { AstroIntegration, ContentEntryType, HookParameters } from 'astro';
3
+ import type { AstroIntegration } from 'astro';
4
4
  import type { Options as RemarkRehypeOptions } from 'remark-rehype';
5
- export declare type MdxOptions = Omit<typeof markdownConfigDefaults, 'remarkPlugins' | 'rehypePlugins'> & {
5
+ export type MdxOptions = Omit<typeof markdownConfigDefaults, 'remarkPlugins' | 'rehypePlugins'> & {
6
6
  extendMarkdownConfig: boolean;
7
7
  recmaPlugins: PluggableList;
8
8
  remarkPlugins: PluggableList;
9
9
  rehypePlugins: PluggableList;
10
10
  remarkRehype: RemarkRehypeOptions;
11
11
  };
12
- declare type IntegrationWithPrivateHooks = {
13
- name: string;
14
- hooks: Omit<AstroIntegration['hooks'], 'astro:config:setup'> & {
15
- 'astro:config:setup': (params: HookParameters<'astro:config:setup'> & {
16
- addPageExtension: (extension: string) => void;
17
- addContentEntryType: (contentEntryType: ContentEntryType) => void;
18
- }) => void | Promise<void>;
19
- };
20
- };
21
- export default function mdx(partialMdxOptions?: Partial<MdxOptions>): IntegrationWithPrivateHooks;
22
- export {};
12
+ export default function mdx(partialMdxOptions?: Partial<MdxOptions>): AstroIntegration;
package/dist/index.js CHANGED
@@ -12,13 +12,8 @@ function mdx(partialMdxOptions = {}) {
12
12
  return {
13
13
  name: "@astrojs/mdx",
14
14
  hooks: {
15
- "astro:config:setup": async ({
16
- updateConfig,
17
- config,
18
- addPageExtension,
19
- addContentEntryType,
20
- command
21
- }) => {
15
+ "astro:config:setup": async (params) => {
16
+ const { updateConfig, config, addPageExtension, addContentEntryType, command } = params;
22
17
  addPageExtension(".mdx");
23
18
  addContentEntryType({
24
19
  extensions: [".mdx"],
@@ -50,6 +45,7 @@ function mdx(partialMdxOptions = {}) {
50
45
  remarkRehypeOptions: mdxOptions.remarkRehype,
51
46
  jsx: true,
52
47
  jsxImportSource: "astro",
48
+ // Note: disable `.md` (and other alternative extensions for markdown files like `.markdown`) support
53
49
  format: "mdx",
54
50
  mdExtensions: []
55
51
  };
@@ -65,6 +61,8 @@ function mdx(partialMdxOptions = {}) {
65
61
  configResolved(resolved) {
66
62
  importMetaEnv = { ...importMetaEnv, ...resolved.env };
67
63
  },
64
+ // Override transform to alter code before MDX compilation
65
+ // ex. inject layouts
68
66
  async transform(_, id) {
69
67
  if (!id.endsWith("mdx"))
70
68
  return;
@@ -75,6 +73,7 @@ function mdx(partialMdxOptions = {}) {
75
73
  ...mdxPluginOpts,
76
74
  elementAttributeNameCase: "html",
77
75
  remarkPlugins: [
76
+ // Ensure `data.astro` is available to all remark plugins
78
77
  toRemarkInitializeAstroData({ userFrontmatter: frontmatter }),
79
78
  ...mdxPluginOpts.remarkPlugins ?? []
80
79
  ],
@@ -91,6 +90,7 @@ function mdx(partialMdxOptions = {}) {
91
90
  },
92
91
  {
93
92
  name: "@astrojs/mdx-postprocess",
93
+ // These transforms must happen *after* JSX runtime transformations
94
94
  transform(code, id) {
95
95
  if (!id.endsWith(".mdx"))
96
96
  return;
@@ -122,6 +122,8 @@ export const Content = (props = {}) => MDXContent({
122
122
  }
123
123
  code += `
124
124
  Content[Symbol.for('astro.needsHeadRendering')] = !Boolean(frontmatter.layout);`;
125
+ code += `
126
+ Content.moduleId = ${JSON.stringify(id)};`;
125
127
  if (command === "dev") {
126
128
  code += `
127
129
  if (import.meta.hot) {
package/dist/plugins.js CHANGED
@@ -41,6 +41,8 @@ function rehypeApplyFrontmatterExport() {
41
41
  const astroData = safelyGetAstroData(vfile.data);
42
42
  if (astroData instanceof InvalidAstroDataError)
43
43
  throw new Error(
44
+ // Copied from Astro core `errors-data`
45
+ // TODO: find way to import error data from core
44
46
  '[MDX] A remark or rehype plugin attempted to inject invalid frontmatter. Ensure "astro.frontmatter" is set to a valid JSON object that is not `null` or `undefined`.'
45
47
  );
46
48
  const { frontmatter } = astroData;
@@ -50,6 +52,7 @@ function rehypeApplyFrontmatterExport() {
50
52
  if (frontmatter.layout) {
51
53
  exportNodes.unshift(
52
54
  jsToTreeNode(
55
+ /** @see 'vite-plugin-markdown' for layout props reference */
53
56
  `import { jsx as layoutJsx } from 'astro/jsx-runtime';
54
57
 
55
58
  export default async function ({ children }) {
@@ -96,13 +99,18 @@ async function getRemarkPlugins(mdxOptions, config) {
96
99
  }
97
100
  function getRehypePlugins(mdxOptions) {
98
101
  let rehypePlugins = [
102
+ // ensure `data.meta` is preserved in `properties.metastring` for rehype syntax highlighters
99
103
  rehypeMetaString,
104
+ // rehypeRaw allows custom syntax highlighters to work without added config
100
105
  [rehypeRaw, { passThrough: nodeTypes }]
101
106
  ];
102
107
  rehypePlugins = [
103
108
  ...rehypePlugins,
104
109
  ...mdxOptions.rehypePlugins,
110
+ // getHeadings() is guaranteed by TS, so this must be included.
111
+ // We run `rehypeHeadingIds` _last_ to respect any custom IDs set by user plugins.
105
112
  ...isPerformanceBenchmark ? [] : [rehypeHeadingIds, rehypeInjectHeadingsExport],
113
+ // computed from `astro.data.frontmatter` in VFile data
106
114
  rehypeApplyFrontmatterExport
107
115
  ];
108
116
  return rehypePlugins;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@astrojs/mdx",
3
3
  "description": "Add support for MDX pages in your Astro site",
4
- "version": "0.18.1",
4
+ "version": "0.18.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/markdown-remark": "^2.1.0",
26
+ "@astrojs/markdown-remark": "^2.1.3",
27
27
  "@astrojs/prism": "^2.1.1",
28
28
  "@mdx-js/mdx": "^2.3.0",
29
29
  "@mdx-js/rollup": "^2.3.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.1.3",
51
+ "astro": "2.2.1",
52
52
  "astro-scripts": "0.0.14",
53
53
  "chai": "^4.3.6",
54
54
  "cheerio": "^1.0.0-rc.11",
@@ -63,7 +63,7 @@
63
63
  "remark-rehype": "^10.1.0",
64
64
  "remark-shiki-twoslash": "^3.1.0",
65
65
  "remark-toc": "^8.0.1",
66
- "vite": "^4.1.2"
66
+ "vite": "^4.2.1"
67
67
  },
68
68
  "engines": {
69
69
  "node": ">=16.12.0"
package/src/index.ts CHANGED
@@ -2,7 +2,7 @@ import { markdownConfigDefaults } from '@astrojs/markdown-remark';
2
2
  import { toRemarkInitializeAstroData } from '@astrojs/markdown-remark/dist/internal.js';
3
3
  import { compile as mdxCompile } from '@mdx-js/mdx';
4
4
  import type { PluggableList } from '@mdx-js/mdx/lib/core.js';
5
- import mdxPlugin, { Options as MdxRollupPluginOptions } from '@mdx-js/rollup';
5
+ import mdxPlugin, { type Options as MdxRollupPluginOptions } from '@mdx-js/rollup';
6
6
  import type { AstroIntegration, ContentEntryType, HookParameters } from 'astro';
7
7
  import { parse as parseESM } from 'es-module-lexer';
8
8
  import fs from 'node:fs/promises';
@@ -23,33 +23,21 @@ export type MdxOptions = Omit<typeof markdownConfigDefaults, 'remarkPlugins' | '
23
23
  remarkRehype: RemarkRehypeOptions;
24
24
  };
25
25
 
26
- type IntegrationWithPrivateHooks = {
27
- name: string;
28
- hooks: Omit<AstroIntegration['hooks'], 'astro:config:setup'> & {
29
- 'astro:config:setup': (
30
- params: HookParameters<'astro:config:setup'> & {
31
- // `addPageExtension` and `contentEntryType` are not a public APIs
32
- // Add type defs here
33
- addPageExtension: (extension: string) => void;
34
- addContentEntryType: (contentEntryType: ContentEntryType) => void;
35
- }
36
- ) => void | Promise<void>;
37
- };
26
+ type SetupHookParams = HookParameters<'astro:config:setup'> & {
27
+ // `addPageExtension` and `contentEntryType` are not a public APIs
28
+ // Add type defs here
29
+ addPageExtension: (extension: string) => void;
30
+ addContentEntryType: (contentEntryType: ContentEntryType) => void;
38
31
  };
39
32
 
40
- export default function mdx(
41
- partialMdxOptions: Partial<MdxOptions> = {}
42
- ): IntegrationWithPrivateHooks {
33
+ export default function mdx(partialMdxOptions: Partial<MdxOptions> = {}): AstroIntegration {
43
34
  return {
44
35
  name: '@astrojs/mdx',
45
36
  hooks: {
46
- 'astro:config:setup': async ({
47
- updateConfig,
48
- config,
49
- addPageExtension,
50
- addContentEntryType,
51
- command,
52
- }) => {
37
+ 'astro:config:setup': async (params) => {
38
+ const { updateConfig, config, addPageExtension, addContentEntryType, command } =
39
+ params as SetupHookParams;
40
+
53
41
  addPageExtension('.mdx');
54
42
  addContentEntryType({
55
43
  extensions: ['.mdx'],
@@ -172,6 +160,7 @@ export default function mdx(
172
160
  // Ensures styles and scripts are injected into a `<head>`
173
161
  // When a layout is not applied
174
162
  code += `\nContent[Symbol.for('astro.needsHeadRendering')] = !Boolean(frontmatter.layout);`;
163
+ code += `\nContent.moduleId = ${JSON.stringify(id)};`;
175
164
 
176
165
  if (command === 'dev') {
177
166
  // TODO: decline HMR updates until we have a stable approach
@@ -81,5 +81,18 @@ describe('Head injection w/ MDX', () => {
81
81
  const bodyLinks = $('body link[rel=stylesheet]');
82
82
  expect(bodyLinks).to.have.a.lengthOf(0);
83
83
  });
84
+
85
+ it('Injection caused by delayed slots', async () => {
86
+ const html = await fixture.readFile('/componentwithtext/index.html');
87
+
88
+ // Using cheerio here because linkedom doesn't support head tag injection
89
+ const $ = cheerio.load(html);
90
+
91
+ const headLinks = $('head link[rel=stylesheet]');
92
+ expect(headLinks).to.have.a.lengthOf(1);
93
+
94
+ const bodyLinks = $('body link[rel=stylesheet]');
95
+ expect(bodyLinks).to.have.a.lengthOf(0);
96
+ });
84
97
  });
85
98
  });
@@ -0,0 +1,14 @@
1
+ ---
2
+ const { inlineStyle, title, display = 'horizontal' } = Astro.props;
3
+ const lineEnding = display === 'horizontal' ? ', ' : '<br>';
4
+ ---
5
+
6
+ {title && <h2 set:html={title} />}
7
+ <address style={inlineStyle}>
8
+ <span class="name">some name</span><Fragment set:html={lineEnding} />
9
+ line 1<Fragment set:html={lineEnding} />
10
+ line 2<Fragment set:html={lineEnding} />
11
+ line 3<Fragment set:html={lineEnding} />
12
+ line 4<Fragment set:html={lineEnding} />
13
+ line 5
14
+ </address>
@@ -0,0 +1,15 @@
1
+ ---
2
+ // Extend the BaseLayout, adding space for a banner at the top of the page
3
+ // after the main heading, then the detail for the actual page
4
+ import ContentLayout from './ContentLayout.astro';
5
+ const { frontmatter } = Astro.props;
6
+ ---
7
+
8
+ <ContentLayout>
9
+ <div class="content-container">
10
+ <article id="main-content" class="pad-z5 flow">
11
+ <h1 set:html={frontmatter.pageHeading ? frontmatter.pageHeading : frontmatter.title} />
12
+ <slot />
13
+ </article>
14
+ </div>
15
+ </ContentLayout>
@@ -0,0 +1,12 @@
1
+ ---
2
+ layout: ../layouts/DocumentLayout.astro
3
+ title: blah blah
4
+ ---
5
+
6
+ import BasicBlock from '../components/BasicBlock.astro';
7
+
8
+ Some text for a paragraph.
9
+
10
+ <BasicBlock title="This causes css in wrong place." />
11
+
12
+ Some other text.