Skip to content

Instantly share code, notes, and snippets.

@DSchau
Last active July 19, 2017 18:22
Show Gist options
  • Save DSchau/6749d0f4a9c23fd2a6a0622961e3ee27 to your computer and use it in GitHub Desktop.
Save DSchau/6749d0f4a9c23fd2a6a0622961e3ee27 to your computer and use it in GitHub Desktop.
Creating a static blog with Gatsby: https://dustinschau.com/blog/getting-started-with-gatsby
import React from 'react';
import Link from 'gatsby-link';
import Helmet from 'react-helmet';
// import '../css/index.css'; // add some style if you want!
export default function Index({
data
}) {
const { edges: posts } = data.allMarkdownRemark;
return (
<div className="blog-posts">
{posts
.filter(post => post.node.frontmatter.title.length > 0)
.map(({ node: post }) => {
return (
<div className="blog-post-preview" key={post.id}>
<h1>
<Link to={post.frontmatter.path}>{post.frontmatter.title}</Link>
</h1>
<h2>{post.frontmatter.date}</h2>
<p>{post.excerpt}</p>
</div>
);
})}
</div>
);
}
export const pageQuery = graphql`
query IndexQuery {
allMarkdownRemark(sort: { order: DESC, fields: [frontmatter___date] }) {
edges {
node {
excerpt(pruneLength: 250)
id
frontmatter {
title
date(formatString: "MMMM DD, YYYY")
path
}
}
}
}
}
`;
import React from 'react';
import Helmet from 'react-helmet';
// import '../css/blog-post.css'; // make it pretty!
export default function Template({
data // this prop will be injected by the GraphQL query we'll write in a bit
}) {
const { markdownRemark: post } = data; // data.markdownRemark holds our post data
return (
<div className="blog-post-container">
<Helmet title={`Your Blog Name - ${post.frontmatter.title}`} />
<div className="blog-post">
<h1>{post.frontmatter.title}</h1>
<div className="blog-post-content" dangerouslySetInnerHTML={{ __html: post.html }} />
</div>
</div>
);
}
import React from 'react';
import Helmet from 'react-helmet';
// import '../css/blog-post.css';
export default function Template({
data
}) {
const { markdownRemark: post } = data;
return (
<div className="blog-post-container">
<Helmet title={`Your Blog Name - ${post.frontmatter.title}`} />
<div className="blog-post">
<h1>{post.frontmatter.title}</h1>
<div className="blog-post-content" dangerouslySetInnerHTML={{ __html: post.html }} />
</div>
</div>
);
}
export const pageQuery = graphql`
query BlogPostByPath($path: String!) {
markdownRemark(frontmatter: { path: { eq: $path } }) {
html
frontmatter {
date(formatString: "MMMM DD, YYYY")
path
title
}
}
}
`;
module.exports = {
siteMetadata: {
title: `Your Name - Blog`,
author: `Your Name`,
},
plugins: [
'gatsby-plugin-catch-links',
'gatsby-plugin-react-helmet',
],
}
module.exports = {
// previous configuration
plugins: [
'gatsby-plugin-catch-links',
'gatsby-plugin-react-helmet',
{
resolve: `gatsby-source-filesystem`,
options: {
path: `${__dirname}/src/pages`,
name: 'pages',
},
}
]
}
module.exports = {
// previous setup
plugins: [
'gatsby-plugin-catch-links',
'gatsby-plugin-react-helmet',
{
resolve: `gatsby-source-filesystem`,
options: {
path: `${__dirname}/src/pages`,
name: 'pages',
},
},
{
resolve: 'gatsby-transformer-remark',
options: {
plugins: [] // just in case those previously mentioned remark plugins sound cool :)
}
},
]
}
const path = require('path');
exports.createPages = ({ boundActionCreators, graphql }) => {
const { createPage } = boundActionCreators;
const blogPostTemplate = path.resolve(`src/templates/blog-post.js`);
}
const path = require('path');
exports.createPages = ({ boundActionCreators, graphql }) => {
const { createPage } = boundActionCreators;
const blogPostTemplate = path.resolve(`src/templates/blog-post.js`);
return graphql(`{
allMarkdownRemark(
sort: { order: DESC, fields: [frontmatter___date] }
limit: 1000
) {
edges {
node {
excerpt(pruneLength: 250)
html
id
frontmatter {
date
path
title
}
}
}
}
}`
)
.then(result => {
if (result.errors) {
return Promise.reject(result.errors);
}
});
}
const path = require('path');
exports.createPages = ({ boundActionCreators, graphql }) => {
const { createPage } = boundActionCreators;
const blogPostTemplate = path.resolve(`src/templates/blog-post.js`);
return graphql(`{
allMarkdownRemark(
sort: { order: DESC, fields: [frontmatter___date] }
limit: 1000
) {
edges {
node {
excerpt(pruneLength: 250)
html
id
frontmatter {
date
path
title
}
}
}
}
}`)
.then(result => {
if (result.errors) {
return Promise.reject(result.errors);
}
result.data.allMarkdownRemark.edges
.forEach(({ node }) => {
createPage({
path: node.frontmatter.path,
component: blogPostTemplate,
context: {} // additional data can be passed via context
});
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment