This repository has been archived by the owner on Jan 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgatsby-node.js
178 lines (157 loc) · 6.01 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
const path = require(`path`);
const { createFilePath } = require(`gatsby-source-filesystem`);
exports.createPages = async ({ graphql, actions, reporter }) => {
const { createPage } = actions;
// Fetch Markdown
const result = await graphql(
`
query {
allFile(
filter: { internal: { mediaType: { eq: "text/markdown" } } }
) {
nodes {
id
sourceInstanceName
childMarkdownRemark {
id
frontmatter {
title
category
type
slug
}
fields {
fileName
language
}
}
}
}
}
`
);
if (result.errors) {
reporter.panicOnBuild(
`There was an error on build loading markdown`,
result.errors
);
return;
}
const { nodes } = result.data.allFile;
const validSources = ["post", "category", "type"];
if (nodes.length > 0) {
nodes.forEach((node /*, index*/) => {
if (validSources.includes(node.sourceInstanceName)) {
const { frontmatter, fields } = node.childMarkdownRemark;
/**
* Prepare links to other languages
*/
let alternatives;
if (node.sourceInstanceName === "post") {
alternatives = nodes
.filter(
(altNode) =>
fields.fileName ===
altNode.childMarkdownRemark.fields
.fileName &&
altNode.sourceInstanceName === "post" &&
altNode.childMarkdownRemark.frontmatter?.type
)
.map((altNode) => {
const {
frontmatter: altFrontmatter,
fields: altFields
} = altNode.childMarkdownRemark;
return {
language: altFields.language,
slug: `${altFrontmatter.type.toLowerCase()}/${
altFrontmatter.slug ?? altFields.fileName
}`
};
});
}
/**
* Prepare Slug
*/
let prepareSlug = [];
// if not default language
if (fields.language !== "fr") {
prepareSlug.push(fields.language);
}
if (node.sourceInstanceName === "post") {
// /articles/... /snippets/...
prepareSlug.push(frontmatter.type.toLowerCase());
}
// slug from frontmatter, else use filename
if (frontmatter.slug) {
prepareSlug.push(`${frontmatter.slug}/`);
} else {
prepareSlug.push(`${fields.fileName}/`);
}
/**
*
* Prepare Template
*
*/
let template;
if (
node.sourceInstanceName === "type" &&
node.childMarkdownRemark.frontmatter.title === "Portfolio"
) {
// TYPE:PORTFOLIO TEMPLATE
template = path.resolve(
`./src/templates/portfolio_template/portfolio_template.jsx`
);
} else if (
node.sourceInstanceName === "post" &&
node.childMarkdownRemark.frontmatter.type === "Portfolio"
) {
// POST:PORTFOLIO TEMPLATE
// SKIP PORTFOLIO ITEM FOR NOW
return;
} else {
template = path.resolve(
`./src/templates/${node.sourceInstanceName}_template/${node.sourceInstanceName}_template.jsx`
);
}
// @todo maybe different createPage per source/type
createPage({
path: prepareSlug.join("/"),
component: template,
context: {
id: node.childMarkdownRemark.id,
title: node.childMarkdownRemark.frontmatter.title,
category: node.childMarkdownRemark.frontmatter.category,
type: node.childMarkdownRemark.frontmatter.type,
language: fields.language,
//previousId,
//nextId,
alternatives
}
});
}
});
}
};
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions;
if (node.internal.type === `MarkdownRemark`) {
const filePath = createFilePath({ node, getNode });
// filePathArray ex.: ['', 'fr', 'a-propos', '']
const filePathArray = filePath.split("/");
const langFromPath = filePathArray[1];
const slugFromPath = filePathArray[2];
// Add language from path to node.fields.language
createNodeField({
name: `language`,
node,
value: langFromPath
});
// Add filename as alternative slug (if none in frontmatter)
createNodeField({
name: `fileName`,
node,
value: slugFromPath
});
}
};