fix similar code issues

This commit is contained in:
wpioneer 2017-08-21 01:28:46 +03:00
parent 32c63d4aaf
commit 32534e10e0

View File

@ -19,7 +19,7 @@ exports.createPages = ({ graphql, boundActionCreators }) => {
{ {
allMarkdownRemark( allMarkdownRemark(
limit: 1000, limit: 1000,
filter: { frontmatter: { layout: { eq: "post" }, draft: { ne: true } } }, filter: { frontmatter: { draft: { ne: true } } },
) { ) {
edges { edges {
node { node {
@ -28,6 +28,7 @@ exports.createPages = ({ graphql, boundActionCreators }) => {
} }
frontmatter { frontmatter {
tags tags
layout
category category
} }
} }
@ -42,6 +43,15 @@ exports.createPages = ({ graphql, boundActionCreators }) => {
} }
_.each(result.data.allMarkdownRemark.edges, (edge) => { _.each(result.data.allMarkdownRemark.edges, (edge) => {
if (_.get(edge, 'node.frontmatter.layout') === 'page') {
createPage({
path: edge.node.fields.slug,
component: slash(pageTemplate),
context: {
slug: edge.node.fields.slug
}
});
} else if (_.get(edge, 'node.frontmatter.layout') === 'post') {
createPage({ createPage({
path: edge.node.fields.slug, path: edge.node.fields.slug,
component: slash(postTemplate), component: slash(postTemplate),
@ -49,14 +59,12 @@ exports.createPages = ({ graphql, boundActionCreators }) => {
slug: edge.node.fields.slug slug: edge.node.fields.slug
} }
}); });
});
let tags = []; let tags = [];
_.each(result.data.allMarkdownRemark.edges, (edge) => {
if (_.get(edge, 'node.frontmatter.tags')) { if (_.get(edge, 'node.frontmatter.tags')) {
tags = tags.concat(edge.node.frontmatter.tags); tags = tags.concat(edge.node.frontmatter.tags);
} }
});
tags = _.uniq(tags); tags = _.uniq(tags);
_.each(tags, (tag) => { _.each(tags, (tag) => {
const tagPath = `/tags/${_.kebabCase(tag)}/`; const tagPath = `/tags/${_.kebabCase(tag)}/`;
@ -70,11 +78,10 @@ exports.createPages = ({ graphql, boundActionCreators }) => {
}); });
let categories = []; let categories = [];
_.each(result.data.allMarkdownRemark.edges, (edge) => {
if (_.get(edge, 'node.frontmatter.category')) { if (_.get(edge, 'node.frontmatter.category')) {
categories = categories.concat(edge.node.frontmatter.category); categories = categories.concat(edge.node.frontmatter.category);
} }
});
categories = _.uniq(categories); categories = _.uniq(categories);
_.each(categories, (category) => { _.each(categories, (category) => {
const categoryPath = `/categories/${_.kebabCase(category)}/`; const categoryPath = `/categories/${_.kebabCase(category)}/`;
@ -86,44 +93,7 @@ exports.createPages = ({ graphql, boundActionCreators }) => {
} }
}); });
}); });
resolve();
});
graphql(
`
{
allMarkdownRemark(
limit: 1000,
filter: { frontmatter: { layout: { eq: "page" }, draft: { ne: true } } },
) {
edges {
node {
fields {
slug
} }
frontmatter {
path
}
}
}
}
}
`
).then((result) => {
if (result.errors) {
console.log(result.errors);
reject(result.errors);
}
_.each(result.data.allMarkdownRemark.edges, (edge) => {
createPage({
path: edge.node.fields.slug,
component: slash(pageTemplate),
context: {
slug: edge.node.fields.slug
}
});
}); });
resolve(); resolve();