-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.eleventy.js
239 lines (202 loc) · 6.9 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
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
import md from "markdown-it";
import mdFN from "markdown-it-footnote";
import * as sass from "sass";
import path from "node:path";
import { promises as fs } from "node:fs";
import { eleventyImageTransformPlugin } from "@11ty/eleventy-img";
import { InputPathToUrlTransformPlugin } from "@11ty/eleventy";
import syntaxHighlight from "@11ty/eleventy-plugin-syntaxhighlight";
import { feedPlugin } from "@11ty/eleventy-plugin-rss";
import directoryOutputPlugin from "@11ty/eleventy-plugin-directory-output";
import { IdAttributePlugin } from "@11ty/eleventy";
import purgeCssPlugin from "eleventy-plugin-purgecss";
import helpers from "./src/_data/helpers.js";
import siteConfig from "./src/_data/site.js";
import fonts from "./src/_data/fonts.js";
export default async function (eleventyConfig) {
/* 11ty Plugins */
/****************/
// Image transforms
eleventyConfig.addPlugin(InputPathToUrlTransformPlugin);
eleventyConfig.addPlugin(eleventyImageTransformPlugin, {
extensions: "html",
formats: ["webp", "auto"],
widths: ["auto"],
urlPath: "/assets/images/",
// optional, attributes assigned on <img> override these values.
defaultAttributes: {
loading: "lazy",
decoding: "async",
},
});
// Syntax highlighting
eleventyConfig.addPlugin(syntaxHighlight);
// Add anchors to headings
eleventyConfig.addPlugin(IdAttributePlugin, {
// Use standard slugify filter
slugify: helpers.toSlug,
});
eleventyConfig.addPlugin(purgeCssPlugin, {
config: {
content: ["./_site/**/*.html"],
css: ["./_site/assets/css/*.css"],
rejected: false
},
quiet: true
});
// RSS / Atom feed
eleventyConfig.addPlugin(feedPlugin, {
type: "atom", // or "rss", "json"
outputPath: `/${siteConfig.rss.collection}.atom`,
collection: {
name: siteConfig.rss.collection, // iterate over `collections.posts`
limit: 0, // 0 means no limit
},
metadata: {
language: "en",
title: `${siteConfig.siteName} | ${siteConfig.rss.title}`,
subtitle: siteConfig.rss.subtitle,
base: `https://${siteConfig.domain}`,
author: {
name: siteConfig.authorName,
email: siteConfig.authorEmail, // Optional
},
},
});
// Directory output on build
eleventyConfig.setQuietMode(true);
eleventyConfig.addPlugin(directoryOutputPlugin, {
warningFileSize: 250 * 1000,
});
/* Passthrough assets */
/**********************/
eleventyConfig.addPassthroughCopy({
"src/assets/css": "assets/css",
"src/404.html": "404.html",
});
/* Collections config */
/**********************/
// Collections are defined in src/_data/site.js in the "nav" object
let postCollections = new Set();
siteConfig.nav.forEach((item) => {
if (item.collection) {
postCollections.add(item.url.replace(/\//g, ""));
}
});
postCollections.forEach((collectionName) => {
eleventyConfig.addCollection(`${collectionName}`, function (collectionApi) {
return collectionApi.getFilteredByGlob(`src/${collectionName}/*.md`);
});
});
/* Markdown Configuration */
/**************************/
let markdownLib = md({
typographer: true,
})
// Footnotes
.use(mdFN);
// Footnote HTML customization
markdownLib.renderer.rules.footnote_block_open = () =>
'<section class="[ footnotes ] [ flow ]">\n' +
"<h2>Notes</h2>\n" +
'<ol role="list">\n';
// Override footnote indicator render to output a number without square brackets
markdownLib.renderer.rules.footnote_caption = (tokens, idx) => {
let n = Number(tokens[idx].meta.id + 1).toString();
if (tokens[idx].meta.subId > 0) n += `:${tokens[idx].meta.subId}`;
return `${n}`;
};
// Add role="list" attribute for accessibility and correct styling
markdownLib.renderer.rules.bullet_list_open = (
tokens,
idx,
options,
env,
self,
) => {
tokens[idx].attrPush(["role", "list"]);
return self.renderToken(tokens, idx, options);
};
// Add role="list" attribute for accessibility and correct styling
markdownLib.renderer.rules.ordered_list_open = (
tokens,
idx,
options,
env,
self,
) => {
tokens[idx].attrPush(["role", "list"]);
return self.renderToken(tokens, idx, options);
};
// Set the Markdown library to use
eleventyConfig.setLibrary("md", markdownLib);
/* Sass support as a template format */
/*************************************/
eleventyConfig.addTemplateFormats("scss");
// Creates the extension for use
eleventyConfig.addExtension("scss", {
outputFileExtension: "css", // optional, default: "html"
// Customizing the permalink for the output file
compileOptions: {
permalink: function (inputContent, inputPath) {
let parsed = path.parse(inputPath);
const outputDir = "/assets/css";
const outputFilePath = path.join(outputDir, `${parsed.name}.css`);
// Return the new permalink
return outputFilePath;
},
},
// `compile` is called once per .scss file in the input directory
compile: async function (inputContent, inputPath) {
let parsed = path.parse(inputPath);
// Adhere to convention of not outputting Sass underscore files
if (parsed.name.startsWith("_")) {
return;
}
let result = sass.compileString(inputContent, {
loadPaths: [parsed.dir || "."],
});
// This is the render function, `data` is the full data cascade
return async (data) => {
return result.css;
};
},
});
/* Custom filters */
/******************/
// Custom filter to determine if current page is within parent link path
// Called like this: {{ pagePath | getLinkActiveState: parentPath }}
eleventyConfig.addFilter("getLinkActiveState", helpers.getLinkActiveState);
// Generate lorem ipsum for use in content
// Called like this: {{ count | loremIpsum: type }}
// Where type is one of: words, sentences, paragraphs
eleventyConfig.addFilter("loremIpsum", helpers.loremIpsum);
// Process input as Markdown, useful for Markdown included in frontmatter
eleventyConfig.addFilter("markdownify", (markdownString) =>
markdownLib.renderInline(markdownString)
);
/* Build event handlers */
/************************/
// Write configured Google Fonts to build output
eleventyConfig.on("eleventy.after", async () => {
const fontBuffers = await fonts.files();
for (const { fontBuffer, fileName } of fontBuffers) {
const outputPath = path.join("_site", fonts.buildFontPath, fileName);
const outputDir = path.dirname(outputPath);
await fs.mkdir(outputDir, { recursive: true });
await fs.writeFile(outputPath, fontBuffer);
}
});
return {
// Set directories to watch
dir: {
input: "src",
includes: "_includes",
data: "_data",
output: "_site",
},
// Define other options like pathPrefix
templateFormats: ["liquid", "md"],
htmlTemplateEngine: "liquid",
};
}