Skip to content

Commit 2de5d7d

Browse files
committed
Resolves #140: substitute placeholders that resolve to true to their key name
Warning: this change breaks the undocumented (and so, unsupported) case in which a key resolving to a boolean true, was actually intended to generate a 'true' permalink path part.
1 parent da328f0 commit 2de5d7d

File tree

9 files changed

+27
-2
lines changed

9 files changed

+27
-2
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ Every `permalinks()` instantiation supports the following options:
7474
- duplicates - what to do when 2 files have the same target destination. See [Ensure files have unique URI's](#ensure-files-have-unique-uris)
7575
- linksets, see [Defining linksets](#defining-linksets)
7676
77-
Placeholder substitution will always `toString` the value. For example, when you have an `:array` placeholder and a file with front-matter `array: ['one','two']`, it will substitute into `'onetwo'`, but you can refer to the n<sup>th</sup> value with a dot-delimited keypath (eg `:array.0`). A boolean `false` will result in an error unless the placeholder is optional (it would then be an empty string = omitted), a boolean `true` will result in the string `'true'`.
77+
Placeholder substitution will mostly `toString` the value. For example, when you have an `:array` placeholder and a file with front-matter `array: ['one','two']`, it will substitute into `'onetwo'`, but you can refer to the n<sup>th</sup> value with a dot-delimited keypath (eg `:array.0`).
78+
79+
A boolean `false` will result in an error unless the placeholder is optional (it would then be an empty string = omitted), **but a boolean `true` will see the placeholder substituted with its key name** (eg `:placeholder` for a file with front-matter `placeholder: true` will become `placeholder`).
7880
7981
### Matching files
8082

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ const replace = ({ pattern, ...options }, data) => {
208208
} else if (key === 'dirname') {
209209
ret[key] = val
210210
} else {
211-
ret[key] = options.slug(val.toString())
211+
ret[key] = options.slug((typeof val === 'boolean' ? key : val).toString())
212212
}
213213
}
214214

test/fixtures/booleans/expected/blog/how-to/use-metalsmith-permalinks/index.html

Whitespace-only changes.

test/fixtures/booleans/expected/im-fixed/index.html

Whitespace-only changes.

test/fixtures/booleans/expected/news/general/new-permalinks-release/index.html

Whitespace-only changes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
blog: true
3+
news: true
4+
permalink: im-fixed
5+
category: fixed
6+
---
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
blog: false
3+
news: true
4+
category: general
5+
---
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
blog: true
3+
news: false
4+
category: how-to
5+
---

test/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ const fixtures = [
4747
folder: 'shorthand',
4848
options: ':title?'
4949
},
50+
{
51+
message: 'should substitute booleans for their key name',
52+
folder: 'booleans',
53+
options: {
54+
pattern: ':blog?/:news?/:category/:basename'
55+
}
56+
},
5057
{
5158
message: 'should format a date',
5259
folder: 'date',

0 commit comments

Comments
 (0)