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
2 changes: 1 addition & 1 deletion packages/docusaurus-mdx-loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"remark-emoji": "^5.0.2",
"remark-frontmatter": "^5.0.0",
"remark-gfm": "^4.0.1",
"remark-mdx": "^3.1.1",
"stringify-object": "^7.0.0",
"tslib": "^2.8.1",
"unified": "^11.0.5",
Expand All @@ -53,7 +54,6 @@
"mdast-util-directive": "3.1.0",
"rehype-stringify": "^10.0.1",
"remark": "^15.0.1",
"remark-mdx": "^3.1.1",
"remark-rehype": "^11.1.2",
"to-vfile": "^8.0.0",
"unist-builder": "^4.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,64 @@ export default function MDXContent(props = {}) {
"
`;

exports[`MDX processor > remarkMdxOptions > does not apply to the md format 1`] = `
"import {Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs} from "react/jsx-runtime";
import {useMDXComponents as _provideComponents} from "@mdx-js/react";
export const toc = [];
function _createMdxContent(props) {
const _components = {
p: "p",
..._provideComponents(),
...props.components
};
return _jsxs(_Fragment, {
children: [_jsx(_components.p, {
children: "import text from './file.txt' with {type: 'text'};"
}), "/n", _jsx(_components.p, {
children: "{text}"
})]
});
}
export default function MDXContent(props = {}) {
const {wrapper: MDXLayout} = {
..._provideComponents(),
...props.components
};
return MDXLayout ? _jsx(MDXLayout, {
...props,
children: _jsx(_createMdxContent, {
...props
})
}) : _createMdxContent(props);
}
"
`;

exports[`MDX processor > remarkMdxOptions > parses import attributes with ecmaVersion 2025 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' };
export const toc = [];
function _createMdxContent(props) {
return _jsx(_Fragment, {
children: text
});
}
export default function MDXContent(props = {}) {
const {wrapper: MDXLayout} = {
..._provideComponents(),
...props.components
};
return MDXLayout ? _jsx(MDXLayout, {
...props,
children: _jsx(_createMdxContent, {
...props
})
}) : _createMdxContent(props);
}
"
`;

exports[`MDX processor > supports emoji=false 1`] = `
"import {jsx as _jsx} from "react/jsx-runtime";
import {useMDXComponents as _provideComponents} from "@mdx-js/react";
Expand Down
36 changes: 36 additions & 0 deletions packages/docusaurus-mdx-loader/src/__tests__/processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,40 @@ describe('MDX processor', () => {
);
expect(result.content).toMatchSnapshot();
});

describe('remarkMdxOptions', () => {
// See https://github.com/mdx-js/mdx/issues/2628
const importAttributes = `import text from './file.txt' with {type: 'text'};

{text}`;

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

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

it('parses import attributes with ecmaVersion 2025', async () => {
const options = createOptions({
markdownConfig: {format: 'mdx'},
remarkMdxOptions: ecmaVersion2025,
});
const result = await processContent(importAttributes, options);
expect(result.content).toMatchSnapshot();
});

it('does not apply to the md format', async () => {
const options = createOptions({
markdownConfig: {format: 'md'},
remarkMdxOptions: ecmaVersion2025,
});
const result = await processContent(importAttributes, options);
expect(result.content).toMatchSnapshot();
});
});
});
4 changes: 4 additions & 0 deletions packages/docusaurus-mdx-loader/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import type {MDXOptions, SimpleProcessors} from './processor';
import type {MarkdownConfig} from '@docusaurus/types';
import type {ResolveMarkdownLink} from './remark/resolveMarkdownLinks';
import type {Options as RemarkMdxOptions} from 'remark-mdx';

export type Options = Partial<MDXOptions> & {
dependencies?: string[];
Expand All @@ -24,6 +25,9 @@ export type Options = Partial<MDXOptions> & {
frontMatter: {[key: string]: unknown};
}) => {[key: string]: unknown};
resolveMarkdownLink?: ResolveMarkdownLink;
// Options passed to remark-mdx (only for the 'mdx' format)
// Note: acornOptions replaces the default {ecmaVersion: 2024, sourceType: 'module'}
remarkMdxOptions?: RemarkMdxOptions;

// Will usually be created by "createMDXLoaderItem"
processors?: SimpleProcessors;
Expand Down
21 changes: 21 additions & 0 deletions packages/docusaurus-mdx-loader/src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import gfm from 'remark-gfm';
// TODO using fork until PR merged: https://github.com/leebyron/remark-comment/pull/3
import remarkComment from '@slorber/remark-comment';
import directive from 'remark-directive';
import remarkMdx from 'remark-mdx';
import {VFile} from 'vfile';
import emoji from 'remark-emoji';
import headings from './remark/headings';
Expand Down Expand Up @@ -93,6 +94,25 @@ function getDefaultRemarkPlugins({options}: {options: Options}): MDXPlugin[] {
];
}

// MDX's createProcessor() registers remark-mdx without options
// See https://github.com/mdx-js/mdx/issues/2628
// 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) {
return [];
}
return [[remarkMdx, options.remarkMdxOptions]];
}

// /!\ this method is synchronous on purpose
// Using async code here can create cache entry race conditions!
export function createProcessorUncached({
Expand All @@ -103,6 +123,7 @@ export function createProcessorUncached({
format: 'md' | 'mdx';
}): SimpleProcessor {
const remarkPlugins: MDXPlugin[] = [
...getRemarkMdxPlugins({options, format}),
...(options.beforeDefaultRemarkPlugins ?? []),
frontmatter,
directive,
Expand Down
6 changes: 3 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading