generated from 5t3ph/eleventy-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
.eleventy.js
106 lines (93 loc) · 2.88 KB
/
.eleventy.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
const path = require("path");
module.exports = function (eleventyConfig, options) {
// Define defaults for your plugin config
const defaults = {
metaKey: "meta",
metaExtension: "meta",
};
// Combine defaults with user defined options
const { metaKey, metaExtension } = {
...defaults,
...options,
};
eleventyConfig.addDataExtension(`${metaExtension}`, {
parser: (contents, filePath) => {
const metaName = path.parse(filePath).name;
return JSON.parse(`{ "${metaName}": ${contents} }`);
},
});
eleventyConfig.addFilter("metaSchema", function (data) {
if (data.tags) {
const { inputPath } = data.page;
const { tags, [metaKey]: meta } = data;
// data[tags[0]] -> data["pages"] -> pages.meta
const schema = tags[0] && data[tags[0]] ? data[tags[0]] : false;
if (schema) {
const required = Object.keys(schema).filter((i) => schema[i].required);
const metaKeys = meta && Object.keys(meta);
// Check for all required
const requiredKeys = [];
for (req of required) {
if (!metaKeys || !metaKeys.includes(req)) {
requiredKeys.push(req);
}
}
if (requiredKeys.length) {
console.error(
"\x1b[41m %s - Missing required %s keys: \033[1m%s \033[0m \x1b[0m",
inputPath,
metaKey,
requiredKeys.join(", ")
);
}
// Validate types
const invalidKeys = [];
for (key in meta) {
if (schema[key]) {
if (
(schema[key].type !== "array" &&
typeof meta[key] !== schema[key].type) ||
(schema[key].type === "array" && !Array.isArray(meta[key]))
) {
// Check type matches
console.error(
"\x1b[33m %s - Incorrect type for %s \033[1m`%s`\033[0m\x1b[33m, change to %s \x1b[0m",
inputPath,
metaKey,
key,
schema[key].type
);
}
} else if (!schema[key]) {
invalidKeys.push(key);
}
}
// Warn for invalid keys
if (invalidKeys.length) {
console.error(
"\x1b[36m %s - Invalid %s keys: \033[1m%s\033[0m \x1b[0m",
inputPath,
metaKey,
invalidKeys.join(", ")
);
}
// Check for misplaced keys (outside meta)
const misplacedKeys = [];
for (key in schema) {
if (data[key]) {
misplacedKeys.push(key);
}
}
// Warn for misplaced keys
if (misplacedKeys.length) {
console.error(
"\x1b[95m %s - Possibly misplaced %s keys: \033[1m%s\033[0m \x1b[0m",
inputPath,
metaKey,
misplacedKeys.join(", ")
);
}
}
}
});
};