【问题标题】:remove body content of index.html using gulp使用 gulp 删除 index.html 的正文内容
【发布时间】:2017-08-30 06:08:48
【问题描述】:

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

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>

【问题讨论】:

    标签: javascript html node.js gulp gulp-rename


    【解决方案1】:

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

    注意:我没有测试过。

    【讨论】:

    • 感谢您的回答,但我想使用脚本本身删除正文内容,对于上述内容,我必须手动添加一些额外的代码,以便从 html 中删除正文内容
    • ,很棒的工作,非常感谢,现在工作正常
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-31
      • 1970-01-01
      • 1970-01-01
      • 2017-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多