gatsby-theme-advanced
Version:
GatsbyJS theme equipped with advanced features.
131 lines (130 loc) • 6.21 kB
JavaScript
;
/* eslint "no-console": "off" */
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createPages = exports.createSchemaCustomization = exports.onCreateNode = void 0;
const lodash_1 = require("lodash");
const url_join_1 = __importDefault(require("url-join"));
const queries_1 = require("./utils/queries");
const feeds_1 = require("./utils/feeds");
const getRelatedPosts_1 = __importDefault(require("./utils/getRelatedPosts"));
const config_1 = require("../src/config");
const POST_PAGE_COMPONENT = require.resolve("../src/templates/post/query.js");
// Generates a slug from provided frontmatter/file path
const generateSlug = (frontmatter) => {
if (frontmatter) {
const { slug, title } = frontmatter;
if (slug)
return `/${(0, lodash_1.kebabCase)(slug)}`;
if (title)
return `/${(0, lodash_1.kebabCase)(title)}`;
}
console.error("Missing post slug and title. Unable to generate a slug.");
return undefined;
};
// Gets invoked on GraphQl node creation
const onCreateNode = ({ node, actions }, userConfig) => {
const config = (0, config_1.withDefaults)(userConfig);
// Filter by Mdx nodes
if (node.internal.type === "Mdx" && node.parent) {
// Parse the frontmatter
const frontmatter = node.frontmatter;
// Generate a slug
const slug = generateSlug(frontmatter);
if (!slug) {
console.error("onCreateNode: Failed to generate a slug for an MDX post. Aborting.");
return;
}
// Route is the pathName without the pathPrefix, used for creating pages
const route = (0, config_1.withBasePath)(config, slug);
// Pathname is used for internal linking
const pathName = (0, url_join_1.default)(config.pathPrefix, route);
// URL is the absolute website url to the post
const url = (0, url_join_1.default)(config.website.url, pathName);
// Set route/url/pathName/slug fields
actions.createNodeField({ node, name: "slug", value: slug });
actions.createNodeField({ node, name: "route", value: route });
actions.createNodeField({ node, name: "pathName", value: pathName });
actions.createNodeField({ node, name: "url", value: url });
}
};
exports.onCreateNode = onCreateNode;
// Customize Gatsby schema
const createSchemaCustomization = ({ actions }) => {
actions.createTypes(`#graphql
${config_1.schema}
`);
};
exports.createSchemaCustomization = createSchemaCustomization;
// Gets invoked on page creation stage
const createPages = ({ graphql, actions }, themeOptions) => __awaiter(void 0, void 0, void 0, function* () {
const config = (0, config_1.withDefaults)(themeOptions);
// Create lists of unique categories and tags
const tagSet = new Set();
const categorySet = new Set();
// Initialize feed metadata
(0, feeds_1.initFeedMeta)();
// Get full post listing
const fullListing = yield (0, queries_1.getIndexListing)(graphql);
// Iterate over posts
fullListing.forEach((post, index) => {
// Add post tags to our set
const { tags } = post;
if (tags) {
tags.forEach((tag) => {
tagSet.add(tag);
});
}
// Add post category to our set
const { category } = post;
if (category) {
categorySet.add(category);
}
// Link the post page to next and previous pages
const nextID = index + 1 < fullListing.length ? index + 1 : 0;
const prevID = index - 1 >= 0 ? index - 1 : fullListing.length - 1;
const nextPost = fullListing[nextID];
const prevPost = fullListing[prevID];
// Posts related to the current post
const relatedPosts = (0, getRelatedPosts_1.default)(post, fullListing);
// Create a post page
actions.createPage({
path: post.route,
component: POST_PAGE_COMPONENT,
context: {
slug: post.slug,
nexttitle: nextPost === null || nextPost === void 0 ? void 0 : nextPost.title,
nextslug: nextPost === null || nextPost === void 0 ? void 0 : nextPost.slug,
prevtitle: prevPost === null || prevPost === void 0 ? void 0 : prevPost.title,
prevslug: prevPost === null || prevPost === void 0 ? void 0 : prevPost.slug,
relatedPosts,
},
});
});
// Create a main "index" feed
yield (0, feeds_1.createFeed)(config, actions, fullListing, "index");
// Create tag listing feeds based on our set
const tagTasks = Array.from(tagSet.keys()).map((tag) => __awaiter(void 0, void 0, void 0, function* () {
const tagListing = yield (0, queries_1.getTagListing)(graphql, tag);
yield (0, feeds_1.createFeed)(config, actions, tagListing, "tag", tag);
}));
yield Promise.all(tagTasks);
// Create category listing feeds based on our set
const categoryTasks = Array.from(categorySet.keys()).map((category) => __awaiter(void 0, void 0, void 0, function* () {
const categoryListing = yield (0, queries_1.getCategoryListing)(graphql, category);
yield (0, feeds_1.createFeed)(config, actions, categoryListing, "category", category);
}));
yield Promise.all(categoryTasks);
});
exports.createPages = createPages;