Skip to navigation Skip to main content
The Trevor Project — Saving Young LGBTQ Lives
Eleventy
Eleventy Documentation
Stable
2.0.1
Canary
3.0.0-alpha.10
Toggle Menu
Eleventy 1.93s
Astro 22.90s

Programmatic API Added in v1.0.0

Contents

在 Eleventy 1.0 中,您可以在自己的 Node 脚本中运行 Eleventy。 (在幕后,Eleventy Serverless 插件就是这样工作的)

示例 Jump to heading

写入文件系统 Jump to heading

请不要忘记 首先在本地项目中安装 Eleventy

现在创建一个名为 my-node-script.js 的文件,其内容如下:

Filename my-node-script.js
const Eleventy = require("@11ty/eleventy");

(async function () {
let elev = new Eleventy();
await elev.write();
})();

然后从命令行运行您的新脚本。 当您运行此命令时,请不要包含 ~ $

node my-node-script.js

不要写入文件系统 Jump to heading

使用 .write() 会将您的输出写入文件系统。如果您希望在不写入的情况下以编程方式检索内容,请使用 .toJSON().toNDJSON().

JSON 输出 Jump to heading

const Eleventy = require("@11ty/eleventy");

(async function () {
let elev = new Eleventy();
let json = await elev.toJSON();
// 所有结果
console.log(json);
})();

ndjson 输出 Jump to heading

const Eleventy = require("@11ty/eleventy");

(async function () {
let elev = new Eleventy();
let stream = await elev.toNDJSON();
stream.on("data", (entry) => {
// 一次流一个输出结果
let json = JSON.parse(entry.toString());
console.log(json);
});
})();

更改输入和输出目录 Jump to heading

第一个参数是输入目录。第二个参数是输出目录。

const Eleventy = require("@11ty/eleventy");

(async function () {
let elev = new Eleventy(".", "_site");

// 使用 `write` 或 `toJSON` 或 `toNDJSON`
})();

完整选项列表 Jump to heading

Eleventy 的第三个参数是一个选项对象。

(文档部分正在进行中,但 欢迎深入了解 Eleventy 类源代码,了解更多信息)

const Eleventy = require("@11ty/eleventy");

(async function () {
let elev = new Eleventy(".", "_site", {
// --quiet
quietMode: true,

// --config
configPath: ".eleventy.js",

config: function (eleventyConfig) {
// 有一些自定义配置 API 操作
// 与 eleventyConfig.addGlobalData 配合良好
},
});

// 使用 `write` 或 `toJSON` 或 `toNDJSON`
})();

Other pages in Getting Started: