-
-
Notifications
You must be signed in to change notification settings - Fork 472
/
UserConfig.js
274 lines (223 loc) · 7.16 KB
/
UserConfig.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
const EventEmitter = require("events");
const chalk = require("chalk");
const semver = require("semver");
const { DateTime } = require("luxon");
const debug = require("debug")("Eleventy:UserConfig");
const pkg = require("../package.json");
// API to expose configuration options in config file
class UserConfig {
constructor() {
this.reset();
}
reset() {
debug("Resetting EleventyConfig to initial values.");
this.events = new EventEmitter();
this.collections = {};
this.liquidOptions = {};
this.liquidTags = {};
this.liquidFilters = {};
this.nunjucksFilters = {};
this.nunjucksAsyncFilters = {};
this.handlebarsHelpers = {};
this.passthroughCopies = {};
this.pugOptions = {};
this.ejsOptions = {};
this.markdownHighlighter = null;
this.libraryOverrides = {};
this.layoutAliases = {};
// now named `transforms` in API
this.filters = {};
this.activeNamespace = "";
this.DateTime = DateTime;
}
versionCheck(expected) {
if (!semver.satisfies(pkg.version, expected)) {
throw new Error(
`This project requires the eleventy version to match '${expected}' but found ${
pkg.version
}. Use \`npm update @11ty/eleventy -g\` to upgrade the eleventy global or \`npm update @11ty/eleventy --save\` to upgrade your local project version.`
);
}
}
on(eventName, callback) {
return this.events.on(eventName, callback);
}
emit(eventName, ...args) {
return this.events.emit(eventName, ...args);
}
// This is a method for plugins, probably shouldn’t use this in projects.
// Projects should use `setLibrary` as documented here:
// https://github.com/11ty/eleventy/blob/master/docs/engines/markdown.md#use-your-own-options
addMarkdownHighlighter(highlightFn) {
this.markdownHighlighter = highlightFn;
}
// tagCallback: function(liquidEngine) { return { parse: …, render: … }} };
addLiquidTag(name, tagFn) {
name = this.getNamespacedName(name);
if (typeof tagFn !== "function") {
throw new Error(
`EleventyConfig.addLiquidTag expects a callback function to be passed in for ${name}: addLiquidTag(name, function(liquidEngine) { return { parse: …, render: … } })`
);
}
if (this.liquidTags[name]) {
debug(
chalk.yellow(
"Warning, overwriting a Liquid tag with `addLiquidTag(%o)`"
),
name
);
}
this.liquidTags[name] = tagFn;
}
addLiquidFilter(name, callback) {
name = this.getNamespacedName(name);
if (this.liquidFilters[name]) {
debug(
chalk.yellow(
"Warning, overwriting a Liquid filter with `addLiquidFilter(%o)`"
),
name
);
}
this.liquidFilters[name] = callback;
}
addNunjucksAsyncFilter(name, callback) {
name = this.getNamespacedName(name);
if (this.nunjucksAsyncFilters[name]) {
debug(
chalk.yellow(
"Warning, overwriting a Nunjucks filter with `addNunjucksAsyncFilter(%o)`"
),
name
);
}
this.nunjucksAsyncFilters[name] = callback;
}
// Support the nunjucks style syntax for asynchronous filter add
addNunjucksFilter(name, callback, isAsync) {
if (isAsync) {
this.addNunjucksAsyncFilter(name, callback);
} else {
name = this.getNamespacedName(name);
if (this.nunjucksFilters[name]) {
debug(
chalk.yellow(
"Warning, overwriting a Nunjucks filter with `addNunjucksFilter(%o)`"
),
name
);
}
this.nunjucksFilters[name] = callback;
}
}
addHandlebarsHelper(name, callback) {
name = this.getNamespacedName(name);
if (this.handlebarsHelpers[name]) {
debug(
chalk.yellow(
"Warning, overwriting a Handlebars helper with `addHandlebarsHelper(%o)`"
),
name
);
}
this.handlebarsHelpers[name] = callback;
}
addFilter(name, callback) {
debug("Adding universal filter %o", this.getNamespacedName(name));
this.addLiquidFilter(name, callback);
this.addNunjucksFilter(name, callback);
// these seem more akin to tags but they’re all handlebars has, so
this.addHandlebarsHelper(name, callback);
}
addTransform(name, callback) {
name = this.getNamespacedName(name);
this.filters[name] = callback;
}
addLayoutAlias(from, to) {
this.layoutAliases[from] = to;
}
getCollections() {
return this.collections;
}
addCollection(name, callback) {
name = this.getNamespacedName(name);
if (this.collections[name]) {
throw new Error(
`config.addCollection(${name}) already exists. Try a different name for your collection.`
);
}
this.collections[name] = callback;
}
addPlugin(pluginCallback) {
if (typeof pluginCallback !== "function") {
throw new Error(
"EleventyConfig.addPlugin expects the first argument to be a function."
);
}
pluginCallback(this);
}
getNamespacedName(name) {
return this.activeNamespace + name;
}
namespace(pluginNamespace, callback) {
this.activeNamespace = pluginNamespace || "";
callback();
this.activeNamespace = "";
}
/**
* Adds a path to a file or directory to the list of pass-through copies
* which are copied as-is to the output.
*
* @param {String} fileOrDir The path to the file or directory that should
* be copied.
* @returns {any} a reference to the `EleventyConfig` object.
* @memberof EleventyConfig
*/
addPassthroughCopy(fileOrDir) {
this.passthroughCopies[fileOrDir] = true;
return this;
}
setTemplateFormats(templateFormats) {
if (typeof templateFormats === "string") {
templateFormats = templateFormats.split(",").map(format => format.trim());
}
this.templateFormats = templateFormats;
}
setLibrary(engineName, libraryInstance) {
// Pug options are passed to `compile` and not in the library constructor so we don’t need to warn
if (engineName === "liquid" && this.mdOptions) {
debug(
"WARNING: using `eleventyConfig.setLibrary` will override any configuration set using `.setLiquidOptions` or with the `liquidOptions` key in the config object. You’ll need to pass these options to the library yourself."
);
}
this.libraryOverrides[engineName.toLowerCase()] = libraryInstance;
}
setPugOptions(options) {
this.pugOptions = options;
}
setLiquidOptions(options) {
this.liquidOptions = options;
}
setEjsOptions(options) {
this.ejsOptions = options;
}
getMergingConfigObject() {
return {
templateFormats: this.templateFormats,
filters: this.filters,
layoutAliases: this.layoutAliases,
passthroughCopies: this.passthroughCopies,
liquidOptions: this.liquidOptions,
liquidTags: this.liquidTags,
liquidFilters: this.liquidFilters,
nunjucksFilters: this.nunjucksFilters,
nunjucksAsyncFilters: this.nunjucksAsyncFilters,
handlebarsHelpers: this.handlebarsHelpers,
pugOptions: this.pugOptions,
ejsOptions: this.ejsOptions,
markdownHighlighter: this.markdownHighlighter,
libraryOverrides: this.libraryOverrides
};
}
}
module.exports = UserConfig;