繁体   English   中英

使用 gulp 删除 index.html 的正文内容

[英]remove body content of index.html using gulp

我可以从索引中重命名文件名。 使用以下配置

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/'));
});

有谁知道如何使用 gulp 删除我的 html 文件的正文内容

index.html

<html>
<body>

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

</body>
</html>

您可以使用gulp-html-replace替换HTML块。

标记需要删除的内容

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

并像这样使用

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/'));
});

然后,您可以使用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/'));
});

注意:我尚未测试。

使用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();
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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