-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.js
executable file
·152 lines (124 loc) · 4.41 KB
/
process.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const slugify = require('slugify');
const markdownData = require('markdown-data');
const markdown = require('markdown-it')({
html: true,
});
const sharp = require('sharp');
const handlebars = require('handlebars');
const handlebarsMoment = require('handlebars.moment');
const handlebarsLayouts = require('handlebars-layouts');
const imageSizes = [400, 800, 1200, 1600, 2000];
handlebarsMoment.registerHelpers(handlebars);
handlebars.registerHelper(handlebarsLayouts(handlebars));
handlebars.registerPartial('layout', fs.readFileSync('./src/templates/layout.hbs', 'utf8'));
const homeTemplate = handlebars.compile(fs.readFileSync('./src/templates/home.hbs', 'utf8'));
const postTemplate = handlebars.compile(fs.readFileSync('./src/templates/post.hbs', 'utf8'));
const makeDirectoryExist = (path) => {
if (!fs.existsSync(path)) {
fs.mkdirSync(path);
}
};
const makeSlug = (string) => {
return slugify(string, {
lower: true,
});
};
const directoryContents = (rootPath, filter) => {
let results = [];
const directories = fs.readdirSync(rootPath);
directories.forEach((directory) => {
const directoryPath = path.join(rootPath, directory);
if (typeof filter === 'function') {
if (!filter(directoryPath)) {
return;
}
}
results.push(directoryPath);
});
return results;
};
const directoriesIn = (rootPath) => {
return directoryContents(rootPath, (itemPath) => {
return fs.lstatSync(itemPath).isDirectory();
});
};
const imagesIn = (rootPath) => {
return directoryContents(rootPath, (itemPath) => {
return path.parse(itemPath).ext === '.jpg';
});
};
const processPost = (postMarkdownPath) => {
let postFile = fs.readFileSync(postMarkdownPath);
let post = markdownData.parse(postFile.toString());
return {
slug: makeSlug(post.data.meta.title),
title: post.data.meta.title,
date: post.data.meta.date,
hero: post.data.meta.hero,
intro: post.data.meta.intro,
html: markdown.render(post.markdown),
};
};
const processImage = async (imagePath, destinationPath, filename, imageSize) => {
let filePath = path.join(destinationPath, filename + '-' + imageSize + '.jpg');
await sharp(imagePath).resize(imageSize, imageSize, {
fit: sharp.fit.inside,
withoutEnlargement: true,
}).toFile(filePath);
};
const processImageSizes = (imagePath, destinationPath) => {
console.log('Processing image: ' + imagePath);
const filename = path.parse(imagePath).name;
let images = imageSizes.slice(0);
return Promise.all(images.map((imageSize) => {
return processImage(imagePath, destinationPath, filename, imageSize);
}));
};
const generatePages = (posts, css) => {
fs.writeFileSync('./public/index.html', homeTemplate({
css,
title: 'Joel Gone Wild',
description: 'I really enjoy exploring new places, so in 2014 I started writing down the things I had done both for others to read, and so future me can read too!',
path: '/',
posts,
}), 'utf8');
for (let post of posts) {
fs.writeFileSync('./public/' + post.slug + '/index.html', postTemplate({
css,
title: post.title,
description: post.intro,
path: '/' + post.slug + '/',
posts,
post,
}), 'utf8');
}
};
return (async () => {
makeDirectoryExist('public');
let posts = [];
let postPaths = [];
if (typeof process.argv[2] !== 'undefined' && process.argv[2] !== '--no-photos') {
postPaths.push(process.argv[2]);
} else {
postPaths = directoriesIn('posts');
}
for (let postPath of postPaths) {
const post = processPost(path.join(postPath, 'post.md'));
const publicPostPath = path.join('public', post.slug);
makeDirectoryExist(publicPostPath);
if (process.argv[2] !== '--no-photos') {
const imagePaths = imagesIn(path.join(postPath, 'photos'));
for (let imagePath of imagePaths) {
await processImageSizes(imagePath, publicPostPath);
}
}
posts.push(post);
}
posts.sort((a, b) => {
return new Date(b.date) - new Date(a.date);
});
generatePages(posts, fs.readFileSync('./build/app.css', 'utf8').trim());
})();