-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcarbon.js
44 lines (34 loc) · 1.32 KB
/
carbon.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const puppeteer = require("puppeteer");
const { languageList, themeList } = require("./config.json");
const getImageFile = async (code, theme, language) => {
return new Promise(async (resolve, reject) => {
try {
const browser = await puppeteer.launch();
const carbonPage = await browser.newPage({
waitUntil: "networkidle0",
timeout: 0,
});
await carbonPage.setViewport({
width: 1920,
height: 1080,
deviceScaleFactor: 2.5,
});
code = encodeURIComponent(code);
const link = `https://carbon.now.sh/?code=${code}&t=${theme}&l=${language}`;
await carbonPage.goto(link);
await carbonPage.waitForSelector("#export-container");
const element = await carbonPage.$("#export-container");
const carbonPic = await element.screenshot({
type: "png",
});
await carbonPage.close();
await browser.close();
resolve(carbonPic);
} catch (e) {
reject(e);
}
});
};
const checkTheme = (theme) => themeList.includes(theme);
const getLanguage = (language) => languageList[language] ?? "auto";
module.exports = { getImageFile, checkTheme, getLanguage };