remark-typescript-code-import is a Remark plugin designed to extract and embed specific code snippets from TypeScript files directly into Markdown or MDX documents. It allows developers to import types, interfaces, functions, or other code blocks by referencing a file path and a specific symbol using a directive syntax (e.g., `::typescript{file="./Component.tsx#ComponentProps"}`). The plugin operates within the Unified ecosystem, requiring `remark-directive` to parse custom directives. Currently at version 1.0.1, it provides TypeScript types and supports Node.js >=16. Its primary differentiator is the targeted import of TypeScript-specific constructs, making it ideal for documentation where code examples need to be synchronized with actual source files without manual copying. Releases appear to be ad-hoc based on changes and fixes rather than a strict schedule.
npm install remark-typescript-code-importVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use the plugin with `remark` and `remark-directive` to import TypeScript code snippets into a Markdown file. It sets up example `.md` and `.tsx` files, then processes the Markdown to show the imported code blocks. It also illustrates the `rootDir` option.
Review your existing Markdown directives (e.g., `::typescript{...}`) and plugin options to align with the new naming conventions introduced in v1.0.0. Consult the changelog for specific details if migrating from `v0.x`.Ensure you install `remark-directive` (`npm install -D remark-directive`) and include it in your Remark processing chain *before* `remark-typescript-code-import`, e.g., `.use(remarkDirective).use(remarkTypescriptCodeImport)`.
If using `<rootDir>/` in your directives, explicitly set the `rootDir` option when configuring the plugin: `.use(remarkTypescriptCodeImport, { rootDir: path.resolve(__dirname, '../../') })` to ensure the correct base path is used for resolution.Run `npm install -D remark-typescript-code-import` (or `yarn add -D remark-typescript-code-import`) and verify the import statement `import remarkTypescriptCodeImport from 'remark-typescript-code-import';` is correct.
Ensure `remark-directive` is installed (`npm install -D remark-directive`) and included in your Remark usage *before* `remarkTypescriptCodeImport`: `.use(remarkDirective).use(remarkTypescriptCodeImport)`.
Verify the file path is correct. If using `<rootDir>/`, ensure the `rootDir` plugin option is set to the absolute path of your project root: `.use(remarkTypescriptCodeImport, { rootDir: process.cwd() })` or a specific absolute path like `path.join(__dirname, '..')`.