···66const browser = await puppeteer.launch();
7788async function og(
99- postname: string,
1010- outputPath: string,
1111- width = 1200,
1212- height = 630
99+ postname: string,
1010+ outputPath: string,
1111+ width = 1200,
1212+ height = 630,
1313) {
1414- const page = await browser.newPage();
1414+ const page = await browser.newPage();
15151616- await page.setViewport({ width, height });
1616+ await page.setViewport({ width, height });
17171818- await page.setContent(template.toString().replace("{{postname}}", postname));
1818+ await page.setContent(template.toString().replace("{{postname}}", postname));
19192020- await page.screenshot({ path: outputPath });
2020+ await page.screenshot({ path: outputPath });
2121}
22222323async function fileExists(path: string): Promise<boolean> {
2424- try {
2525- await Bun.file(path);
2626- return true;
2727- } catch (e) {
2828- return false;
2929- }
2424+ try {
2525+ await Bun.file(path);
2626+ return true;
2727+ } catch (e) {
2828+ return false;
2929+ }
3030}
31313232try {
3333- // check if the public/blog folder exists
3434- // if not exit
3535- // if it does, get all the folders and then get the title tag from the index.html
3333+ // check if the public/blog folder exists
3434+ // if not exit
3535+ // if it does, get all the folders and then get the title tag from the index.html
36363737- if (!(await fileExists("public/"))) {
3838- console.error("public/ does not exist");
3939- process.exit(1);
4040- }
3737+ if (!(await fileExists("public/"))) {
3838+ console.error("public/ does not exist");
3939+ process.exit(1);
4040+ }
41414242- // read all the files in the current directory filtering for index.htmls
4343- const files = (await readdir("public/", { recursive: true })).filter((file) =>
4444- file.endsWith("index.html")
4545- );
4242+ // read all the files in the current directory filtering for index.htmls
4343+ const files = (await readdir("public/", { recursive: true })).filter((file) =>
4444+ file.endsWith("index.html"),
4545+ );
46464747- const directories = new Set(
4848- files.map((file) => file.replace("index.html", ""))
4949- );
4747+ const directories = new Set(
4848+ files.map((file) => file.replace("index.html", "")),
4949+ );
50505151- const existing = (await readdir("static/")).filter((file) =>
5252- directories.has(file)
5353- );
5151+ const existing = (await readdir("static/")).filter((file) =>
5252+ directories.has(file),
5353+ );
54545555- // create not existing
5656- for (const dir of directories) {
5757- if (!existing.includes(dir)) {
5858- await mkdir(`static/${dir.split("/").slice(0, -1).join("/")}`, {
5959- recursive: true,
6060- });
6161- }
6262- }
5555+ // create not existing
5656+ for (const dir of directories) {
5757+ if (!existing.includes(dir)) {
5858+ await mkdir(`static/${dir.split("/").slice(0, -1).join("/")}`, {
5959+ recursive: true,
6060+ });
6161+ }
6262+ }
63636464- console.log("Generating OG images for", files.length, "files");
6464+ console.log("Generating OG images for", files.length, "files");
65656666- // for each file, get the title tag from the index.html
6767- for (const file of files) {
6868- const index = await Bun.file(`public/${file}`).text();
6969- let title: string;
7070- if (file.startsWith("tags/")) {
7171- const parts = file.split("/");
7272- title = `Tag: ${parts[1]}`; // take the next directory as the title
7373- } else {
7474- const match = index.match(/<title>(.*?)<\/title>/);
7575- if (match) {
7676- title = match[1];
7777- } else {
7878- console.error(`No title found for ${file}`);
7979- continue;
8080- }
8181- }
6666+ // for each file, get the title tag from the index.html
6767+ for (const file of files) {
6868+ const index = await Bun.file(`public/${file}`).text();
6969+ let title: string;
7070+ if (file.startsWith("tags/")) {
7171+ const parts = file.split("/");
7272+ title = `Tag: ${parts[1]}`; // take the next directory as the title
7373+ } else {
7474+ const match = index.match(/<title>(.*?)<\/title>/);
7575+ if (match) {
7676+ title = match[1];
7777+ } else {
7878+ console.error(`No title found for ${file}`);
7979+ continue;
8080+ }
8181+ }
82828383- console.log("Generating OG for", title);
8484- await og(title, `static/${file.replace("index.html", "og.png")}`);
8585- }
8383+ console.log("Generating OG for", title);
8484+ await og(title, `static/${file.replace("index.html", "og.png")}`);
8585+ }
8686} catch (e) {
8787- console.error(e);
8787+ console.error(e);
8888} finally {
8989- await browser.close();
8989+ await browser.close();
9090}