-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9959f77
Showing
6 changed files
with
232 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module.exports = { | ||
"rules": { | ||
"indent": [ | ||
2, | ||
2, | ||
{ | ||
"VariableDeclarator": 2, | ||
"SwitchCase": 1 | ||
} | ||
], | ||
"linebreak-style": [ | ||
2, | ||
"unix" | ||
], | ||
"semi": [ | ||
2, | ||
"always" | ||
], | ||
"curly": "error", | ||
"quotes": [ | ||
"error", | ||
"double" | ||
] | ||
}, | ||
"env": { | ||
"es6": true, | ||
"node": true | ||
}, | ||
"extends": "eslint:recommended" | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
build/ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* global hexo */ | ||
var assign = require("object-assign"); | ||
var ImageSizesProcessor = require("./lib/ImageSizesProcessor"); | ||
var imsizeTag = require("./lib/imsize-tag")(hexo); | ||
var debug = require("debug")("hexo:image_sizes"); | ||
|
||
hexo.config.image_sizes = assign({ | ||
pattern: /\.(jpg|jpeg|png)$/i, | ||
profiles: [] | ||
}, hexo.config.image_sizes); | ||
|
||
debug("Registering for files matching " + hexo.config.image_sizes.pattern); | ||
|
||
var profiles = hexo.config.image_sizes.profiles; | ||
Object.keys(profiles).forEach(function registerImageProcessor(profileName) { | ||
var profile = profiles[profileName]; | ||
debug(profileName, profile); | ||
var processor = new ImageSizesProcessor(hexo, profileName, { | ||
"width": profile.width, | ||
"height": profile.height, | ||
"allowEnlargement": profile.allowEnlargement | ||
}); | ||
hexo.extend.processor.register(hexo.config.image_sizes.pattern, function (file) { | ||
processor.process(file); | ||
}); | ||
}); | ||
|
||
// Register the "imsize" tag | ||
hexo.extend.tag.register("imsize", imsizeTag, {ends: true}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
var sharp = require("sharp"); | ||
var debug = require("debug")("hexo:image_sizes"); | ||
var path = require("path"); | ||
var mkdirp = require("mkdirp"); | ||
|
||
/* | ||
* Wrap mkdirp in a Promise. mkdirp makes a directory if it doesn't already | ||
* exist, and will also make of the directory's ancestors if they don't already | ||
* exist. | ||
*/ | ||
function mkdir (path) { | ||
return new Promise(function (resolve, reject) { | ||
mkdirp(path, function (err) { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(path); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
var ImageSizesProcessor = function (hexo, name, options) { | ||
this.hexo = hexo; | ||
this.name = name; | ||
this.options = options; | ||
debug(name + " processor constructed"); | ||
}; | ||
|
||
/* | ||
* Given the hexo File instance `file`, return a Promise representing the | ||
* completion of resizing this image file. | ||
*/ | ||
ImageSizesProcessor.prototype.process = function (file) { | ||
|
||
var outPath = path.join( | ||
this.hexo.public_dir, | ||
path.dirname(file.path), | ||
this.name + "-" + path.basename(file.path) | ||
); | ||
|
||
var width = this.options.width; | ||
var height = this.options.height; | ||
var allowEnlargement = this.options.allowEnlargement; | ||
|
||
// file.source is the file's full path on disk | ||
var verb = file.type; | ||
debug(verb + " " + outPath); | ||
if (verb === "create" || verb === "update") { | ||
return mkdir(path.dirname(outPath)).then(function () { | ||
var resizer = sharp(file.source).resize(width, height); | ||
if (!allowEnlargement) { | ||
resizer.withoutEnlargement(); | ||
} | ||
return resizer.toFile(outPath); | ||
}); | ||
} else { | ||
// File type indicates that no action is required | ||
return Promise.resolve(); | ||
} | ||
|
||
}; | ||
|
||
module.exports = ImageSizesProcessor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
var path = require("path"); | ||
var debug = require("debug")("hexo:image_sizes"); | ||
var yaml = require("js-yaml"); | ||
var hexoUtil = require("hexo-util"); | ||
|
||
/* | ||
Find a profile in the given `profiles` object, whose keys are the profile names. | ||
*/ | ||
function findProfile(profiles, name) { | ||
return Object.keys(profiles).find(function (profileName) { | ||
return profileName === name; | ||
}); | ||
} | ||
|
||
/* | ||
Try to find a profile in `profiles` with the given `profileName`. If none | ||
exists, try to find the default profile. If that fails, return null. | ||
*/ | ||
function resolveProfileName(config, name) { | ||
var profiles = config.profiles; | ||
var profile = findProfile(profiles, name); | ||
if (profile) { | ||
return profile; | ||
} | ||
var defaultProfile = config.defaultProfile; | ||
if (defaultProfile) { | ||
profile = findProfile(profiles, defaultProfile); | ||
} | ||
if (profile) { | ||
return profile; | ||
} | ||
return null; | ||
} | ||
|
||
/* | ||
This function takes in a reference to the hexo object and returns a function to | ||
interpret imsize tags. The Hexo Tag API is documented at | ||
https://hexo.io/api/tag.html. | ||
*/ | ||
function factory (hexo) { | ||
return function imsizeTag (args, content) { | ||
var config = hexo.config.image_sizes; | ||
|
||
try { | ||
var doc = yaml.safeLoad(content); | ||
} catch (err) { | ||
console.error(err); | ||
return; | ||
} | ||
|
||
var src = doc.src; | ||
var profileName = doc.profile; | ||
var alt = doc.alt; | ||
|
||
var profileMatch = resolveProfileName(config, profileName); | ||
if (!profileMatch) { | ||
console.error(`hexo-image-sizes found no profile matching ${profileName} and no default profile.`); | ||
} | ||
|
||
// Even if there is no matching profile, continue. Default to using the | ||
// full-size image. | ||
|
||
var profilePrefix = ""; | ||
if (profileMatch) { | ||
profilePrefix = profileMatch + "-"; | ||
} | ||
|
||
// Prefix the image name wih the profile name | ||
var newSrc = path.join( | ||
path.dirname(src), | ||
profilePrefix + path.basename(src) | ||
); | ||
|
||
var attrs = { | ||
"src": newSrc | ||
}; | ||
|
||
// If the user added alt-text, add it to the img tag. Avoid adding an empty | ||
// alt property. | ||
if (alt) { | ||
attrs.alt = alt; | ||
} | ||
|
||
return hexoUtil.htmlTag("img", attrs); | ||
}; | ||
} | ||
|
||
module.exports = factory; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "hexo-image-sizes", | ||
"version": "1.0.0", | ||
"description": "Generate images of various sizes from source images.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha" | ||
}, | ||
"author": "Travis Geis <travis@travisgeis.com> (travisgeis.com)", | ||
"license": "ISC", | ||
"dependencies": { | ||
"debug": "^2.2.0", | ||
"hexo-util": "^0.6.0", | ||
"js-yaml": "^3.6.1", | ||
"mkdirp": "^0.5.1", | ||
"object-assign": "^4.1.0", | ||
"sharp": "^0.16.0" | ||
} | ||
} |