hexo-yam/lib/json.js

36 lines
998 B
JavaScript
Raw Normal View History

2024-12-02 08:48:09 +00:00
'use strict'
const { match, logFn } = require('./tools')
function minifyJson() {
const hexo = this
const options = hexo.config.minify.json
const route = hexo.route
2024-12-02 08:48:09 +00:00
/** @type {string[]} */
const routeList = route.list()
const { globOptions, include, verbose } = options
2024-12-02 08:48:09 +00:00
return Promise.all((match(routeList, include, globOptions)).map(path => {
return new Promise((/** @type {(value: void) => void} */ resolve, reject) => {
2024-12-02 08:48:09 +00:00
const assetPath = route.get(path)
let assetTxt = ''
assetPath.on('data', chunk => (assetTxt += chunk))
assetPath.on('end', () => {
if (assetTxt.length) {
try {
const result = JSON.stringify(JSON.parse(assetTxt))
if (verbose) logFn.call(this, assetTxt, result, path, 'json')
route.set(path, result)
2024-12-02 08:48:09 +00:00
} catch (err) {
reject(new Error(`Path: ${path}\n${err}`))
}
}
resolve()
2024-12-02 08:48:09 +00:00
})
})
}))
}
module.exports = {
minifyJson
}