-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgatsby-node.ts
More file actions
93 lines (80 loc) · 2.91 KB
/
gatsby-node.ts
File metadata and controls
93 lines (80 loc) · 2.91 KB
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
import type { GatsbyNode } from 'gatsby';
import type { AllMarkdownContentQuery } from 'root/graphql-types';
import type { Edge, GroupedByTypeAndLanguage } from 'src/types';
import { resolve } from 'path';
import { EDGE_TYPES } from './src/constants';
import { EMPTY_OBJECT } from './src/constants/fallbacks';
import { groupByTypeAndLanguage } from './src/utilities/groupByTypeAndLanguage';
export const onCreateBabelConfig: GatsbyNode['onCreateBabelConfig'] = (gatsbyNodeHelpers) => {
gatsbyNodeHelpers.actions.setBabelPlugin({
name: '@babel/plugin-transform-react-jsx',
options: { runtime: 'automatic' },
});
};
export const createPages: GatsbyNode['createPages'] = (args) => {
const {
actions: { createPage },
graphql,
} = args;
const blogPostTemplate = resolve('src/templates/post.tsx');
const pageTemplate = resolve('src/templates/page.tsx');
return graphql<AllMarkdownContentQuery>(`
query AllMarkdownContent {
allMarkdownRemark(sort: { frontmatter: { date: DESC } }) {
edges {
node {
fields {
langKey
slug
}
frontmatter {
language
title
type
}
}
}
}
}
`)
.then((result) => {
if (result.errors) {
throw result.errors;
}
const groupedByTypeAndLanguage =
result.data?.allMarkdownRemark.edges.reduce<GroupedByTypeAndLanguage>(groupByTypeAndLanguage, EMPTY_OBJECT) ??
EMPTY_OBJECT;
Object.keys(groupedByTypeAndLanguage[EDGE_TYPES.PAGE] ?? EMPTY_OBJECT).forEach(
(langKey: Edge['node']['fields']['langKey']) => {
groupedByTypeAndLanguage?.[EDGE_TYPES.PAGE]?.[langKey]?.forEach((edge) => {
const { langKey, slug } = edge.node.fields;
createPage({
component: pageTemplate,
context: { langKey, language: langKey, slug },
path: slug,
});
});
},
);
Object.keys(groupedByTypeAndLanguage[EDGE_TYPES.POST] ?? EMPTY_OBJECT).forEach(
(langKey: Edge['node']['fields']['langKey']) => {
groupedByTypeAndLanguage?.[EDGE_TYPES.POST]?.[langKey]?.forEach((edge, index) => {
const { langKey, slug } = edge.node.fields;
const next = index === 0 ? null : groupedByTypeAndLanguage?.[EDGE_TYPES.POST]?.[langKey]?.[index - 1]?.node;
const previous =
(index === groupedByTypeAndLanguage?.[EDGE_TYPES.POST]?.[langKey]?.length ?? 0 - 1)
? null
: groupedByTypeAndLanguage?.[EDGE_TYPES.POST]?.[langKey]?.[index + 1]?.node;
createPage({
component: blogPostTemplate,
context: { langKey, language: langKey, next, previous, slug },
path: slug,
});
});
},
);
})
.catch((error) => {
throw error;
});
};