-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.eleventy.js
More file actions
92 lines (77 loc) · 2.57 KB
/
.eleventy.js
File metadata and controls
92 lines (77 loc) · 2.57 KB
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
// Code syntax highlighting plugin.
// This plugin automatically formats all code during build.
const syntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight");
// Compresses image plugin
const Image = require("@11ty/eleventy-img");
// Render Latex equations
const markdownIt = require("markdown-it");
// Node JS path functions
const path = require('path');
module.exports = async function (eleventyConfig) {
// All folders called "assets" will be blindly copied
// into the assets output folder.
eleventyConfig.addPassthroughCopy({ "src/**/assets/*": "assets/" });
// Displays date in a human-readable way.
// Usage: {{ <your date> | postDate }}
eleventyConfig.addFilter("postDate", (dateObj) => {
return dateObj.toLocaleDateString("en-GB", { year: 'numeric', month: 'long', day: 'numeric' });
});
// Displays date in a machine-readable way.
// Usage: {{ <your date> | machineDate }}
eleventyConfig.addFilter("machineDate", (dateObj) => {
return dateObj.toISOString();
});
const { katex } = (await import("@mdit/plugin-katex"));
eleventyConfig.setLibrary(
"md",
markdownIt({
html: true,
breaks: false,
linkify: true,
}).use(katex, { output: "mathml" })
);
// Code syntax highlighting
eleventyConfig.addPlugin(syntaxHighlight);
// Image shortcode
eleventyConfig.addShortcode("image", async function (src, alt, width = 90) {
if (!alt) {
throw new Error(`Missing alt text for image: ${src}`);
}
// Directory containing the page
const inputDir = path.dirname(this.page.inputPath);
// Input path of the image
const inputPath = path.join(inputDir, "assets", src);
// URL to original image (relies on assets passthrough copy above)
const originalUrl = `/assets/${src}`;
let metadata = await Image(inputPath, {
widths: [400, 800, 1200],
formats: ["jpg", "svg"],
urlPath: "/img/",
outputDir: "public/img/",
svgShortCircuit: true, // don't rasterize SVG
filenameFormat: (id, src, width, format) => {
const imgName = path.parse(src).name;
return `${imgName}-${width}.${format}`
}
});
let imageAttributes = {
alt,
sizes: "(max-width: 50rem) 100vw, 50rem",
style: `width: ${width}%;`,
loading: "lazy",
decoding: "async",
};
return `
<a href="${originalUrl}" target="_blank" rel="noopener">
${Image.generateHTML(metadata, imageAttributes)}
</a>
`;
});
return {
// Change the input and output directory
dir: {
input: "src",
output: "public"
}
}
};