WDataHtmlMinify.mjs

import isestr from 'wsemi/src/isestr.mjs'
// import htmlminify from 'html-minifier'
import htmlminify from './importHtmlminify.mjs' //html-minifier於npm上未提供dist, 故須於github自行下載
// console.log(htmlminify)


/**
 * 壓縮HTML
 *
 * @param {String} h 輸入HTML字串
 * @param {Object} [opt={}] 輸入設定物件,預設{}
 * @return {String} 回傳Promise,resolve回傳ltdt(各數據列為物件陣列),reject回傳錯誤訊息
 * @example
 *
 * let hin = `
 * <html>
 *     <head></head>
 *     <body>abc</body>
 * </html>
 * `
 * let hout = wdhm(hin)
 * console.log(hout)
 * // => <html><head></head><body>abc</body></html>
 *
 */
function WDataHtmlMinify(h, opt = {}) {

    //check
    if (!isestr(h)) {
        return ''
    }

    let optDef = {
        includeAutoGeneratedTags: true,
        removeAttributeQuotes: false, //因要將svg視為html處理, 故不能使用移除雙引號
        removeComments: true,
        removeRedundantAttributes: true,
        removeScriptTypeAttributes: true,
        removeStyleLinkTypeAttributes: true,
        sortClassName: true,
        useShortDoctype: true,
        collapseWhitespace: true
    }
    let optUse = {
        ...optDef,
        ...opt,
    }

    //htmlminify
    let hh = ''
    try {
        hh = htmlminify(h, optUse)
    }
    catch (err) {
        hh = err.toString()
    }

    return hh
}


export default WDataHtmlMinify