@metalsmith/collections 1.2.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/README.md +4 -4
- package/lib/index.js +24 -12
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
### Changelog
|
|
2
|
+
#### [v1.2.1](https://github.com/metalsmith/collections/compare/v1.2.1...v1.2.0) / 2022-02-03
|
|
2
3
|
|
|
4
|
+
- Fixes [`#99`](https://github.com/metalsmith/collections/issues/99): collection key on file metadata - no dupes, no nested arrays
|
|
5
|
+
- Fixes regression: incorrect previous & next refs when reverse: true
|
|
6
|
+
- Fixes typo's in README
|
|
3
7
|
#### [v1.2.0](https://github.com/metalsmith/collections/compare/v1.2.0...v1.1.0) / 2022-01-29
|
|
4
8
|
|
|
5
9
|
- Feature: sortBy now also understands nested metadata properties, e.g. `sortBy: 'meta.display.order'`
|
package/README.md
CHANGED
|
@@ -93,12 +93,12 @@ There are 2 ways to create collections & they can be used together:
|
|
|
93
93
|
news: {
|
|
94
94
|
metadata: {
|
|
95
95
|
title: 'Latest news',
|
|
96
|
-
description: 'All the latest in politics & world news'
|
|
96
|
+
description: 'All the latest in politics & world news',
|
|
97
97
|
slug: 'news'
|
|
98
98
|
},
|
|
99
99
|
pattern: 'news/**/*.html',
|
|
100
100
|
sortBy: 'pubdate',
|
|
101
|
-
reverse: true
|
|
101
|
+
reverse: true
|
|
102
102
|
},
|
|
103
103
|
services: 'services/**/*.html'
|
|
104
104
|
})
|
|
@@ -147,7 +147,7 @@ Here is an example of using [@metalsmith/layouts](https://github.com/metalsmith/
|
|
|
147
147
|
|
|
148
148
|
```handlebars
|
|
149
149
|
<h1>{{ title }}</h1> {{!-- something-happened.md title --}}
|
|
150
|
-
<a href="/{{ collections.news.slug }}">Back to news</a> {{!-- news collection metadata.slug --}}
|
|
150
|
+
<a href="/{{ collections.news.metadata.slug }}">Back to news</a> {{!-- news collection metadata.slug --}}
|
|
151
151
|
{{ contents | safe }}
|
|
152
152
|
<hr>
|
|
153
153
|
{{!-- previous & next are added by @metalsmith/collections --}}
|
|
@@ -260,7 +260,7 @@ metalsmith.use(
|
|
|
260
260
|
news: {
|
|
261
261
|
metadata: {
|
|
262
262
|
title: 'Latest news',
|
|
263
|
-
description: 'All the latest in politics & world news'
|
|
263
|
+
description: 'All the latest in politics & world news',
|
|
264
264
|
slug: 'news'
|
|
265
265
|
}
|
|
266
266
|
}
|
package/lib/index.js
CHANGED
|
@@ -80,7 +80,7 @@ function normalizeOptions(options) {
|
|
|
80
80
|
function initializeCollections(options) {
|
|
81
81
|
options = normalizeOptions(options)
|
|
82
82
|
const collectionNames = Object.keys(options)
|
|
83
|
-
const
|
|
83
|
+
const mappedCollections = collectionNames.map((name) => {
|
|
84
84
|
return Object.assign({ name: name }, options[name])
|
|
85
85
|
})
|
|
86
86
|
|
|
@@ -103,15 +103,15 @@ function initializeCollections(options) {
|
|
|
103
103
|
if (!collectionNames.includes(name)) {
|
|
104
104
|
collectionNames.push(name)
|
|
105
105
|
const normalized = Object.assign({}, defaultOptions)
|
|
106
|
-
|
|
106
|
+
mappedCollections.push(Object.assign({ name }, normalized))
|
|
107
107
|
}
|
|
108
108
|
})
|
|
109
109
|
}
|
|
110
110
|
})
|
|
111
111
|
|
|
112
|
-
debug('Identified %s collections: %s',
|
|
112
|
+
debug('Identified %s collections: %s', mappedCollections.length, collectionNames.join())
|
|
113
113
|
|
|
114
|
-
|
|
114
|
+
mappedCollections.forEach((collection) => {
|
|
115
115
|
const { pattern, filterBy, sortBy, reverse, refer, limit } = collection
|
|
116
116
|
const name = collection.name
|
|
117
117
|
const matches = []
|
|
@@ -126,9 +126,12 @@ function initializeCollections(options) {
|
|
|
126
126
|
// pattern-matched files might or might not have a "collection" property defined in front-matter
|
|
127
127
|
// and might also be included in multiple collections
|
|
128
128
|
if (!data.collection) {
|
|
129
|
-
data.collection =
|
|
130
|
-
} else {
|
|
131
|
-
data.collection = [data.collection
|
|
129
|
+
data.collection = []
|
|
130
|
+
} else if (typeof data.collection === 'string') {
|
|
131
|
+
data.collection = [data.collection]
|
|
132
|
+
}
|
|
133
|
+
if (!data.collection.includes(collection.name)) {
|
|
134
|
+
data.collection = [...data.collection, collection.name]
|
|
132
135
|
}
|
|
133
136
|
return data
|
|
134
137
|
})
|
|
@@ -163,12 +166,21 @@ function initializeCollections(options) {
|
|
|
163
166
|
metadata[name].metadata = collection.metadata
|
|
164
167
|
}
|
|
165
168
|
if (refer) {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
169
|
+
if (reverse) {
|
|
170
|
+
metadata[name].forEach((file, i) => {
|
|
171
|
+
Object.assign(file, {
|
|
172
|
+
next: i > 0 ? metadata[name][i - 1] : null,
|
|
173
|
+
previous: i < metadata[name].length - 1 ? metadata[name][i + 1] : null
|
|
174
|
+
})
|
|
170
175
|
})
|
|
171
|
-
}
|
|
176
|
+
} else {
|
|
177
|
+
metadata[name].forEach((file, i) => {
|
|
178
|
+
Object.assign(file, {
|
|
179
|
+
previous: i > 0 ? metadata[name][i - 1] : null,
|
|
180
|
+
next: i < metadata[name].length - 1 ? metadata[name][i + 1] : null
|
|
181
|
+
})
|
|
182
|
+
})
|
|
183
|
+
}
|
|
172
184
|
}
|
|
173
185
|
|
|
174
186
|
metadata.collections[name] = metadata[name]
|