codeImport()
Mengimpor kode dari file eksternal ke dalam fenced code block.
import { codeImport } from "@unifast/node";Signature
function codeImport(options?: CodeImportPluginOptions): UnifastPluginParameter
options?
Konfigurasi untuk perilaku import kode
| Properti | Tipe | Default | Deskripsi |
|---|---|---|---|
rootDir | string | — | Direktori root untuk meresolusikan path file. Jika dihilangkan, path akan diresolusi relatif terhadap direktori kerja saat ini. |
Penggunaan
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.tsContoh
Import file dasar
Konten code block diganti dengan isi file, dengan tetap mempertahankan bahasa untuk syntax highlighting.
Dengan 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 rootDirTanpa 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