forked from breatheco-de/content
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-images.js
87 lines (78 loc) · 2.58 KB
/
create-images.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
79
80
81
82
83
84
85
86
87
const nodeHtmlToImage = require('node-html-to-image')
const fm = require('front-matter');
const fs = require('fs');
const path = require('path');
const {promisify} = require('util');
const stat = promisify(fs.stat);
const createThumb = async (lessonPath) => {
const fileName = lessonPath.replace(/^.*[\\\/]/, '').split('.').slice(0, -1).join('.').toLowerCase();
if(!fs.existsSync(lessonPath)) console.log("Invalid path "+lessonPath);
if(fs.existsSync(`./src/assets/${fileName}.jpeg`)) return true;
console.log("Trying to createe "+lessonPath);
const content = fs.readFileSync(lessonPath, 'utf8');
//'Gogogo now!'.match(/(go)+/ig)
const { slug, title, date, tags, status, authors, subtitle, cover } = fm(content).attributes;
if(cover) return;
console.log("beatch ", title);
try{
let result = await nodeHtmlToImage({
output: './src/assets/'+fileName+'.png',
html: `<html>
<head>
<style>
body {
width: 2480px;
height: 3508px;
}
</style>
</head>
<body>${title}</body>
</html>
`
})
console.log(`asd`, result);
console.log('Image for '+fileName+" created successfully!");
}
catch(error){
console.log(error);
}
return true;
}
const pfs = (_path) =>
new Promise((resolve, reject) =>
fs.readdir(_path, (err, filenames) => err ? reject(err) : resolve(filenames)))
const walk = async function(dir) {
var results = [];
const list = await pfs(dir);
if (!list.length) return results;
for(let i = 0; i<list.length; i++){
let file = list[i];
file = path.resolve(dir, file);
const _stat = await stat(file);
if (_stat && _stat.isDirectory()) {
const res = await walk(file);
results = results.concat(res);
return results;
} else {
results = results.concat(list);
return results;
}
}
};
(async () => {
try{
const files = await walk('src/content/lesson');
console.log("files", files);
for(let i = 0; i<files.length; i++){
console.log("Trying", files.length);
const resp = await createThumb('./src/content/lesson/'+files[i]);
console.log("Success!! All thumbs have been generated".green);
process.exit(0);
}
// console.log("list", list)
}
catch(error){
console.log("Error scanning lesson files".red);
process.exit(1);
}
})();