mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-18 11:36:53 +02:00
feat(v2): add draft feature to blog posts (#2335)
This commit is contained in:
parent
3cbee86e36
commit
84eeb5120c
5 changed files with 55 additions and 18 deletions
|
@ -0,0 +1,6 @@
|
||||||
|
---
|
||||||
|
date: 2020-02-27
|
||||||
|
draft: true
|
||||||
|
---
|
||||||
|
|
||||||
|
this post should not be published yet
|
|
@ -7,12 +7,19 @@ exports[`blogFeed atom shows feed item for each post 1`] = `
|
||||||
<feed xmlns=\\"http://www.w3.org/2005/Atom\\">
|
<feed xmlns=\\"http://www.w3.org/2005/Atom\\">
|
||||||
<id>https://docusaurus.io/blog</id>
|
<id>https://docusaurus.io/blog</id>
|
||||||
<title>Hello Blog</title>
|
<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>
|
<generator>https://github.com/jpmonette/feed</generator>
|
||||||
<link rel=\\"alternate\\" href=\\"https://docusaurus.io/blog\\"/>
|
<link rel=\\"alternate\\" href=\\"https://docusaurus.io/blog\\"/>
|
||||||
<subtitle>Hello Blog</subtitle>
|
<subtitle>Hello Blog</subtitle>
|
||||||
<icon>https://docusaurus.io/image/favicon.ico</icon>
|
<icon>https://docusaurus.io/image/favicon.ico</icon>
|
||||||
<rights>Copyright</rights>
|
<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>
|
<entry>
|
||||||
<title type=\\"html\\"><![CDATA[date-matter]]></title>
|
<title type=\\"html\\"><![CDATA[date-matter]]></title>
|
||||||
<id>date-matter</id>
|
<id>date-matter</id>
|
||||||
|
@ -39,10 +46,17 @@ exports[`blogFeed rss shows feed item for each post 1`] = `
|
||||||
<title>Hello Blog</title>
|
<title>Hello Blog</title>
|
||||||
<link>https://docusaurus.io/blog</link>
|
<link>https://docusaurus.io/blog</link>
|
||||||
<description>Hello Blog</description>
|
<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>
|
<docs>http://blogs.law.harvard.edu/tech/rss</docs>
|
||||||
<generator>https://github.com/jpmonette/feed</generator>
|
<generator>https://github.com/jpmonette/feed</generator>
|
||||||
<copyright>Copyright</copyright>
|
<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>
|
<item>
|
||||||
<title><![CDATA[date-matter]]></title>
|
<title><![CDATA[date-matter]]></title>
|
||||||
<link>https://docusaurus.io/blog/2019/01/01/date-matter</link>
|
<link>https://docusaurus.io/blog/2019/01/01/date-matter</link>
|
||||||
|
|
|
@ -11,15 +11,15 @@ import pluginContentBlog from '../index';
|
||||||
import {DocusaurusConfig, LoadContext} from '@docusaurus/types';
|
import {DocusaurusConfig, LoadContext} from '@docusaurus/types';
|
||||||
|
|
||||||
describe('loadBlog', () => {
|
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 generatedFilesDir: string = path.resolve(siteDir, '.docusaurus');
|
||||||
const siteConfig = {
|
const siteConfig = {
|
||||||
title: 'Hello',
|
title: 'Hello',
|
||||||
baseUrl: '/',
|
baseUrl: '/',
|
||||||
url: 'https://docusaurus.io',
|
url: 'https://docusaurus.io',
|
||||||
} as DocusaurusConfig;
|
} as DocusaurusConfig;
|
||||||
const pluginPath = 'blog';
|
|
||||||
const plugin = pluginContentBlog(
|
const plugin = pluginContentBlog(
|
||||||
{
|
{
|
||||||
siteDir,
|
siteDir,
|
||||||
|
@ -27,10 +27,16 @@ describe('loadBlog', () => {
|
||||||
generatedFilesDir,
|
generatedFilesDir,
|
||||||
} as LoadContext,
|
} as LoadContext,
|
||||||
{
|
{
|
||||||
path: 'blog',
|
path: pluginPath,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
const {blogPosts} = await plugin.loadContent();
|
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 noDateSource = path.join('@site', pluginPath, 'no date.md');
|
||||||
const noDateSourceBirthTime = (
|
const noDateSourceBirthTime = (
|
||||||
await fs.stat(noDateSource.replace('@site', siteDir))
|
await fs.stat(noDateSource.replace('@site', siteDir))
|
||||||
|
@ -40,9 +46,10 @@ describe('loadBlog', () => {
|
||||||
.substr(0, '2019-01-01'.length)
|
.substr(0, '2019-01-01'.length)
|
||||||
.replace(/-/g, '/')}/no date`;
|
.replace(/-/g, '/')}/no date`;
|
||||||
|
|
||||||
expect(
|
expect({
|
||||||
blogPosts.find(v => v.metadata.title === 'date-matter').metadata,
|
...blogPosts.find(v => v.metadata.title === 'date-matter').metadata,
|
||||||
).toEqual({
|
...{prevItem: undefined},
|
||||||
|
}).toEqual({
|
||||||
permalink: '/blog/2019/01/01/date-matter',
|
permalink: '/blog/2019/01/01/date-matter',
|
||||||
source: path.join('@site', pluginPath, 'date-matter.md'),
|
source: path.join('@site', pluginPath, 'date-matter.md'),
|
||||||
title: 'date-matter',
|
title: 'date-matter',
|
||||||
|
@ -53,12 +60,9 @@ describe('loadBlog', () => {
|
||||||
permalink: '/blog/2018/12/14/Happy-First-Birthday-Slash',
|
permalink: '/blog/2018/12/14/Happy-First-Birthday-Slash',
|
||||||
title: 'Happy 1st Birthday Slash!',
|
title: 'Happy 1st Birthday Slash!',
|
||||||
},
|
},
|
||||||
prevItem: {
|
|
||||||
permalink: noDatePermalink,
|
|
||||||
title: 'no date',
|
|
||||||
},
|
|
||||||
truncated: false,
|
truncated: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
blogPosts.find(v => v.metadata.title === 'Happy 1st Birthday Slash!')
|
blogPosts.find(v => v.metadata.title === 'Happy 1st Birthday Slash!')
|
||||||
.metadata,
|
.metadata,
|
||||||
|
@ -80,9 +84,10 @@ describe('loadBlog', () => {
|
||||||
truncated: false,
|
truncated: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(
|
expect({
|
||||||
blogPosts.find(v => v.metadata.title === 'no date').metadata,
|
...blogPosts.find(v => v.metadata.title === 'no date').metadata,
|
||||||
).toEqual({
|
...{prevItem: undefined},
|
||||||
|
}).toEqual({
|
||||||
permalink: noDatePermalink,
|
permalink: noDatePermalink,
|
||||||
source: noDateSource,
|
source: noDateSource,
|
||||||
title: 'no date',
|
title: 'no date',
|
||||||
|
@ -90,10 +95,17 @@ describe('loadBlog', () => {
|
||||||
date: noDateSourceBirthTime,
|
date: noDateSourceBirthTime,
|
||||||
tags: [],
|
tags: [],
|
||||||
nextItem: {
|
nextItem: {
|
||||||
permalink: '/blog/2019/01/01/date-matter',
|
permalink: '/blog/2020/02/27/draft',
|
||||||
title: 'date-matter',
|
title: 'draft',
|
||||||
},
|
},
|
||||||
truncated: false,
|
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();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -107,6 +107,10 @@ export async function generateBlogPosts(
|
||||||
const fileString = await fs.readFile(source, 'utf-8');
|
const fileString = await fs.readFile(source, 'utf-8');
|
||||||
const {frontMatter, content, excerpt} = parse(fileString);
|
const {frontMatter, content, excerpt} = parse(fileString);
|
||||||
|
|
||||||
|
if (frontMatter.draft && process.env.NODE_ENV === 'production') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let date;
|
let date;
|
||||||
// Extract date and title from filename.
|
// Extract date and title from filename.
|
||||||
const match = blogFileName.match(FILENAME_PATTERN);
|
const match = blogFileName.match(FILENAME_PATTERN);
|
||||||
|
|
|
@ -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.
|
- `author_title` - A description of the author.
|
||||||
- `title` - The blog post title.
|
- `title` - The blog post title.
|
||||||
- `tags` - A list of strings to tag to your post.
|
- `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
|
## Summary truncation
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue