codeImport()

Mengimpor kode dari file eksternal ke dalam fenced code block.

import { codeImport } from "@unifast/node";

Signature

function codeImport(options?: CodeImportPluginOptions): UnifastPlugin

Parameter

options?

Konfigurasi untuk perilaku import kode

PropertiTipeDefaultDeskripsi
rootDirstringDirektori 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.ts

Contoh

Import file dasar

```ts file="./src/utils.ts"
```

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 rootDir

Tanpa 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