]> Witch of Git - web/blog/blob - .eleventy.js
Add aside shortcode
[web/blog] / .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")();
6
7 module.exports = (eleventyConfig) => {
8 eleventyConfig.addNunjucksFilter('date', nunjucksDate);
9 eleventyConfig.addPlugin(pluginRss);
10 eleventyConfig.addPlugin(pluginSyntaxHighlight);
11
12 eleventyConfig.addPassthroughCopy("img");
13 eleventyConfig.addPassthroughCopy("static");
14
15 eleventyConfig.addNunjucksShortcode("youtube", youtubeShortcode);
16 eleventyConfig.addPairedNunjucksShortcode("tweet", tweetShortcode);
17 eleventyConfig.addPairedShortcode("aside", asideShortcode);
18
19 eleventyConfig.addFilter("markdown", value => md.renderInline(value));
20 eleventyConfig.addFilter("groupby", groupbyFilter);
21
22 eleventyConfig.addCollection("years", collection => {
23 const posts = collection.getFilteredByTag("posts");
24 const items = groupby(posts, item => item.date.getFullYear());
25 return items.reduce((obj, [k, v]) => (obj[k] = v, obj), {});
26 });
27
28 return {
29 markdownTemplateEngine: "njk",
30 };
31 };
32
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)();
41 } else {
42 item = item[seg];
43 }
44 }
45 return item;
46 }
47
48 function groupby(items, keyFn) {
49 const results = [];
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]]);
54 } else {
55 results[results.length-1][1].push(item);
56 }
57 }
58 return results;
59 }
60
61 function groupbyFilter(items, path) {
62 return groupby(items, item => access(item, path));
63 }
64
65 function youtubeShortcode(items, inWidth = 560, inHeight = 315) {
66 const width = items.width || inWidth;
67 const height = items.height || inHeight;
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;
71 if (!video) {
72 throw "Required argument 'video'.";
73 }
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>
78 </div>`;
79 }
80
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> &mdash; ${items.name} (@${items.at})
86 <a href="${items.link}">${items.date}</a>
87 </blockquote>
88 <script async src="https://platform.twitter.com/widgets.js" charset="utf-8">
89 </script>
90 </div>`;
91 }
92
93 function asideShortcode(content, style='') {
94 const html = md.render(content);
95 return `<div class="aside ${style}">${html}</div>`;
96 }