-
Notifications
You must be signed in to change notification settings - Fork 82
/
gatsby-node.js
166 lines (153 loc) · 6.07 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
const path = require('path')
const inflection = require('inflection')
const StyleLintPlugin = require('stylelint-webpack-plugin')
const WebpackNotifierPlugin = require('webpack-notifier')
exports.onCreateNode = ({ node, boundActionCreators }) => {
const { createNodeField } = boundActionCreators
const PAGES_BASE_DIR = path.resolve(__dirname, './src/pages')
const PATTERNS_BASE_DIR = path.resolve(__dirname, './src/patternfly/patterns')
const COMPONENTS_BASE_DIR = path.resolve(__dirname, './src/patternfly/components')
const LAYOUTS_BASE_DIR = path.resolve(__dirname, './src/patternfly/layouts')
const isMarkdown = (node.internal.type === 'MarkdownRemark')
if (isMarkdown) {
const isPage = (node.fileAbsolutePath.includes(PAGES_BASE_DIR))
const isPattern = (node.fileAbsolutePath.includes(PATTERNS_BASE_DIR))
const isComponent = (node.fileAbsolutePath.includes(COMPONENTS_BASE_DIR))
const isLayout = (node.fileAbsolutePath.includes(LAYOUTS_BASE_DIR))
if (isPage) {
let relativePath = path.relative(PAGES_BASE_DIR, node.fileAbsolutePath)
let pagePath = `/${relativePath}`.replace(/\.md$/, '')
createNodeField({ node, name: 'path', value: pagePath })
createNodeField({ node, name: 'type', value: 'page' })
createNodeField({ node, name: 'contentType', value: 'page' })
} else if (isComponent) {
let componentName = path.basename(path.dirname(node.fileAbsolutePath))
let componentSlug = inflection.dasherize(componentName)
let pagePath = `/components/${componentName}/docs`
createNodeField({ node, name: 'path', value: pagePath })
createNodeField({ node, name: 'type', value: 'documentation' })
createNodeField({ node, name: 'contentType', value: 'component' })
} else if (isPattern) {
let patternName = path.basename(path.dirname(node.fileAbsolutePath))
let patternSlug = inflection.dasherize(patternName)
let pagePath = `/patterns/${patternName}/docs`
createNodeField({ node, name: 'path', value: pagePath })
createNodeField({ node, name: 'type', value: 'documentation' })
createNodeField({ node, name: 'contentType', value: 'pattern' })
} else if (isLayout) {
let layoutName = path.basename(path.dirname(node.fileAbsolutePath))
let layoutSlug = inflection.dasherize(layoutName)
let pagePath = `/layouts/${layoutName}/docs`
createNodeField({ node, name: 'path', value: pagePath })
createNodeField({ node, name: 'type', value: 'documentation' })
createNodeField({ node, name: 'contentType', value: 'layout' })
}
}
}
exports.createPages = ({ boundActionCreators, graphql }) => {
const { createPage } = boundActionCreators
return graphql(`
{
allMarkdownRemark {
edges {
node {
fields {
type
path
contentType
}
}
}
}
}
`).then(result => {
if (result.errors) {
return Promise.reject(result.errors)
}
return result.data.allMarkdownRemark.edges.forEach(({ node }) => {
createPage({
path: node.fields.path,
component: path.resolve(__dirname, `./src/templates/${node.fields.type}.js`),
context: {
path: node.fields.path,
type: node.fields.type,
contentType: node.fields.contentType
}
})
})
})
}
exports.onCreatePage = async ({ page, boundActionCreators }) => {
const { createPage, createNodeField } = boundActionCreators
const CATEGORY_PAGE_REGEX = /^\/(components|patterns|layouts)\/$/
const CATEGORY_CHILD_PAGE_REGEX = /^\/(components|patterns|layouts)\/([A-Za-z0-9_-]+)/
const DEMO_PAGE_REGEX = /^\/(demos)\/([A-Za-z0-9_-]+)/
return new Promise((resolve, reject) => {
let isCategoryPage = page.path.match(CATEGORY_PAGE_REGEX)
let isCategoryChildPage = page.path.match(CATEGORY_CHILD_PAGE_REGEX)
let isDemoPage = page.path.match(DEMO_PAGE_REGEX)
page.context.type = 'page'
page.context.category = 'page'
page.context.slug = ''
page.context.name = ''
page.context.title = ''
if (isCategoryPage) {
page.context.type = 'category'
page.context.category = page.path.match(CATEGORY_PAGE_REGEX)[1]
} else if (isCategoryChildPage) {
let pageCategory = page.path.match(CATEGORY_CHILD_PAGE_REGEX)[1]
let pageSlug = page.path.match(CATEGORY_CHILD_PAGE_REGEX)[2]
let pageName = pageSlug.replace('-', ' ')
let pageTitle = inflection.titleize(pageName)
page.context.type = inflection.singularize(pageCategory)
page.context.category = pageCategory
page.context.slug = pageSlug
page.context.name = pageName
page.context.title = pageTitle
} else if (isDemoPage) {
let pageCategory = page.path.match(DEMO_PAGE_REGEX)[1]
let pageSlug = page.path.match(DEMO_PAGE_REGEX)[2]
let pageName = pageSlug.replace('-', ' ')
let pageTitle = inflection.titleize(pageName)
page.context.type = inflection.singularize(pageCategory)
page.context.category = pageCategory
page.context.slug = pageSlug
page.context.name = pageName
page.context.title = pageTitle
page.layout = 'demo'
}
createPage(page)
resolve()
})
}
exports.modifyWebpackConfig = ({ config, stage }) => {
config.loader('markdown-loader', function(current) {
current.test = /\.md$/
current.loader = 'html-loader!markdown-loader'
return current
})
config.merge({
resolve: {
alias: {
'@siteComponents': path.resolve(__dirname, './src/_site'),
'@patterns': path.resolve(__dirname, './src/patternfly/patterns'),
'@components': path.resolve(__dirname, './src/patternfly/components'),
'@layouts': path.resolve(__dirname, './src/patternfly/layouts'),
'@project': path.resolve(__dirname, './src')
}
},
plugins: [
new StyleLintPlugin({
configFile: './.stylelintrc',
fix: false,
failOnError: false,
emitErrors: true
}),
new WebpackNotifierPlugin({
title: 'PF-Next',
skipFirstNotification: true
})
]
})
return config
}