Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ export default function MDXContent(props = {}) {
"
`;

exports[`MDX processor > remarkMdxOptions > parses import attributes with ecmaVersion 2025 1`] = `
exports[`MDX processor > remarkMdxOptions > parses import attributes by default (ES2025) 1`] = `
"import {Fragment as _Fragment, jsx as _jsx} from "react/jsx-runtime";
import {useMDXComponents as _provideComponents} from "@mdx-js/react";
import text from './file.txt' with { type: 'text' };
Expand Down
27 changes: 11 additions & 16 deletions packages/docusaurus-mdx-loader/src/__tests__/processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,31 +152,26 @@ describe('MDX processor', () => {

{text}`;

const ecmaVersion2025: Options['remarkMdxOptions'] = {
acornOptions: {ecmaVersion: 2025, sourceType: 'module'},
};

it('does not parse import attributes by default', async () => {
it('parses import attributes by default (ES2025)', async () => {
const options = createOptions({markdownConfig: {format: 'mdx'}});
await expect(processContent(importAttributes, options)).rejects.toThrow(
/Could not parse import\/exports with acorn/,
);
const result = await processContent(importAttributes, options);
expect(result.content).toMatchSnapshot();
});

it('parses import attributes with ecmaVersion 2025', async () => {
it('can downgrade to ES2024', async () => {
const options = createOptions({
markdownConfig: {format: 'mdx'},
remarkMdxOptions: ecmaVersion2025,
remarkMdxOptions: {
acornOptions: {ecmaVersion: 2024, sourceType: 'module'},
},
});
const result = await processContent(importAttributes, options);
expect(result.content).toMatchSnapshot();
await expect(processContent(importAttributes, options)).rejects.toThrow(
/Could not parse import\/exports with acorn/,
);
});

it('does not apply to the md format', async () => {
const options = createOptions({
markdownConfig: {format: 'md'},
remarkMdxOptions: ecmaVersion2025,
});
const options = createOptions({markdownConfig: {format: 'md'}});
const result = await processContent(importAttributes, options);
expect(result.content).toMatchSnapshot();
});
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-mdx-loader/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export type Options = Partial<MDXOptions> & {
}) => {[key: string]: unknown};
resolveMarkdownLink?: ResolveMarkdownLink;
// Options passed to remark-mdx (only for the 'mdx' format)
// Note: acornOptions replaces the default {ecmaVersion: 2024, sourceType: 'module'}
// Replaces our default {acornOptions: {ecmaVersion: 2025, sourceType: 'module'}}
remarkMdxOptions?: RemarkMdxOptions;

// Will usually be created by "createMDXLoaderItem"
Expand Down
11 changes: 8 additions & 3 deletions packages/docusaurus-mdx-loader/src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import type {PluginOptions as TransformLinksOptions} from './remark/transformLin
import type {PluginOptions as TransformImageOptions} from './remark/transformImage';
import type {PluginOptions as UnusedDirectivesOptions} from './remark/unusedDirectives';
import type {ProcessorOptions} from '@mdx-js/mdx';
import type {Options as RemarkMdxOptions} from 'remark-mdx';
import type {Pluggable} from 'unified';

export type SimpleProcessorResult = {
Expand All @@ -60,6 +61,11 @@ export type SimpleProcessor = {

export type MDXPlugin = Pluggable;

// MDX defaults to ES2024, we opt-in for ES2025 syntax (import attributes...)
const DefaultRemarkMdxOptions: RemarkMdxOptions = {
acornOptions: {ecmaVersion: 2025, sourceType: 'module'},
};

export type MDXOptions = {
admonitions: boolean | Partial<AdmonitionOptions>;
remarkPlugins: MDXPlugin[];
Expand Down Expand Up @@ -99,18 +105,17 @@ function getDefaultRemarkPlugins({options}: {options: Options}): MDXPlugin[] {
// unified dedupes plugins by reference: using remark-mdx again reconfigures
// the existing attacher in place, at its original pipeline position
// This only applies to the 'mdx' format: we must not add remark-mdx for 'md'
// Without options, we keep the exact createProcessor() pipeline untouched
function getRemarkMdxPlugins({
options,
format,
}: {
options: Options;
format: 'md' | 'mdx';
}): MDXPlugin[] {
if (format === 'md' || !options.remarkMdxOptions) {
if (format === 'md') {
return [];
}
return [[remarkMdx, options.remarkMdxOptions]];
return [[remarkMdx, options.remarkMdxOptions ?? DefaultRemarkMdxOptions]];
}

// /!\ this method is synchronous on purpose
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Import attributes tests

MDX files are parsed with ES2025 syntax, allowing the usage of [import attributes](https://github.com/tc39/proposal-import-attributes).

```mdx-code-block
import data from './data.json' with {type: 'json'};

<pre>{JSON.stringify(data)}</pre>
```
1 change: 1 addition & 0 deletions website/_dogfooding/_pages tests/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@ import Readme from "../README.mdx"
- [Style Isolation tests](/tests/pages/style-isolation)
- [IdealImage tests](/tests/pages/ideal-image)
- [Live Playground tests](/tests/pages/live-playground-tests)
- [Import attributes tests](/tests/pages/import-attributes-tests)
- [Linking tests](./linking/index.md)
Loading