forked from keep-network/tbtc-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
160 lines (136 loc) · 4.55 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
const fs = require('fs')
const path = require(`path`)
const { createFilePath } = require('gatsby-source-filesystem')
const { fmImagesToRelative } = require("gatsby-remark-relative-images")
const { parseBaseConfig, writeConfig } = require('./src/cms/config/index.js')
const config = require('./gatsby-config')
const { defaultLocale, supportedLocales } = config.siteMetadata
exports.onPreBootstrap = () => {
const base = parseBaseConfig()
const fileCollections =
base.collections.filter(c => c.name === "pages" || c.name === "header")
const files = fileCollections
.reduce((acc, curr) => acc.concat(curr.files.map(f => f.file )), [])
const nonDefaultLocales = supportedLocales.filter(l => l !== defaultLocale)
// Generate markdown pages for each locale for each file if they do not
// already exist. File collections do not allow admin editors to create new
// items in the collection. Each page must be explicitly added.
// https://www.netlifycms.org/docs/collection-types/#file-collections
// Since they are a copy, they will have English by default until edited.
nonDefaultLocales.forEach(locale => {
files.forEach(file => {
const localeFile = file.replace(/(.*)\.(.*)$/, `$1.${locale}.$2`)
if (!fs.existsSync(localeFile)) {
fs.copyFileSync(file, localeFile)
}
})
})
}
exports.createPages = async ({ actions, graphql, reporter }) => {
const { createPage } = actions
const result = await graphql(`
{
allMarkdownRemark(
sort: { order: DESC, fields: [frontmatter___date] }
limit: 1000
filter: { fileAbsolutePath: { regex: "/(pages)/" } }
) {
edges {
node {
id
fields {
locale
slug
}
frontmatter {
template
}
}
}
}
}
`)
// Handle errors
if (result.errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`)
return
}
// Render pages based on their frontmatter template, if available, or a
// default template at src/templates/default.js
result.data.allMarkdownRemark.edges.forEach(({ node }) => {
const templateName = node.frontmatter.template || `default`
const template = path.resolve(path.join('src/templates/', `${templateName}.js`))
createPage({
path: node.fields.slug,
component: template,
// additional data can be passed via context
context: { id: node.id, locale: node.fields.locale },
})
})
}
function localeFromFilename(filename) {
const potentialMatch = filename.match(/.*\.(\w+)\.\w+$/i)
return potentialMatch && potentialMatch[1] || null
}
function localePath(path, locale) {
let pathComponents = path.split('/').filter(s => s !== '')
let last = pathComponents.pop()
if (last === `index.${locale}`) {
last = ''
} else {
last = last.replace(new RegExp(`\\.${locale}$`, 'i'), '')
}
pathComponents.push(last)
return `/${locale}/${pathComponents.join('/')}`
}
exports.onCreateNode = async ({ graphql, node, actions, getNode }) => {
const { createNodeField } = actions
// fmImagesToRelative is changing frontmatter links to relative links >:/
if (!node.frontmatter ||
(node.frontmatter && node.frontmatter.template !== `header-nav`)) {
fmImagesToRelative(node) // convert image paths for gatsby images
}
// set a slug for all markdown nodes, and use any supported 639-1 language
// code prepended to the file extension to set a language-specific URL root
if (node.internal.type === `MarkdownRemark`) {
let locale = localeFromFilename(node.fileAbsolutePath)
if (!supportedLocales.includes(locale)) {
locale = defaultLocale
}
let slug = createFilePath({ node, getNode })
if (locale !== defaultLocale) {
slug = localePath(slug, locale)
}
createNodeField({
name: `slug`,
node,
value: slug,
})
createNodeField({
name: `locale`,
node,
value: locale,
})
}
// create new pages for non-default localeuages
else if (node.internal.type === 'SitePage') {
let locale = defaultLocale
if (node.component) {
locale = localeFromFilename(node.component)
if(!supportedLocales.includes(locale)) {
locale = defaultLocale
} else if (locale !== defaultLocale) {
node.path = localePath(path, locale)
}
}
createNodeField({
name: `locale`,
node,
value: locale,
})
}
}
exports.onPostBootstrap = () => {
// generate the netlify config
writeConfig(path.join(__dirname, './static/admin/config.yml'))
}