codeImport()
Importiert Code aus externen Dateien in eingezäunte Code-Blöcke.
import { codeImport } from "@unifast/node";Signatur
function codeImport(options?: CodeImportPluginOptions): UnifastPluginParameter
options?
Konfiguration für das Verhalten des Code-Imports
| Eigenschaft | Typ | Standard | Beschreibung |
|---|---|---|---|
rootDir | string | — | Wurzelverzeichnis für die Auflösung von Dateipfaden. Wird es weggelassen, werden Pfade relativ zum aktuellen Arbeitsverzeichnis aufgelöst. |
Verwendung
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.tsBeispiele
Einfacher Datei-Import
Der Inhalt des Code-Blocks wird durch den Dateiinhalt ersetzt, wobei die Sprache für die Syntax-Hervorhebung erhalten bleibt.
Mit 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 rootDirOhne 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