Filter out files from further processing.
As the name suggest this is a hoast module.
Install hoast-filter using npm.
$ npm install hoast-filter
The parameters are given via a single object with the properties listed below.
engine
: A custom function can be specified to use for filtering with instead of the default pattern matching. The function can be asynchronous and gets given one argument the file data and requires one return value of type Boolean.- Type:
Function
- Required:
If pattern not specified
- Type:
patterns
: Glob patterns to match file paths with. If the engine function is set the file paths matches to the patterns it will be skipped and continue to be processed.- Type:
String
orArray of strings
- Required:
If engine not specified
- Type:
patternOptions
: Options for the glob pattern matching. See planckmatch options for more details on the pattern options.- Type:
Object
- Default:
{}
- Type:
patternOptions.all
: This options is added topatternOptions
, and determines whether all patterns need to match instead of only one.- Type:
Boolean
- Default:
false
- Type:
All files that match the glob patterns will be further processed after the filter call
Cli
{
"modules": {
"hoast-filter": {
"patterns": "*.md"
},
"read": {}
}
}
Script
const Hoast = require(`hoast`);
const read = Hoast.read,
filter = require(`hoast-filter`);
Hoast(__dirname)
.use(filter({
patterns: `*.md`
}))
.use(read())
.process();
In the example only the markdown files continue past the filter.
The filter can also be inverted whereby it will filter out files matching the glob patterns instead of only keeping those that match the patterns.
Cli
{
"modules": {
"hoast-filter": {
"patterns": [
"*"
"!(layouts/*)"
],
"patternOptions": {
"all": true,
"extended": true
}
},
"read": {}
}
}
Script
const Hoast = require(`hoast`);
const read = Hoast.read,
filter = require(`hoast-filter`);
Hoast(__dirname)
.use(filter({
patterns: [
`*`
`!(layouts/*)`
],
patternOptions: {
all: true,
extended: true
}
}))
.use(read())
.process();
In the example the layouts directory will be filter out from further processing.
The engine function can also be used to specify a custom filter based on the file's data.
CLI
engine
option is not compatible with the CLI tool as it requires a reference to a self specified function.
Script
const path = require(`path`);
const Hoast = require(`hoast`);
const read = Hoast.read,
filter = require(`hoast-filter`);
Hoast(__dirname)
.use(filter({
engine: function(file) {
return path.extname(file.path) === `.hbs`;
},
patterns: [
`*`
`!(layouts/*)`
],
patternOptions: {
all: true,
extended: true
}
}))
.use(read())
.process();
In the example any files within the layouts directory will be filtered out if they do not have the
.hbs
extension. All other files outside the layouts directory will remain part of the files array and be processed further.