-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
118 lines (110 loc) · 3.29 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
const fetch = require('node-fetch');
const fs = require('fs');
const path = require('path');
/* eslint-disable import/no-extraneous-dependencies */
const webpack = require('webpack');
/**
* Fetches the `Build for IBM.com` document from the main repo and constructs as a Gatsby page
*
* @returns {Promise<void>}
* @private
*/
async function createBuildForIBMPage() {
const dest = path.resolve(
__dirname,
'src/pages/developing/building-for-ibm-dotcom'
);
const buildForIBMsrc =
'https://raw.githubusercontent.com/carbon-design-system/carbon-for-ibm-dotcom/main/docs/building-for-ibm-dotcom.md';
const buildForIBMheading = `${dest}/heading.txt`;
const buildForIBMdest = `${dest}/index.mdx`;
if (!fs.existsSync(buildForIBMdest)) {
// Download the `Building for IBM.com` docs page from the Carbon for IBM.com repo
await fetch(buildForIBMsrc, {
method: 'get',
headers: {
'Content-Type': 'text/html',
},
})
.then((response) => response.text())
.then((response) => {
const heading = fs.readFileSync(buildForIBMheading);
const final = response.substring(
response.indexOf('prettier-ignore-end') + 23,
response.length - 1
);
fs.writeFileSync(buildForIBMdest, `${heading}${final}`);
})
.catch((err) => {
console.error(err);
});
}
}
/**
* Fetches the `Carbon CDN Style Helpers` document from the main repo
* and constructs as a Gatsby page
*
* @returns {Promise<void>}
* @private
*/
async function createCDNStyleHelpersPage() {
const dest = path.resolve(
__dirname,
'src/pages/developing/carbon-cdn-style-helpers'
);
const styleHelperssrc =
'https://raw.githubusercontent.com/carbon-design-system/carbon-for-ibm-dotcom/main/packages/web-components/docs/carbon-cdn-style-helpers.md';
const styleHelpersheading = `${dest}/heading.txt`;
const styleHelpersdest = `${dest}/index.mdx`;
if (!fs.existsSync(styleHelpersdest)) {
// Download the `Building for IBM.com` docs page from the Carbon for IBM.com repo
await fetch(styleHelperssrc, {
method: 'get',
headers: {
'Content-Type': 'text/html',
},
})
.then((response) => response.text())
.then((response) => {
const heading = fs.readFileSync(styleHelpersheading);
const final = response.substring(
response.indexOf('prettier-ignore-end') + 23,
response.length - 1
);
fs.writeFileSync(styleHelpersdest, `${heading}${final}`);
})
.catch((err) => {
console.error(err);
});
}
}
exports.createPages = async () => {
await createBuildForIBMPage();
await createCDNStyleHelpersPage();
};
exports.onCreateWebpackConfig = ({ actions }) => {
// Allows importing html files for component code examples
actions.setWebpackConfig({
module: {
rules: [
{
test: /\.md$/,
use: ['html-loader', 'markdown-loader'],
},
{
test: /\.html$/,
use: {
loader: 'html-loader',
options: {
minimize: false,
},
},
},
],
},
plugins: [new webpack.IgnorePlugin({ resourceRegExp: /canvas/ })],
resolve: {
modules: [path.resolve(__dirname, 'src'), 'node_modules'],
},
});
};