Merge pull request #25 from curbengh/minify-xml

feat: utilise better minify-xml
This commit is contained in:
MDLeom 2020-09-03 14:37:16 +09:30 committed by GitHub
commit c2bcaa7d69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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`.
- **globOptions** - See [globbing](#globbing) section.
For more options, see [minify-xml](https://github.com/kristian/minify-xml#options).
## JSON
Remove whitespaces in json.

View File

@ -8,6 +8,7 @@ const zlib = require('zlib')
const { promisify } = require('util')
const gzip = promisify(zlib.gzip)
const br = promisify(zlib.brotliCompress)
const { minify: compressXml } = require('minify-xml')
const micromatch = require('micromatch')
const isMatch = (path = '', patterns = [], options = {}) => {
@ -226,7 +227,7 @@ function minifyXml () {
const { route } = hexo
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 new Promise((resolve, reject) => {
@ -236,15 +237,7 @@ function minifyXml () {
assetPath.on('end', () => {
if (assetTxt.length) {
try {
/* !
* 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, '><')
const result = compressXml(assetTxt, { ...options })
if (verbose) logFn.call(this, assetTxt, result, path, 'xml')
resolve(route.set(path, result))
} catch (err) {

View File

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

View File

@ -189,4 +189,18 @@ describe('xml', () => {
const result = await x()
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)
})
})
})