generated from 5t3ph/11ty-netlify-jumpstart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.eleventy.js
176 lines (156 loc) · 5.44 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
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
const socialImages = require("@11tyrocks/eleventy-plugin-social-images");
const emojiRegex = require("emoji-regex");
const slugify = require("slugify");
const syntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight");
const pluginRss = require("@11ty/eleventy-plugin-rss");
const markdownIt = require("markdown-it");
const markdownItAnchor = require("markdown-it-anchor");
const packageVersion = require("./package.json").version;
const Image = require("@11ty/eleventy-img");
module.exports = function (eleventyConfig) {
eleventyConfig.addPlugin(socialImages);
eleventyConfig.addPlugin(syntaxHighlight);
eleventyConfig.addPlugin(pluginRss);
eleventyConfig.addWatchTarget("./src/sass/");
eleventyConfig.addPassthroughCopy("./src/css");
eleventyConfig.addPassthroughCopy("./src/fonts");
eleventyConfig.addPassthroughCopy("./src/img");
eleventyConfig.addPassthroughCopy("./src/android-chrome-*.png");
eleventyConfig.addPassthroughCopy("./src/apple-touch-icon.png");
eleventyConfig.addPassthroughCopy("./src/favicon.*");
eleventyConfig.addPassthroughCopy("./src/favicon*.*");
eleventyConfig.addPassthroughCopy("./src/site.webmanifest");
eleventyConfig.addPassthroughCopy("./src/scripts");
eleventyConfig.addShortcode("year", () => `${new Date().getFullYear()}`);
eleventyConfig.addShortcode("packageVersion", () => `v${packageVersion}`);
eleventyConfig.addFilter("slug", (str) => {
if (!str) {
return;
}
const regex = emojiRegex();
// Remove Emoji first
let string = str.replace(regex, "");
return slugify(string, {
lower: true,
replacement: "-",
remove: /[*+~·,()'"`´%!?¿:@\/]/g,
});
});
eleventyConfig.addFilter("respectCRLF", (str) => {
if (!str) {
return;
}
const regex = /\r\n/ig
return str.replace(regex, "<br />");
})
eleventyConfig.addFilter("list", (ul) => {
const getValue = (listItem_object) => {
let curr = listItem_object;
while (curr.nodeType !== "text") {
curr = curr.content[0];
}
return curr.value;
}
const ulRender = (ul_object) => {
return "<ul>" + ul_object.reduce((prev, curr) => {
let listItems = "";
if (prev && typeof(prev) === "object"){
listItems += "<li>" + getValue(prev);
if (prev.content.length > 1) {
listItems += "<ul>";
if (prev.content[1].content.length === 1){
listItems += "<li>" + getValue(prev.content[1]) + "</li>";
} else {
listItems += prev.content[1].content.reduce((prev2, curr2) => {
let prevListItems = "";
if (prev2 && typeof(prev2) === "object"){
prevListItems += "<li>" + getValue(prev2) + "</li>";
}
if (prev2 && typeof(prev2) === "string") {
prevListItems += prev2
}
if (curr2){
prevListItems += "<li>" + getValue(curr2) + "</li>";
}
return prevListItems;
});
}
listItems += "</ul>";
}
listItems += "</li>"
}
if (prev && typeof(prev) === "string") {
listItems += prev
}
if (curr) {
listItems += "<li>" + getValue(curr);
if (curr.content.length > 1) {
listItems += "<ul>";
if (curr.content[1].content.length === 1){
listItems += "<li>" + getValue(curr.content[1]) + "</li>";
} else {
listItems += curr.content[1].content.reduce((prev3, curr3) => {
let currListItems = "";
if (prev3 && typeof(prev3) === "object"){
currListItems += "<li>" + getValue(prev3) + "</li>";
}
if (prev3 && typeof(prev3) === "string") {
currListItems += prev3
}
if (curr3){
currListItems += "<li>" + getValue(curr3) + "</li>";
}
return currListItems;
});
}
listItems += "</ul>";
}
listItems += "</li>"
}
return listItems;
}) + "</ul>";
}
return ulRender(ul);
})
eleventyConfig.addNunjucksAsyncShortcode("stringify", async function(json){
return JSON.stringify(json);
});
eleventyConfig.addNunjucksAsyncShortcode("Image", async function(src, alt, outputFormat = "jpeg") {
if(alt === undefined) {
// You bet we throw an error on missing alt (alt="" works okay)
throw new Error(`Missing \`alt\` on Image from: ${src}`);
}
let stats = await Image(src, {
widths: [300],
formats: [outputFormat],
urlPath: "/images/",
outputDir: "./public/images/"
});
let props = stats[outputFormat].pop();
return `<img src="${props.url}" width="${props.width}" height="${props.height}" alt="${alt}">`;
});
/* Markdown Overrides */
let markdownLibrary = markdownIt({
html: true,
}).use(markdownItAnchor, {
permalink: true,
permalinkClass: "tdbc-anchor",
permalinkSymbol: "#",
permalinkSpace: false,
level: [1, 2, 3],
slugify: (s) =>
s
.trim()
.toLowerCase()
.replace(/[\s+~\/]/g, "-")
.replace(/[().`,%·'"!?¿:@*]/g, ""),
});
eleventyConfig.setLibrary("md", markdownLibrary);
return {
passthroughFileCopy: true,
dir: {
input: "src",
output: "public",
},
};
};