hexo-yam/lib/filter.js

103 lines
3.3 KiB
JavaScript
Raw Normal View History

2016-05-26 11:09:41 +00:00
/* global hexo */
'use strict';
var CleanCSS = require('clean-css'),
UglifyJS = require('uglify-js'),
Htmlminifier = require('html-minifier').minify,
streamToArray = require('stream-to-array');
var Promise = require('bluebird');
var minimatch = require('minimatch');
function logic_html(str, data) {
var hexo = this,
2016-05-26 11:46:01 +00:00
options = hexo.config.neat_html;
2016-05-26 11:09:41 +00:00
// Return if disabled.
if (false === options.enable) return;
var path = data.path;
var exclude = options.exclude;
if (exclude && !Array.isArray(exclude)) exclude = [exclude];
if (path && exclude && exclude.length) {
for (var i = 0, len = exclude.length; i < len; i++) {
if (minimatch(path, exclude[i])) return str;
}
}
var log = hexo.log || console.log;
var result = Htmlminifier(str, options);
var saved = ((str.length - result.length) / str.length * 100).toFixed(2);
log.log('neat the html: %s [ %s saved]', path, saved + '%');
2016-06-22 11:15:47 +00:00
var now = new Date();
// var prefix='<!-- build time:'+ now.getFullYear() +"年"+(now.getMonth() + 1)+"月"+now.getDate()+"日"
// +now.getHours()+":"+now.getMinutes()+":"
var prefix = '<!-- build time:' + now.toLocaleString() + " -->";
var end = '<!-- rebuild by neat -->';
result = prefix + result + end;
2016-05-26 11:09:41 +00:00
return result;
};
function logic_css(str, data) {
var hexo = this,
2016-06-22 11:15:47 +00:00
options = hexo.config.neat_css;
2016-05-26 11:09:41 +00:00
// Return if disabled.
if (false === options.enable) return;
var path = data.path;
var exclude = options.exclude;
if (exclude && !Array.isArray(exclude)) exclude = [exclude];
if (path && exclude && exclude.length) {
for (var i = 0, len = exclude.length; i < len; i++) {
if (minimatch(path, exclude[i])) return str;
}
}
2016-06-22 11:15:47 +00:00
2016-05-26 11:09:41 +00:00
var log = hexo.log || console.log;
2016-06-22 11:15:47 +00:00
return new Promise(function(resolve, reject) {
new CleanCSS(options).minify(str, function(err, result) {
2016-05-26 11:09:41 +00:00
if (err) return reject(err);
var saved = ((str.length - result.styles.length) / str.length * 100).toFixed(2);
2016-06-22 11:15:47 +00:00
var prefix = '/* build time:' + new Date().toLocaleString() + "*/\n";
var end = '\n/* rebuild by neat */';
var css_result = prefix + result.styles + end;
resolve(css_result);
2016-05-26 11:09:41 +00:00
log.log('neat the css: %s [ %s saved]', path, saved + '%');
});
});
}
function logic_js(str, data) {
var hexo = this,
2016-05-26 11:46:01 +00:00
options = hexo.config.neat_js;
2016-05-26 11:09:41 +00:00
// Return if disabled.
if (false === options.enable) return;
var path = data.path;
var exclude = options.exclude;
if (exclude && !Array.isArray(exclude)) exclude = [exclude];
if (path && exclude && exclude.length) {
for (var i = 0, len = exclude.length; i < len; i++) {
if (minimatch(path, exclude[i])) return str;
}
}
var log = hexo.log || console;
var result = UglifyJS.minify(str, options);
var saved = ((str.length - result.code.length) / str.length * 100).toFixed(2);
log.log('neat the js: %s [ %s saved]', path, saved + '%');
2016-06-22 11:15:47 +00:00
var prefix = '// build time:' + new Date().toLocaleString() + "\n";
var end = '\n//rebuild by neat ';
var js_result = prefix + result.code + end;
return js_result;
2016-05-26 11:09:41 +00:00
}
module.exports = {
logic_html: logic_html,
logic_css: logic_css,
logic_js: logic_js,
2016-06-22 11:15:47 +00:00
};