metalsmith-collections/Readme.md

117 lines
2.6 KiB
Markdown
Raw Normal View History

2014-02-05 15:06:46 -08:00
# metalsmith-collections
2014-03-06 15:03:08 -08:00
A [Metalsmith](https://github.com/segmentio/metalsmith) plugin that lets you group files together into an ordered collection, like blog posts. That way you can loop over them to generate an index, or add 'next' and 'previous' links between them.
2014-03-06 14:44:56 -08:00
## Features
- can match files by `collection` metadata
- can match files by pattern
2014-03-06 14:53:09 -08:00
- adds collections to global metadata
- adds `next` and `previous` references to each file in the collection
2014-02-05 15:06:46 -08:00
## Installation
$ npm install metalsmith-collections
2014-03-06 14:50:46 -08:00
## Usage
2014-03-06 14:44:56 -08:00
2014-03-06 14:50:46 -08:00
There are two ways to create collections:
2014-03-06 14:44:56 -08:00
- **by pattern** - this is just passing a globing pattern that will group any files that match into the same collection.
- **by metadata** - this is adding a specific `collection` metadata field to each item that you want to add to a collection.
2014-03-06 14:50:46 -08:00
The simplest way to create a collection is to use a pattern to match the files you want to group together:
2014-03-06 14:44:56 -08:00
2014-03-06 14:50:46 -08:00
```js
var collections = require('metalsmith-collections');
metalsmith.use(collections({
articles: '*.md'
}));
2014-03-06 14:44:56 -08:00
```
2014-03-06 14:50:46 -08:00
Which is just a shorthand. You could also add additional options:
2014-03-06 14:44:56 -08:00
2014-03-06 14:51:06 -08:00
```js
2014-03-06 14:50:46 -08:00
metalsmith.use(collections({
articles: {
pattern: '*.md',
sortBy: 'date',
reverse: true
2014-03-06 14:44:56 -08:00
}
2014-03-06 14:50:46 -08:00
}));
2014-03-06 14:44:56 -08:00
```
2014-03-06 14:50:46 -08:00
But you can also match based on a `collection` property in each file's metadata by omitting a pattern, and adding the property to your files:
2014-02-05 15:06:46 -08:00
2014-03-06 14:51:06 -08:00
```js
2014-03-06 14:50:46 -08:00
metalsmith.use(collections({
articles: {
sortBy: 'date',
reverse: true
2014-02-05 15:06:46 -08:00
}
2014-03-06 14:50:46 -08:00
}));
2014-02-05 15:06:46 -08:00
```
```markdown
---
title: My Article
collection: articles
date: 2013-02-21
---
My article contents...
```
2014-03-06 14:50:46 -08:00
All of the files with a matching `collection` will be added to an array that is exposed as a key of the same name on the global Metalsmith `metadata`.
2014-10-13 13:09:41 +02:00
You can omit passing any options to the plugin when matching based on a `collection` property.
2014-02-05 15:06:46 -08:00
2014-09-08 13:34:06 -04:00
### Collection Metadata
Additional metadata can be added to the collection object.
```js
metalsmith.use(collections({
articles: {
sortBy: 'date',
reverse: true,
metadata: {
name: 'Articles',
description: 'The Articles listed here...'
}
}
}));
```
Collection metadata can also be assigned from a `json` or `yaml` file.
```js
metalsmith.use(collections({
articles: {
sortBy: 'date',
reverse: true,
metadata: 'path/to/file.json'
}
}));
```
2014-03-06 14:50:46 -08:00
## CLI Usage
2014-02-05 15:06:46 -08:00
2014-03-06 14:50:46 -08:00
All of the same options apply, just add them to the `"plugins"` key in your `metalsmith.json` configuration:
2014-02-05 15:06:46 -08:00
2014-03-06 14:50:46 -08:00
```json
{
"plugins": {
"metalsmith-collections": {
"articles": {
"sortBy": "date",
"reverse": true
}
}
}
}
2014-02-05 15:06:46 -08:00
```
## License
2014-03-06 15:03:08 -08:00
MIT