Skip to content

Commit

Permalink
Feature: accept single bool param to include/omit drafts from the output
Browse files Browse the repository at this point in the history
  • Loading branch information
webketje committed Jul 28, 2022
1 parent 509d086 commit 0183e74
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ extends:
- 'eslint:recommended'
- 'prettier'
parserOptions:
ecmaVersion: 2017
ecmaVersion: 2020
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
- Align core plugin setup, add test, run prettier [`29b03ff`](https://github.com/metalsmith/drafts/commit/29b03ffa8cf34b98502e1f99491cbaac9955ce34)

<!-- auto-changelog-above -->

## [1.1.0][] - 2021-07-13

- Finalize move to @metalsmith/drafts
Expand Down
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ Pass the plugin with any options to `Metalsmith.use`.
```js
const drafts = require('@metalsmith/drafts')

metalsmith.use(drafts())
metalsmith.use(drafts({ default: false })) // same as default
metalsmith.use(drafts()) // same as { include: false }
metalsmith.use(drafts(true)) // same as { include: true }
metalsmith.use(drafts({ default: false, include: false })) // same as default
```

Add `draft: true` to your files' YAML front-matter to mark them as drafts:
Expand All @@ -42,15 +43,12 @@ draft: true
---
```

To build pages that are marked as draft during development, use the metalsmith-if plugin to check the node environment and include the draft page in the build accordingly.
To build pages that are marked as draft during development, you can use the Node environment and include the draft page in the build accordingly.

```js
const when = require('metalsmith-if');
...
const isProduction = process.env.NODE_ENV === 'production';
...
.use(when(isProduction, drafts()))
const inDevelopment = process.env.NODE_ENV === 'development'

metalsmith.use(drafts(inDevelopment))
```

### Default value for `draft`
Expand Down
17 changes: 12 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/**
* @typedef {Object} Options
* @property {Boolean} [default=false]
* @property {Boolean} [include=true]
*/

const defaultOptions = {
default: false
default: false,
include: false
}

/**
Expand All @@ -27,14 +29,19 @@ function isDraft(value, fallback) {
* @param {Options} [options]
* @returns {import('metalsmith').Plugin}
*/
function configureDrafts(options) {
options = options instanceof Object ? Object.assign({}, defaultOptions, options) : defaultOptions
const draftFallback = isDraft(options.default, false)
function configureDrafts(options = defaultOptions) {
if (typeof options === 'boolean') {
options = { ...defaultOptions, include: options }
} else {
options = { ...defaultOptions, ...options }
}

console.log(options)
return function drafts(files, metalsmith, done) {
setImmediate(done)
if (options.include) return
Object.keys(files).forEach((path) => {
if (isDraft(files[path].draft, draftFallback)) {
if (isDraft(files[path].draft, options.default)) {
delete files[path]
}
})
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/bool-param/expected/draft.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

draft
1 change: 1 addition & 0 deletions test/fixtures/bool-param/expected/live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
live
5 changes: 5 additions & 0 deletions test/fixtures/bool-param/src/draft.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
draft: true
---

draft
1 change: 1 addition & 0 deletions test/fixtures/bool-param/src/live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
live
10 changes: 10 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ describe('@metalsmith/drafts', function () {
})
})

it('should allow a single (bool) param to include/omit drafts from output', function (done) {
Metalsmith('test/fixtures/bool-param')
.use(drafts(true))
.build(function (err) {
if (err) return done(err)
equal('test/fixtures/bool-param/expected', 'test/fixtures/bool-param/build')
done()
})
})

it('should accept numbers & strings as values to ease using env variables', function (done) {
Metalsmith('test/fixtures/input-types')
.use(drafts())
Expand Down

0 comments on commit 0183e74

Please sign in to comment.