gatsby-theme-advanced
Version:
GatsbyJS theme equipped with advanced features.
111 lines (110 loc) • 5.4 kB
JavaScript
;
/* eslint "no-console": "off" */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.jsonPostIntoPost = exports.queryIntoListing = exports.queryIntoPost = exports.mdxNodeIntoPost = void 0;
// Re-export types
__exportStar(require("./types"), exports);
// Convert MDX based GraphQL query responses into a Post object
function mdxNodeIntoPost(mdxNode) {
var _a, _b, _c, _d, _e, _f, _g, _h;
const { frontmatter } = mdxNode;
if (!frontmatter)
throw Error(`Post missing frontmatter. Post slug: ${((_a = mdxNode.fields) === null || _a === void 0 ? void 0 : _a.slug) || "not defined"}. Aborting.`);
if (!frontmatter.title)
throw Error(`Post missing title. Post slug: ${((_b = mdxNode.fields) === null || _b === void 0 ? void 0 : _b.slug) || "not defined"}. Aborting.`);
if (!frontmatter.datePublished)
throw Error(`Post missing publication date. Post slug: ${((_c = mdxNode.fields) === null || _c === void 0 ? void 0 : _c.slug) || "not defined"}. Aborting.`);
if (!mdxNode.fields)
throw Error(`Post missing fields. Post title: ${frontmatter.title}. Aborting.`);
if (!mdxNode.fields.slug)
throw Error(`Post missing slug. Post title: ${frontmatter.title}. Aborting.`);
if (!mdxNode.fields.pathName)
throw Error(`Post missing pathName. Post slug: ${mdxNode.fields.slug}. Aborting.`);
if (!mdxNode.fields.url)
throw Error(`Post missing url. Post slug: ${mdxNode.fields.slug}. Aborting.`);
if (!mdxNode.fields.route)
throw Error(`Post missing route. Post slug: ${mdxNode.fields.slug}. Aborting.`);
if (!mdxNode.timeToRead)
throw Error(`Post missing timeToRead. Post slug: ${mdxNode.fields.slug}. Aborting.`);
if (!frontmatter.cover)
throw Error(`Post missing cover image. Post slug: ${((_d = mdxNode.fields) === null || _d === void 0 ? void 0 : _d.slug) || "not defined"}. Aborting.`);
if (!frontmatter.coverAlt)
throw Error(`Post missing cover alt. Post slug: ${((_e = mdxNode.fields) === null || _e === void 0 ? void 0 : _e.slug) || "not defined"}. Aborting.`);
if (!frontmatter.description)
console.warn(`Post missing description. Post slug: ${((_f = mdxNode.fields) === null || _f === void 0 ? void 0 : _f.slug) || "not defined"}. SEO capabilities will be limited.`);
const tagList = frontmatter.tags
? frontmatter.tags.filter((tag) => typeof tag !== "undefined")
: [];
return {
title: frontmatter.title,
description: frontmatter.description,
coverImg: (_g = frontmatter.cover.childImageSharp) === null || _g === void 0 ? void 0 : _g.gatsbyImageData,
coverImageUrl: frontmatter.cover.publicURL,
coverImageAlt: frontmatter.coverAlt,
datePublished: new Date(frontmatter.datePublished),
dateModified: new Date(frontmatter.dateModified || frontmatter.datePublished),
category: frontmatter.category,
tags: tagList,
body: mdxNode.body,
internalContent: (_h = mdxNode.internal) === null || _h === void 0 ? void 0 : _h.content,
excerpt: mdxNode.excerpt,
timeToRead: mdxNode.timeToRead,
slug: mdxNode.fields.slug,
route: mdxNode.fields.route,
pathName: mdxNode.fields.pathName,
url: mdxNode.fields.url,
};
}
exports.mdxNodeIntoPost = mdxNodeIntoPost;
// Convert MDX post query into a Post
const queryIntoPost = (data) => {
const postData = data.mdx;
if (!postData)
throw Error("convertPostQueryResponseIntoPost: Query doesn't contain post data. Aborting.");
return mdxNodeIntoPost(postData);
};
exports.queryIntoPost = queryIntoPost;
// Convert MDX based GraphQL query responses into a Post list
const queryIntoListing = (listing) => {
const { edges } = listing.allMdx;
const nodes = edges.map((edge) => edge.node);
return nodes.map((node) => mdxNodeIntoPost(node));
};
exports.queryIntoListing = queryIntoListing;
// Convert PostMeta to a Post
const jsonPostIntoPost = (meta) => {
const { dateModified, datePublished, slug, route, pathName, url, timeToRead, title, category, coverImg, coverImageAlt, coverImageUrl, description, excerpt, tags, relatedPosts, } = meta;
return {
title,
description,
coverImg,
coverImageUrl,
coverImageAlt,
datePublished: new Date(datePublished),
dateModified: new Date(dateModified),
category,
tags,
excerpt,
timeToRead,
slug,
route,
pathName,
url,
relatedPosts: relatedPosts ? relatedPosts.map(exports.jsonPostIntoPost) : undefined,
};
};
exports.jsonPostIntoPost = jsonPostIntoPost;