remove body content of index.html using gulp

1.2k views Asked by At

I can able to rename file name from index. using following configuration

Gulp.js

var rename = require("gulp-rename");

gulp.task('modify-html', function () {
  gulp.src('reports/index.html')
    .pipe(rename('/app.html'))
    .pipe(gulp.dest('reports/'));
});

Does anyone know how to delete the body content of my html file using gulp

index.html

<html>
<body>

<h1>My First Heading</h1>
<p>My first paragraph.</p>

</body>
</html>
2

There are 2 answers

2
Satpal On BEST ANSWER

You can use gulp-html-replace it replaces the HTML block.

Mark the content which needs to be removed

<!-- build:remove -->
Put all the content which needs to be removed, in the block
<!-- endbuild -->

and use it like

var htmlreplace = require('gulp-html-replace')
gulp.task('modify-html', function () {
  gulp.src('reports/index.html')
    .pipe(htmlreplace({ remove : '' }))
    .pipe(rename('/app.html'))
    .pipe(gulp.dest('reports/'));
});

Then, You can use gulp-dom

var dom = require('gulp-dom');
gulp.task('modify-html', function () {
    gulp.src('reports/index.html')
    .pipe(dom(function () {
            this.querySelector('body').innerHTML = '';
            return this;
        }))
    .pipe(rename('/app.html'))
    .pipe(gulp.dest('reports/'));
});

Note: I have not tested it.

0
Tristanisginger On

Use gulp-string-replace

var replace = require('gulp-string-replace');

gulp.task('clearBody', function (done) {
        
    gulp.src('file.html')
    .pipe(replace(new RegExp('<body>(.*?)<\/body>', 'm'), '<body><\/body>'))
    .pipe(gulp.dest('newFile.html'));
           
    done();
});