简体   繁体   中英

How to use cheerio.load in .pipe (cheerio (function ($) {?

I found an HTML compression gulp plugin that I want to use. I encountered a coding problem.

This is written in someone else's plugin:

gulp.task ('indexHtml', function () {
    return gulp.src ('index.html')
        .pipe (cheerio (function ($) {
            $ ('script'). remove ();
            $ ('link'). remove ();
            $ ('body'). append ('<script src = "skin / zhuce / MergeMin / app.full.min.js"> </ script>');
            $ ('head'). append ('<link rel = "stylesheet" href = "skin / zhuce / MergeMin / app.full.min.css">');
        })))
        .pipe (gulp.dest ('test /'));
});

But the output is unified Unicode. Cause some characters cannot be read only in the browser to explain.

I searched and found a solution that others pointed out:

Use cheerio.load (html, {decodeEntities: false});

Incoming parameters

But I do not know how to do it in Node.js.

I'm facing this nesting in the above code:

.pipe (cheerio (function ($) {

I do not know how to change: cheerio.load (html, {decodeEntities: false}); .

Assuming you are using gulp-cheerio, you need to change:

.pipe (cheerio (function ($) {
        $ ('script'). remove ();
        $ ('link'). remove ();
        $ ('body'). append ('<script src = "skin / zhuce / MergeMin / app.full.min.js"> </ script>');
        $ ('head'). append ('<link rel = "stylesheet" href = "skin / zhuce / MergeMin / app.full.min.css">');
    })))

To:

.pipe (cheerio ({
    run: (function ($) {
        $ ('script'). remove ();
        $ ('link'). remove ();
        $ ('body'). append ('<script src = "skin / zhuce / MergeMin / app.full.min.js"> </ script>');
        $ ('head'). append ('<link rel = "stylesheet" href = "skin / zhuce / MergeMin / app.full.min.css">');
    }))),
    parserOptions: {
    decodeEntities: false,
    //other options here
  }
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM