-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (39 loc) · 1.4 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
const Fontmin = require('fontmin')
const minimatch = require('minimatch')
const Promise = require('bluebird')
const streamToArrayAsync = Promise.promisify(require('stream-to-array'))
const options = Object.assign({}, hexo.config.fontmin)
function compressTTF () {
const routes = hexo.route.list().filter(function (path) {
return minimatch(path, '**/*.ttf', { nocase: true })
})
hexo.log.info('[fontmin] %d fonts found.', routes.length)
return Promise.map(routes, function (route) {
return streamToArrayAsync(hexo.route.get(route))
.then(function (arr) {
return Buffer.concat(arr)
})
.then(function (buffer) {
const fontmin = new Fontmin()
if (options.text) {
fontmin.use(Fontmin.glyph({
text: options.text
}))
}
fontmin.src(buffer)
return Promise.fromNode(function (cb) {
fontmin.run(cb)
})
.then(function (files) {
const file = files.shift()
const length = buffer.length
if (file && length > file.contents.length) {
hexo.route.set(route, file.contents)
hexo.log.info('[fontmin] %s (%d -> %d = -%s%)', route, length, file.contents.length, Number(file.contents.length * 100 / length).toFixed(2))
}
})
})
})
}
/* global hexo */
hexo.extend.filter.register('after_generate', compressTTF)