Registry / web-framework / remark-mdx-react-docgen-typescript

remark-mdx-react-docgen-typescript

JSON →
library1.0.1jsnpmunverified

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.

npm install remark-mdx-react-docgen-typescript
INSTALL
IMPORT
SIG · REMARK-MDX-REACT-D
R
remark-mdx-react-docgen-typescript
web-frameworkjavascriptv1.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

remarkReactDocgen
import remarkReactDocgen from 'remark-mdx-react-docgen-typescript';
import { remarkReactDocgen } from 'remark-mdx-react-docgen-typescript';
This package exports a default plugin function. Named imports are incorrect.
remarkReactDocgen (CommonJS)
const remarkReactDocgen = require('remark-mdx-react-docgen-typescript').default;
const remarkReactDocgen = require('remark-mdx-react-docgen-typescript');
When using CommonJS `require` with an ESM default export, `.default` is necessary to access the plugin function.
Options Type
import type { Options } from 'remark-mdx-react-docgen-typescript';
Import the `Options` type for type-safe configuration of the plugin.

This quickstart demonstrates how to set up `remark-mdx-react-docgen-typescript` with `@mdx-js/mdx` to process an MDX file containing a `::component-docs` directive. It shows the necessary imports for `remark-directive` and the docgen plugin, then compiles the MDX, illustrating how component documentation is extracted and inserted as JSX props into a `<ComponentDocs>` component.

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();
Debug
Known issues
breakingIn `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.
fix
Review the official `v1.0.0` release notes and usage examples to identify any renamed imports, options, or configurations and update your `unified`/`remark` plugin chain accordingly.
affects: >=1.0.0
gotchaThe 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.
fix
Ensure `remarkDirective` is listed in your `remarkPlugins` array *before* `remarkReactDocgen` when configuring `@mdx-js/mdx` or `unified`.
affects: >=0.1.0
gotchaWhen 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.
fix
Set the `rootDir` option in the plugin configuration to your project's root path (e.g., `process.cwd()`) and use `<rootDir>/path/to/component.tsx` in your MDX directives for project-relative paths.
affects: >=0.1.0
gotchaThe `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.
fix
Provide a valid `tsConfigPath` within the `reactDocGenOptions` object, pointing to the `tsconfig.json` file relevant to your React components, or ensure your `tsconfig.json` is correctly configured for `react-docgen-typescript` to find it automatically.
affects: >=0.1.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'type')
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.
fix
Add `remarkDirective` to your `remarkPlugins` array before `remarkReactDocgen`: `remarkPlugins: [remarkDirective, remarkReactDocgen]`.
Error: Could not find any component in file:
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>/`.
fix
Verify 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:
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.
fix
Check 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.
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies
remark-directiverequiredRequired for processing the custom `::component-docs` directive syntax in MDX.
react-docgen-typescriptrequiredCore dependency for extracting component documentation from TypeScript React files.
Agent activity
4 hits · last 30 days
node
4
Resources