-
Notifications
You must be signed in to change notification settings - Fork 26
/
.eleventy.js
137 lines (112 loc) · 4.23 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
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
const Nunjucks = require("nunjucks");
const eleventyNavigationPlugin = require("@11ty/eleventy-navigation");
module.exports = function (eleventyConfig) {
eleventyConfig.addPlugin(eleventyNavigationPlugin);
let nunjucksEnvironment = new Nunjucks.Environment(
new Nunjucks.FileSystemLoader("_includes")
);
eleventyConfig.setLibrary("njk", nunjucksEnvironment);
const markdownIt = require("markdown-it");
// create a new markdown-it instance with the plugin
const markdownItAnchor = require("markdown-it-anchor");
const markdownLib = markdownIt({ html: true }).use(markdownItAnchor);
// replace the default markdown-it instance
eleventyConfig.setLibrary("md", markdownLib);
eleventyConfig.addPassthroughCopy("static");
eleventyConfig.setBrowserSyncConfig({
files: [
"**/*.njk",
"**/*.md",
"_site/**/*.css",
"_site/**/*.js",
// "!_site/**",
],
});
// Add Nunjucks filter
eleventyConfig.addNunjucksFilter("rewriteSlug", (slug) => {
slug = slug.replace("/README", "");
return slug.toLowerCase();
});
eleventyConfig.addNunjucksFilter("rewriteTitle", (slug) => {
slug = slug
.replaceAll("/", " ")
.replace("README", "")
.replaceAll("_", " ")
.trim();
return slug;
});
eleventyConfig.addNunjucksFilter("rewriteBasename", (slug) => {
slug = slug.replace("/README.md", "");
return slug.toLowerCase();
});
eleventyConfig.addNunjucksFilter("generateNav", (collections) => {
// Sort by alphabetical
collections.sort((a, b) => a.url.localeCompare(b.url));
// let navItems = {};
collections.forEach((collection) => {
const dirname = collection.template.parsed.dirname
.replace("./", "/")
.toLowerCase();
const basename = collection.template.parsed.basename.toLowerCase();
collection.url = collection.url.replace("README/", "");
collection.data.eleventyNavigation = {};
if (dirname.split("/").length > 2) {
collection.data.eleventyNavigation.url = collection.url;
if (basename === "readme.md") {
let lastIndex = dirname.lastIndexOf("/");
collection.data.eleventyNavigation.parent = dirname
.substring(0, lastIndex)
.replace("/", " ")
.trim();
collection.data.eleventyNavigation.key = dirname
.replace("/", " ")
.trim();
} else {
collection.data.eleventyNavigation.parent = dirname
.replace("/", " ")
.trim();
collection.data.eleventyNavigation.key =
dirname.replace("/", " ").trim() + " " + basename;
collection.data.eleventyNavigation.title = basename;
}
} else if (dirname.split("/").length > 1) {
collection.data.eleventyNavigation.url = collection.url;
if (basename === "readme.md") {
// collection.data.eleventyNavigation.parent = "Home";
collection.data.eleventyNavigation.key = dirname
.replace("/", " ")
.trim();
} else {
collection.data.eleventyNavigation.parent = dirname
.replace("/", " ")
.trim();
collection.data.eleventyNavigation.key =
dirname.replace("/", " ").trim() + " " + basename;
collection.data.eleventyNavigation.title = basename;
}
}
});
// console.log(navItems);
return collections;
});
// Re-write exported path
eleventyConfig.addCollection("pages", (collection) => {
return collection.getAllSorted().map((item) => {
item.outputPath = item.outputPath.replace(
"README/index.html",
"index.html"
);
item.outputPath = item.outputPath.toLowerCase();
});
});
return {
templateFormats: ["njk", "html", "md"],
htmlTemplateEngine: "nunjucks",
dir: {
input: "./",
includes: "_includes",
layouts: "_includes/layouts",
output: "_site",
},
};
};