/
gatsby-node.js
91 lines (79 loc) · 1.85 KB
/
gatsby-node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const BLOG_TEMPLATE_PATH = "./src/templates/blog_template.tsx";
const PAGE_TEMPLATE_PATH = "./src/templates/page_template.tsx";
const TAG_TEMPLATE_PATH = "./src/templates/tag_template.tsx";
function onCreateNode({ node, getNode, actions }) {
const { createNodeField } = actions;
if(node.internal.type !== "Mdx") {
return;
}
const { sourceInstanceName } = getNode(node.parent);
createNodeField({
node,
name: "source",
value: sourceInstanceName
});
}
async function createPages({ actions, graphql }) {
const { data } = await graphql(`
query {
allMdx {
nodes {
id
fields {
source
}
frontmatter {
title
date(formatString: "MMMM D, YYYY")
author
tags
description
image {
publicURL
childImageSharp {
gatsbyImageData
}
}
}
body
slug
}
}
}
`);
const uniqueTags = new Set();
data.allMdx.nodes.forEach(node => {
const sourceName = node.fields.source;
const slug = node.slug;
const id = node.id;
let path;
let component;
if(sourceName == "blog") {
const tags = node.frontmatter.tags;
tags.forEach(tag => uniqueTags.add(tag));
path = `/blog/${ slug }`;
component = require.resolve(BLOG_TEMPLATE_PATH);
} else if(sourceName == "page") {
path = `/${ slug }`;
component = require.resolve(PAGE_TEMPLATE_PATH);
}
actions.createPage({
path,
component,
context: {
id
}
});
});
uniqueTags.forEach(tag => {
actions.createPage({
path: `/tag/${ tag }`,
component: require.resolve(TAG_TEMPLATE_PATH),
context: {
tag
}
});
});
}
exports.onCreateNode = onCreateNode;
exports.createPages = createPages;