Skip to content

Commit e374404

Browse files
committed
(docs) fix closing puppeteer
1 parent 2be3c92 commit e374404

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

docs/docusaurus.config.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type {Config} from '@docusaurus/types';
33
import type * as Preset from '@docusaurus/preset-classic';
44
import marked from 'marked';
55

6-
import renderSocialCard from './src/utils';
6+
import renderSocialCards from './src/utils';
77

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

@@ -55,17 +55,9 @@ const config: Config = {
5555
copyright: `Copyright © ${new Date().getFullYear()} Jakub T. Jankiewicz`,
5656
title: 'LIPS Scheme blog',
5757
description: 'LIPS Scheme blog RSS Feed',
58-
createFeedItems: async ({ blogPosts,...params }) => {
58+
createFeedItems: async ({ blogPosts, ...params }) => {
5959
if (isProd) {
60-
await Promise.all(blogPosts.map(blogPost => {
61-
const author = blogPost.metadata.authors[0];
62-
const slug = blogPost.metadata.permalink.replace(/^.*\//, '');
63-
const title = blogPost.metadata.title;
64-
const fullname = author.name;
65-
const avatar = author.imageURL;
66-
const date = blogPost.metadata.date;
67-
return renderSocialCard({ title, fullname, avatar, slug, date: new Date(date) });
68-
}));
60+
await renderSocialCards(blogPosts);
6961
}
7062
const feedItems = await params.defaultCreateFeedItems({ blogPosts, ...params });
7163
feedItems.forEach((feedItem,index) => {

docs/src/utils.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import puppeteer from 'puppeteer';
44
import fs from 'fs/promises';
55
import { Liquid } from 'liquidjs';
66
import crypto from 'crypto';
7+
import { type CreateFeedItemsFn } from '@docusaurus/plugin-content-blog';
78

89
const liquid = new Liquid();
910

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

2324
type RenderOptions = {
25+
browser: Awaited<ReturnType<typeof puppeteer.launch>>;
2426
title: string;
2527
fullname: string;
2628
avatar: string;
2729
slug: string;
2830
date: Date
2931
};
3032

31-
const browser = puppeteer.launch({
32-
headless: true
33-
});
34-
3533
function formatDate(lang: string, date: Date) {
3634
const options = { year: 'numeric', month: 'short', day: 'numeric' } as const;
3735
return date.toLocaleDateString(lang, options);
@@ -44,7 +42,7 @@ function mktemp(suffix: string) {
4442
return path.join(os.tmpdir(), `${prefix}-${suffix}`);
4543
}
4644

47-
export default async function render({ title, fullname, avatar, slug, date }: RenderOptions) {
45+
export async function render({ title, browser, fullname, avatar, slug, date }: RenderOptions) {
4846
const output_svg = await liquid.render(await svg, {
4947
fullname,
5048
title,
@@ -58,7 +56,7 @@ export default async function render({ title, fullname, avatar, slug, date }: Re
5856
await fs.mkdir(directory, { recursive: true });
5957
}
6058
const filename = `${directory}${slug}.png`;
61-
const page = await (await browser).newPage();
59+
const page = await browser.newPage();
6260
await page.setViewport({
6361
height: 630,
6462
width: 1200
@@ -74,3 +72,28 @@ export default async function render({ title, fullname, avatar, slug, date }: Re
7472
console.log(`[Docusaurs] Writing ${filename}`);
7573
await page.close();
7674
}
75+
76+
type BlogPosts = Parameters<CreateFeedItemsFn>[0]['blogPosts'];
77+
78+
export default async function renderBlogArticles(posts: BlogPosts) {
79+
const browser = await puppeteer.launch({
80+
headless: true
81+
});
82+
for (const post of posts) {
83+
const author = post.metadata.authors[0];
84+
const slug = post.metadata.permalink.replace(/^.*\//, '');
85+
const title = post.metadata.title;
86+
const fullname = author.name;
87+
const avatar = author.imageURL;
88+
const date = post.metadata.date;
89+
await render({
90+
browser,
91+
title,
92+
fullname,
93+
avatar,
94+
slug,
95+
date: new Date(date)
96+
});
97+
}
98+
await browser.close();
99+
}

0 commit comments

Comments
 (0)