|
| 1 | +import React from 'react'; |
| 2 | +import { Link, graphql } from 'gatsby'; |
| 3 | +import Helmet from 'react-helmet'; |
| 4 | +import moment from 'moment'; |
| 5 | +import Layout from '../layouts'; |
| 6 | + |
| 7 | +export default function TagTemplate({ pageContext, data }) { |
| 8 | + const { tag, posts } = pageContext; |
| 9 | + const { siteMetadata } = data.site; |
| 10 | + |
| 11 | + // We already have the paths, but we also want title+date for display. |
| 12 | + // Pull the needed fields for *only* the posts that belong to this tag. |
| 13 | + const postData = data.allMarkdownRemark.edges.map(e => e.node); |
| 14 | + |
| 15 | + return ( |
| 16 | + <Layout> |
| 17 | + <Helmet> |
| 18 | + <title>{`Tag: ${tag} - ${siteMetadata.title}`}</title> |
| 19 | + </Helmet> |
| 20 | + |
| 21 | + <h1>Posts tagged “{tag}”</h1> |
| 22 | + |
| 23 | + <ul style={{ listStyle: 'none', padding: 0 }}> |
| 24 | + {postData.map(({ frontmatter, excerpt, fields, html }) => ( |
| 25 | + <li key={frontmatter.path} style={{ marginBottom: '2rem' }}> |
| 26 | + <h2> |
| 27 | + <Link to={frontmatter.path}>{frontmatter.title}</Link> |
| 28 | + </h2> |
| 29 | + <p style={{ fontStyle: 'italic' }}> |
| 30 | + {moment(frontmatter.date).format('MMMM Do YYYY')} |
| 31 | + </p> |
| 32 | + <p>{excerpt}</p> |
| 33 | + </li> |
| 34 | + ))} |
| 35 | + </ul> |
| 36 | + </Layout> |
| 37 | + ); |
| 38 | +} |
| 39 | + |
| 40 | +// Query only the posts whose path is in the context‐provided array |
| 41 | +export const pageQuery = graphql` |
| 42 | + query($posts: [String!]!) { |
| 43 | + site { |
| 44 | + siteMetadata { |
| 45 | + title |
| 46 | + } |
| 47 | + } |
| 48 | + allMarkdownRemark(filter: { frontmatter: { path: { in: $posts } } }) { |
| 49 | + edges { |
| 50 | + node { |
| 51 | + frontmatter { |
| 52 | + title |
| 53 | + path |
| 54 | + date |
| 55 | + } |
| 56 | + excerpt(pruneLength: 140) |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | +`; |
0 commit comments