Bun API 参考
Bun API 参考
本文档列出了站点生成器使用的核心 Bun API。
Bun.file()
读取文件内容:
const file = Bun.file("content/guide/hello.md");
const text = await file.text();
const exists = await file.exists();
Bun.write()
写入文件:
await Bun.write("dist/index.html", htmlContent);
Bun.markdown.html()
将 Markdown 转换为 HTML:
const html = Bun.markdown.html("# Hello World");
// 输出: <h1>Hello World</h1>
Bun.Glob
文件匹配:
import { Glob } from "bun";
const glob = new Glob("content/**/*.md");
for await (const file of glob.scan()) {
console.log(file);
}
Shell 命令
// 创建目录
await Bun.$`mkdir -p dist/assets`;
// 删除目录
await Bun.$`rm -rf dist`;