-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.eleventy.js
51 lines (43 loc) · 1.61 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
const fs = require("fs");
module.exports = function (eleventyConfig) {
eleventyConfig.addShortcode("formatSnippetBody", function (content, indent) {
// escape " and create array of lines
const snippet = content.replace(/"/g, '\\"').trim().split("\n");
const spaceIndent = indent > 0 ? indent : 2;
const formattedSnippet = snippet.map((line, index) => {
// count the number of leading whitespace characters (tab or spaces)
const indentCount = line.search(/\S/) !== -1 ? line.search(/\S/) : 0;
let newLine = line;
if (line.charAt(0) === "\t") {
newLine = line.replace(/\t/g, "\\t");
} else {
// add a \t for each space tab according to indentCount
newLine = newLine
.trimStart()
.replace(/^/, "\\t".repeat(indentCount / spaceIndent));
}
// don't add a comma after the last line
return index === snippet.length - 1 ? `"${newLine}"` : `"${newLine}",`;
});
return formattedSnippet.join("\n");
});
eleventyConfig.addFilter("isArray", (value) => Array.isArray(value));
// get all subdirs under src/snippets
const snippetDirs = fs
.readdirSync("./src/snippets", { withFileTypes: true })
.filter((item) => item.isDirectory())
.map((item) => item.name);
// create a collection for each subdir under src/snippets
for (const dir of snippetDirs) {
eleventyConfig.addCollection(`${dir}`, function (collection) {
return collection.getFilteredByGlob(`src/snippets/${dir}/**/*.njk`);
});
}
return {
dir: {
input: "src",
output: "dist",
includes: "_includes",
},
};
};