-
Notifications
You must be signed in to change notification settings - Fork 137
/
codesandbox.js
200 lines (186 loc) · 5.6 KB
/
codesandbox.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
const versions = require('../versions.json');
const overpass = require('./fonts');
const { parse } = require('../helpers/acorn');
const { capitalize } = require('./capitalize');
const getStaticParams = (title, html) => {
const imgAssetRegex = /['"](\/assets\/images\/.*)['"]/g;
let imgAsset;
while ((imgAsset = imgAssetRegex.exec(html))) {
const imgName = imgAsset[1];
html = html.replace(imgName, `https://www.patternfly.org/v4${imgName}`);
}
return {
files: {
'index.html': {
content: `
<!DOCTYPE html>
<html lang="en" class="pf-m-redhat-font">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="fonts.css" />
<!-- Include latest PatternFly CSS via CDN -->
<link
rel="stylesheet"
href="https://unpkg.com/@patternfly/patternfly/patternfly.css"
crossorigin="anonymous"
>
<link rel="stylesheet" href="style.css" />
<title>PatternFly ${title} CodeSandbox Example</title>
</head>
<body>
${html}
</body>
</html>
`,
},
'package.json': {
content: {},
},
'style.css': {
content: ''
},
'fonts.css': {
content: overpass
},
'sandbox.config.json': {
content: { template: 'static' }
}
},
template: 'static',
}
};
// Allow 4 formats for example identifiers
// 1. Example = () => { return <jsx />; }
// 2. class Example {}
// 3. function Example() { return <jsx />; }
// 4. const Example = () => { return <jsx />; }
const allowedIdentifiers = [
'ClassDeclaration',
'FunctionDeclaration',
'ExpressionStatement',
'VariableDeclaration'
];
function getExampleDeclaration(code) {
code = code
.replace(/export\s+default\s+/g, '')
.replace(/export\s+/g, '');
const { body } = parse(code);
const lastParsed = body[body.length - 1];
if (allowedIdentifiers.includes(lastParsed.type)) {
return lastParsed;
}
}
function getIdentifier(title) {
return capitalize(
title
.replace(/^[^A-Za-z]/, '')
.replace(/\s+([a-z])?/g, (_, match) => match ? capitalize(match) : '')
.replace(/[^A-Za-z0-9_]/g, '')
);
}
function prettyExampleCode(title, code, declaration, identifier) {
// Create identifier from title
const ident = identifier || getIdentifier(title);
const jsxBlock = code.substring(declaration.start, declaration.end);
if (identifier) {
return code.replace(jsxBlock, `const ${jsxBlock}`);
}
if (jsxBlock.includes('\n')) {
// Make pretty
return code.replace(jsxBlock, `const ${ident} = () => (\n ${
jsxBlock
.replace(/\n/g, '\n ')
.replace(/;[ \t]*$/, '')
}\n)`);
}
else {
return code.replace(jsxBlock, `const ${ident} = () => ${jsxBlock}`);
}
}
// TODO: Make React examples work and use a template that has our assets.
function getReactParams(title, code, scope) {
let toRender = null;
try {
let declaration = getExampleDeclaration(code);
if (declaration.type === 'ExpressionStatement') {
if (!declaration.expression.left) {
// () => <jsx />
code = prettyExampleCode(title, code, declaration);
toRender = getIdentifier(title);
} else if (declaration.expression.type === 'AssignmentExpression') {
// Basic = () => <jsx />
code = prettyExampleCode(title, code, declaration, declaration.expression.left.name);
toRender = declaration.expression.left.name;
}
} else if (declaration.type === 'VariableDeclaration') {
toRender = declaration.declarations[0].id.name;
} else if (declaration.id) {
toRender = declaration.id.name;
}
}
catch (err) {
// Ignore
}
const imgImportRegex = /import\s*(\w*).*['"](.*)(\.(png|jpe?g|webp|gif|svg))['"]/g;
let imgImportMatch;
while ((imgImportMatch = imgImportRegex.exec(code))) {
const imgName = imgImportMatch[1];
code = code.replace(imgImportMatch[0], `const ${imgName} = "https://www.patternfly.org/v4${scope[imgName]}"`);
}
const dependencies = {
'@patternfly/react-core': versions.Releases[0].versions['@patternfly/react-core']
};
Object.entries(versions.Releases[0].versions)
.filter(([pkg]) => code.includes(pkg))
.forEach(([pkg, version]) => dependencies[pkg] = version);
return {
files: {
'index.html': {
content: `<!DOCTYPE html>
<html lang="en" class="pf-m-redhat-font">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>PatternFly-React ${title} CodeSandbox Example</title>
</head>
<body>
<noscript>
You need to enable JavaScript to run this app.
</noscript>
<div id="root" style="height:100%"></div>
</body>
</html>`,
},
'index.js': {
content: `import ReactDOM from 'react-dom';
import "@patternfly/react-core/dist/styles/base.css";
import './fonts.css';
${code}
const rootElement = document.getElementById("root");
ReactDOM.render(<${toRender} />, rootElement);`
},
'fonts.css': {
content: overpass
},
'package.json': {
content: {
dependencies: {
...dependencies,
'react': '^16.8.0',
'react-dom': '^16.8.0'
}
},
},
'sandbox.config.json': {
content: { template: 'create-react-app' }
}
},
}
}
module.exports = {
getReactParams,
getStaticParams,
getExampleDeclaration,
prettyExampleCode
};