This repository has been archived on 2022-10-07. You can view files and clone it, but cannot push or open issues or pull requests.
skynet-webportal/packages/website/gatsby-node.js

117 lines
2.9 KiB
JavaScript
Raw Normal View History

2021-03-31 17:08:38 +00:00
const path = require(`path`);
const { createFilePath } = require(`gatsby-source-filesystem`);
2021-03-23 12:07:19 +00:00
2021-03-28 14:46:32 +00:00
exports.onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
resolve: {
fallback: {
crypto: require.resolve("crypto-browserify"),
stream: require.resolve("stream-browserify"),
},
},
});
};
2021-03-31 17:08:38 +00:00
exports.createPages = async ({ graphql, actions, reporter }) => {
const { createPage } = actions;
// Define a template for news post
const PostTemplate = path.resolve(`./src/templates/news-post.js`);
2021-03-31 22:37:02 +00:00
// Get all markdown news posts sorted by date and all possible authors
2021-03-31 17:08:38 +00:00
const result = await graphql(
`
{
allMarkdownRemark(sort: { fields: [frontmatter___date], order: ASC }, limit: 1000) {
nodes {
id
fields {
slug
}
}
}
}
`
);
if (result.errors) {
reporter.panicOnBuild(`There was an error loading your news posts`, result.errors);
return;
}
const posts = result.data.allMarkdownRemark.nodes;
// Create news posts pages
// But only if there's at least one markdown file found at "/data/news" (defined in gatsby-config.js)
// `context` is available in the template as a prop and as a variable in GraphQL
if (posts.length > 0) {
posts.forEach((post, index) => {
const previousPostId = index === 0 ? null : posts[index - 1].id;
const nextPostId = index === posts.length - 1 ? null : posts[index + 1].id;
createPage({
path: post.fields.slug,
component: PostTemplate,
context: {
id: post.id,
previousPostId,
nextPostId,
},
});
});
}
};
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions;
if (node.internal.type === `MarkdownRemark`) {
const value = createFilePath({ node, getNode });
createNodeField({
name: `slug`,
node,
value,
});
}
};
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions;
// Explicitly define the siteMetadata {} object
// This way those will always be defined even if removed from gatsby-config.js
// Also explicitly define the Markdown frontmatter
// This way the "MarkdownRemark" queries will return `null` even when no
// news posts are stored inside "/data/news" instead of returning an error
createTypes(`
type SiteSiteMetadata {
author: String
siteUrl: String
social: Social
}
type Author {
name: String
summary: String
}
type Social {
twitter: String
}
type MarkdownRemark implements Node {
frontmatter: Frontmatter
fields: Fields
}
type Frontmatter {
title: String
description: String
date: Date @dateformat
2021-03-31 22:37:02 +00:00
author: String
2021-03-31 17:08:38 +00:00
}
type Fields {
slug: String
}
`);
};