Processes HTML pages and modifies dom elements using Cheerio. Provides a config style and standard cheerio api to manipulate dom element attributes and classes.
$ npm install metalsmith-batch-dom --save-dev
Pass with options to Metalsmith#use
:
Dom manipulation can be defined using an object where the key is the dom selector and value is an object of functions keys and arguments values.
var dom = require('metalsmith-batch-dom');
metalsmith.use(dom({
'a': {
filter: function() {
var href = $(this).attr('href');
return href.indexOf('http://www.google.com') !== -1
&& href.indexOf('http://google.com') !== -1;
},
attr: {
rel: 'external',
target: '_blank',
},
addClass: 'is-external'
}
}))
While in json config mode, filtering type functions (filter, reject, eq) are preferred before manipulation type functions (attr, addClass). If you need to customize this behavior, use the function style configuration.
A function can be used as options. The function is called for each file with the cheerio html, file metadata, and filename as arguments.
var dom = require('metalsmith-batch-dom');
metalsmith.use(dom(function($, metadata, filename) {
$('a').each(function() {
var link = $(this);
link.attr('href', '#!' + link.attr('href'));
})
}));
MIT