Necesito una tarea de Gulp que revisará todos los documentos HTML asignados y eliminará ciertos atributos (como estilo=""). Pensé que podría haberlo hecho de la misma manera que lo hago a través del navegador, pero parece que no. Esto es lo que estoy tratando de hacer:
// function to take multiple attributes from an element const discardAttributes = (element, ...attributes) => attributes.forEach((attribute) => element.removeAttribute(attribute)); // run the function on multiple elements document.querySelectorAll("table, thead, tbody, tr, th, td").forEach((elem) => { discardAttributes(elem, "cellspacing", "cellpadding", "width", "style"); });Luego me gustaría tomar la fórmula anterior y crear un gulp.task así:
const gulp = require("gulp"); gulp.task("clean", async () => { gulp.src("src/*.html") .pipe(discardAttributes()) .pipe(gulp.dest("dist")); });Si hay un complemento que pueda usar que haga esto, por favor comparta, pero también, me gustaría aprender cómo hacerlo manualmente de esta manera.
¿Necesitaría usar through2?
Gracias.
Puede usar alguna biblioteca de nodo dom y envolverla con trago. Por ejemplo, podría probar jsdom y wrapper gulp-dom:
const gulp = require('gulp'); const dom = require("gulp-dom"); gulp.task("default", async () => { gulp.src("src/*.html") .pipe(dom(function() { // function to take multiple attributes from an element const discardAttributes = (element, ...attributes) => attributes.forEach((attribute) => element.removeAttribute(attribute)); // run the function on multiple elements return this.querySelectorAll("table, thead, tbody, tr, th, td").forEach((elem) => { discardAttributes(elem, "cellspacing", "cellpadding", "width", "style"); }); })) .pipe(gulp.dest("dist")); });