Allow controlling number of blog posts in Recent Blogs sidebar. (#432)

New feature. Allow controlling number of blog posts in Recent Blogs sidebar. Can have qty or ALL.
This commit is contained in:
Eric Nakagawa 2018-02-02 08:11:51 -08:00 committed by Joel Marcey
parent c99cdefd3a
commit dfb70e1829
3 changed files with 35 additions and 4 deletions

View file

@ -68,6 +68,19 @@ Not this.
Or this. Or this.
``` ```
## Changing How Many Blog Posts Show on Sidebar
By default, 5 recent blog posts are shown on the sidebar.
You can configure a specifc amount of blog posts to show by adding a `blogSidebarCount` setting to your `siteConfig.js`.
The available options are an integer repreenting the number of posts you wish to show or a string with the value 'ALL'.
Example:
```
blogSidebarCount: 'ALL'
```
## RSS Feed ## RSS Feed
Docusaurus provides a simple RSS feed for your blog posts. Both RSS and Atom feed formats are supported. This data is automatically to your website page's HTML <HEAD> tag. Docusaurus provides a simple RSS feed for your blog posts. Both RSS and Atom feed formats are supported. This data is automatically to your website page's HTML <HEAD> tag.

View file

@ -13,16 +13,28 @@ const MetadataBlog = require('./MetadataBlog.js');
class BlogSidebar extends React.Component { class BlogSidebar extends React.Component {
render() { render() {
let blogSidebarCount = 5;
let blogSidebarTitle = 'Recent Posts';
if (this.props.config.blogSidebarCount) {
if (this.props.config.blogSidebarCount == 'ALL') {
blogSidebarCount = MetadataBlog.length;
blogSidebarTitle = 'All Blog Posts';
} else {
blogSidebarCount = this.props.config.blogSidebarCount;
}
}
const contents = [ const contents = [
{ {
name: 'Recent Posts', name: blogSidebarTitle,
links: MetadataBlog.slice(0, 5), links: MetadataBlog.slice(0, blogSidebarCount),
}, },
]; ];
const title = this.props.current && this.props.current.title; const title = this.props.current && this.props.current.title;
const current = { const current = {
id: title || '', id: title || '',
category: 'Recent Posts', category: blogSidebarTitle,
}; };
return ( return (
<Container className="docsNavContainer" id="docsNav" wrapper={false}> <Container className="docsNavContainer" id="docsNav" wrapper={false}>

View file

@ -365,17 +365,23 @@ function generateMetadataBlog() {
filePathDateArr[2] + filePathDateArr[2] +
'T06:00:00.000Z' 'T06:00:00.000Z'
); );
// allow easier sorting of blog by providing seconds since epoch
metadata.seconds = Math.round(metadata.date.getTime() / 1000);
metadatas.push(metadata); metadatas.push(metadata);
}); });
const sortedMetadatas = metadatas.sort(
(a, b) => parseInt(b.seconds) - parseInt(a.seconds)
);
fs.writeFileSync( fs.writeFileSync(
__dirname + '/../core/MetadataBlog.js', __dirname + '/../core/MetadataBlog.js',
'/**\n' + '/**\n' +
' * @generated\n' + ' * @generated\n' +
' */\n' + ' */\n' +
'module.exports = ' + 'module.exports = ' +
JSON.stringify(metadatas, null, 2) + JSON.stringify(sortedMetadatas, null, 2) +
';\n' ';\n'
); );
} }