Skip to content

Commit

Permalink
feat(v2): add draft feature to blog posts
Browse files Browse the repository at this point in the history
  • Loading branch information
lex111 committed Feb 27, 2020
1 parent 6670c53 commit 2e34f76
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
date: 2020-02-27
draft: true
---

this post should not be published yet
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@ exports[`blogFeed atom shows feed item for each post 1`] = `
<feed xmlns=\\"http://www.w3.org/2005/Atom\\">
<id>https://docusaurus.io/blog</id>
<title>Hello Blog</title>
<updated>2019-01-01T00:00:00.000Z</updated>
<updated>2020-02-27T00:00:00.000Z</updated>
<generator>https://github.com/jpmonette/feed</generator>
<link rel=\\"alternate\\" href=\\"https://docusaurus.io/blog\\"/>
<subtitle>Hello Blog</subtitle>
<icon>https://docusaurus.io/image/favicon.ico</icon>
<rights>Copyright</rights>
<entry>
<title type=\\"html\\"><![CDATA[draft]]></title>
<id>draft</id>
<link href=\\"https://docusaurus.io/blog/2020/02/27/draft\\"/>
<updated>2020-02-27T00:00:00.000Z</updated>
<summary type=\\"html\\"><![CDATA[this post should not be published yet]]></summary>
</entry>
<entry>
<title type=\\"html\\"><![CDATA[date-matter]]></title>
<id>date-matter</id>
Expand All @@ -39,10 +46,17 @@ exports[`blogFeed rss shows feed item for each post 1`] = `
<title>Hello Blog</title>
<link>https://docusaurus.io/blog</link>
<description>Hello Blog</description>
<lastBuildDate>Tue, 01 Jan 2019 00:00:00 GMT</lastBuildDate>
<lastBuildDate>Thu, 27 Feb 2020 00:00:00 GMT</lastBuildDate>
<docs>http://blogs.law.harvard.edu/tech/rss</docs>
<generator>https://github.com/jpmonette/feed</generator>
<copyright>Copyright</copyright>
<item>
<title><![CDATA[draft]]></title>
<link>https://docusaurus.io/blog/2020/02/27/draft</link>
<guid>https://docusaurus.io/blog/2020/02/27/draft</guid>
<pubDate>Thu, 27 Feb 2020 00:00:00 GMT</pubDate>
<description><![CDATA[this post should not be published yet]]></description>
</item>
<item>
<title><![CDATA[date-matter]]></title>
<link>https://docusaurus.io/blog/2019/01/01/date-matter</link>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,33 @@ import pluginContentBlog from '../index';
import {DocusaurusConfig, LoadContext} from '@docusaurus/types';

describe('loadBlog', () => {
test('simple website', async () => {
const siteDir = path.join(__dirname, '__fixtures__', 'website');
const siteDir = path.join(__dirname, '__fixtures__', 'website');
const pluginPath = 'blog';
const getBlogPosts = async () => {
const generatedFilesDir: string = path.resolve(siteDir, '.docusaurus');
const siteConfig = {
title: 'Hello',
baseUrl: '/',
url: 'https://docusaurus.io',
} as DocusaurusConfig;
const pluginPath = 'blog';
const plugin = pluginContentBlog(
{
siteDir,
siteConfig,
generatedFilesDir,
} as LoadContext,
{
path: 'blog',
path: pluginPath,
},
);
const {blogPosts} = await plugin.loadContent();

return blogPosts;
};

test('simple website', async () => {
const blogPosts = await getBlogPosts();

const noDateSource = path.join('@site', pluginPath, 'no date.md');
const noDateSourceBirthTime = (
await fs.stat(noDateSource.replace('@site', siteDir))
Expand Down Expand Up @@ -59,6 +66,7 @@ describe('loadBlog', () => {
},
truncated: false,
});

expect(
blogPosts.find(v => v.metadata.title === 'Happy 1st Birthday Slash!')
.metadata,
Expand Down Expand Up @@ -93,7 +101,18 @@ describe('loadBlog', () => {
permalink: '/blog/2019/01/01/date-matter',
title: 'date-matter',
},
prevItem: {
permalink: '/blog/2020/02/27/draft',
title: 'draft',
},
truncated: false,
});
});

test('draft blog post not exists in production build', async () => {
process.env.NODE_ENV = 'production';
const blogPosts = await getBlogPosts();

expect(blogPosts.find(v => v.metadata.title === 'draft')).toBeUndefined();
});
});
4 changes: 4 additions & 0 deletions packages/docusaurus-plugin-content-blog/src/blogUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ export async function generateBlogPosts(
const fileString = await fs.readFile(source, 'utf-8');
const {frontMatter, content, excerpt} = parse(fileString);

if (frontMatter.draft && process.env.NODE_ENV === 'production') {
return;
}

let date;
// Extract date and title from filename.
const match = blogFileName.match(FILENAME_PATTERN);
Expand Down
1 change: 1 addition & 0 deletions website/docs/blog.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ The only required field is `title`; however, we provide options to add author in
- `author_title` - A description of the author.
- `title` - The blog post title.
- `tags` - A list of strings to tag to your post.
- `draft` - A boolean flag to indicate that the blog post is work in process and therefore should not be published yet. However, draft blog posts will be displayed during development.

## Summary truncation

Expand Down

0 comments on commit 2e34f76

Please sign in to comment.