From 07d9bbeb7c37701c6e5e93dc851eb97add50207a Mon Sep 17 00:00:00 2001 From: Nick Reese Date: Tue, 10 Aug 2021 10:12:02 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20add=20in=20draft=20funct?= =?UTF-8?q?ionality=20to=20markdown=20plugin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/markdown/README.md | 6 ++++-- packages/markdown/index.js | 28 +++++++++++++++++++++++++--- packages/markdown/package-lock.json | 2 +- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/packages/markdown/README.md b/packages/markdown/README.md index 36cbdc6..c53484d 100644 --- a/packages/markdown/README.md +++ b/packages/markdown/README.md @@ -27,7 +27,8 @@ plugins: { } ``` -## Example Config +## Example Config + ```javascript plugins: { '@elderjs/plugin-markdown': { @@ -44,7 +45,7 @@ plugins: { } ``` -> **Caveat:** Currently, when your contents path is outside of the src directory (eg: `rootdir/articles/` ) the dev server will not be able to pickup changes on your markdown files. However it can build just fine. +> **Caveat:** Currently, when your contents path is outside of the src directory (eg: `rootdir/articles/` ) the dev server will not be able to pickup changes on your markdown files. However it can build just fine. ## Customizing the Defaults @@ -109,3 +110,4 @@ The default remarkPlugins are exported for ease of use via the package. `const { - By default, if there is a `date` field in your frontmatter it will sort all of the markdown for that route by it. - If there is a `slug` field in your frontmatter it will use that for the slug, if not it falls back to the filename. +- If `draft: true` is in a file's frontmatter or a slug is prefixed with `draft-` these markdown files will be hidden when `process.env.NODE_ENV === 'production'`. They will also be prefixed with `DRAFT: [Post Title Here]` to make this functionality more obvious. diff --git a/packages/markdown/index.js b/packages/markdown/index.js index ad3e6cc..7ea2d86 100644 --- a/packages/markdown/index.js +++ b/packages/markdown/index.js @@ -106,7 +106,6 @@ const plugin = { html, data, }); - plugin.requests.push({ slug, route }); } // if there is a date in frontmatter, sort them by most recent @@ -120,6 +119,28 @@ const plugin = { ); } } + + // Remove drafts on production. Add DRAFT: to the title on dev. + Object.keys(plugin.markdown).forEach((route) => { + if (process.env.NODE_ENV === 'production') { + plugin.markdown[route] = plugin.markdown[route].filter( + (md) => !md.frontmatter.draft && md.slug.indexOf('draft-') !== 0, + ); + } else { + plugin.markdown[route].forEach((md) => { + if (md.frontmatter.draft || md.slug.indexOf('draft') === 0) { + md.frontmatter.title = `DRAFT: ${md.frontmatter.title || 'MISSING TITLE'}`; + } + }); + } + }); + + // loop through object to create requests + Object.keys(plugin.markdown).forEach((route) => { + plugin.markdown[route].forEach((md) => { + plugin.requests.push({ slug: md.slug, route }); + }); + }); } return plugin; @@ -132,7 +153,7 @@ const plugin = { //theme is the only option available - for now. useTableOfContents: false, // adds tocTree and tocHtml to each route's data object. createRoutes: true, // creates routes in allRequests based on collected md files. - contents: {} + contents: {}, }, shortcodes: [], hooks: [ @@ -168,7 +189,8 @@ const plugin = { { hook: 'allRequests', name: 'mdFilesToAllRequests', - description: 'Add collected md files to allRequests array using the frontmatter slug or filename as the slug.', + description: + 'Add collected md files to allRequests array using the frontmatter slug or filename as the slug. Users can modify the plugin.requests before this hook to change generated requests.', priority: 50, // default run: async ({ allRequests, plugin }) => { if (plugin.config.routes.length > 0 && plugin.config.createRoutes) { diff --git a/packages/markdown/package-lock.json b/packages/markdown/package-lock.json index 9ac0312..3677ffc 100644 --- a/packages/markdown/package-lock.json +++ b/packages/markdown/package-lock.json @@ -1,6 +1,6 @@ { "name": "@elderjs/plugin-markdown", - "version": "0.1.43", + "version": "1.2.0", "lockfileVersion": 1, "requires": true, "dependencies": {