]>
Witch of Git - web/blog/blob - .eleventy.js
1 const pluginRss
= require("@11ty/eleventy-plugin-rss");
2 const pluginSyntaxHighlight
= require("@11ty/eleventy-plugin-syntaxhighlight");
3 const Nunjucks
= require("nunjucks");
4 const nunjucksDate
= require("nunjucks-date");
5 const md
= require("markdown-it")();
7 module
.exports
= (eleventyConfig
) => {
8 eleventyConfig
.addNunjucksFilter('date', nunjucksDate
);
9 eleventyConfig
.addPlugin(pluginRss
);
10 eleventyConfig
.addPlugin(pluginSyntaxHighlight
);
12 eleventyConfig
.addPassthroughCopy("img");
13 eleventyConfig
.addPassthroughCopy("static");
15 eleventyConfig
.addNunjucksShortcode("youtube", youtubeShortcode
);
16 eleventyConfig
.addPairedNunjucksShortcode("tweet", tweetShortcode
);
18 eleventyConfig
.addFilter("markdown", value
=> md
.renderInline(value
));
19 eleventyConfig
.addFilter("groupby", groupbyFilter
);
21 eleventyConfig
.addCollection("years", collection
=> {
22 const posts
= collection
.getFilteredByTag("posts");
23 const items
= groupby(posts
, item
=> item
.date
.getFullYear());
25 return items
.reduce((obj
, [k
, v
]) => (obj
[k
] = v
, obj
), {});
29 markdownTemplateEngine
: "njk",
33 function access(item
, path
) {
34 const segments
= path
.split(".");
35 for (const seg
of segments
) {
36 if (item
=== undefined) { return null; }
37 if (seg
.endsWith("()")) {
38 const method
= item
[seg
.slice(0, -2)];
39 if (method
=== undefined) { return null; }
40 item
= method
.bind(item
)();
48 function groupby(items
, keyFn
) {
50 for (const item
of items
) {
51 const key
= keyFn(item
);
52 if (results
.length
== 0 || key
!= results
[results
.length
-1][0]) {
53 results
.push([key
, [item
]]);
55 results
[results
.length
-1][1].push(item
);
61 function groupbyFilter(items
, path
) {
62 return groupby(items
, item
=> access(item
, path
));
65 function youtubeShortcode(items
, inWidth
, inHeight
) {
66 const width
= items
.width
|| inWidth
|| 560;
67 const height
= items
.height
|| inHeight
|| 315;
68 const allow
= items
.allow
|| "accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
69 const border
= items
.border
|| "0";
70 const video
= items
.video
|| items
;
72 throw "Required argument 'video'.";
74 const src
= "https://www.youtube.com/embed/" + video
;
75 return `<div class="row flex-center">
76 <iframe width="${width}" height="${height}" src="${src}"
77 frameborder="${border}" allow="${allow}" allowfullscreen></iframe>
81 function tweetShortcode(content
, items
) {
82 // @TODO: Handle parsing date
83 return `<div class="row flex-center">
84 <blockquote class="twitter-tweet">
85 <p lang="en" dir="ltr">${content}</p> — ${items.name} (@${items.at})
86 <a href="${items.link}">${items.date}</a>
88 <script async src="https://platform.twitter.com/widgets.js" charset="utf-8">