mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-11 08:07:26 +02:00
feat: add "unlisted" front matter option for blog posts (#1396)
* Add `unlisted` header option for blog posts, fixes #1393. Previously, the blog sidebar listed the most recent blog posts sorted by their publish date. This commit adds the ability to hide a specific blog post from the sidebar and the feed of all blog posts by adding the `unlisted: true` header option. ```md title: My Draft Post unlisted: true # Hide from blog sidebar and main blog page feed. --- ``` * Rename "unlisted" into "draft".
This commit is contained in:
parent
60f9228d67
commit
da3c91373e
4 changed files with 30 additions and 19 deletions
|
@ -10,6 +10,8 @@ const BlogPost = require('./BlogPost.js');
|
||||||
const BlogSidebar = require('./BlogSidebar.js');
|
const BlogSidebar = require('./BlogSidebar.js');
|
||||||
const Container = require('./Container.js');
|
const Container = require('./Container.js');
|
||||||
const MetadataBlog = require('./MetadataBlog.js');
|
const MetadataBlog = require('./MetadataBlog.js');
|
||||||
|
|
||||||
|
const MetadataPublicBlog = MetadataBlog.filter(item => !item.draft);
|
||||||
const Site = require('./Site.js');
|
const Site = require('./Site.js');
|
||||||
const utils = require('./utils.js');
|
const utils = require('./utils.js');
|
||||||
|
|
||||||
|
@ -40,27 +42,28 @@ class BlogPageLayout extends React.Component {
|
||||||
/>
|
/>
|
||||||
<Container className="mainContainer postContainer blogContainer">
|
<Container className="mainContainer postContainer blogContainer">
|
||||||
<div className="posts">
|
<div className="posts">
|
||||||
{MetadataBlog.slice(page * perPage, (page + 1) * perPage).map(
|
{MetadataPublicBlog.slice(
|
||||||
post => (
|
page * perPage,
|
||||||
<BlogPost
|
(page + 1) * perPage,
|
||||||
post={post}
|
).map(post => (
|
||||||
content={post.content}
|
<BlogPost
|
||||||
truncate
|
post={post}
|
||||||
key={
|
content={post.content}
|
||||||
utils.getPath(post.path, this.props.config.cleanUrl) +
|
truncate
|
||||||
post.title
|
key={
|
||||||
}
|
utils.getPath(post.path, this.props.config.cleanUrl) +
|
||||||
config={this.props.config}
|
post.title
|
||||||
/>
|
}
|
||||||
),
|
config={this.props.config}
|
||||||
)}
|
/>
|
||||||
|
))}
|
||||||
<div className="docs-prevnext">
|
<div className="docs-prevnext">
|
||||||
{page > 0 && (
|
{page > 0 && (
|
||||||
<a className="docs-prev" href={this.getPageURL(page - 1)}>
|
<a className="docs-prev" href={this.getPageURL(page - 1)}>
|
||||||
← Prev
|
← Prev
|
||||||
</a>
|
</a>
|
||||||
)}
|
)}
|
||||||
{MetadataBlog.length > (page + 1) * perPage && (
|
{MetadataPublicBlog.length > (page + 1) * perPage && (
|
||||||
<a className="docs-next" href={this.getPageURL(page + 1)}>
|
<a className="docs-next" href={this.getPageURL(page + 1)}>
|
||||||
Next →
|
Next →
|
||||||
</a>
|
</a>
|
||||||
|
|
|
@ -10,6 +10,8 @@ const SideNav = require('./nav/SideNav.js');
|
||||||
|
|
||||||
const MetadataBlog = require('./MetadataBlog.js');
|
const MetadataBlog = require('./MetadataBlog.js');
|
||||||
|
|
||||||
|
const MetadataPublicBlog = MetadataBlog.filter(item => !item.draft);
|
||||||
|
|
||||||
class BlogSidebar extends React.Component {
|
class BlogSidebar extends React.Component {
|
||||||
render() {
|
render() {
|
||||||
let blogSidebarCount = 5;
|
let blogSidebarCount = 5;
|
||||||
|
@ -17,7 +19,7 @@ class BlogSidebar extends React.Component {
|
||||||
let blogSidebarTitle = blogSidebarTitleConfig.default || 'Recent Posts';
|
let blogSidebarTitle = blogSidebarTitleConfig.default || 'Recent Posts';
|
||||||
if (this.props.config.blogSidebarCount) {
|
if (this.props.config.blogSidebarCount) {
|
||||||
if (this.props.config.blogSidebarCount === 'ALL') {
|
if (this.props.config.blogSidebarCount === 'ALL') {
|
||||||
blogSidebarCount = MetadataBlog.length;
|
blogSidebarCount = MetadataPublicBlog.length;
|
||||||
blogSidebarTitle = blogSidebarTitleConfig.all || 'All Blog Posts';
|
blogSidebarTitle = blogSidebarTitleConfig.all || 'All Blog Posts';
|
||||||
} else {
|
} else {
|
||||||
blogSidebarCount = this.props.config.blogSidebarCount;
|
blogSidebarCount = this.props.config.blogSidebarCount;
|
||||||
|
@ -28,7 +30,7 @@ class BlogSidebar extends React.Component {
|
||||||
{
|
{
|
||||||
type: 'CATEGORY',
|
type: 'CATEGORY',
|
||||||
title: blogSidebarTitle,
|
title: blogSidebarTitle,
|
||||||
children: MetadataBlog.slice(0, blogSidebarCount).map(item => ({
|
children: MetadataPublicBlog.slice(0, blogSidebarCount).map(item => ({
|
||||||
type: 'LINK',
|
type: 'LINK',
|
||||||
item,
|
item,
|
||||||
})),
|
})),
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
title: Draft Example
|
||||||
|
draft: true
|
||||||
|
---
|
||||||
|
This blog post should not appear in the sidebar or the blog feed because it is a draft.
|
|
@ -28,6 +28,7 @@ module.exports = function(type) {
|
||||||
|
|
||||||
readMetadata.generateMetadataBlog(siteConfig);
|
readMetadata.generateMetadataBlog(siteConfig);
|
||||||
const MetadataBlog = require('../core/MetadataBlog.js');
|
const MetadataBlog = require('../core/MetadataBlog.js');
|
||||||
|
const MetadataPublicBlog = MetadataBlog.filter(item => !item.draft);
|
||||||
|
|
||||||
const feed = new Feed({
|
const feed = new Feed({
|
||||||
title: `${siteConfig.title} Blog`,
|
title: `${siteConfig.title} Blog`,
|
||||||
|
@ -38,10 +39,10 @@ module.exports = function(type) {
|
||||||
link: blogRootURL,
|
link: blogRootURL,
|
||||||
image: siteImageURL,
|
image: siteImageURL,
|
||||||
copyright: siteConfig.copyright,
|
copyright: siteConfig.copyright,
|
||||||
updated: new Date(MetadataBlog[0].date),
|
updated: new Date(MetadataPublicBlog[0].date),
|
||||||
});
|
});
|
||||||
|
|
||||||
MetadataBlog.forEach(post => {
|
MetadataPublicBlog.forEach(post => {
|
||||||
const url = `${blogRootURL}/${post.path}`;
|
const url = `${blogRootURL}/${post.path}`;
|
||||||
const description = utils.blogPostHasTruncateMarker(post.content)
|
const description = utils.blogPostHasTruncateMarker(post.content)
|
||||||
? renderMarkdown(utils.extractBlogPostBeforeTruncate(post.content))
|
? renderMarkdown(utils.extractBlogPostBeforeTruncate(post.content))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue