MDX React Docgen Typescript Remark Plugin
remark-mdx-react-docgen-typescript is a Remark plugin designed to integrate `react-docgen-typescript` functionality directly into MDX documents. It allows developers to automatically extract and display TypeScript React component documentation within their MDX content using a directive syntax (e.g., `::component-docs{file="./Component.tsx"}`). The plugin processes MDX files, identifies these directives, and replaces them with JSX `<ComponentDocs>` elements containing `propsData` derived from the specified React component files. The current stable version is 1.0.1. Its release cadence appears to be ad-hoc, with recent updates in late 2023. Key differentiators include its tight integration with the `unified` and `remark` ecosystems, specifically for MDX, and its ability to leverage `react-docgen-typescript` for type-aware documentation generation, making it suitable for monorepos or design systems that use TypeScript and MDX for documentation.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'type')
cause The `::component-docs` directive was not properly parsed, often because `remark-directive` was not included or not placed before `remark-mdx-react-docgen-typescript` in the plugin chain.fixAdd `remarkDirective` to your `remarkPlugins` array before `remarkReactDocgen`: `remarkPlugins: [remarkDirective, remarkReactDocgen]`. -
Error: Could not find any component in file:
cause The `file` path specified in the `::component-docs` directive is incorrect, or `react-docgen-typescript` failed to identify a React component within the specified file. This can also happen if the `rootDir` option is not correctly set for paths using `<rootDir>/`.fixVerify the `file` path in your MDX directive is correct relative to the MDX file or correctly configured with `<rootDir>/` and the `rootDir` plugin option. Ensure the target file exports a valid React component. -
Error: Could not parse file with react-docgen-typescript:
cause Likely an issue with `react-docgen-typescript`'s ability to process the TypeScript component file. This often points to problems with the `tsconfig.json` configuration or the TypeScript code itself.fixCheck your `reactDocGenOptions.tsConfigPath` to ensure it points to a valid `tsconfig.json`. Verify the `tsconfig.json` includes the component files and has appropriate compiler options (e.g., `jsx`). Ensure the TypeScript component file is valid and compilable.
Warnings
- breaking In `v1.0.0`, a breaking change was introduced due to a 'naming correction'. While specific details are not provided in the changelog excerpt, it likely involved changes to API surface, options names, or how the plugin is imported/configured, requiring users to update their setup.
- gotcha The plugin relies on `remark-directive` to parse the `::component-docs` syntax. If `remark-directive` is not included in the `remarkPlugins` array *before* `remark-mdx-react-docgen-typescript`, the custom directives will not be recognized, and the component documentation will not be extracted.
- gotcha When using the `file` attribute in the `::component-docs` directive, paths are relative to the MDX file by default. If you need to specify paths relative to your project root, you must prefix them with `<rootDir>/` and configure the `rootDir` option in the plugin.
- gotcha The `reactDocGenOptions` must be correctly configured, particularly `tsConfigPath`, if `react-docgen-typescript` struggles to parse your TypeScript components. Incorrect `tsconfig.json` paths or configurations can lead to incomplete or failed documentation extraction.
Install
-
npm install remark-mdx-react-docgen-typescript -
yarn add remark-mdx-react-docgen-typescript -
pnpm add remark-mdx-react-docgen-typescript
Imports
- remarkReactDocgen
import { remarkReactDocgen } from 'remark-mdx-react-docgen-typescript';import remarkReactDocgen from 'remark-mdx-react-docgen-typescript';
- remarkReactDocgen (CommonJS)
const remarkReactDocgen = require('remark-mdx-react-docgen-typescript');const remarkReactDocgen = require('remark-mdx-react-docgen-typescript').default; - Options Type
import type { Options } from 'remark-mdx-react-docgen-typescript';
Quickstart
import { readFile } from 'node:fs/promises';
import { compile } from '@mdx-js/mdx';
import remarkDirective from 'remark-directive';
import remarkReactDocgen from 'remark-mdx-react-docgen-typescript';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// Create a dummy Component.tsx for demonstration purposes
const componentCode = `
/**
* A simple example component.
* @param greeting The greeting message.
*/
export function MyComponent({ greeting }: { greeting: string }) {
return <div>{greeting}, World!</div>;
};
`;
// In a real scenario, this would be a file on disk.
// For quickstart, we'll simulate the MDX and component content.
const mdxContent = `
# My Component Documentation
This is an example of documenting MyComponent:
::component-docs{file="./MyComponent.tsx" greeting="Hello from MDX"}
`;
// Simulate writing the component file (optional, for direct execution)
// In a real project, MyComponent.tsx would exist alongside your MDX.
// For this quickstart, we'll compile the MDX assuming MyComponent.tsx exists
// or is mocked during compilation.
// To run this, you'd typically have MyComponent.tsx in the same directory
// as your MDX file. For a self-contained example:
// await fs.promises.writeFile(path.join(__dirname, 'MyComponent.tsx'), componentCode);
async function compileMdxWithDocgen() {
try {
// Assuming 'MyComponent.tsx' is available where your MDX points.
// For this example, we'll configure rootDir to point to __dirname
// and ensure react-docgen-typescript can find the component.
const { contents } = await compile(mdxContent, {
jsx: true,
remarkPlugins: [
remarkDirective,
[remarkReactDocgen, { rootDir: __dirname, reactDocGenOptions: { tsConfigPath: path.join(__dirname, 'tsconfig.json') } }]
],
});
console.log(contents);
} catch (error) {
console.error('MDX Compilation Error:', error);
}
}
// A tsconfig.json is often required for react-docgen-typescript
// Example content for tsconfig.json:
// {"compilerOptions": {"jsx": "react", "module": "esnext", "target": "esnext", "esModuleInterop": true, "skipLibCheck": true}}
// Ensure it exists in __dirname for this example to work with a real file.
compileMdxWithDocgen();