Remark Plugin to Import TypeScript Code
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.
Common errors
-
Error: Cannot find module 'remark-typescript-code-import'
cause The package `remark-typescript-code-import` has not been installed or the import path is incorrect.fixRun `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. -
TypeError: Unknown directive `typescript`
cause The `remark-directive` plugin is not active in the Remark processing chain, or it is placed after `remark-typescript-code-import`.fixEnsure `remark-directive` is installed (`npm install -D remark-directive`) and included in your Remark usage *before* `remarkTypescriptCodeImport`: `.use(remarkDirective).use(remarkTypescriptCodeImport)`. -
Error: Cannot resolve file path: <rootDir>/your-file.tsx (or similar file not found error)
cause The `rootDir` option is not correctly configured for paths starting with `<rootDir>/`, or the file simply does not exist at the resolved path.fixVerify 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, '..')`.
Warnings
- breaking Version 1.0.0 introduced breaking changes related to 'naming convention fixes'. Code written for `v0.x` might require updates to directive attributes or names to function correctly.
- gotcha The `remark-typescript-code-import` plugin relies on `remark-directive` to parse its custom directives. If `remark-directive` is not installed and explicitly added to your Remark processing chain, the custom directives will not be recognized, and the plugin will not function.
- gotcha File paths for imports are relative to the Markdown file by default. When using `<rootDir>/` prefixes, the `rootDir` option must be correctly configured, or it defaults to `process.cwd()` which might not be the expected project root.
Install
-
npm install remark-typescript-code-import -
yarn add remark-typescript-code-import -
pnpm add remark-typescript-code-import
Imports
- remarkTypescriptCodeImport
const remarkTypescriptCodeImport = require('remark-typescript-code-import');import remarkTypescriptCodeImport from 'remark-typescript-code-import';
- remark
import remark from 'remark';
import { remark } from 'remark'; - remarkDirective
import remarkDirective from 'remark-directive';
Quickstart
import { remark } from 'remark';
import path from 'node:path';
import { readSync } from 'to-vfile';
import remarkDirective from 'remark-directive';
import remarkTypescriptCodeImport from 'remark-typescript-code-import';
import { writeFileSync, mkdirSync } from 'node:fs';
// Create dummy files for the example
mkdirSync('docs', { recursive: true });
writeFileSync('docs/example.md', '::typescript{file="./Component.tsx#ComponentProps"}\n::typescript{file="<rootDir>/src/types.ts#User"}');
writeFileSync('docs/Component.tsx', 'type ComponentProps = {\n propA: string;\n}\n\nfunction Component(props: ComponentProps) {\n // ...\n}');
mkdirSync('src', { recursive: true });
writeFileSync('src/types.ts', 'export type User = {\n id: string;\n name: string;\n};');
async function processMarkdown() {
const result = await remark()
.use(remarkDirective)
.use(remarkTypescriptCodeImport, { rootDir: process.cwd() })
.process(readSync(path.join(process.cwd(), 'docs', 'example.md')));
console.log('--- Processed Markdown ---\n');
console.log(String(result));
console.log('\n--- End Processed Markdown ---');
}
processMarkdown();