I am in need of a Gulp task that can iterate through all specified HTML documents and eliminate specific attributes (such as style=""). I initially attempted to accomplish this task the same way I would do it via the browser, but it seems that's not possible. Here is my proposed solution:
// Define a function to remove multiple attributes from an element
const discardAttributes = (element, ...attributes) =>
attributes.forEach((attribute) => element.removeAttribute(attribute));
// Apply the function to various elements
document.querySelectorAll("table, thead, tbody, tr, th, td").forEach((elem) => {
discardAttributes(elem, "cellspacing", "cellpadding", "width", "style");
});
My goal is now to encapsulate the above logic into a gulp.task like so:
const gulp = require("gulp");
gulp.task("clean", async () => {
gulp.src("src/*.html")
.pipe(discardAttributes())
.pipe(gulp.dest("dist"));
});
If there exists a plug-in that can help achieve this task, please recommend, but I am also interested in learning how to do it manually as shown above.
Would utilizing 'through2' be necessary for this implementation?
Thank you.