-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathprocess.js
68 lines (58 loc) · 1.93 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
var Promise = require('bluebird')
var getCache = require('./cache')
var types = require('./types')
var replace = require('string-replace-async')
var loadFileContent = require('./utils').loadFileContent
var getTypeName = require('./utils').getTypeName
var base64Decode = require('./utils').base64Decode
function processType(options) {
var hexo = options.hexo
var content = options.content
var type = options.type
var config = options.config
var stats = options.stats
var cache = getCache(config)
var name = getTypeName(type)
var generate = types[type].generate
var serialize = types[type].serialize
var regex = new RegExp('__' + name + '\\([^\\(]+\\)', 'g')
return replace(content, regex, function (placeholder) {
var matches = placeholder.match('__' + name + '\\(([^\\(]+)\\)')
var url = base64Decode(matches[1])
var stream = hexo.route.get(url)
if (stream == null) {
return Promise.reject(new Error('Can not find file: ' + url))
}
return loadFileContent(stream)
.then(function (buffer) {
var cached = cache.getCache(url, type)
if (cached) {
stats.addCached()
hexo.log.debug('[LQIP] Generating `' + type + '` type for ' + url + ' (from cache)')
return cached
}
stats.addGenerated()
hexo.log.debug('[LQIP] Generating `' + type + '` type for ' + url)
return generate(url, buffer, config[type])
})
.then(function (data) {
cache.saveCache(url, type, data)
return serialize(data)
})
})
}
exports.processHtmlFile = function processHtmlFile(options) {
var hexo = options.hexo
var initialContent = options.content
var config = options.config
var stats = options.stats
return Promise.reduce(Object.keys(types), function(content, type) {
return processType({
hexo: hexo,
content: content,
type: type,
config: config,
stats: stats
})
}, initialContent)
}