-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
gatsby-node.js
108 lines (96 loc) · 2.5 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
const path = require(`path`)
const webpack = require(`webpack`)
const PATH_TO_MD_PAGES = path.resolve(`src/markdown`)
const PAGE_TEMPLATE = path.resolve(`src/templates/page-template.js`)
const _getMarkdownNodeIdAndLanguage = ({ fileAbsolutePath }) => {
const relativePath = path.relative(PATH_TO_MD_PAGES, fileAbsolutePath)
const toc = relativePath.split("/")
const pageType = toc[0]
const mdfile = toc[toc.length - 1]
const pageId = toc.slice(1, toc.length - 1).join("/")
const lang = path.basename(mdfile, ".md")
return { pageType, pageId, lang }
}
exports.onCreateNode = ({ node, actions }) => {
const { createNodeField } = actions
if (node.internal.type === `MarkdownRemark`) {
const { pageType, pageId, lang } = _getMarkdownNodeIdAndLanguage(node)
const pageData = {
pageId,
type: pageType,
path: `/${pageId}`,
lang,
}
createNodeField({
node,
name: "page",
value: pageData,
})
}
}
// https://stackoverflow.com/a/56710438
exports.onCreatePage = ({ page, actions }) => {
const { createPage, deletePage } = actions
const oldPage = Object.assign({}, page)
page.context.language = page.context.intl.language
if (page.context.language !== oldPage.context.language) {
// Replace new page with old page
deletePage(oldPage)
createPage(page)
}
}
exports.createPages = async ({ graphql, actions, getNode }) => {
const { createPage } = actions
const {
data: {
allMarkdownRemark: { nodes },
},
} = await graphql(`
{
allMarkdownRemark {
nodes {
id
fileAbsolutePath
frontmatter {
title
date
}
html
rawMarkdownBody
}
}
}
`)
nodes.forEach(({ id, fileAbsolutePath }) => {
const node = getNode(id)
const { fields } = node
const {
page: { path: pagePath, lang, pageId },
} = fields
createPage({
path: pagePath,
component: PAGE_TEMPLATE,
context: {
fields,
pageId,
lang,
fileAbsolutePath,
},
})
})
}
// Don't load unneeded formatjs formats
exports.onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
plugins: [
new webpack.NormalModuleReplacementPlugin(
/@formatjs[/\\]intl-relativetimeformat/,
path.resolve(`./src/stub.js`)
),
new webpack.NormalModuleReplacementPlugin(
/@formatjs[/\\]intl-pluralrules/,
path.resolve(`./src/stub.js`)
),
],
})
}