-
Notifications
You must be signed in to change notification settings - Fork 3
/
.eleventy.js
209 lines (177 loc) · 5.84 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
const { DateTime } = require("luxon");
const markdownIt = require("markdown-it");
const pluginRss = require("@11ty/eleventy-plugin-rss");
const markdownItAnchor = require("markdown-it-anchor");
const readingTime = require("eleventy-plugin-reading-time");
const markdownItFootnote = require("markdown-it-footnote");
const markdownItAttrs = require("markdown-it-attrs");
const markdownItMath = require("markdown-it-mathjax3");
const markdownItVideo = require("markdown-it-video");
const path = require("path");
const Image = require("@11ty/eleventy-img");
// Image processing functions
async function processImage(src, alt, sizes, widths) {
let metadata = await Image(src, {
widths,
formats: ["webp", "jpeg"],
outputDir: "./_site/images/",
urlPath: "/images/",
filenameFormat: (id, src, width, format) => {
const extension = path.extname(src);
const name = path.basename(src, extension);
return `${name}-${width}w.${format}`;
},
});
let imageAttributes = {
alt,
sizes,
loading: "lazy",
decoding: "async",
};
return Image.generateHTML(metadata, imageAttributes);
}
async function portraitimageShortcode(
src,
alt,
sizes = "(min-width: 650px) 1vw, 50vw"
) {
return processImage(src, alt, sizes, [500]);
}
async function landimageShortcode(
src,
alt,
sizes = "(min-width: 650px) 1vw, 50vw"
) {
return processImage(src, alt, sizes, [900, 1500]);
}
// Heading Anchor configuration
const position = {
false: "push",
true: "unshift",
};
const renderPermalink = (slug, opts, state, idx) => {
const space = () =>
Object.assign(new state.Token("text", "", 0), { content: " " });
const linkTokens = [
Object.assign(new state.Token("link_open", "a", 1), {
attrs: [
["class", opts.permalinkClass],
["href", opts.permalinkHref(slug, state)],
],
}),
Object.assign(new state.Token("html_block", "", 0), {
content: `<span aria-hidden="true" class="header-anchor__symbol">#</span>`,
}),
new state.Token("link_close", "a", -1),
];
if (opts.permalinkSpace) {
linkTokens[position[!opts.permalinkBefore]](space());
}
state.tokens[idx + 1].children[position[opts.permalinkBefore]](...linkTokens);
};
module.exports = function (eleventyConfig) {
// Add shortcodes and plugins
eleventyConfig.addLiquidShortcode("porimage", portraitimageShortcode);
eleventyConfig.addLiquidShortcode("lanimage", landimageShortcode);
eleventyConfig.addPlugin(pluginRss);
eleventyConfig.addPlugin(readingTime);
eleventyConfig.ignores.delete("**/README.md/**");
// Configure front matter parsing
eleventyConfig.setFrontMatterParsingOptions({
excerpt: true,
excerpt_separator: "<-->",
});
// Configure BrowserSync
eleventyConfig.setBrowserSyncConfig({
files: "./_site/css/**/*.css",
});
// Passthrough file copy
eleventyConfig.addPassthroughCopy("src/media");
eleventyConfig.addPassthroughCopy("src/CNAME");
// Add date formatting filter
eleventyConfig.addFilter("formatDate", (dateObj) => {
return DateTime.fromJSDate(dateObj).toLocaleString(DateTime.DATE_MED);
});
// Configure Markdown
const markdownOptions = {
html: true,
breaks: true,
linkify: true,
};
const markdownItAnchorOptions = {
permalink: true,
renderPermalink,
};
const markdownLib = new markdownIt(markdownOptions)
.use(markdownItAnchor, markdownItAnchorOptions)
.use(markdownItFootnote)
.use(require("markdown-it-imsize"), { autofill: true })
.use(markdownItAttrs)
.use(markdownItMath, { tex: { tags: "equ" } })
.use(markdownItVideo, {
youtube: { width: 640, height: 390 },
vimeo: { width: 500, height: 281 },
vine: { width: 600, height: 600, embed: "simple" },
prezi: { width: 550, height: 400 },
});
// Customize Markdown rendering
markdownLib.renderer.rules.table_open = () =>
'<div class="table-wrapper">\n<table>\n';
markdownLib.renderer.rules.table_close = () => "</table>\n</div>";
markdownLib.renderer.rules.footnote_block_open = () =>
"<hr>" +
'<h4 class="mt-3">Footnotes</h4>\n' +
'<section class="footnotes">\n' +
'<ol class="footnotes-list">\n';
markdownLib.renderer.rules.footnote_ref = function (
tokens,
idx,
options,
env,
slf
) {
const id = slf.rules.footnote_anchor_name(tokens, idx, options, env, slf);
const caption = slf.rules.footnote_caption(tokens, idx, options, env, slf);
let refid = id;
if (tokens[idx].meta.subId > 0) refid += `:${tokens[idx].meta.subId}`;
return `<sup class="footnote-ref"><a class="footnote-link" href="#fn${id}" id="fnref${refid}">${caption}</a></sup>`;
};
eleventyConfig.setLibrary("md", markdownLib);
// Create collections
eleventyConfig.addCollection("posts", function (collection) {
const coll = collection.getFilteredByTag("post");
for (let i = 0; i < coll.length; i++) {
const prevPost = coll[i - 1];
const nextPost = coll[i + 1];
coll[i].data["prevPost"] = prevPost;
coll[i].data["nextPost"] = nextPost;
}
return coll;
});
eleventyConfig.addCollection("mathposts", function (collection) {
const colle = collection.getFilteredByTag("math");
for (let i = 0; i < colle.length; i++) {
const prevPostmath = colle[i - 1];
const nextPostmath = colle[i + 1];
colle[i].data["prevPostmath"] = prevPostmath;
colle[i].data["nextPostmath"] = nextPostmath;
}
return colle;
});
// Add filter for post tags
eleventyConfig.addFilter("postTags", (tags) =>
Object.keys(tags)
.filter((k) => !["posts", "post", "all", "mathposts"].includes(k))
.map((k) => ({ name: k, count: tags[k].length }))
.sort((a, b) => b.count - a.count)
);
// Configure input/output directories
return {
dir: {
input: "src",
includes: "_includes",
layouts: "_layouts",
data: "_data",
},
};
};