Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(v2): add draft feature to blog posts #2335

Merged
merged 1 commit into from
Feb 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
44 changes: 28 additions & 16 deletions packages/docusaurus-plugin-content-blog/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,32 @@ 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 All @@ -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',
Expand All @@ -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,
Expand All @@ -80,20 +84,28 @@ 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},
Comment on lines +88 to +89
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was necessary to modify the existing tests here and below, so that the tests pass successfully in the CI.

The fact is that in fixtures there is a no date file, it does not have a date, which means that it is calculated from the creation date. But the “oddity” of CI is that it copies, as I suppose this file, and therefore its creation date will always be equal to the current test execution time, which is why expected and received data no longer match!

As I wrote above, this issue applies only to CI, locally tests pass successfully without these changes. But in order to achieve successful execution of tests in CI, we need to exclude the prevItem key from the metadata object since this element will change in the CI environment (CIrcieCI).

I don’t know how clearly I have stated the essence of the problem, but in this case I consider such a solution acceptable, because we ran into details of handling this CI.

}).toEqual({
permalink: noDatePermalink,
source: noDateSource,
title: 'no date',
description: `no date`,
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();
});
});
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