Skip to content

Commit

Permalink
BREAKING: resolves #140: substitute placeholders that resolve to true…
Browse files Browse the repository at this point in the history
… to their key name
  • Loading branch information
webketje committed Nov 6, 2024
1 parent 3b754ab commit 8e1d0e8
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 2 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ Every `permalinks()` instantiation supports the following options:
- duplicates - what to do when 2 files have the same target destination. See [Ensure files have unique URI's](#ensure-files-have-unique-uris)
- linksets, see [Defining linksets](#defining-linksets)
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'`.
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`).
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`).
### Matching files
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ const replace = ({ pattern, ...options }, data) => {
} else if (key === 'dirname') {
ret[key] = val
} else {
ret[key] = options.slug(val.toString())
ret[key] = options.slug((typeof val === 'boolean' ? key : val).toString())
}
}

Expand Down
Empty file.
Empty file.
Empty file.
6 changes: 6 additions & 0 deletions test/fixtures/booleans/src/fixed-permalink.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
blog: true
news: true
permalink: im-fixed
category: fixed
---
5 changes: 5 additions & 0 deletions test/fixtures/booleans/src/new-permalinks-release.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
blog: false
news: true
category: general
---
5 changes: 5 additions & 0 deletions test/fixtures/booleans/src/use-metalsmith-permalinks.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
blog: true
news: false
category: how-to
---
7 changes: 7 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ const fixtures = [
folder: 'shorthand',
options: ':title?'
},
{
message: 'should substitute booleans for their key name',
folder: 'booleans',
options: {
pattern: ':blog?/:news?/:category/:basename'
}
},
{
message: 'should format a date',
folder: 'date',
Expand Down

0 comments on commit 8e1d0e8

Please sign in to comment.