-
Notifications
You must be signed in to change notification settings - Fork 126
/
gatsby-node.js
139 lines (117 loc) · 3.15 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
138
139
const path = require('path');
const { createFilePath } = require('gatsby-source-filesystem');
const fetch = (...args) =>
import(`node-fetch`).then(({ default: fetch }) => fetch(...args))
exports.sourceNodes = async ({
actions: { createNode },
createContentDigest,
}) => {
const result = await fetch(`https://api.github.com/repos/decaporg/decap-cms/releases?per_page=3`)
const resultData = await result.json()
createNode({
releases: resultData,
id: `decap-releases`,
parent: null,
children: [],
internal: {
type: `DecapReleases`,
contentDigest: createContentDigest(resultData),
},
})
}
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions;
const docPage = path.resolve('./src/templates/doc-page.js');
const blogPost = path.resolve('./src/templates/blog-post.js');
const featurePage = path.resolve('./src/templates/feature-page.js');
// get all markdown with a frontmatter path field and title
const allMarkdown = await graphql(`
{
allMarkdownRemark(filter: { frontmatter: { title: { ne: null } } }) {
edges {
node {
fields {
slug
}
frontmatter {
title
}
}
}
}
}
`);
if (allMarkdown.errors) {
console.error(allMarkdown.errors); // eslint-disable-line no-console
throw Error(allMarkdown.errors);
}
allMarkdown.data.allMarkdownRemark.edges.forEach(({ node }) => {
const { slug } = node.fields;
let template = docPage;
if (slug.includes('blog/')) {
template = blogPost;
}
if (slug.includes('features/')) {
template = featurePage;
}
createPage({
path: slug,
component: template,
context: {
slug,
},
});
});
// redirects from older URLs
const { createRedirect } = actions
createRedirect({
fromPath: `/docs/add-to-your-site/`,
toPath: `/docs/basic-steps/`,
})
createRedirect({
fromPath: `/chat`,
toPath: `https://discord.gg/KZRDXmTm9v`,
})
};
function pad(n) {
return n >= 10 ? n : `0${n}`;
}
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions;
if (node.internal.type === 'MarkdownRemark') {
const value = createFilePath({ node, getNode });
const { relativePath } = getNode(node.parent);
let slug = value;
if (relativePath.includes('blog/')) {
const date = new Date(node.frontmatter.date);
const year = date.getFullYear();
const month = date.getMonth() + 1;
const filename = path.basename(relativePath, '.md');
slug = `/blog/${year}/${pad(month)}/${filename}`;
createNodeField({
node,
name: 'date',
value: date.toJSON(),
});
}
// used for doc posts
createNodeField({
node,
name: 'slug',
value: slug,
});
// used to create GitHub edit link
createNodeField({
node,
name: 'path',
value: relativePath,
});
}
};
exports.onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
resolve: {
extensions: ['.ts', '.tsx', '.js', '.json'],
},
});
};