]>
Witch of Git - web/blog/blob - eleventy.config.js
1 import pluginRss
from "@11ty/eleventy-plugin-rss";
2 import pluginSyntaxHighlight
from "@11ty/eleventy-plugin-syntaxhighlight";
3 import uslug
from "uslug";
4 import anchor
from "markdown-it-anchor";
5 import markdownIt
from "markdown-it";
6 import CleanCSS
from "clean-css";
7 import htmlMinifier
from "html-minifier-terser";
8 import util
from "util";
9 import { DateTime
} from "luxon";
11 const md
= markdownIt({
15 slugify
: s
=> uslug(s
),
17 permalinkBefore
: true,
18 permalinkClass
: "header-anchor c-sun dec-none",
21 export default async
function (eleventyConfig
) {
22 eleventyConfig
.addDateParsing(parseDate
);
23 // @TODO: Update to the new virtual template setup?
24 eleventyConfig
.addPlugin(pluginRss
);
25 eleventyConfig
.addPlugin(pluginSyntaxHighlight
);
27 eleventyConfig
.setLibrary("md", md
);
29 eleventyConfig
.addTransform("html-minifier", htmlMinifierTransform
);
31 eleventyConfig
.addPassthroughCopy("img");
32 eleventyConfig
.addPassthroughCopy("static");
33 eleventyConfig
.addPassthroughCopy("stamps");
34 eleventyConfig
.addPassthroughCopy({ "favicon": "/" });
36 eleventyConfig
.addNunjucksShortcode("youtube", youtubeShortcode
);
37 eleventyConfig
.addPairedNunjucksShortcode("tweet", tweetShortcode
);
38 eleventyConfig
.addPairedShortcode("aside", asideShortcode
);
39 eleventyConfig
.addPairedShortcode("figure", figureShortcode
);
41 eleventyConfig
.addFilter("date", dateFilter
);
42 eleventyConfig
.addFilter("markdown", value
=> md
.renderInline(value
));
43 eleventyConfig
.addFilter("groupby", groupbyFilter
);
44 eleventyConfig
.addFilter("cssmin", css
=>
45 new CleanCSS({}).minify(css
).styles
);
46 eleventyConfig
.addFilter("debug", util
.inspect
);
47 eleventyConfig
.addFilter("toRfc3339", toRfc3339Filter
);
48 eleventyConfig
.addFilter("hasTime", hasTimeFilter
);
50 eleventyConfig
.setDataDeepMerge(true);
52 eleventyConfig
.addCollection("years", collection
=> {
53 const posts
= collection
.getFilteredByTag("posts");
54 const items
= groupby(posts
, item
=> item
.date
.getFullYear());
55 return items
.reduce((obj
, [k
, v
]) => (obj
[k
] = v
, obj
), {});
59 markdownTemplateEngine
: "njk",
64 * @param {{string | Date | DateTime}} anyDate
67 function dateTime(anyDate
) {
68 if (anyDate
instanceof Date
) {
69 return DateTime
.fromJSDate(anyDate
);
71 if (DateTime
.isDateTime(anyDate
)) {
74 let date
= parseDate(anyDate
);
75 if (date
== null) { throw Error(`failed to parse date ${anyDate}`); }
79 function dateFilter(date
, format
) {
80 return dateTime(date
).toFormat(format
);
83 function toRfc3339Filter(date
) {
84 return dateTime(date
).toISO({ suppressMilliseconds
: true });
87 function hasTimeFilter(date
) {
88 date
= dateTime(date
);
92 || date
.millisecond
!= 0;
96 * @param {string} dateText
97 * @returns {{DateTime | null}}
99 function parseDate(dateText
) {
101 if (!dateText
) dateText
= "";
103 "yyyy-MM-dd HH:mm:ss z",
107 for (const format
of formats
) {
108 const date
= DateTime
.fromFormat(dateText
, format
, { setZone
: true });
109 if (date
.isValid
) { return date
; }
116 function access(item
, path
) {
117 const segments
= path
.split(".");
118 for (const seg
of segments
) {
119 if (item
=== undefined) { return null; }
120 if (seg
.endsWith("()")) {
121 const method
= item
[seg
.slice(0, -2)];
122 if (method
=== undefined) { return null; }
123 item
= method
.bind(item
)();
131 function groupby(items
, keyFn
) {
133 for (const item
of items
) {
134 const key
= keyFn(item
);
135 if (results
.length
== 0 || key
!= results
[results
.length
-1][0]) {
136 results
.push([key
, [item
]]);
138 results
[results
.length
-1][1].push(item
);
144 function groupbyFilter(items
, path
) {
145 return groupby(items
, item
=> access(item
, path
));
148 function youtubeShortcode(items
, inWidth
= 560, inHeight
= 315) {
149 const width
= items
.width
|| inWidth
;
150 const height
= items
.height
|| inHeight
;
151 const allow
= items
.allow
|| "accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
152 const border
= items
.border
|| "0";
153 const video
= items
.video
|| items
;
155 throw "Required argument 'video'.";
157 const src
= "https://www.youtube.com/embed/" + video
;
158 return `<div class="youtube row hcenter">
159 <iframe width="${width}" height="${height}" src="${src}"
160 frameborder="${border}" allow="${allow}" allowfullscreen></iframe>
164 function tweetShortcode(content
, items
) {
165 // @TODO: Handle parsing date
166 return `<div class="row hcenter">
167 <blockquote class="twitter-tweet">
168 <p lang="en" dir="ltr">${content}</p> — ${items.name} (@${items.at})
169 <a href="${items.link}">${items.date}</a>
171 <script async src="https://platform.twitter.com/widgets.js" charset="utf-8">
176 function asideShortcode(content
, style
='') {
177 const html
= md
.render(content
);
179 return `<aside class="${style}">${html}</aside>`;
181 return `<aside>${html}</aside>`;
185 function figureShortcode(content
, { src
, alt
}) {
186 const captionHtml
= md
.render(content
);
187 return `<figure class="col hcenter">
188 <img src="${src}" alt="${alt}">
189 <figcaption>${captionHtml}</figcaption>
193 function htmlMinifierTransform(content
, outputPath
) {
194 if (outputPath
.endsWith(".html")) {
195 return htmlMinifier
.minify(content
, {
196 useShortDoctype
: true,
197 removeComments
: true,
198 collapseWhitespace
: true,