diff --git a/packages/docusaurus-plugin-content-blog/src/__tests__/__fixtures__/website/blog/draft.md b/packages/docusaurus-plugin-content-blog/src/__tests__/__fixtures__/website/blog/draft.md new file mode 100644 index 0000000000000..57ac92323be31 --- /dev/null +++ b/packages/docusaurus-plugin-content-blog/src/__tests__/__fixtures__/website/blog/draft.md @@ -0,0 +1,6 @@ +--- +date: 2020-02-27 +draft: true +--- + +this post should not be published yet diff --git a/packages/docusaurus-plugin-content-blog/src/__tests__/__snapshots__/generateBlogFeed.test.ts.snap b/packages/docusaurus-plugin-content-blog/src/__tests__/__snapshots__/generateBlogFeed.test.ts.snap index f2bb73f5c36ea..5df2f18082a87 100644 --- a/packages/docusaurus-plugin-content-blog/src/__tests__/__snapshots__/generateBlogFeed.test.ts.snap +++ b/packages/docusaurus-plugin-content-blog/src/__tests__/__snapshots__/generateBlogFeed.test.ts.snap @@ -7,12 +7,19 @@ exports[`blogFeed atom shows feed item for each post 1`] = ` https://docusaurus.io/blog Hello Blog - 2019-01-01T00:00:00.000Z + 2020-02-27T00:00:00.000Z https://github.com/jpmonette/feed Hello Blog https://docusaurus.io/image/favicon.ico Copyright + + <![CDATA[draft]]> + draft + + 2020-02-27T00:00:00.000Z + + <![CDATA[date-matter]]> date-matter @@ -39,10 +46,17 @@ exports[`blogFeed rss shows feed item for each post 1`] = ` Hello Blog https://docusaurus.io/blog Hello Blog - Tue, 01 Jan 2019 00:00:00 GMT + Thu, 27 Feb 2020 00:00:00 GMT http://blogs.law.harvard.edu/tech/rss https://github.com/jpmonette/feed Copyright + + <![CDATA[draft]]> + https://docusaurus.io/blog/2020/02/27/draft + https://docusaurus.io/blog/2020/02/27/draft + Thu, 27 Feb 2020 00:00:00 GMT + + <![CDATA[date-matter]]> https://docusaurus.io/blog/2019/01/01/date-matter diff --git a/packages/docusaurus-plugin-content-blog/src/__tests__/index.test.ts b/packages/docusaurus-plugin-content-blog/src/__tests__/index.test.ts index 5deb22e10655f..1255e3661f14a 100644 --- a/packages/docusaurus-plugin-content-blog/src/__tests__/index.test.ts +++ b/packages/docusaurus-plugin-content-blog/src/__tests__/index.test.ts @@ -11,15 +11,15 @@ 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, @@ -27,10 +27,17 @@ describe('loadBlog', () => { 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)) @@ -59,6 +66,7 @@ describe('loadBlog', () => { }, truncated: false, }); + expect( blogPosts.find(v => v.metadata.title === 'Happy 1st Birthday Slash!') .metadata, @@ -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(); + }); }); diff --git a/packages/docusaurus-plugin-content-blog/src/blogUtils.ts b/packages/docusaurus-plugin-content-blog/src/blogUtils.ts index f067a36cde051..6a0be67e944c8 100644 --- a/packages/docusaurus-plugin-content-blog/src/blogUtils.ts +++ b/packages/docusaurus-plugin-content-blog/src/blogUtils.ts @@ -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); diff --git a/website/docs/blog.md b/website/docs/blog.md index 1b84ecdbdff73..746b99a4abbee 100644 --- a/website/docs/blog.md +++ b/website/docs/blog.md @@ -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