forked from etclabscore/gatsby-theme-pristine
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gatsby-node.js
137 lines (128 loc) · 3.8 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
const path = require("path");
const _ = require("lodash");
const slash = require(`slash`)
const defaultTemplate = path.resolve(__dirname, "src/templates/default.tsx");
function findDoc(doc) {
if (!doc.link) return null
return (
doc.link === this.link ||
doc.link === this.link.substring(0, this.link.length - 1)
)
}
function getSibling(index, list, direction) {
if (direction === `next`) {
const next = index === list.length - 1 ? null : list[index + 1]
return next
} else if (direction === `prev`) {
const prev = index === 0 ? null : list[index - 1]
return prev
} else {
reporter.warn(
`Did not provide direction to sibling function for building next and prev links`
)
return null
}
}
exports.createPages = ({ graphql, actions, reporter }) => {
const { createPage, createRedirect } = actions
return new Promise((resolve, reject) => {
// Query for markdown nodes to use in creating pages.
graphql(`
query {
site {
siteMetadata {
title
menuLinks {
name
link
ignoreNextPrev
}
}
}
allMdx(
sort: { order: ASC, fields: [fields___slug] }
limit: 10000
filter: { fileAbsolutePath: { ne: null } }
) {
edges {
node {
fields {
slug
}
frontmatter {
title
}
}
}
}
}
`).then((result) => {
if (result.errors) {
return reject(result.errors)
}
const allPages = result.data.allMdx.edges;
const links = result.data.site.siteMetadata.menuLinks;
allPages.forEach(({ node }) => {
const slug = _.get(node, `fields.slug`);
if (!slug) return;
const docIndex = links.findIndex(findDoc, {
link: slug
});
let nextAndPrev = {}
if (docIndex > -1) {
nextAndPrev.prev = links[docIndex - 1] || null;
nextAndPrev.next = links[docIndex + 1] || null;
}
if (nextAndPrev.prev && nextAndPrev.prev.ignoreNextPrev) {
delete nextAndPrev.prev;
}
if (nextAndPrev.next && nextAndPrev.next.ignoreNextPrev) {
delete nextAndPrev.next;
}
createPage({
path: `${node.fields.slug}`, // required
component: slash(defaultTemplate),
context: {
slug: node.fields.slug,
...nextAndPrev,
},
});
});
resolve();
});
})
};
// Create slugs for files, set released status for blog posts.
exports.onCreateNode = ({ node, actions, getNode, reporter }) => {
const { createNodeField } = actions
const isMarkdown = [`MarkdownRemark`, `Mdx`].includes(node.internal.type) && getNode(node.parent).internal.type === `File`
const isTypescriptJSX = node.extension === "tsx" && node.internal.type === "File"
if (isTypescriptJSX) {
const parsedFilePath = path.parse(node.relativePath)
if (parsedFilePath.name !== `index` && parsedFilePath.dir !== ``) {
slug = `/${parsedFilePath.dir}/${parsedFilePath.name}`
} else if (parsedFilePath.dir === ``) {
slug = `/${parsedFilePath.name}`
} else {
slug = `/${parsedFilePath.dir}`
}
if (slug) {
return createNodeField({ node, name: `slug`, value: slug })
}
}
if (isMarkdown) {
const parsedFilePath = path.parse(node.fileAbsolutePath)
if (parsedFilePath.name !== `index` && parsedFilePath.dir !== ``) {
slug = `/${parsedFilePath.name}`
} else {
slug = `/${parsedFilePath.name}`
}
return createNodeField({ node, name: `slug`, value: slug });
}
return null;
}