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 000000000000..57ac92323be3 --- /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 f2bb73f5c36e..5df2f18082a8 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 5deb22e10655..917433244dd5 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,16 @@ 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)) @@ -40,9 +46,10 @@ describe('loadBlog', () => { .substr(0, '2019-01-01'.length) .replace(/-/g, '/')}/no date`; - expect( - blogPosts.find(v => v.metadata.title === 'date-matter').metadata, - ).toEqual({ + expect({ + ...blogPosts.find(v => v.metadata.title === 'date-matter').metadata, + ...{prevItem: undefined}, + }).toEqual({ permalink: '/blog/2019/01/01/date-matter', source: path.join('@site', pluginPath, 'date-matter.md'), title: 'date-matter', @@ -53,12 +60,9 @@ describe('loadBlog', () => { permalink: '/blog/2018/12/14/Happy-First-Birthday-Slash', title: 'Happy 1st Birthday Slash!', }, - prevItem: { - permalink: noDatePermalink, - title: 'no date', - }, truncated: false, }); + expect( blogPosts.find(v => v.metadata.title === 'Happy 1st Birthday Slash!') .metadata, @@ -80,9 +84,10 @@ describe('loadBlog', () => { truncated: false, }); - expect( - blogPosts.find(v => v.metadata.title === 'no date').metadata, - ).toEqual({ + expect({ + ...blogPosts.find(v => v.metadata.title === 'no date').metadata, + ...{prevItem: undefined}, + }).toEqual({ permalink: noDatePermalink, source: noDateSource, title: 'no date', @@ -90,10 +95,17 @@ describe('loadBlog', () => { date: noDateSourceBirthTime, tags: [], nextItem: { - permalink: '/blog/2019/01/01/date-matter', - title: 'date-matter', + 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 f067a36cde05..6a0be67e944c 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 1b84ecdbdff7..746b99a4abbe 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