|
1 | 1 | import { readFileSync } from "fs"; |
2 | 2 | import slugify from "../utils/slugify.js"; |
| 3 | +import { titleCase } from "../utils/titleCase.js"; |
3 | 4 | const flowerFile = readFileSync("src/_data/anim/starynight.txt", "utf8"); |
4 | 5 |
|
5 | 6 | // These two functions implement the email obfuscation technique |
6 | 7 | const railsEncode = (msg, rails) => |
7 | | - fence(msg.length, rails) |
8 | | - .map((i) => msg[i]) |
9 | | - .join(""); |
| 8 | + fence(msg.length, rails) |
| 9 | + .map((i) => msg[i]) |
| 10 | + .join(""); |
10 | 11 |
|
11 | 12 | function fence(length, rails) { |
12 | | - const cycle_len = 2 * rails - 2; |
13 | | - return Array.from({ length: rails }).flatMap((_, r) => |
14 | | - Array.from({ length }, (_, i) => i).filter( |
15 | | - (i) => i % cycle_len === r || i % cycle_len === cycle_len - r, |
16 | | - ), |
17 | | - ); |
| 13 | + const cycle_len = 2 * rails - 2; |
| 14 | + return Array.from({ length: rails }).flatMap((_, r) => |
| 15 | + Array.from({ length }, (_, i) => i).filter( |
| 16 | + (i) => i % cycle_len === r || i % cycle_len === cycle_len - r, |
| 17 | + ), |
| 18 | + ); |
18 | 19 | } |
19 | 20 |
|
20 | 21 | /** @param {import("@11ty/eleventy").UserConfig} eleventyConfig */ |
21 | 22 | export default function filters(eleventyConfig) { |
22 | | - eleventyConfig.addFilter("titleCase", (str) => { |
23 | | - str = str |
24 | | - .split(" ") |
25 | | - .map((word) => { |
26 | | - if (/[A-Z]/.test(word)) { |
27 | | - return word; |
28 | | - } |
29 | | - return word.charAt(0).toUpperCase() + word.slice(1); |
30 | | - }) |
31 | | - .join(" "); |
32 | | - // Certain minor words should be left lowercase unless |
33 | | - // they are the first or last words in the string |
34 | | - let lowers = [ |
35 | | - "A", |
36 | | - "An", |
37 | | - "The", |
38 | | - "And", |
39 | | - "But", |
40 | | - "Or", |
41 | | - "For", |
42 | | - "Nor", |
43 | | - "As", |
44 | | - "At", |
45 | | - "By", |
46 | | - "For", |
47 | | - "From", |
48 | | - "In", |
49 | | - "Into", |
50 | | - "Near", |
51 | | - "Of", |
52 | | - "On", |
53 | | - "Onto", |
54 | | - "To", |
55 | | - "With", |
56 | | - ]; |
57 | | - for (const lower of lowers) |
58 | | - str = str.replace(new RegExp("\\s" + lower + "\\s", "g"), (txt) => |
59 | | - txt.toLowerCase(), |
60 | | - ); |
| 23 | + eleventyConfig.addFilter("titleCase", titleCase); |
61 | 24 |
|
62 | | - return str; |
63 | | - }); |
| 25 | + // I frankly don't recall why I don't use the built-in slug filter |
| 26 | + // but I'm sure I had a good reason |
| 27 | + eleventyConfig.addFilter("slugshive", (path) => slugify(path)); |
64 | 28 |
|
65 | | - // I frankly don't recall why I don't use the built-in slug filter |
66 | | - // but I'm sure I had a good reason |
67 | | - eleventyConfig.addFilter("slugshive", (path) => slugify(path)); |
| 29 | + eleventyConfig.addFilter( |
| 30 | + "rails", |
| 31 | + (str, n) => |
| 32 | + `<a class="rails" href="mailto:${railsEncode( |
| 33 | + str, |
| 34 | + n, |
| 35 | + )}" n="${n}">${railsEncode(str, n)}</a>`, |
| 36 | + ); |
68 | 37 |
|
69 | | - eleventyConfig.addFilter( |
70 | | - "rails", |
71 | | - (str, n) => |
72 | | - `<a class="rails" href="mailto:${railsEncode( |
73 | | - str, |
74 | | - n, |
75 | | - )}" n="${n}">${railsEncode(str, n)}</a>`, |
76 | | - ); |
| 38 | + // sorry |
| 39 | + eleventyConfig.addFilter("footerBase", () => { |
| 40 | + return ( |
| 41 | + "\n".repeat(flowerFile.split("?")[0].split("\n").length - 1) + |
| 42 | + // @ts-ignore |
| 43 | + flowerFile |
| 44 | + .match(/([^\n]*)\n\?/)[1] |
| 45 | + .replace(/[0-9]/g, (match) => |
| 46 | + " ".repeat(Number(match) + 2).substring(1), |
| 47 | + ) |
| 48 | + ); |
| 49 | + }); |
77 | 50 |
|
78 | | - // sorry |
79 | | - eleventyConfig.addFilter("footerBase", () => { |
80 | | - return ( |
81 | | - "\n".repeat(flowerFile.split("?")[0].split("\n").length - 1) + |
82 | | - // @ts-ignore |
83 | | - flowerFile |
84 | | - .match(/([^\n]*)\n\?/)[1] |
85 | | - .replace(/[0-9]/g, (match) => |
86 | | - " ".repeat(Number(match) + 2).substring(1), |
87 | | - ) |
88 | | - ); |
89 | | - }); |
| 51 | + eleventyConfig.addFilter("random", function (array) { |
| 52 | + return array[Math.floor(Math.random() * array.length)]; |
| 53 | + }); |
90 | 54 |
|
91 | | - eleventyConfig.addFilter("random", function (array) { |
92 | | - return array[Math.floor(Math.random() * array.length)]; |
93 | | - }); |
| 55 | + eleventyConfig.addFilter("rainbow", function (i, n) { |
| 56 | + return `hsl(${(360 / n) * i},50%,60%)`; |
| 57 | + }); |
94 | 58 |
|
95 | | - eleventyConfig.addFilter("rainbow", function (i, n) { |
96 | | - return `hsl(${(360 / n) * i},50%,60%)`; |
97 | | - }); |
| 59 | + eleventyConfig.addFilter("dateToRfc3339", (date) => { |
| 60 | + if (!date) return; |
| 61 | + let s = date.toISOString().split("."); |
| 62 | + s.pop(); |
| 63 | + return s.join("") + "Z"; |
| 64 | + }); |
98 | 65 |
|
99 | | - eleventyConfig.addFilter("dateToRfc3339", (date) => { |
100 | | - if (!date) return; |
101 | | - let s = date.toISOString().split("."); |
102 | | - s.pop(); |
103 | | - return s.join("") + "Z"; |
104 | | - }); |
105 | | - |
106 | | - eleventyConfig.addFilter("getNewestCollectionItemDate", (collection) => { |
107 | | - return new Date( |
108 | | - Math.max( |
109 | | - ...collection.map((item) => new Date(item.date).getTime()), |
110 | | - ), |
111 | | - ); |
112 | | - }); |
| 66 | + eleventyConfig.addFilter("getNewestCollectionItemDate", (collection) => { |
| 67 | + return new Date( |
| 68 | + Math.max(...collection.map((item) => new Date(item.date).getTime())), |
| 69 | + ); |
| 70 | + }); |
113 | 71 | } |
0 commit comments