commit 89e2f9c0a32ec5673d1bc0ec29096b7247c9e9c2
parent b715d5bde795b766fe10ddaaf05f48bb94f23319
Author: Agastya Chandrakant <me@hanabi.in>
Date: Wed, 4 Mar 2020 23:34:46 +0530
cat => author
Diffstat:
5 files changed, 97 insertions(+), 99 deletions(-)
diff --git a/gatsby-node.js b/gatsby-node.js
@@ -41,41 +41,40 @@ const createTagPages = function(createPage, posts) {
});
};
-const createCatPages = function(createPage, posts) {
- const allCatsTemplate = path.resolve(
+const createAuthorPages = function(createPage, posts) {
+ const allAuthorsTemplate = path.resolve(
'src',
'templates',
- 'allCatsTemplate.js'
+ 'allAuthorsTemplate.js'
);
- const singleCatTemplate = path.resolve(
+ const singleAuthorTemplate = path.resolve(
'src',
'templates',
- 'singleCatTemplate.js'
+ 'singleAuthorTemplate.js'
);
- const postsByCat = {};
+ const postsByAuthor = {};
posts.forEach(({ node }) => {
- if (node.frontmatter.categories) {
- node.frontmatter.categories.forEach(cat => {
- if (!postsByCat[cat]) postsByCat[cat] = [];
- postsByCat[cat].push(node);
- });
+ if (node.frontmatter.author) {
+ const author = node.frontmatter.author;
+ if (!postsByAuthor[author]) postsByAuthor[author] = [];
+ postsByAuthor[author].push(node);
}
});
- const cats = Object.keys(postsByCat);
+ const authors = Object.keys(postsByAuthor);
createPage({
path: '/authors',
- component: allCatsTemplate,
- context: { cats, postsByCat },
+ component: allAuthorsTemplate,
+ context: { authors, postsByAuthor },
});
- cats.forEach(cat => {
- const posts = postsByCat[cat];
+ authors.forEach(author => {
+ const posts = postsByAuthor[author];
createPage({
- path: `/authors/${cat}`,
- component: singleCatTemplate,
+ path: `/authors/${author}`,
+ component: singleAuthorTemplate,
context: {
- cat,
+ author,
posts,
},
});
@@ -99,7 +98,6 @@ exports.createPages = function({ graphql, actions }) {
node {
frontmatter {
author
- categories
path
tags
title
@@ -112,7 +110,7 @@ exports.createPages = function({ graphql, actions }) {
).then(function(result) {
const posts = result.data.allMarkdownRemark.edges;
createTagPages(createPage, posts);
- createCatPages(createPage, posts);
+ createAuthorPages(createPage, posts);
posts.forEach(function({ node }, index) {
createPage({
path: 'post' + node.frontmatter.path,
diff --git a/src/templates/SingleCatTemplate.js b/src/templates/SingleCatTemplate.js
@@ -1,41 +0,0 @@
-import React from 'react';
-import { Link } from 'gatsby';
-import Layout from '../components/layout';
-import SEO from '../components/seo';
-
-function SingleCatTemplate({ pageContext }) {
- const { posts, cat } = pageContext;
- return (
- <Layout heading={`Author: ${cat}`} link="/authors" slug="authors">
- <SEO title={`Author: ${cat}`} />
- <div className="list-container">
- <ul className="list">
- {posts.map(post => {
- const { author, path, title } = post.frontmatter;
- return (
- <li key={path} className="list-item">
- <article>
- <div className="meta">
- <span>
- <span className="screen-reader">Quote by </span>
- <span>
- <Link to={`/authors/${author}`}>{author}</Link>
- </span>
- </span>
- </div>
- <header className="list-item-header">
- <h3 className="list-item-title">
- <Link to={`/post/${path}`}>{title}</Link>
- </h3>
- </header>
- </article>
- </li>
- );
- })}
- </ul>
- </div>
- </Layout>
- );
-}
-
-export default SingleCatTemplate;
diff --git a/src/templates/allAuthorsTemplate.js b/src/templates/allAuthorsTemplate.js
@@ -0,0 +1,37 @@
+import React from 'react';
+import { Link } from 'gatsby';
+import Layout from '../components/layout';
+import SEO from '../components/seo';
+
+function AllAuthorsTemplate({ pageContext }) {
+ const { authors, postsByAuthor } = pageContext;
+ const count = Object.values(postsByAuthor).map(el => el.length);
+ const [max, min] = [Math.max(...count), Math.min(...count)];
+ return (
+ <Layout heading="Authors" slug="authors">
+ <SEO title="Authors" />
+ <div className="term-cloud-container">
+ <ul className="term-cloud">
+ {authors.map(author => {
+ const numerator = postsByAuthor[author].length - min;
+ const denominator = max - min;
+ const num = denominator ? numerator / denominator : 1;
+ const weight = 100 * Math.round((2 * numerator) / denominator + 3);
+ return (
+ <li key={author}>
+ <Link
+ to={`/authors/${author}`}
+ style={{ fontSize: `${1 + num}em`, fontWeight: `${weight}` }}
+ >
+ {author}
+ </Link>
+ </li>
+ );
+ })}
+ </ul>
+ </div>
+ </Layout>
+ );
+}
+
+export default AllAuthorsTemplate;
diff --git a/src/templates/allCatsTemplate.js b/src/templates/allCatsTemplate.js
@@ -1,37 +0,0 @@
-import React from 'react';
-import { Link } from 'gatsby';
-import Layout from '../components/layout';
-import SEO from '../components/seo';
-
-function AllCatsTemplate({ pageContext }) {
- const { postsByCat, cats } = pageContext;
- const count = Object.values(postsByCat).map(el => el.length);
- const [max, min] = [Math.max(...count), Math.min(...count)];
- return (
- <Layout heading="Authors" slug="authors">
- <SEO title="Authors" />
- <div className="term-cloud-container">
- <ul className="term-cloud">
- {cats.map(cat => {
- const numerator = postsByCat[cat].length - min;
- const denominator = max - min;
- const num = denominator ? numerator / denominator : 1;
- const weight = 100 * Math.round((2 * numerator) / denominator + 3);
- return (
- <li key={cat}>
- <Link
- to={`/authors/${cat}`}
- style={{ fontSize: `${1 + num}em`, fontWeight: `${weight}` }}
- >
- {cat}
- </Link>
- </li>
- );
- })}
- </ul>
- </div>
- </Layout>
- );
-}
-
-export default AllCatsTemplate;
diff --git a/src/templates/singleAuthorTemplate.js b/src/templates/singleAuthorTemplate.js
@@ -0,0 +1,41 @@
+import React from 'react';
+import { Link } from 'gatsby';
+import Layout from '../components/layout';
+import SEO from '../components/seo';
+
+function SingleAuthorTemplate({ pageContext }) {
+ const { posts, author } = pageContext;
+ return (
+ <Layout heading={`Author: ${author}`} link="/authors" slug="authors">
+ <SEO title={`Author: ${author}`} />
+ <div className="list-container">
+ <ul className="list">
+ {posts.map(post => {
+ const { author, path, title } = post.frontmatter;
+ return (
+ <li key={path} className="list-item">
+ <article>
+ <div className="meta">
+ <span>
+ <span className="screen-reader">Quote by </span>
+ <span>
+ <Link to={`/authors/${author}`}>{author}</Link>
+ </span>
+ </span>
+ </div>
+ <header className="list-item-header">
+ <h3 className="list-item-title">
+ <Link to={`/post/${path}`}>{title}</Link>
+ </h3>
+ </header>
+ </article>
+ </li>
+ );
+ })}
+ </ul>
+ </div>
+ </Layout>
+ );
+}
+
+export default SingleAuthorTemplate;