我正在寻找能够删除所有<div>
代码和结束代码的Gulp模块,而无需移除所述<div>
的内容/子代。
这样的事情存在吗?
示例代码:
<div id="MACONTAINER" contenteditable="true" maconstraint="ITEM.IMAGE1 <> ''" mashowalllanguages="FALSE">
<tr>
<td class="img">
<div id="MASECTION" contenteditable="true" maconstraint="ITEM.URL1 <> '' AND ITEM.PROMOCODE1 <> ''" mashowalllanguages="FALSE">
<a href="~PROBE(101)~" title="~ITEM.ALT1~">
<img width="580" height="~ITEM.IMAGE1HEIGHT~" alt="~ITEM.ALT1~" src="~ITEM.IMAGE1~" />
</a>
</div>
<div id="MASECTION" contenteditable="true" maconstraint="ITEM.URL1 <> '' AND ITEM.PROMOCODE1 = ''" mashowalllanguages="FALSE">
<a href="~PROBE(105)~" title="~ITEM.ALT1~">
<img width="580" height="~ITEM.IMAGE1HEIGHT~" alt="~ITEM.ALT1~" src="~ITEM.IMAGE1~" />
</a>
</div>
<div id="MASECTION" contenteditable="true" maconstraint="ITEM.URL1 = ''" mashowalllanguages="FALSE">
<img width="580" height="~ITEM.IMAGE1HEIGHT~" alt="~ITEM.ALT1~" src="~ITEM.IMAGE1~" />
</div>
</td>
</tr>
</div>
所需代码:
<tr>
<td class="img">
<a href="~PROBE(101)~" title="~ITEM.ALT1~">
<img width="580" height="~ITEM.IMAGE1HEIGHT~" alt="~ITEM.ALT1~" src="~ITEM.IMAGE1~" />
</a>
<a href="~PROBE(105)~" title="~ITEM.ALT1~">
<img width="580" height="~ITEM.IMAGE1HEIGHT~" alt="~ITEM.ALT1~" src="~ITEM.IMAGE1~" />
</a>
<img width="580" height="~ITEM.IMAGE1HEIGHT~" alt="~ITEM.ALT1~" src="~ITEM.IMAGE1~" />
</td>
</tr>
答案 0 :(得分:0)
您可以使用gulp-replace等使用正则表达式的插件:
var gulp = require('gulp'),
replace = require('gulp-replace');
gulp.task('strip-tags', function() {
return gulp.src('template.html')
.pipe(replace(/<div.*?id=.MACONTAINER..*>((?:.|\s)+)<\/div>/im, '$1'))
.pipe(gulp.dest('./dist'));
});
但是正则表达式对于那种任务来说有点脆弱......
你也可以推出自己的&#34;插件&#34;并这样做:
var gulp = require('gulp'),
through = require('through2'),
cheerio = require('cheerio');
gulp.task('strip-tags2', function() {
return gulp.src('template.html')
.pipe(through.obj(function(file, enc, cb) {
var $ = cheerio.load(file.contents),
extract = $('#MACONTAINER').children().html();
file.contents = new Buffer(extract);
cb(null, file);
}))
.pipe(gulp.dest('./dist'));
});