Skip to content

Commit

Permalink
Allow controlling number of blog posts in Recent Blogs sidebar. (#432)
Browse files Browse the repository at this point in the history
New feature. Allow controlling number of blog posts in Recent Blogs sidebar. Can have qty or ALL.
  • Loading branch information
ericnakagawa authored and JoelMarcey committed Feb 2, 2018
1 parent c99cdef commit dfb70e1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
13 changes: 13 additions & 0 deletions docs/guides-blog.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ Not 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

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.
Expand Down
18 changes: 15 additions & 3 deletions lib/core/BlogSidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,28 @@ const MetadataBlog = require('./MetadataBlog.js');

class BlogSidebar extends React.Component {
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 = [
{
name: 'Recent Posts',
links: MetadataBlog.slice(0, 5),
name: blogSidebarTitle,
links: MetadataBlog.slice(0, blogSidebarCount),
},
];
const title = this.props.current && this.props.current.title;

const current = {
id: title || '',
category: 'Recent Posts',
category: blogSidebarTitle,
};
return (
<Container className="docsNavContainer" id="docsNav" wrapper={false}>
Expand Down
8 changes: 7 additions & 1 deletion lib/server/readMetadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,17 +365,23 @@ function generateMetadataBlog() {
filePathDateArr[2] +
'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);
});

const sortedMetadatas = metadatas.sort(
(a, b) => parseInt(b.seconds) - parseInt(a.seconds)
);

fs.writeFileSync(
__dirname + '/../core/MetadataBlog.js',
'/**\n' +
' * @generated\n' +
' */\n' +
'module.exports = ' +
JSON.stringify(metadatas, null, 2) +
JSON.stringify(sortedMetadatas, null, 2) +
';\n'
);
}
Expand Down

0 comments on commit dfb70e1

Please sign in to comment.