feat: utilise better minify-xml

- replace pretty-data
This commit is contained in:
MDLeom 2020-09-02 11:23:20 +00:00
parent 9b94a2c2df
commit 8bbecf676f
No known key found for this signature in database
GPG Key ID: 06C236E63CBC68AA
4 changed files with 20 additions and 10 deletions

View File

@ -215,6 +215,8 @@ minify:
- **removeComments** - Remove [comments](https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction) in xml. Defaults to `true`. - **removeComments** - Remove [comments](https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction) in xml. Defaults to `true`.
- **globOptions** - See [globbing](#globbing) section. - **globOptions** - See [globbing](#globbing) section.
For more options, see [minify-xml](https://github.com/kristian/minify-xml#options).
## JSON ## JSON
Remove whitespaces in json. Remove whitespaces in json.

View File

@ -8,6 +8,7 @@ const zlib = require('zlib')
const { promisify } = require('util') const { promisify } = require('util')
const gzip = promisify(zlib.gzip) const gzip = promisify(zlib.gzip)
const br = promisify(zlib.brotliCompress) const br = promisify(zlib.brotliCompress)
const { minify: compressXml } = require('minify-xml')
const micromatch = require('micromatch') const micromatch = require('micromatch')
const isMatch = (path = '', patterns = [], options = {}) => { const isMatch = (path = '', patterns = [], options = {}) => {
@ -226,7 +227,7 @@ function minifyXml () {
const { route } = hexo const { route } = hexo
const routeList = route.list() const routeList = route.list()
const { globOptions, include, removeComments, verbose } = options const { globOptions, include, verbose } = options
return Promise.all((match(routeList, include, globOptions)).map((path) => { return Promise.all((match(routeList, include, globOptions)).map((path) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -236,15 +237,7 @@ function minifyXml () {
assetPath.on('end', () => { assetPath.on('end', () => {
if (assetTxt.length) { if (assetTxt.length) {
try { try {
/* ! const result = compressXml(assetTxt, { ...options })
* Regex patterns are adapted from pretty-data 0.50.0
* Licensed MIT (c) 2012-2017 Vadim Kiryukhin ( vkiryukhin @ gmail.com )
* https://github.com/vkiryukhin/pretty-data
*/
const text = removeComments
? assetTxt.replace(/<![ \r\n\t]*(--([^-]|[\r\n]|-[^-])*--[ \r\n\t]*)>/g, '')
: assetTxt
const result = text.replace(/>\s{0,}</g, '><')
if (verbose) logFn.call(this, assetTxt, result, path, 'xml') if (verbose) logFn.call(this, assetTxt, result, path, 'xml')
resolve(route.set(path, result)) resolve(route.set(path, result))
} catch (err) { } catch (err) {

View File

@ -26,6 +26,7 @@
"clean-css": "^4.2.1", "clean-css": "^4.2.1",
"html-minifier": "^4.0.0", "html-minifier": "^4.0.0",
"micromatch": "^4.0.2", "micromatch": "^4.0.2",
"minify-xml": "^2.1.1",
"svgo": "^1.2.2", "svgo": "^1.2.2",
"terser": "^4.0.0" "terser": "^4.0.0"
}, },

View File

@ -189,4 +189,18 @@ describe('xml', () => {
const result = await x() const result = await x()
expect(result.length).toBe(0) expect(result.length).toBe(0)
}) })
test('avoid processing CDATA', async () => {
const input = '<foo><![CDATA[<p>lorem</p>\n<p>ipsum</p>]]></foo>'
hexo.route.set(path, input)
await x()
const output = hexo.route.get(path)
let result = ''
output.on('data', (chunk) => (result += chunk))
output.on('end', () => {
expect(result).toBe(input)
})
})
}) })