-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
75 lines (59 loc) · 1.85 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
var Promise = require('bluebird')
var Stats = require('./stats').Stats
var getCache = require('./cache')
var isHtmlFile = require('./utils').isHtmlFile
var getTypeName = require('./utils').getTypeName
var loadFileContent = require('./utils').loadFileContent
var base64Encode = require('./utils').base64Encode
var processHtmlFile = require('./process').processHtmlFile
var stats = new Stats()
function getConfig(hexo) {
var config = hexo.config
var theme = Object.assign({}, config, hexo.theme.config, config.theme_config)
return Object.assign({
cache: 'lqip-cache.json'
}, theme.lqip)
}
exports.getConfig = getConfig
exports.afterClean = function () {
var hexo = this
var config = getConfig(hexo)
var cache = getCache(config)
return cache.clean()
}
exports.afterGenerate = function () {
var hexo = this
var config = getConfig(hexo)
var route = hexo.route
var routes = route.list()
var htmlFiles = routes.filter(isHtmlFile)
return Promise.map(htmlFiles, function (filePath) {
return loadFileContent(route.get(filePath)).then(function (buffer) {
return route.set(filePath, function () {
return processHtmlFile({hexo: hexo, content: buffer.toString(), config: config, stats: stats})
})
})
})
}
exports.beforeExit = function () {
var hexo = this
var messages = []
if (stats.generated) {
messages.push(stats.generated + ' files generated')
}
if (stats.cached) {
messages.push(stats.cached + ' files from cache')
}
if (messages.length > 0) {
hexo.log.info('[LQIP] processing summary: ' + messages.join(', '))
}
}
exports.lqipFor = function lqipFor(path, opts) {
var hexo = this
var config = hexo.theme.lqip || {}
var options = Object.assign({
type: config.default_type || 'potrace',
}, opts)
var name = getTypeName(options.type)
return '__' + name + '(' + base64Encode(path) +')'
}