This is no longer maintained, code is archived and available for forking though.
Infrastructure plugin for transforming page DOM via jsdom. Use this for small tweaks, or build your plugin on top of this one.
const domTransform = require('metalsmith-dom-transform');
metalsmith.use(
domTransform({
transforms: [
// Set target=_blank on all links
function(dom, file, {files, metalsmith}, done) {
Array.from(dom.querySelectorAll('a[href]')).forEach(link => {
link.target = '_blank';
});
done();
},
// Make all images 200px wide
function(dom, file, {files, metalsmith}, done) {
Array.from(dom.querySelectorAll('img')).forEach(img => {
img.width = 200;
});
done();
},
// Remove all <iframe> elements
function(dom, file, {files, metalsmith}, done) {
Array.from(dom.querySelectorAll('iframe')).forEach(iframe => {
iframe.remove();
});
done();
}
]
})
);
There is currently only one option:
transforms
: array of functions that serve as DOM transformations. Each function takes three arguments:dom
: The root of the DOM for that pagefile
: File path for the page being transformedinfo
: Object that holds the same arguments passed to all metalsmith plugins, namely:files
: Dictionary of all files being processed by this metalsmith instancemetalsmith
: Metalsmith instance
done
: Callback for transformation completion
Uses async
/await
, so requires a relatively recent (8.x or higher) version of node.
If you create a plugin on top of this one, add a pull request and add yours to this list.
2.0.1
: Upgrade dependencies2.0.0
: Change transform function parameters1.0.1
: Don't serialize HTML if nothing changed (may cause HTML output differences)1.0.0
: Addmetalsmith
parameter0.0.2
: Fix stupid bug where async did not work0.0.1
: Initial release
- metalsmith-batch-dom: Manipulations based on query selectors and Cheerio, no metadata, all transforms must be synchronous.