|
| 1 | +require("dotenv").config(); |
1 | 2 | const chromium = require("chrome-aws-lambda");
|
2 |
| -const fs = require("fs"); |
3 |
| -const path = require("path"); |
4 | 3 |
|
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) }; |
7 | 13 |
|
8 | 14 | const browser = await chromium.puppeteer.launch({
|
9 | 15 | args: chromium.args,
|
| 16 | + defaultViewport: chromium.defaultViewport, |
10 | 17 | executablePath: await chromium.executablePath,
|
11 | 18 | headless: chromium.headless,
|
12 | 19 | });
|
13 | 20 |
|
14 | 21 | const page = await browser.newPage();
|
15 | 22 |
|
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, |
25 | 26 | });
|
26 | 27 |
|
27 |
| - // Wait until the document is fully rendered |
28 | 28 | await page.evaluateHandle("document.fonts.ready");
|
29 | 29 |
|
30 |
| - // Set the viewport to your preferred image size |
31 | 30 | await page.setViewport({
|
32 | 31 | width: 600,
|
33 | 32 | height: 315,
|
34 | 33 | deviceScaleFactor: 2,
|
35 | 34 | });
|
36 | 35 |
|
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; |
40 | 39 |
|
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"; |
47 | 45 |
|
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); |
53 | 52 |
|
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); |
60 | 54 |
|
61 |
| - console.log(`Image: ${post.slug}.png`); |
| 55 | + await browser.close(); |
62 | 56 |
|
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 | +} |
70 | 59 |
|
71 |
| - await browser.close(); |
72 |
| - console.log("Social images complete!"); |
| 60 | +(async () => { |
| 61 | + await screenshot("gallery", "Gallery", "Olivia%20Ng"); |
73 | 62 | })();
|
0 commit comments