codeImport()
將外部檔案的程式碼匯入圍籬式程式碼區塊中。
import { codeImport } from "@unifast/node";函式簽名
function codeImport(options?: CodeImportPluginOptions): UnifastPlugin參數
options?
程式碼匯入行為的設定
| 屬性 | 型別 | 預設值 | 說明 |
|---|---|---|---|
rootDir | string | — | 解析檔案路徑時所使用的根目錄。若未指定,則會以目前工作目錄為基準解析路徑。 |
用法
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範例
基本的檔案匯入
程式碼區塊的內容會被替換為該檔案的內容,同時保留語言資訊以便進行語法高亮。
搭配 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未指定 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