-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.eleventy.js
76 lines (66 loc) · 2.45 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
const pluginRss = require('@11ty/eleventy-plugin-rss');
const Image = require("@11ty/eleventy-img");
const markdownIt = require('markdown-it');
const emoji = require('markdown-it-emoji').full;
const crypto = require('crypto');
const fs = require('fs');
const md = markdownIt({html: true})
.use(emoji);
module.exports = function(eleventyConfig) {
const outputDir = 'blog.codepoints.net';
eleventyConfig.addPlugin(pluginRss);
eleventyConfig.setLibrary('md', md);
eleventyConfig.addPassthroughCopy('img');
eleventyConfig.addPassthroughCopy('assets');
eleventyConfig.addFilter('json_encode', s => JSON.stringify(s));
eleventyConfig.addShortcode('image', async function(src, alt, sizes='', loading='', width=null) {
const metadata = await Image(src, {
widths: width? [width] : [240, 480, 960],
formats: ['webp', 'jpeg'],
outputDir: `${outputDir}/img`,
});
return Image.generateHTML(metadata, {
alt,
width,
sizes,
loading,
decoding: 'async',
});
});
eleventyConfig.addShortcode('cp', async function(int, width=16) {
let hex = (typeof int === 'number'? int.toString(16) : int).toUpperCase();
while (hex.length < 4) {
hex = '0' + hex;
}
return `<a class="ln cp" href="https://codepoints.net/U+${hex}" data-cp="U+${hex}">U+${hex}</a>`;
//const title = (await fetch(`https://codepoints.net/api/v1/codepoint/${hex}?property=na`).then(response => response.json()))?.na || '';
//return `<a class="ln cp" href="https://codepoints.net/U+${hex}" data-cp="U+${hex}">
// <svg width="${width}" height="${width}" class="cpfig__img">
// <use href="https://codepoints.net/image/${hex.replace(/..$/, '00')}.svg#U${hex}"/>
// </svg>
// <span class="title">${title}</span>
//</a>`;
});
eleventyConfig.addFilter("bust", async (url) => {
const [urlPart, paramPart] = url.split("?");
const params = new URLSearchParams(paramPart || "");
const relativeUrl = (urlPart.charAt(0) == "/") ? urlPart.substring(1): urlPart;
const hasher = new Promise((res, rej) => {
const hash = crypto.createHash('md5');
const rStream = fs.createReadStream(relativeUrl);
rStream.on('data', (data) => {
hash.update(data);
});
rStream.on('end', () => {
res(hash.digest('hex'));
});
});
params.set("v", await hasher);
return `${urlPart}?${params}`;
});
return {
dir: {
output: outputDir,
},
};
};