Skip to content

Commit 46b2db9

Browse files
committed
Documents potential pitfall when setting default object
1 parent 6207037 commit 46b2db9

File tree

1 file changed

+39
-4
lines changed

1 file changed

+39
-4
lines changed

README.md

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,41 @@ You can set a default at a file's nested keypath:
9595
metalsmith.use(
9696
defaultValues({
9797
pattern: '**/*.md',
98-
pubdate(file) { return new Date() }
99-
'config.scripts.app': '/app.js',
98+
defaults: {
99+
pubdate(file) { return new Date() }
100+
'config.scripts.app': '/app.js'
101+
}
102+
})
103+
)
104+
```
105+
106+
#### Setting default objects
107+
108+
If you assign a default object like this:
109+
110+
```js
111+
metalsmith.use(defaultValues({ defaults: { someObject: { id: 'some' } } }))
112+
```
113+
114+
All files to which the value was set will refer to the same object (`files['index.html'].someObject === files['other.html'].someObject`). If the object needs to be unique for each file, use a default setter function or specify each property as a keypath:
115+
116+
```js
117+
// using a function
118+
metalsmith.use(
119+
defaultValues({
120+
defaults: {
121+
someObject: () => ({ id: 'some', other: true })
122+
}
123+
})
124+
)
125+
126+
// using keypaths
127+
metalsmith.use(
128+
defaultValues({
129+
defaults: {
130+
'someObject.id': 'some',
131+
'someObject.other': true
132+
}
100133
})
101134
)
102135
```
@@ -109,8 +142,10 @@ You can set a file's default contents (which is a Node buffer) and any other Buf
109142
metalsmith.use(
110143
defaultValues({
111144
pattern: '**/*.md',
112-
strategy: 'overwrite',
113-
contents: Buffer.from('TO DO')
145+
defaults: {
146+
strategy: 'overwrite',
147+
contents: Buffer.from('TO DO')
148+
}
114149
})
115150
)
116151
```

0 commit comments

Comments
 (0)