codeImport()
Import code from external files into fenced code blocks.
import { codeImport } from "@unifast/node";Signature
function codeImport(options?: CodeImportPluginOptions): UnifastPluginParameters
options?
Configuration for code import behavior
| Property | Type | Default | Description |
|---|---|---|---|
rootDir | string | — | Root directory for resolving file paths. When omitted, paths are resolved relative to the current working directory. |
Usage
import { compile, codeImport } from "@unifast/node";
const md = `
\`\`\`ts file="./src/example.ts"
\`\`\`
`;
const result = compile(md, {
plugins: [
codeImport({
rootDir: "/path/to/project",
}),
],
});
console.log(result.output);
// The code block is replaced with the contents of ./src/example.tsExamples
Basic file import
The code block content is replaced with the file contents, preserving the language for syntax highlighting.
With rootDir
import { compile, codeImport } from "@unifast/node";
const result = compile(md, {
plugins: [
codeImport({ rootDir: "/home/user/project" }),
],
});
console.log(result.output);
// File paths are resolved relative to rootDirWithout rootDir
import { compile, codeImport } from "@unifast/node";
const result = compile(md, {
plugins: [codeImport()],
});
console.log(result.output);
// File paths are resolved relative to the current working directory