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

Add documentation on adding list of blog posts #3510

Merged
merged 3 commits into from
Jan 14, 2018
Merged
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
79 changes: 76 additions & 3 deletions docs/docs/adding-a-list-of-markdown-blog-posts.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,80 @@
title: Adding a List of Markdown Blog Posts
---

This is a stub. Help our community expand it.
Once you have added Markdown pages to your site, you are just one step away from being able to list your posts on a dedicated index page.

Please use the [Gatsby Style Guide](/docs/gatsby-style-guide/) to ensure your
pull request gets accepted.
### Creating posts

As described [here](/docs/docs/adding-markdown-pages.md), you will have to create your posts in Markdown files which will look like this:

```md
---
path: "/blog/my-first-post"
date: "2017-11-07"
title: "My first blog post"
---

Has anyone heard about GatsbyJS yet?
```

### Creating the page

The first step will be to create the page which will display your posts, in `src/pages/`. You can for example use `index.js`.

```js
import React from 'react';
import PostLink from '../components/post-link'

const IndexPage = ({ data: { allMarkdownRemark: { edges } } }) => {
const Posts = edges
.filter(edge => !!edge.node.frontmatter.date) // You can filter your posts based on some criteria
.map(edge => (
<PostLink key={edge.node.id} post={edge.node} />
));

return <div>{Posts}</div>;
};

export default IndexPage;
```

### Creating the GraphQL query

The only thing left to do is to provide the data to your component with a GraphQL query.
Copy link
Contributor

Choose a reason for hiding this comment

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

Make clear this goes at the bottom of the index.js file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!


```js
import React from 'react';
import PostLink from '../components/post-link'

const IndexPage = ({ data: { allMarkdownRemark: { edges } } }) => {
const Posts = edges
.filter(edge => !!edge.node.frontmatter.date) // You can filter your posts based on some criteria
.map(edge => (
<PostLink key={edge.node.id} post={edge.node} />
));

return <div>{Posts}</div>;
};

export default IndexPage;

export const pageQuery = graphql`
query IndexQuery {
allMarkdownRemark(sort: { order: DESC, fields: [frontmatter___date] }) {
edges {
node {
id
excerpt(pruneLength: 250)
frontmatter {
date(formatString: "MMMM DD, YYYY")
path
title
}
}
}
}
}
`;
```

This should get you a page with your posts sorted by descending date. You can further customise the `frontmatter` and the page component to get desired effects!