-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgatsby-node.js
115 lines (97 loc) · 2.84 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
const { createFilePath } = require('gatsby-source-filesystem')
const _getMarkdownNodeInfo = node => {
// Files are defined with "name-with-dashes.lang.mdx"
// name returns "name-with-dashes.lang"
// So grab the lang from that string
let lang
let isDefault = false
try {
lang = path.basename(node.fileAbsolutePath, ".mdx").split('.')[1]
} finally {
// Check if post.name is "index" -- because that's the file for default language
// (In this case "en")
if (lang === undefined) {
lang = 'en'
isDefault = true
}
}
const name = path.dirname(node.fileAbsolutePath)
return { name, isDefault, lang }
}
exports.onCreateNode = ({ node, actions, getNode, getNodes }) => {
const { createNodeField } = actions
if (node.internal.type === `Mdx`) {
let path = createFilePath({ node, getNode })
const { name, lang, isDefault } = _getMarkdownNodeInfo(node)
const versions = []
// if is default language then load versions in other languages
if (isDefault) {
// generate all versions of the node (including default language version)
getNodes().forEach(other => {
if (other.internal.type === `Mdx`) {
const info = _getMarkdownNodeInfo(other)
if (name === info.name) {
versions.push({
id: other.id,
lang: info.lang,
title: other.frontmatter.title,
date: other.frontmatter.date,
name: info.name,
})
}
}
})
} else {
// remove last element of path for slug (i.e. "/index.es")
let tokens = path.split('/')
tokens.pop()
path = tokens.join('/')
}
createNodeField({ node, name: `slug`, value: path })
createNodeField({ node, name: `isDefault`, value: isDefault })
createNodeField({ node, name: `lang`, value: lang })
createNodeField({ node, name: `versions`, value: versions })
}
}
const path = require("path")
exports.createPages = async ({ graphql, actions, reporter }) => {
const { createPage } = actions
const result = await graphql
(`
query {
allMdx {
edges {
node {
id
body
fields {
slug
isDefault
versions {
id
}
}
}
}
}
}
`)
if (result.errors) {
reporter.panicOnBuild('🚨 ERROR: Loading "createPages" query')
}
// Create blog post pages.
const posts = result.data.allMdx.edges
posts.forEach(({ node }) => {
// Only build pages for default nodes
if (node.fields.isDefault) {
const ids = node.fields.versions.map(node => node.id)
createPage({
path: node.fields.slug,
component: path.resolve(`./src/templates/post.jsx`),
context: {
ids,
},
})
}
})
}