the home site for me: also iteration 3 or 4 of my site

feat: add script to generate OG images and update head.html for dynamic image paths

Changed files
+45 -5
templates
tools
+3
package.json
···
"name": "zera",
"module": "index.ts",
"type": "module",
+
"scripts": {
+
"build:all": "zola build; bun run tools/genOG.ts"
+
},
"devDependencies": {
"@types/bun": "latest"
},
+5 -4
templates/head.html
···
%} {% elif section.description %} {% set description = section.description |
truncate(length=150) %} {% elif config.description %} {% set description =
config.description | truncate(length=150) %} {% endif %} {% if page.extra.image
-
%} {% set image = get_url(path=page.extra.image, trailing_slash=false) %} {%
-
elif section.extra.image %} {% set image = get_url(path=section.extra.image,
-
trailing_slash=false) %} {% elif config.extra.favicon %} {% set image =
-
get_url(path=config.extra.favicon, trailing_slash=false) %} {% endif %} {% if
+
%} {% set image = get_url(path="og.png", trailing_slash=false) %} {% elif
+
section.extra.image %} {% set image = get_url(path=section.extra.image,
+
trailing_slash=false) %} {% elif page.path %} {% set image =
+
get_url(path=page.path ~ "og.png", trailing_slash=false) %} {% else %} {% set
+
image = get_url(path="og.png", trailing_slash=false) %} {% endif %} {% if
page.permalink %} {% set url = page.permalink %} {% elif section.permalink %} {%
set url = section.permalink %} {% elif config.base_url %} {% set url =
config.base_url %} {% endif %} {% if title %}
+37 -1
tools/genOG.ts
···
import puppeteer from "puppeteer";
+
import { readdir } from "node:fs/promises";
const template = await Bun.file("tools/og.html").text();
···
await page.screenshot({ path: outputPath });
}
+
async function fileExists(path: string): Promise<boolean> {
+
try {
+
await Bun.file(path);
+
return true;
+
} catch (e) {
+
return false;
+
}
+
}
+
try {
-
await og("an early tale of tomfoolery", "og.png");
+
// check if the public/blog folder exists
+
// if not exit
+
// if it does, get all the folders and then get the title tag from the index.html
+
+
if (!(await fileExists("public/"))) {
+
console.error("public/ does not exist");
+
process.exit(1);
+
}
+
+
// read all the files in the current directory filtering for index.htmls
+
const files = (await readdir("public/", { recursive: true })).filter((file) =>
+
file.endsWith("index.html")
+
);
+
+
console.log("Generating OG images for", files.length, "files");
+
+
// for each file, get the title tag from the index.html
+
for (const file of files) {
+
const index = await Bun.file(`public/${file}`).text();
+
const title = index.match(/<title>(.*?)<\/title>/)![1];
+
if (!title) {
+
console.error(`No title found for ${file}`);
+
continue;
+
}
+
console.log("Generating OG for", title);
+
await og(title, `public/${file.replace("index.html", "og.png")}`);
+
}
} catch (e) {
console.error(e);
} finally {