-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathscreenshot.js
78 lines (59 loc) · 1.77 KB
/
screenshot.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const settings = {
port: 8080,
screenShotWidth: 1300
}
const puppeteer = require('puppeteer');
const fs = require('fs').promises;
var fm = require('front-matter');
const slugify = require("slugify");
const folder = './hell/entries/';
slugify.extend({'<': '', '>': ''})
const readDir = async folder => {
return await fs.readdir(folder);
}
const readFile = async path => {
return await fs.readFile(path, 'utf8')
}
const takeScreenshot = async (attributes) => {
let title = attributes.title;
if (attributes.seo_title) {
title = attributes.seo_title
}
const path = slugify(title, {
lower: true
})
const browser = await puppeteer.launch({args: ['--no-sandbox', '--disable-setuid-sandbox']});
console.log('Opening browser')
const page = await browser.newPage();
await page.setViewport({
width: settings.screenShotWidth,
height: settings.screenShotWidth / 2
});
await page.goto(`http://localhost:${settings.port}/${path}`);
console.log(`Opening http://localhost:${settings.port}/${path}`)
await page.$eval('html', html => {
html.classList.add('class', 'screenshot')
});
const [response] = await Promise.all([
page.waitForNavigation(),
page.screenshot({path: `./hell/images/og/${path}.png`})
])
await browser.close();
console.log('Closing browser')
return browser;
}
readDir(folder).then( async (files) => {
console.log('Reading dir')
for (let i = 0; i < files.length; i++) {
const file = files[i];
if (file.indexOf('.md') > -1) {
console.log(`Processing ${folder+file}`)
const fileContent = await readFile(folder+file)
var content = fm(fileContent)
await takeScreenshot(content.attributes).catch(err => {
console.log(err)
})
}
}
console.log('End reading dir')
});