From 0183e740a74e929e080520b0f425f0687e0ce9d1 Mon Sep 17 00:00:00 2001 From: Kevin Van Lierde Date: Thu, 28 Jul 2022 20:52:18 +0200 Subject: [PATCH] Feature: accept single bool param to include/omit drafts from the output --- .eslintrc.yml | 2 +- CHANGELOG.md | 1 + README.md | 14 ++++++-------- lib/index.js | 17 ++++++++++++----- test/fixtures/bool-param/expected/draft.md | 2 ++ test/fixtures/bool-param/expected/live.md | 1 + test/fixtures/bool-param/src/draft.md | 5 +++++ test/fixtures/bool-param/src/live.md | 1 + test/index.js | 10 ++++++++++ 9 files changed, 39 insertions(+), 14 deletions(-) create mode 100644 test/fixtures/bool-param/expected/draft.md create mode 100644 test/fixtures/bool-param/expected/live.md create mode 100644 test/fixtures/bool-param/src/draft.md create mode 100644 test/fixtures/bool-param/src/live.md diff --git a/.eslintrc.yml b/.eslintrc.yml index 09d8941..551384b 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -5,4 +5,4 @@ extends: - 'eslint:recommended' - 'prettier' parserOptions: - ecmaVersion: 2017 + ecmaVersion: 2020 diff --git a/CHANGELOG.md b/CHANGELOG.md index 46191a9..eac5943 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) + ## [1.1.0][] - 2021-07-13 - Finalize move to @metalsmith/drafts diff --git a/README.md b/README.md index 54f676f..0d29f00 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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` diff --git a/lib/index.js b/lib/index.js index b862d03..7d2b680 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,10 +1,12 @@ /** * @typedef {Object} Options * @property {Boolean} [default=false] + * @property {Boolean} [include=true] */ const defaultOptions = { - default: false + default: false, + include: false } /** @@ -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] } }) diff --git a/test/fixtures/bool-param/expected/draft.md b/test/fixtures/bool-param/expected/draft.md new file mode 100644 index 0000000..2303103 --- /dev/null +++ b/test/fixtures/bool-param/expected/draft.md @@ -0,0 +1,2 @@ + +draft \ No newline at end of file diff --git a/test/fixtures/bool-param/expected/live.md b/test/fixtures/bool-param/expected/live.md new file mode 100644 index 0000000..5fce625 --- /dev/null +++ b/test/fixtures/bool-param/expected/live.md @@ -0,0 +1 @@ +live \ No newline at end of file diff --git a/test/fixtures/bool-param/src/draft.md b/test/fixtures/bool-param/src/draft.md new file mode 100644 index 0000000..9e60072 --- /dev/null +++ b/test/fixtures/bool-param/src/draft.md @@ -0,0 +1,5 @@ +--- +draft: true +--- + +draft \ No newline at end of file diff --git a/test/fixtures/bool-param/src/live.md b/test/fixtures/bool-param/src/live.md new file mode 100644 index 0000000..5fce625 --- /dev/null +++ b/test/fixtures/bool-param/src/live.md @@ -0,0 +1 @@ +live \ No newline at end of file diff --git a/test/index.js b/test/index.js index 94525ec..5d114fb 100644 --- a/test/index.js +++ b/test/index.js @@ -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())