2

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>
Jenson Raby
  • 769
  • 2
  • 6
  • 26

2 Answers2

2

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.

Satpal
  • 132,252
  • 13
  • 159
  • 168
  • Thanks for your answer, but i want to remove the body content using script itself, For the above one i have to add manually some extra code which necessary to remove body content from html – Jenson Raby Aug 30 '17 at 06:29
  • 1
    ,awesome work, thank you very much, its working fine right now – Jenson Raby Aug 30 '17 at 09:01
0

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();
});
Tristanisginger
  • 2,181
  • 4
  • 28
  • 41