From 8bbecf676f763dca6f841c1790efb7c56d305511 Mon Sep 17 00:00:00 2001
From: MDLeom <43627182+curbengh@users.noreply.github.com>
Date: Wed, 2 Sep 2020 11:23:20 +0000
Subject: [PATCH] feat: utilise better minify-xml
- replace pretty-data
---
README.md | 2 ++
lib/filter.js | 13 +++----------
package.json | 1 +
test/xml.test.js | 14 ++++++++++++++
4 files changed, 20 insertions(+), 10 deletions(-)
diff --git a/README.md b/README.md
index 6837d91..891ab3c 100644
--- a/README.md
+++ b/README.md
@@ -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.
diff --git a/lib/filter.js b/lib/filter.js
index 7cb9381..6eb86a7 100644
--- a/lib/filter.js
+++ b/lib/filter.js
@@ -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(//g, '')
- : assetTxt
- const result = text.replace(/>\s{0,}<')
+ const result = compressXml(assetTxt, { ...options })
if (verbose) logFn.call(this, assetTxt, result, path, 'xml')
resolve(route.set(path, result))
} catch (err) {
diff --git a/package.json b/package.json
index 1307e69..00cfa90 100644
--- a/package.json
+++ b/package.json
@@ -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"
},
diff --git a/test/xml.test.js b/test/xml.test.js
index e6abd1e..5a3c185 100644
--- a/test/xml.test.js
+++ b/test/xml.test.js
@@ -189,4 +189,18 @@ describe('xml', () => {
const result = await x()
expect(result.length).toBe(0)
})
+
+ test('avoid processing CDATA', async () => {
+ const input = '
ipsum
]]>' + 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) + }) + }) })