forked from keep-network/tbtc-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
136 lines (113 loc) · 3.58 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
const path = require(`path`)
const { createFilePath } = require('gatsby-source-filesystem')
const { fmImagesToRelative } = require("gatsby-remark-relative-images")
const writeConfig = require('./src/cms/config/index.js').writeConfig
const config = require('./gatsby-config')
const { defaultLocale, supportedLocales } = config.siteMetadata
exports.createPages = async ({ actions, graphql, reporter }) => {
const { createPage } = actions
const result = await graphql(`
{
allMarkdownRemark(
sort: { order: DESC, fields: [frontmatter___date] }
limit: 1000
) {
edges {
node {
id
fields {
locale
slug
}
frontmatter {
template
}
}
}
}
}
`)
// Handle errors
if (result.errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`)
return
}
// Filter out the content that we don't want pages created for
const postOrPage = result.data.allMarkdownRemark.edges
.filter(edge => edge.node.frontmatter.template !== "announcement");
// Render pages based on their frontmatter template, if available, or a
// default template at src/templates/default.js
postOrPage.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(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') {
const nonDefaultLocale = supportedLocales.filter(l => l !== defaultLocale)
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'))
}