Skip to content

Commit

Permalink
(docs) fix closing puppeteer
Browse files Browse the repository at this point in the history
  • Loading branch information
jcubic committed Nov 5, 2024
1 parent 2be3c92 commit e374404
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
14 changes: 3 additions & 11 deletions docs/docusaurus.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {Config} from '@docusaurus/types';
import type * as Preset from '@docusaurus/preset-classic';
import marked from 'marked';

import renderSocialCard from './src/utils';
import renderSocialCards from './src/utils';

const isProd = process.env.NODE_ENV === 'production';

Expand Down Expand Up @@ -55,17 +55,9 @@ const config: Config = {
copyright: `Copyright © ${new Date().getFullYear()} Jakub T. Jankiewicz`,
title: 'LIPS Scheme blog',
description: 'LIPS Scheme blog RSS Feed',
createFeedItems: async ({ blogPosts,...params }) => {
createFeedItems: async ({ blogPosts, ...params }) => {
if (isProd) {
await Promise.all(blogPosts.map(blogPost => {
const author = blogPost.metadata.authors[0];
const slug = blogPost.metadata.permalink.replace(/^.*\//, '');
const title = blogPost.metadata.title;
const fullname = author.name;
const avatar = author.imageURL;
const date = blogPost.metadata.date;
return renderSocialCard({ title, fullname, avatar, slug, date: new Date(date) });
}));
await renderSocialCards(blogPosts);
}
const feedItems = await params.defaultCreateFeedItems({ blogPosts, ...params });
feedItems.forEach((feedItem,index) => {
Expand Down
35 changes: 29 additions & 6 deletions docs/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import puppeteer from 'puppeteer';
import fs from 'fs/promises';
import { Liquid } from 'liquidjs';
import crypto from 'crypto';
import { type CreateFeedItemsFn } from '@docusaurus/plugin-content-blog';

const liquid = new Liquid();

Expand All @@ -21,17 +22,14 @@ async function path_exists(path: string) {
}

type RenderOptions = {
browser: Awaited<ReturnType<typeof puppeteer.launch>>;
title: string;
fullname: string;
avatar: string;
slug: string;
date: Date
};

const browser = puppeteer.launch({
headless: true
});

function formatDate(lang: string, date: Date) {
const options = { year: 'numeric', month: 'short', day: 'numeric' } as const;
return date.toLocaleDateString(lang, options);
Expand All @@ -44,7 +42,7 @@ function mktemp(suffix: string) {
return path.join(os.tmpdir(), `${prefix}-${suffix}`);
}

export default async function render({ title, fullname, avatar, slug, date }: RenderOptions) {
export async function render({ title, browser, fullname, avatar, slug, date }: RenderOptions) {
const output_svg = await liquid.render(await svg, {
fullname,
title,
Expand All @@ -58,7 +56,7 @@ export default async function render({ title, fullname, avatar, slug, date }: Re
await fs.mkdir(directory, { recursive: true });
}
const filename = `${directory}${slug}.png`;
const page = await (await browser).newPage();
const page = await browser.newPage();
await page.setViewport({
height: 630,
width: 1200
Expand All @@ -74,3 +72,28 @@ export default async function render({ title, fullname, avatar, slug, date }: Re
console.log(`[Docusaurs] Writing ${filename}`);
await page.close();
}

type BlogPosts = Parameters<CreateFeedItemsFn>[0]['blogPosts'];

export default async function renderBlogArticles(posts: BlogPosts) {
const browser = await puppeteer.launch({
headless: true
});
for (const post of posts) {
const author = post.metadata.authors[0];
const slug = post.metadata.permalink.replace(/^.*\//, '');
const title = post.metadata.title;
const fullname = author.name;
const avatar = author.imageURL;
const date = post.metadata.date;
await render({
browser,
title,
fullname,
avatar,
slug,
date: new Date(date)
});
}
await browser.close();
}

0 comments on commit e374404

Please sign in to comment.