2022-01-09 21:12:31 +01:00
|
|
|
import { GatsbyNode } from "gatsby";
|
|
|
|
import { createFilePath } from "gatsby-source-filesystem";
|
2022-09-29 22:28:06 +02:00
|
|
|
import readingTime from "reading-time";
|
2022-01-09 21:12:31 +01:00
|
|
|
|
|
|
|
import * as constants from "./constants";
|
|
|
|
import * as types from "./types";
|
|
|
|
import * as utils from "./utils";
|
|
|
|
|
2022-09-29 22:28:06 +02:00
|
|
|
const onCreateNode: GatsbyNode["onCreateNode"] = ({ node, actions, getNode }) => {
|
2022-01-09 21:12:31 +01:00
|
|
|
const { createNodeField } = actions;
|
|
|
|
|
|
|
|
if (node.internal.type === "MarkdownRemark") {
|
|
|
|
const { frontmatter, parent }: types.Edge["node"] = node;
|
|
|
|
const { tags, category, slug } = frontmatter || {};
|
|
|
|
|
|
|
|
if (slug) {
|
|
|
|
const dirname = parent && getNode(parent)?.relativeDirectory;
|
|
|
|
const value =
|
|
|
|
typeof dirname === "string"
|
|
|
|
? utils.concat("/", dirname, "/", slug)
|
|
|
|
: utils.concat("/", slug);
|
|
|
|
|
|
|
|
createNodeField({ node, name: "slug", value });
|
|
|
|
} else {
|
|
|
|
const value = createFilePath({ node, getNode });
|
|
|
|
createNodeField({ node, name: "slug", value });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tags) {
|
2022-04-24 18:21:11 +02:00
|
|
|
const value = tags.map((tag) =>
|
2022-09-29 22:28:06 +02:00
|
|
|
utils.concat(constants.routes.tagRoute, "/", utils.toKebabCase(tag), "/")
|
2022-01-09 21:12:31 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
createNodeField({ node, name: "tagSlugs", value });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (category) {
|
|
|
|
const value = utils.concat(
|
|
|
|
constants.routes.categoryRoute,
|
|
|
|
"/",
|
|
|
|
utils.toKebabCase(category),
|
2022-09-29 22:28:06 +02:00
|
|
|
"/"
|
2022-01-09 21:12:31 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
createNodeField({ node, name: "categorySlug", value });
|
|
|
|
}
|
2022-09-29 22:28:06 +02:00
|
|
|
|
|
|
|
createNodeField({
|
|
|
|
node,
|
|
|
|
name: "readingTime",
|
|
|
|
value: readingTime(node.rawMarkdownBody as string),
|
|
|
|
});
|
2022-01-09 21:12:31 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export { onCreateNode };
|