Skip to content

Commit 2463c88

Browse files
committed
titleCase in og
1 parent 4405a9d commit 2463c88

3 files changed

Lines changed: 101 additions & 95 deletions

File tree

conf/filters.js

Lines changed: 51 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,71 @@
11
import { readFileSync } from "fs";
22
import slugify from "../utils/slugify.js";
3+
import { titleCase } from "../utils/titleCase.js";
34
const flowerFile = readFileSync("src/_data/anim/starynight.txt", "utf8");
45

56
// These two functions implement the email obfuscation technique
67
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("");
1011

1112
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+
);
1819
}
1920

2021
/** @param {import("@11ty/eleventy").UserConfig} eleventyConfig */
2122
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);
6124

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));
6428

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+
);
6837

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+
});
7750

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+
});
9054

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+
});
9458

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+
});
9865

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+
});
11371
}

src/og.11ty.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { svg } from "../utils/ogHelpers.js";
22
import { Resvg } from "@resvg/resvg-js";
3+
import { titleCase } from "../utils/titleCase.js";
34

45
export default class Og {
56
data() {
@@ -22,8 +23,8 @@ export default class Og {
2223
};
2324
}
2425
async render(page) {
25-
let svgd = await svg({
26-
title: page.entry.data.title,
26+
const svgd = await svg({
27+
title: titleCase(page.entry.data.title),
2728
desc: page.entry.data.description,
2829
date: page.entry.data.date?.toLocaleDateString(),
2930
color: page.entry.data.color,

utils/titleCase.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
*
3+
* @param {string} str
4+
* @returns {string}
5+
*/
6+
export function titleCase(str) {
7+
str = str
8+
.split(" ")
9+
.map((word) => {
10+
if (/[A-Z]/.test(word)) {
11+
return word;
12+
}
13+
return word.charAt(0).toUpperCase() + word.slice(1);
14+
})
15+
.join(" ");
16+
// Certain minor words should be left lowercase unless
17+
// they are the first or last words in the string
18+
let lowers = [
19+
"A",
20+
"An",
21+
"The",
22+
"And",
23+
"But",
24+
"Or",
25+
"For",
26+
"Nor",
27+
"As",
28+
"At",
29+
"By",
30+
"For",
31+
"From",
32+
"In",
33+
"Into",
34+
"Near",
35+
"Of",
36+
"On",
37+
"Onto",
38+
"To",
39+
"With",
40+
];
41+
for (const lower of lowers)
42+
str = str.replace(new RegExp("\\s" + lower + "\\s", "g"), (txt) =>
43+
txt.toLowerCase(),
44+
);
45+
46+
return str;
47+
}

0 commit comments

Comments
 (0)