https://github.com/morganconrad/metalsmith-select
Temporarily process a subset of Metalsmith files with other plugins
https://github.com/morganconrad/metalsmith-select
filter metalsmith-plugin subset
Last synced: over 1 year ago
JSON representation
Temporarily process a subset of Metalsmith files with other plugins
- Host: GitHub
- URL: https://github.com/morganconrad/metalsmith-select
- Owner: MorganConrad
- License: mit
- Created: 2017-10-15T14:26:30.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2020-04-25T23:24:51.000Z (about 6 years ago)
- Last Synced: 2025-01-11T14:45:57.523Z (over 1 year ago)
- Topics: filter, metalsmith-plugin, subset
- Language: JavaScript
- Homepage:
- Size: 7.81 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](http://travis-ci.org/MorganConrad/metalsmith-select)
[](https://github.com/MorganConrad/metalsmith-select)
[](https://www.npmjs.org/package/metalsmith-select)
[](https://snyk.io/test/github/morganconrad/metalsmith-select)
[](https://coveralls.io/github/MorganConrad/metalsmith-select)
# metalsmith-select
Creates ("selects") a temporary subset of Metalsmith's filedata, on which you can use plugins. Then call `done()` to return to normal flow with all files. Useful if your plugins are very long, expensive, or conflicting.
**Warning**: If the plugins add or remove "files", **this wil not work**. It does work if they modify existing files, metalsmith, etc...
Functional Programmers might think of this as "`filter().forEach()`", but, unlike modules like [metalsmith-filter](https://www.npmjs.com/package/metalsmith-filter) it does not permanently change the original files object.
Procedural Programmers could think of this as "`if/then/else`": for all files, if they pass the selection criteria, then use the plugins.
```
const select = require('metalsmith-select');
...
.use(select(options)) // select a subset of files
.thenUse(plugin1(options1)) // plugin1 uses the subset
.thenUse(plugin2(options2)) // as does plugin2
.elseUse(yaPlugin(yaOptions)) // rejected files go through yaPlugin
.done()
.use(...) // back to original flow with full set of files
```
### options
Due to the special nature of this plugin, Javascript only, no CLI. (???)
Usually **options** is an object of key/testCriteria properties, e.g.
```
{
key1: testCriteria1,
key2: testCriteria2
}
```
For each property, `let valueToBeTested = fileData[key];` **Exception**: when the key is `__filename__`, use the filename.
The selection test depends on testCriteria.
boolean : whether valueToBeTested exists (or not)
RegExp : RegExp.test(valueToBeTested)
String : convert String to a RegExp and .test(valueToBeTested)
function: testCriteria(valueToBeTested, metalsmith)
Files that pass **all** of the tests get passed to `thenUse()`.
Rejected files get passed to `elseUse()`.
If **options** is null or {}, all files are accepted, say, if you want to use [metalsmith-filter](https://www.npmjs.com/package/metalsmith-filter) instead.
If **options** is a function, the selection criteria is `options(fileData, metalsmith)`. This gives you complete control for offbeat cases.
### Notes, Todos, and Caveats
Side Effect: **select** adds a `__filename__` property to every file object.
To simulate an OR, just `use(select())` twice:
```
use(select(options_left_half_of_OR))
.thenUse(somePlugin(itsOptions))
.use(select((options_right_half_of_OR))
.thenUse(somePlugin(itsOptions))
```
I haven't tested `select` all that much in practice, and it's doing tricky stuff, so beware.
### Examples
Only pass files with a field "usePrismJS" to metalsmith-prism
```
use(select({ usePrismJS: true })
.thenUse(metalsmithPrism())
```