-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.eleventy.js
202 lines (179 loc) · 5.25 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
const path = require("path");
const htmlmin = require("html-minifier");
const MarkdownIt = require("markdown-it"),
mk = require("markdown-it-katex"),
{ removeMdPlugin } = require("markdown-it-remove");
const Image = require("@11ty/eleventy-img");
const syntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight");
const excerptFilter = require("./plugins/excerpt");
const pluginSEO = require("eleventy-plugin-seo");
const { readFile } = require("fs/promises");
const MARKDOWN_OPTIONS = {
html: true,
linkify: true,
typographer: true,
};
const md = new MarkdownIt(MARKDOWN_OPTIONS).use(mk);
const mdStrip = new MarkdownIt(MARKDOWN_OPTIONS).use(removeMdPlugin, {}); // should this have mk as well?
module.exports = function (eleventyConfig) {
/**
* Upgrade helper
* Uncomment if you need help upgrading to new major version.
*/
//eleventyConfig.addPlugin(UpgradeHelper);
/**
* Files to copy
* https://www.11ty.dev/docs/copy/
*/
// eleventyConfig.addPassthroughCopy("src/img");
// Evil.
/**
* TODO: please please please optimize images :)
*/
eleventyConfig.addPassthroughCopy(
"src/**/*.(png|jpg|jpeg|gif|svg|webp|avif)"
);
eleventyConfig.addPassthroughCopy({ static: "/" });
eleventyConfig.setLibrary("md", md);
/**
* Plugin stuff
*/
eleventyConfig.addPlugin(syntaxHighlight);
const siteData = require("./_data/site.json");
siteData.author = siteData.author.name
eleventyConfig.addPlugin(pluginSEO, siteData);
/**
* Frontmatter stuff
*/
// eleventyConfig.setFrontMatterParsingOptions({
// excerpt: true,
// excerpt_separator: "<!-- excerpt -->",
// });
/**
* HTML Minifier for production builds
*/
eleventyConfig.addTransform("htmlmin", function (content, outputPath) {
if (
process.env.ELEVENTY_ENV == "production" &&
outputPath &&
outputPath.endsWith(".html")
) {
let minified = htmlmin.minify(content, {
useShortDoctype: true,
removeComments: true,
collapseWhitespace: true,
});
return minified;
}
return content;
});
/**
* Filters
*/
eleventyConfig.addFilter("readableDate", (dateObj) => {
return dateObj.toLocaleDateString("en-US", {
year: "numeric",
month: "long",
day: "numeric",
});
});
eleventyConfig.addFilter("toHTML", (str) => {
try {
return md.renderInline(str);
} catch (e) {
return str;
}
});
// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-date-string
eleventyConfig.addFilter("htmlDateString", (dateObj) => {
return dateObj.toJSON().slice(0, 10);
});
eleventyConfig.addFilter(
"excerpt",
excerptFilter(md, ["<!-- excerpt -->", "<p>---</p>", "</p>"])
);
/**
* Shortcodes
*/
eleventyConfig.addShortcode("mentionPageId", function (pageId){
try {
const { url, data: { title } } = this.ctx.collections.all.find(page => {
return normalizePageId(page.data?.pageId) === normalizePageId(pageId)
});
if (!url || !title) return `[link]`;
return `[${title}](${url})`;
} catch (e) {
return `https://notion.so/${pageId}`
}
})
eleventyConfig.addPairedShortcode("figure", function (content, caption = "") {
return `<figure>${md.renderInline(
content.trim()
)}${caption ? `<figcaption>${md.renderInline(caption)}</figcaption>` : ""}</figure>`;
});
eleventyConfig.addPairedShortcode("aside", function(content) {
return `<aside>${md.renderInline(content.trim())}</aside>`;
});
eleventyConfig.addNunjucksAsyncShortcode("image", imageShortcode);
return {
dir: {
input: "src",
data: "../_data",
layouts: "_includes/layouts",
},
markdownTemplateEngine: "njk",
};
};
async function imageShortcode(
src,
alt = "",
widths = [24, 400, 800, 1280],
formats = ["avif", "webp", "jpeg"],
sizes = "100vw"
) {
const isRelative = !/^(\/|https?:\/\/)/.test(src);
const absoluteSrc = isRelative
? path.join(this.page.inputPath, "..", src)
: src;
const stats = await new Image(absoluteSrc, {
widths: [...widths, null],
formats: [...formats, null],
svgShortCircuit: true,
urlPath: "/assets/img",
outputDir: "./_site/assets/img",
sharpJpegOptions: {
progressive: true
},
sharpPngOptions: {
progressive: true
}
});
return generateImageHTMl(stats, {
alt: mdStrip.renderInline(alt),
sizes,
loading: "lazy",
decoding: "async",
});
}
/**
* Return SVG as object :() if it's interactive and svg
* Wrapper around the EleventyImage.generateHTML function
* @param {Image.Metadata} metadata
*/
async function generateImageHTMl(metadata, options) {
if (metadata.svg) {
const { outputPath, url, width, height } = metadata.svg[0];
if (outputPath) {
const { alt } = options || {};
const contents = await readFile(outputPath, { encoding: 'utf-8' }); // at least I think it is :(
if (/<script/.test(contents)) { // i.e. it's interactive so let's do the dangerous thing!
return `<object data="${url}" width="${width}" height="${height}">${alt}</object>`
}
}
}
return Image.generateHTML(metadata, options)
}
function normalizePageId(pageId = '') {
// if (pageId) console.log(pageId)
return pageId.replace(/\-/g, '')
}