|
| 1 | +const fs = require("fs") |
| 2 | +const puppeteer = require("puppeteer") |
| 3 | + |
| 4 | +const isItDark = require("./isItDark") |
| 5 | + |
| 6 | +const filesRaw = [] |
| 7 | +const workingFolder = "../data/" |
| 8 | +const files = fs.readdirSync(workingFolder) |
| 9 | + |
| 10 | +files.forEach(file => { |
| 11 | + if (file.endsWith(".json")) { |
| 12 | + const data = require(`../data/${file}`) |
| 13 | + filesRaw.push({ id: data.id, title: data.title, color: data.colorPref }) |
| 14 | + } |
| 15 | +}) |
| 16 | + |
| 17 | +const main = async () => { |
| 18 | + const browser = await puppeteer.launch() |
| 19 | + |
| 20 | + browser.on("disconnected", () => { |
| 21 | + return 0 |
| 22 | + }) |
| 23 | + |
| 24 | + const page = await browser.newPage() |
| 25 | + |
| 26 | + await page.setViewport({ |
| 27 | + width: 1200, |
| 28 | + height: 630, |
| 29 | + deviceScaleFactor: 1, |
| 30 | + }) |
| 31 | + |
| 32 | + for (let i = 0; i < filesRaw.length; i++) { |
| 33 | + const imgHTML = ` |
| 34 | + <!DOCTYPE html> |
| 35 | + <html lang="en"> |
| 36 | + <head> |
| 37 | + <meta charset="UTF-8" /> |
| 38 | + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 39 | + <meta http-equiv="X-UA-Compatible" content="ie=edge" /> |
| 40 | + <title>HTML to Generate Thumbnail</title> |
| 41 | + <style> |
| 42 | + @font-face { |
| 43 | + font-family: "Hind Siliguri"; |
| 44 | + src: url("./src/assets/fonts/HindSiliguri-Light.woff2") format("woff2"), |
| 45 | + url("./src/assets/fonts/HindSiliguri-Light.woff") format("woff"); |
| 46 | + font-weight: 300; |
| 47 | + font-style: normal; |
| 48 | + } |
| 49 | +
|
| 50 | + @font-face { |
| 51 | + font-family: "Hind Siliguri"; |
| 52 | + src: url("./src/assets/fonts/HindSiliguri-Regular.woff2") format("woff2"), |
| 53 | + url("./src/assets/fonts/HindSiliguri-Regular.woff") format("woff"); |
| 54 | + font-weight: 400; |
| 55 | + font-style: normal; |
| 56 | + } |
| 57 | +
|
| 58 | + @font-face { |
| 59 | + font-family: "Hind Siliguri"; |
| 60 | + src: url("./src/assets/fonts/HindSiliguri-Bold.woff2") format("woff2"), |
| 61 | + url("./src/assets/fonts/HindSiliguri-Bold.woff") format("woff"); |
| 62 | + font-weight: 800; |
| 63 | + font-style: normal; |
| 64 | + } |
| 65 | +
|
| 66 | + * { |
| 67 | + margin: 0; |
| 68 | + padding: 0; |
| 69 | + box-sizing: border-box; |
| 70 | + } |
| 71 | +
|
| 72 | + body { |
| 73 | + font-family: "Hind Siliguri"; |
| 74 | + background: ${filesRaw[i].color}; |
| 75 | + color: ${isItDark(filesRaw[i].color) ? "#fffeff" : "#201f22"} |
| 76 | + } |
| 77 | + </style> |
| 78 | + </head> |
| 79 | + <body> |
| 80 | + <div |
| 81 | + style="height: 100vh; width: 100vw; display: flex; flex-direction: column; align-items: center; justify-content: space-evenly" |
| 82 | + > |
| 83 | + <div style="font-size: 36px; font-weight: bold;"> |
| 84 | + <span |
| 85 | + style="display:inline-block; background-color: ${ |
| 86 | + isItDark(filesRaw[i].color) ? "#fffeff" : "#201f22" |
| 87 | + }; color: ${ |
| 88 | + filesRaw[i].color |
| 89 | + }; padding: 0 8px; border-radius: 4px; margin-right: 5px;" |
| 90 | + >ডেভ</span |
| 91 | + >সংকেত |
| 92 | + </div> |
| 93 | + <div style="text-align: center; padding: 0 120px;"> |
| 94 | + <h2 style="font-size: 72px; line-height: 78px;">${ |
| 95 | + filesRaw[i].title |
| 96 | + }</h2> |
| 97 | + <p style="font-size: 22px;">বাংলা ডেভেলপার চিটশিট</p> |
| 98 | + </div> |
| 99 | + <pre style="font-family: consolas; font-weight: bold; font-size: 26px;"> |
| 100 | + https://devsonket.com/${filesRaw[i].id}</pre |
| 101 | + > |
| 102 | + </div> |
| 103 | + </body> |
| 104 | + </html> |
| 105 | + ` |
| 106 | + await page.setContent(imgHTML) |
| 107 | + await page.screenshot({ |
| 108 | + type: "jpeg", |
| 109 | + path: `../static/static/thumbnail/${filesRaw[i].id}.jpg`, |
| 110 | + quality: 85, |
| 111 | + }) |
| 112 | + } |
| 113 | + await browser.close() |
| 114 | +} |
| 115 | + |
| 116 | +main() |
0 commit comments