site/gatsby-node.js
2020-03-03 22:30:55 +03:00

90 lines
2.5 KiB
JavaScript

const path = require('path')
exports.createPages = async ({ actions, graphql }) => {
const {createPage} = actions;
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}
) {
edges{
node{
frontmatter{
title
slug
date(formatString: "DD.MM.YYYY")
}
html
}
}
}
}`)
if( result.errors ){ throw result.errors; }
const courseTemplate = path.resolve(`./src/components/templates/courseTemplate.js`);
const courses = result.data.courses.edges;
courses.forEach(({node}) => {
createPage({
path: node.frontmatter.path,
component: courseTemplate,
})
});
///////////////////////////////////////////////////////////////////
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(),
},
})
}
}