Skip to content

Commit eebbbe6

Browse files
committed
Fix authorEl
1 parent add9d18 commit eebbbe6

File tree

3 files changed

+37
-48
lines changed

3 files changed

+37
-48
lines changed

api/social-img.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ async function screenshot(slug, title, author) {
4444
: "Style Stage";
4545

4646
if (author) {
47-
var author = document.createElement("SMALL");
48-
author.innerHTML = `By ${author}`;
49-
h1.appendChild(author);
47+
var authorEl = document.createElement("SMALL");
48+
authorEl.innerHTML = `By ${author}`;
49+
h1.appendChild(authorEl);
5050
}
5151
}, pageData);
5252

functions/images.js

Lines changed: 34 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,62 @@
1+
require("dotenv").config();
12
const chromium = require("chrome-aws-lambda");
2-
const fs = require("fs");
3-
const path = require("path");
43

5-
(async () => {
6-
console.log("Starting social images...");
4+
async function screenshot(slug, title, author) {
5+
const baseURL = process.env.URL || "https://social-images--moderncss-styles.netlify.app/";
6+
const url = `${baseURL}/social-template/`;
7+
let options = {
8+
path: "./image.png",
9+
type: "png",
10+
// encoding: "base64",
11+
};
12+
let pageData = { slug, title: decodeURIComponent(title), author: decodeURIComponent(author) };
713

814
const browser = await chromium.puppeteer.launch({
915
args: chromium.args,
16+
defaultViewport: chromium.defaultViewport,
1017
executablePath: await chromium.executablePath,
1118
headless: chromium.headless,
1219
});
1320

1421
const page = await browser.newPage();
1522

16-
// Load html from template
17-
const html = fs.readFileSync(path.resolve(__dirname, "./template.html")).toString();
18-
19-
// Get generated data json
20-
const pages = require("./pages.json");
21-
22-
// Render html, wait for 0 network connections to ensure webfonts downloaded
23-
await page.setContent(html, {
24-
waitUntil: ["networkidle0"],
23+
await page.goto(url, {
24+
waitUntil: ["load", "networkidle0"],
25+
timeout: 5000,
2526
});
2627

27-
// Wait until the document is fully rendered
2828
await page.evaluateHandle("document.fonts.ready");
2929

30-
// Set the viewport to your preferred image size
3130
await page.setViewport({
3231
width: 600,
3332
height: 315,
3433
deviceScaleFactor: 2,
3534
});
3635

37-
// Create a `previews` directory in the public folder
38-
const dir = path.resolve(__dirname, "../public/previews");
39-
if (!fs.existsSync(dir)) fs.mkdirSync(dir);
36+
await page.evaluate(({ slug, title, author }) => {
37+
const h1 = document.querySelector("h1");
38+
h1.innerHTML = title;
4039

41-
// Go over all the posts
42-
for (const post of pages) {
43-
// Update the H1 element with the post title
44-
await page.evaluate((post) => {
45-
const title = document.querySelector("h1");
46-
title.innerHTML = post.title;
40+
const subtitle = document.querySelector("h2");
41+
subtitle.innerHTML =
42+
slug === "home"
43+
? "A modern CSS showcase styled by community contributions"
44+
: "Style Stage";
4745

48-
const subtitle = document.querySelector("h2");
49-
subtitle.innerHTML =
50-
post.slug === "home"
51-
? "A modern CSS showcase styled by community contributions"
52-
: "Style Stage";
46+
if (author) {
47+
var authorEl = document.createElement("SMALL");
48+
authorEl.innerHTML = `By ${author}`;
49+
h1.appendChild(authorEl);
50+
}
51+
}, pageData);
5352

54-
if (post.author) {
55-
var author = document.createElement("SMALL");
56-
author.innerHTML = `By ${post.author}`;
57-
title.appendChild(author);
58-
}
59-
}, post);
53+
let output = await page.screenshot(options);
6054

61-
console.log(`Image: ${post.slug}.png`);
55+
await browser.close();
6256

63-
// Save a screenshot to public/img/slug-of-post.png
64-
await page.screenshot({
65-
path: `${dir}/${post.slug}.png`,
66-
type: "png",
67-
clip: { x: 0, y: 0, width: 600, height: 315 },
68-
});
69-
}
57+
return output;
58+
}
7059

71-
await browser.close();
72-
console.log("Social images complete!");
60+
(async () => {
61+
await screenshot("gallery", "Gallery", "Olivia%20Ng");
7362
})();

image.png

31.3 KB
Loading

0 commit comments

Comments
 (0)