Tag and category pages

This commit is contained in:
2020-09-30 17:16:27 -05:00
parent 40f5df18b5
commit 8b9d33cb44
5 changed files with 77 additions and 5 deletions

View File

@@ -33,6 +33,11 @@ exports.createPages = async ({ graphql, actions }) => {
fieldValue
}
}
categoriesGroup: allMarkdownRemark {
group(field: frontmatter___categories) {
fieldValue
}
}
}
`);
@@ -55,4 +60,14 @@ exports.createPages = async ({ graphql, actions }) => {
}
});
});
result.data.categoriesGroup.group.forEach((category) => {
createPage({
path: `/categories/${category.fieldValue}/`,
component: path.resolve(`./src/templates/category.js`),
context: {
category: category.fieldValue
}
});
});
};

View File

@@ -7,6 +7,13 @@ export default function Home({ data }) {
const post = data.allMarkdownRemark.edges[0].node;
const siteMetadata = data.site.siteMetadata;
var tags = post.frontmatter.tags.map((tag) => {
return (<a key={tag} href={'/tags/' + tag}>#{tag}</a>);
});
var categories = post.frontmatter.categories.map((category) => {
return (<a key={category} href={'/categories/' + category}>{category}</a>);
});
return (
<Page headerInfo={siteMetadata}>
<article className="post">
@@ -16,11 +23,11 @@ export default function Home({ data }) {
{post.frontmatter.formattedDate}, {post.frontmatter.fromNowDate}
</time>
<nav className="post-tags">
{tags}
</nav>
<nav className="post-categories">
<span>posted in:</span>
{categories}
</nav>
</div>
<div dangerouslySetInnerHTML={{ __html: post.html }} />

50
src/templates/category.js Normal file
View File

@@ -0,0 +1,50 @@
import React from "react";
import { graphql } from "gatsby";
import Page from "./page";
export default function Category({ data, pageContext }) {
var { category } = pageContext;
var siteMetadata = data.site.siteMetadata;
var totalCount = data.allMarkdownRemark.totalCount;
var postListItems = data.allMarkdownRemark.edges.map(function(edge) {
return (
<li><a href={edge.node.fields.slug}>{edge.node.frontmatter.title}</a></li>
);
});
return (
<Page headerInfo={siteMetadata} pageTitle={'Category: ' + category + ' (' + totalCount + ')'}>
<p>
All posts categorized under "{category}".
</p>
<ul>
{postListItems}
</ul>
</Page>
)
}
export const query = graphql`
query($category: String!) {
allMarkdownRemark(
sort: { fields: [frontmatter___date], order: DESC }
filter: { frontmatter: { categories: { in: [$category] } } }
)
{
totalCount
edges {
node {
fields {
slug
}
frontmatter {
title
}
}
}
}
site {
...HeaderInfo
}
}
`

View File

@@ -11,7 +11,7 @@ export default function Post({ data }) {
return (<a key={tag} href={'/tags/' + tag}>#{tag}</a>);
});
var categories = post.frontmatter.categories.map((category) => {
return (<a key={category} href={'/categories/' + category}>#{category}</a>);
return (<a key={category} href={'/categories/' + category}>{category}</a>);
});
return (
@@ -37,7 +37,7 @@ export default function Post({ data }) {
shortname='fishybusiness'
config={
{
url: "https://www.benramey.com" + data.sitePage.path,
url: "https://www.benramey.com" + post.slug,
identifier: post.slug,
title: post.frontmatter.title,
language: 'en_US'

View File

@@ -15,7 +15,7 @@ export default function Tag({ data, pageContext }) {
return (
<Page headerInfo={siteMetadata} pageTitle={'Tag: ' + tag + ' (' + totalCount + ')'}>
<p>
All posts tagged with "{tag}";
All posts tagged with "{tag}".
</p>
<ul>
{postListItems}