Skip to content

Commit

Permalink
Change category to multiple tags
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusbalcytis committed Jan 16, 2019
1 parent bea65a9 commit 4ca375c
Show file tree
Hide file tree
Showing 10 changed files with 307 additions and 305 deletions.
30 changes: 14 additions & 16 deletions gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const _ = require('lodash');
const BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
const path = require('path');
const Promise = require('bluebird');

const {createFilePath} = require(`gatsby-source-filesystem`);

const users = {};
Expand Down Expand Up @@ -59,7 +58,8 @@ exports.createPages = ({graphql, actions}) => {
return new Promise((resolve, reject) => {
const postTemplate = path.resolve("./src/templates/PostTemplate.js");
const pageTemplate = path.resolve("./src/templates/PageTemplate.js");
const categoryTemplate = path.resolve("./src/templates/CategoryTemplate.js");
const tagTemplate = path.resolve("./src/templates/TagTemplate.js");

resolve(
graphql(`
{
Expand All @@ -78,7 +78,7 @@ exports.createPages = ({graphql, actions}) => {
}
frontmatter {
title
category
tags
}
}
}
Expand All @@ -92,28 +92,25 @@ exports.createPages = ({graphql, actions}) => {

const items = result.data.allMarkdownRemark.edges;

// Create category list
const categorySet = new Set();
// Create tag list
const tagSet = new Set();
items.forEach(edge => {
const {
node: {
frontmatter: {category}
frontmatter: {tags}
}
} = edge;

if (category && category !== null) {
categorySet.add(category);
}

(tags || []).forEach(tag => tagSet.add(tag));
});

// Create category pages
const categoryList = Array.from(categorySet);
categoryList.forEach(category => {
// Create tag pages
tagSet.forEach(tag => {
createPage({
path: `/category/${_.kebabCase(category)}/`,
component: categoryTemplate,
path: `/tag/${_.kebabCase(tag)}/`,
component: tagTemplate,
context: {
category
tag
}
});
});
Expand Down Expand Up @@ -174,3 +171,4 @@ exports.onCreateWebpackConfig = ({stage, actions}, options) => {
});
}
};

8 changes: 4 additions & 4 deletions src/components/Blog/Item.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const Item = props => {
fields: { slug, prefix, author, readingTime: {text: readingTime} },
frontmatter: {
title,
category,
cover: cover
cover,
tags
}
}
} = props;
Expand Down Expand Up @@ -46,9 +46,9 @@ const Item = props => {
<span>
<FaUser size={18} /> {author.frontmatter.name}
</span>
{category && (
{tags && tags.map(tag =>
<span>
<FaTag size={18} /> {category}
<FaTag size={18} /> {tag}
</span>
)}
</p>
Expand Down
Loading

0 comments on commit 4ca375c

Please sign in to comment.