site/gatsby-node.js

90 lines
2.5 KiB
JavaScript
Raw Normal View History

2020-01-15 17:45:50 +03:00
const path = require('path')
exports.createPages = async ({ actions, graphql }) => {
const {createPage} = actions;
2020-03-03 22:30:55 +03:00
2020-02-16 20:41:33 +03:00
const result = await graphql(`
{
courses: allMarkdownRemark(
filter: {frontmatter: {
content_type: {eq: "page_education"},
published: {eq: true}}}
){
edges{
node{
html
frontmatter{
title
path
}
}
}
}
news: allMarkdownRemark(
filter: {frontmatter: {content_type: {eq: "post"}, published: {eq: true}}},
sort: {fields: [frontmatter___date], order: DESC}
) {
2020-01-15 17:45:50 +03:00
edges{
node{
frontmatter{
title
2020-02-16 20:41:33 +03:00
slug
date(formatString: "DD.MM.YYYY")
2020-01-15 17:45:50 +03:00
}
2020-02-16 20:41:33 +03:00
html
2020-01-15 17:45:50 +03:00
}
}
}
}`)
2020-02-16 20:41:33 +03:00
if( result.errors ){ throw result.errors; }
const courseTemplate = path.resolve(`./src/components/templates/courseTemplate.js`);
const courses = result.data.courses.edges;
2020-01-15 17:45:50 +03:00
courses.forEach(({node}) => {
createPage({
path: node.frontmatter.path,
2020-03-03 22:30:55 +03:00
component: courseTemplate,
2020-01-15 17:45:50 +03:00
})
2020-02-16 20:41:33 +03:00
});
///////////////////////////////////////////////////////////////////
const newsTemplate = path.resolve(`./src/components/templates/news.js`);
const posts = result.data.news.edges;
const postsPerPage = 5;
const numberOfNewsItems = posts.length;
const numberOfNewsPages = Math.ceil(numberOfNewsItems / postsPerPage);
for (let pageIndex = 0; pageIndex < numberOfNewsPages; pageIndex++) {
const pageNumber = pageIndex + 1;
const path = pageIndex === 0 ? '/news' : `/news/${pageNumber}`;
const skip = pageIndex * postsPerPage;
function getPreviousPageLink() {
if (!pageIndex) return null
if (pageIndex === 1) return '/news'
return `/news/${pageIndex}`
}
function getNextPageLink() {
if (pageNumber < numberOfNewsPages) return `news/${pageNumber + 1}`
return null
}
createPage({
path,
component: newsTemplate,
context: {
limit: postsPerPage,
skip,
next: getNextPageLink(),
prev: getPreviousPageLink(),
},
})
}
2020-01-15 17:45:50 +03:00
}