Skip to content

Commit

Permalink
feat: 🎸 add in draft functionality to markdown plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
nickreese committed Aug 10, 2021
1 parent 27b74f1 commit 07d9bbe
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
6 changes: 4 additions & 2 deletions packages/markdown/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ plugins: {
}
```

## Example Config
## Example Config

```javascript
plugins: {
'@elderjs/plugin-markdown': {
Expand All @@ -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

Expand Down Expand Up @@ -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.
28 changes: 25 additions & 3 deletions packages/markdown/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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: [
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion packages/markdown/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 07d9bbe

Please sign in to comment.