codeImport()
Import mã từ các file bên ngoài vào các khối mã fenced.
import { codeImport } from "@unifast/node";Chữ ký
function codeImport(options?: CodeImportPluginOptions): UnifastPluginTham số
options?
Cấu hình cho hành vi import mã
| Thuộc tính | Kiểu | Mặc định | Mô tả |
|---|---|---|---|
rootDir | string | — | Thư mục gốc để phân giải đường dẫn file. Khi bỏ qua, đường dẫn được phân giải tương đối so với thư mục làm việc hiện tại. |
Cách dùng
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.tsVí dụ
Import file cơ bản
Nội dung của khối mã được thay bằng nội dung của file, giữ nguyên ngôn ngữ để hỗ trợ syntax highlighting.
Với 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 rootDirKhông có 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