forked from andresgalante/patternfly-next
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
205 lines (193 loc) · 7.3 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
const path = require('path');
const inflection = require('inflection');
const StyleLintPlugin = require('stylelint-webpack-plugin');
const WebpackNotifierPlugin = require('webpack-notifier');
const resolveAliases = require('./build/resolveAliases');
const glob = require('glob');
exports.onCreateNode = ({ node, actions }) => {
const { createNodeField } = actions;
const PAGES_BASE_DIR = path.resolve(__dirname, './src/site/pages');
const COMPONENTS_BASE_DIR = path.resolve(__dirname, './src/patternfly/components');
const DEMOS_BASE_DIR = path.resolve(__dirname, './src/patternfly/demos');
const LAYOUTS_BASE_DIR = path.resolve(__dirname, './src/patternfly/layouts');
const UTILITIES_BASE_DIR = path.resolve(__dirname, './src/patternfly/utilities');
const isMarkdown = node.internal.type === 'MarkdownRemark';
if (isMarkdown) {
const isPage = node.fileAbsolutePath.includes(PAGES_BASE_DIR);
const isComponent = node.fileAbsolutePath.includes(COMPONENTS_BASE_DIR);
const isLayout = node.fileAbsolutePath.includes(LAYOUTS_BASE_DIR);
const isDemo = node.fileAbsolutePath.includes(DEMOS_BASE_DIR);
const isUtility = node.fileAbsolutePath.includes(UTILITIES_BASE_DIR);
if (isPage) {
const relativePath = path.relative(PAGES_BASE_DIR, node.fileAbsolutePath);
const 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) {
const componentName = path.basename(path.dirname(node.fileAbsolutePath));
const pagePath = `/components/${componentName}/docs`;
createNodeField({ node, name: 'path', value: pagePath });
createNodeField({ node, name: 'type', value: 'documentation' });
createNodeField({ node, name: 'contentType', value: 'component' });
} else if (isLayout) {
const layoutName = path.basename(path.dirname(node.fileAbsolutePath));
const pagePath = `/layouts/${layoutName}/docs`;
createNodeField({ node, name: 'path', value: pagePath });
createNodeField({ node, name: 'type', value: 'documentation' });
createNodeField({ node, name: 'contentType', value: 'layout' });
} else if (isDemo) {
const demoName = path.basename(path.dirname(node.fileAbsolutePath));
const pagePath = `/demos/${demoName}/docs`;
createNodeField({ node, name: 'path', value: pagePath });
createNodeField({ node, name: 'type', value: 'documentation' });
createNodeField({ node, name: 'contentType', value: 'demo' });
} else if (isUtility) {
const utilityName = path.basename(path.dirname(node.fileAbsolutePath));
const pagePath = `/utilities/${utilityName}/docs`;
createNodeField({ node, name: 'path', value: pagePath });
createNodeField({ node, name: 'type', value: 'documentation' });
createNodeField({ node, name: 'contentType', value: 'utility' });
}
}
};
exports.createPages = ({ actions, graphql }) => {
const { createPage } = actions;
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/site/templates/${node.fields.type}.js`),
layout: 'index',
context: {
pagePath: node.fields.path,
type: node.fields.type,
contentType: node.fields.contentType
}
});
});
});
};
exports.onCreatePage = async ({ page, actions }) => {
const { createPage } = actions;
const CATEGORY_PAGE_REGEX = /^\/(components|layouts|demos|utilities)\/$/;
const CATEGORY_CHILD_PAGE_REGEX = /^\/(components|layouts|demos|utilities)\/([A-Za-z0-9_-]+)/;
return new Promise((resolve, reject) => {
const isCategoryPage = page.path.match(CATEGORY_PAGE_REGEX);
const isCategoryChildPage = page.path.match(CATEGORY_CHILD_PAGE_REGEX);
page.context.type = 'page';
page.context.category = 'page';
page.context.slug = '';
page.context.name = '';
page.context.title = '';
page.layout = 'index';
if (isCategoryPage) {
page.context.type = 'category';
page.context.category = page.path.match(CATEGORY_PAGE_REGEX)[1];
} else if (isCategoryChildPage) {
const pageCategory = page.path.match(CATEGORY_CHILD_PAGE_REGEX)[1];
const pageSlug = page.path.match(CATEGORY_CHILD_PAGE_REGEX)[2];
const pageName = pageSlug.replace('-', ' ');
const 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;
}
createPage(page);
if (isCategoryChildPage) {
// create full demo page for each component
const demoPage = Object.assign({}, page);
demoPage.layout = 'demo';
const nodePath = demoPage.path;
demoPage.path = `${nodePath.substr(0, nodePath.length - 1)}-full/`;
createPage(demoPage);
}
resolve();
});
};
let partialsToLocationsMap = null;
const continueWebpackConfig = ({ stage, actions }) => {
actions.setWebpackConfig({
module: {
rules: [
{
test: /\.md$/,
loader: 'html-loader!markdown-loader'
},
{
test: /\.hbs$/,
query: {
extensions: '.hbs',
partialResolver(partial, callback) {
if (partialsToLocationsMap[partial]) {
callback(null, partialsToLocationsMap[partial]);
} else {
console.log(`Could not find partial: ${partial}`);
callback(new Error(`Could not find partial: ${partial}`), '');
}
},
helperDirs: path.resolve(__dirname, './build/helpers')
},
loader: 'handlebars-loader'
}
]
},
resolve: {
alias: resolveAliases
},
resolveLoader: {
alias: { raw: 'raw-loader' }
},
plugins: [
new StyleLintPlugin({
configFile: './.stylelintrc',
fix: false,
failOnError: false,
emitErrors: true,
files: 'src/**/*.scss',
defaultSeverity: 'error'
}),
new WebpackNotifierPlugin({
title: 'PatternFly 4',
skipFirstNotification: true
})
]
});
};
exports.onCreateWebpackConfig = ({ stage, actions }) =>
new Promise((resolve, reject) => {
if (partialsToLocationsMap === null) {
partialsToLocationsMap = {};
glob(path.resolve(__dirname, './src/patternfly/**/*.hbs'), { ignore: '**/examples/**' }, (err, files) => {
files.forEach(file => {
const fileNameArr = file.split('/');
const fileName = fileNameArr[fileNameArr.length - 1].slice(0, -4);
partialsToLocationsMap[fileName] = file;
});
continueWebpackConfig({ stage, actions });
resolve();
});
} else {
continueWebpackConfig({ stage, actions });
resolve();
}
});