Created
June 10, 2022 16:44
-
-
Save SarahElson/1b39a5d12d28962c44cf44b140a3f0c1 to your computer and use it in GitHub Desktop.
Execute the code on the index page now.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react" | |
import { Link, graphql } from "gatsby" | |
import Bio from "../components/bio" | |
import Layout from "../components/layout" | |
import Sample from "../components/sample" | |
const BlogIndexDetails = ({ data, location }) => { | |
const siteTitle = data.site.siteMetadata?.title || `Title` | |
const posts = data.allMarkdownRemark.nodes | |
if (posts.length === 0) { | |
return ( | |
<Layout location={location} title={siteTitle}> | |
<SEO title="All posts" /> | |
<Bio /> | |
<p> | |
No required blogs found. Add markdown posts to "content/blog" (or the | |
directory you specified for the "gatsby-source-filesystem" plugin in | |
gatsby-config.js). | |
</p> | |
</Layout> | |
) | |
} | |
return ( | |
<Layout location={location} title={siteTitle}> | |
<SEO title="All posts" /> | |
<Bio /> | |
<ol style={{ listStyle: `none` }}> | |
{posts.map(post => { | |
const title = post.frontmatter.title || post.fields.slug | |
return ( | |
<li key={post.fields.slug}> | |
<article | |
className="post-list-item" | |
itemScope | |
itemType="http://schema.org/Article" | |
> | |
<header> | |
<h2> | |
<Link | |
data-testid={post.fields.slug + "-link"} | |
to={post.fields.slug} | |
itemProp="url" | |
> | |
<span itemProp="headline">{title}</span> | |
</Link> | |
</h2> | |
<small>{post.frontmatter.date}</small> | |
</header> | |
<section> | |
<p | |
data-testid={post.fields.slug + "-desc"} | |
dangerouslySetInnerHTML={{ | |
__html: post.frontmatter.description || post.excerpt, | |
}} | |
itemProp="description" | |
/> | |
</section> | |
</article> | |
</li> | |
) | |
})} | |
</ol> | |
</Layout> | |
) | |
} | |
export default BlogIndexDetails | |
export const pageQuery = graphql` | |
query { | |
site { | |
siteMetadata { | |
title | |
} | |
} | |
allMarkdownRemark(sort: { fields: [frontmatter___date], order: DESC }) { | |
nodes { | |
excerpt | |
fields { | |
slug | |
} | |
frontmatter { | |
date(formatString: "MMMM DD, YYYY") | |
title | |
description | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment