2

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

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 文件的正文内容

索引.html

<html>
<body>

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

</body>
</html>
4

2 回答 2

2

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

注意:我没有测试过。

于 2017-08-30T06:14:14.963 回答
0

利用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();
});
于 2022-02-09T14:38:00.350 回答