Upgrade to Gatsby v2

This commit is contained in:
alxshelepenok
2018-11-09 20:08:48 +03:00
parent e83dfc6dff
commit 8b92891329
204 changed files with 18708 additions and 3904 deletions

75
gatsby/create-pages.js Normal file
View File

@@ -0,0 +1,75 @@
'use strict';
const path = require('path');
const _ = require('lodash');
const createCategoriesPages = require('./pagination/create-categories-pages.js');
const createTagsPages = require('./pagination/create-tags-pages.js');
const createPostsPages = require('./pagination/create-posts-pages.js');
const createPages = async ({ graphql, actions }) => {
const { createPage } = actions;
// 404
createPage({
path: '/404',
component: path.resolve('./src/templates/not-found-template.js')
});
// Tags list
createPage({
path: '/tags',
component: path.resolve('./src/templates/tags-list-template.js')
});
// Categories list
createPage({
path: '/categories',
component: path.resolve('./src/templates/categories-list-template.js')
});
// Posts and pages from markdown
const result = await graphql(`
{
allMarkdownRemark(
filter: { frontmatter: { draft: { ne: true } } }
) {
edges {
node {
frontmatter {
layout
}
fields {
slug
}
}
}
}
}
`);
const { edges } = result.data.allMarkdownRemark;
_.each(edges, (edge) => {
if (_.get(edge, 'node.frontmatter.layout') === 'page') {
createPage({
path: edge.node.fields.slug,
component: path.resolve('./src/templates/page-template.js'),
context: { slug: edge.node.fields.slug }
});
} else if (_.get(edge, 'node.frontmatter.layout') === 'post') {
createPage({
path: edge.node.fields.slug,
component: path.resolve('./src/templates/post-template.js'),
context: { slug: edge.node.fields.slug }
});
}
});
// Feeds
await createTagsPages(graphql, actions);
await createCategoriesPages(graphql, actions);
await createPostsPages(graphql, actions);
};
module.exports = createPages;

37
gatsby/on-create-node.js Normal file
View File

@@ -0,0 +1,37 @@
'use strict';
const _ = require('lodash');
const { createFilePath } = require('gatsby-source-filesystem');
const onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions;
if (node.internal.type === 'MarkdownRemark') {
if (typeof node.frontmatter.path !== 'undefined') {
createNodeField({
node,
name: 'slug',
value: node.frontmatter.path
});
} else {
const value = createFilePath({ node, getNode });
createNodeField({
name: 'slug',
node,
value,
});
}
if (node.frontmatter.tags) {
const tagSlugs = node.frontmatter.tags.map((tag) => `/tags/${_.kebabCase(tag)}/`);
createNodeField({ node, name: 'tagSlugs', value: tagSlugs });
}
if (node.frontmatter.category) {
const categorySlug = `/categories/${_.kebabCase(node.frontmatter.category)}/`;
createNodeField({ node, name: 'categorySlug', value: categorySlug });
}
}
};
module.exports = onCreateNode;

View File

@@ -0,0 +1,39 @@
'use strict';
const _ = require('lodash');
const path = require('path');
const siteConfig = require('../../config.js');
module.exports = async (graphql, actions) => {
const { createPage } = actions;
const result = await graphql(`
{
allMarkdownRemark(
filter: { frontmatter: { layout: { eq: "post" }, draft: { ne: true } } }
) {
group(field: frontmatter___category) {
fieldValue
totalCount
}
}
}
`);
_.each(result.data.allMarkdownRemark.group, (category) => {
const numPages = Math.ceil(category.totalCount / siteConfig.postsPerPage);
for (let i = 0; i < numPages; i += 1) {
createPage({
path: i === 0 ? `/category/${_.kebabCase(category.fieldValue)}` : `/category/${_.kebabCase(category.fieldValue)}/page/${i}`,
component: path.resolve('./src/templates/category-template.js'),
context: {
page: i,
category: category.fieldValue,
limit: siteConfig.postsPerPage,
skip: i * siteConfig.postsPerPage,
}
});
}
});
};

View File

@@ -0,0 +1,30 @@
'use strict';
const path = require('path');
const siteConfig = require('../../config.js');
module.exports = async (graphql, actions) => {
const { createPage } = actions;
const result = await graphql(`
{
allMarkdownRemark(
filter: { frontmatter: { layout: { eq: "post" }, draft: { ne: true } } }
) { totalCount }
}
`);
const numPages = Math.ceil(result.data.allMarkdownRemark.totalCount / siteConfig.postsPerPage);
for (let i = 0; i < numPages; i += 1) {
createPage({
path: i === 0 ? '/' : `/page/${i}`,
component: path.resolve('./src/templates/index-template.js'),
context: {
page: i,
limit: siteConfig.postsPerPage,
skip: i * siteConfig.postsPerPage,
}
});
}
};

View File

@@ -0,0 +1,39 @@
'use strict';
const _ = require('lodash');
const path = require('path');
const siteConfig = require('../../config.js');
module.exports = async (graphql, actions) => {
const { createPage } = actions;
const result = await graphql(`
{
allMarkdownRemark(
filter: { frontmatter: { layout: { eq: "post" }, draft: { ne: true } } }
) {
group(field: frontmatter___tags) {
fieldValue
totalCount
}
}
}
`);
_.each(result.data.allMarkdownRemark.group, (tag) => {
const numPages = Math.ceil(tag.totalCount / siteConfig.postsPerPage);
for (let i = 0; i < numPages; i += 1) {
createPage({
path: i === 0 ? `/tag/${_.kebabCase(tag.fieldValue)}` : `/tag/${_.kebabCase(tag.fieldValue)}/page/${i}`,
component: path.resolve('./src/templates/tag-template.js'),
context: {
page: i,
tag: tag.fieldValue,
limit: siteConfig.postsPerPage,
skip: i * siteConfig.postsPerPage,
}
});
}
});
};