-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
93 lines (74 loc) · 3.07 KB
/
index.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
/* global hexo */
var ImageResizer = require("./lib/ImageResizer");
var imsizeTag = require("./lib/imsize-tag")(hexo);
var debug = require("debug")("hexo:image_sizes");
const path = require('path');
hexo.config.image_sizes = Object.assign({
pattern: /\.(jpg|jpeg|png)$/i,
profiles: [],
}, hexo.config.image_sizes);
debug("Registering for files matching " + hexo.config.image_sizes.pattern);
let profiles = hexo.config.image_sizes.profiles;
hexo.locals.set("image_sizes_db", {
updatedPaths: {}, // Files that have changed since Hexo last ran (map from absolute path to Hexo file object)
imagesToGenerate: {}, // Files the blog actually uses (map from profile type to file info from imsizeTag)
});
// Observe Hexo file activity and record which files are updated
// Each file object is a Hexo file (https://hexo.io/api/box.html)
hexo.extend.processor.register(hexo.config.image_sizes.pattern, function (file) {
let updatedPaths = hexo.locals.get("image_sizes_db").updatedPaths;
let verb = file.type;
let hexoRelativeInput = file.path;
updatedPaths[hexoRelativeInput] = file;
debug(`Observed ${verb} ${hexoRelativeInput}`);
});
// Register the "imsize" tag. These tags interact with the image_sizes_db to
// record when a post uses an image and embed the image in the post.
hexo.extend.tag.register("imsize", imsizeTag, {ends: true});
// Switch the slashes in case we get Windows-style paths.
function normalize(p) {
return path.normalize(p).replace(new RegExp(path.sep, 'g'), '/');
}
// Generate images the site has used in imsize tags
hexo.extend.filter.register("after_generate", function() {
let db = hexo.locals.get("image_sizes_db");
debug(JSON.stringify(db, null, 2));
let updatedPaths = db.updatedPaths;
let imagesToGenerate = db.imagesToGenerate;
let profilesGenerated = Object.keys(imagesToGenerate).map((profileName) => {
let profile = profiles[profileName];
let resizer = new ImageResizer(hexo, profileName, {
"width": profile.width,
"height": profile.height,
"allowEnlargement": profile.allowEnlargement,
"autoRotate": profile.autoRotate !== false,
});
let toGenerate = imagesToGenerate[profileName];
let imagesInProfileGenerated = toGenerate.map((fileInfo) => {
let {hexoRelativeInput, hexoRelativeOutput} = fileInfo;
// TODO factor out this check for verb:
let file = updatedPaths[normalize(hexoRelativeInput)];
if (!file) {
debug(`Unknown file:\t${hexoRelativeInput}`);
return;
}
let verb = file.type;
if (!(verb === "create" || verb === "update")) {
debug(`Unchanged file:\t${hexoRelativeInput}`);
return;
}
debug("Resizing image\n", {
hexoRelativeInput,
hexoRelativeOutput,
profileName,
});
return resizer.resizeRoute({
originalRouteName: hexoRelativeInput,
resizedRouteName: hexoRelativeOutput,
});
});
return Promise.all(imagesInProfileGenerated);
});
// Return undefined to leave original file data unmodified
return Promise.all(profilesGenerated).then(() => undefined);
});