-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathCodeBlock.tsx
More file actions
308 lines (272 loc) · 9 KB
/
CodeBlock.tsx
File metadata and controls
308 lines (272 loc) · 9 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// @ts-ignore
import assets from 'url:../pages/**/*.{png,jpg,svg}' with {env: 'react-client'};
import {cache, ReactNode} from 'react';
import {Code, ICodeProps} from './Code';
import {CodePlatter, FileProvider, Pre} from './CodePlatter';
import {ExampleOutput} from './ExampleOutput';
import {ExpandableCode} from './ExpandableCode';
import {findPackageJSON} from 'module';
import fs from 'fs';
import {highlight, Language} from 'tree-sitter-highlight';
import path from 'path';
import {style} from '@react-spectrum/s2/style' with {type: 'macro'};
import {Tab, TabList, TabPanel, Tabs} from '@react-spectrum/s2';
import {VisualExample, VisualExampleProps} from './VisualExample';
const example = style({
backgroundColor: 'layer-1',
borderRadius: 'xl',
marginY: {
default: 32,
':is([data-example-switcher] *)': 0
},
padding: {
default: 12,
lg: 24
}
});
const standaloneCode = style({
'--code-padding-start': {
type: 'paddingStart',
value: {
default: 12,
lg: 32
}
},
'--code-padding-end': {
type: 'paddingEnd',
value: '--code-padding-start'
},
padding: '--code-padding-start',
marginY: {
default: 32,
':is([data-example-switcher] *)': 0
},
backgroundColor: 'layer-1',
borderRadius: 'xl',
font: {
default: 'code-xs',
lg: 'code-sm'
},
overflow: 'auto'
});
interface CodeBlockProps extends VisualExampleProps {
render?: ReactNode,
children: string,
dir?: string,
files?: string[],
expanded?: boolean,
hidden?: boolean,
includeAllImports?: boolean,
showCoachMark?: boolean
}
export function CodeBlock({render, children, dir, files, expanded, hidden, includeAllImports, ...props}: CodeBlockProps) {
if (hidden) {
return null;
}
let displayCode = children.replace(/(vanilla-starter|tailwind-starter)\//g, './');
if (!render) {
return (
<pre className={standaloneCode}>
<Code {...props} styles={style({display: 'block', width: 'fit', minWidth: 'full'})}>{displayCode}</Code>
</pre>
);
}
let resolveFrom = path.resolve('pages', dir || (props.type === 's2' ? 's2' : 'react-aria'), 'index.tsx');
let downloadFiles = getExampleFiles(resolveFrom, children, props.type);
let code = (
<TruncatedCode maxLines={expanded ? Infinity : 6} {...props}>
{displayCode}
</TruncatedCode>
);
if (props.docs) {
return (
<VisualExample
{...props}
component={render}
files={files}
downloadFiles={downloadFiles}
code={code} />
);
}
let content = (
<FileProvider value={downloadFiles}>
<CodePlatter
type={props.type}
showCoachMark={props.showCoachMark}>
{code}
</CodePlatter>
</FileProvider>
);
return (
<div role="group" aria-label="Example" className={example}>
<ExampleOutput
component={render}
align={props.align} />
<div>
{files ?
<Files
files={includeAllImports ? findAllFiles(files) : files}
maxLines={expanded ? Infinity : 6}
type={props.type}>
{content}
</Files>
: content}
</div>
</div>
);
}
export function CodeBlockBase({children, lang}: {children: string, lang: string}) {
// @ts-ignore
let highlighted = highlight(children, Language[lang.toUpperCase()]);
return (
<pre className="m-0">
<code className="source" dangerouslySetInnerHTML={{__html: highlighted}} />
</pre>
);
}
interface TruncatedCodeProps extends ICodeProps {
children: string,
maxLines?: number
}
function TruncatedCode({children, maxLines = 6, ...props}: TruncatedCodeProps) {
let lines = children.split('\n');
return lines.length > maxLines
? (
<ExpandableCode hasHighlightedLine={/- begin (highlight|focus)/.test(children)}>
<Pre>
<Code {...props}>{children}</Code>
</Pre>
</ExpandableCode>
)
: (
<div className={style({overflow: 'auto'})}>
<Pre>
<Code {...props}>{children}</Code>
</Pre>
</div>
);
}
export function Files({children, files, type, defaultSelected, maxLines}: {children?: ReactNode, files: string[], type?: 'vanilla' | 'tailwind' | 's2', defaultSelected?: string, maxLines?: number}) {
return (
<Tabs
key={files.join('|')}
aria-label="Files"
defaultSelectedKey={defaultSelected || (children ? 'example' : undefined)}
density="compact"
data-files>
<TabList styles={style({marginBottom: 20})}>
{children && <Tab id="example">Example</Tab>}
{files.map(file => <Tab key={file} id={file}>{path.basename(file)}</Tab>)}
</TabList>
{children && <TabPanel id="example" shouldForceMount data-example>{children}</TabPanel>}
{files.map(file => <TabPanel key={file} id={file}><File filename={file} maxLines={maxLines} type={type} /></TabPanel>)}
</Tabs>
);
}
const readFile = cache((file: string) => fs.readFileSync(file, 'utf8'));
export function File({filename, maxLines, type}: {filename: string, maxLines?: number, type?: 'vanilla' | 'tailwind' | 's2'}) {
let contents = readFile(path.isAbsolute(filename) ? filename : path.resolve('../../../', filename)).replace(/(vanilla-starter|tailwind-starter)\//g, './');
return (
<CodePlatter type={type}>
<TruncatedCode lang={path.extname(filename).slice(1)} hideImports={false} maxLines={maxLines}>{contents}</TruncatedCode>
</CodePlatter>
);
}
// Reads files, parses imports, and loads recursively.
export function getFiles(files: string[], type: string | undefined, npmDeps = {}) {
let fileContents = {};
for (let file of findAllFiles(files, npmDeps)) {
let name = path.basename(file);
let contents = readFile(file);
fileContents[name] = contents
.replace(/(vanilla-starter|tailwind-starter)\//g, './')
.replace(/import (.*?) from ['"]url:(.*?)['"]/g, (_, name, specifier) => {
return `const ${name} = '${resolveUrl(specifier, file)}'`;
});
}
if (type === 'tailwind' && !fileContents['index.css']) {
fileContents['index.css'] = readFile(path.resolve('../../../starters/tailwind/src/index.css'));
}
return {files: fileContents, deps: npmDeps};
}
function findAllFiles(files: string[], npmDeps = {}) {
files = files.map(file => path.isAbsolute(file) ? file : path.resolve('../../../', file));
let queue: string[] = [...files];
let allFiles = new Set<string>();
for (let i = 0; i < queue.length; i++) {
let file = queue[i];
let contents = readFile(file);
allFiles.add(file);
let deps = parseFile(file, contents, npmDeps);
for (let dep of deps) {
if (!allFiles.has(dep)) {
queue.push(dep);
}
}
}
let addedFiles = [...allFiles.values()].filter(f => !files.includes(f)).sort();
return [...files, ...addedFiles];
}
function parseFile(file: string, contents: string, npmDeps = {}, urls = {}) {
let deps = new Set<string>();
for (let [, specifier] of contents.matchAll(/import (?:.|\n)*?['"](.+?)['"]/g)) {
specifier = specifier.replace(/(vanilla-starter|tailwind-starter)\//g, (m, s) => 'starters/' + (s === 'vanilla-starter' ? 'docs' : 'tailwind') + '/src/');
if (specifier.startsWith('url:')) {
urls[specifier] = resolveUrl(specifier.slice(4), file);
continue;
}
if (!/^(\.|starters)/.test(specifier)) {
let dep = specifier.startsWith('@') ? specifier.split('/').slice(0, 2).join('/') : specifier.split('/')[0];
npmDeps[dep] ??= '^' + getPackageVersion(dep);
continue;
}
let resolved = specifier.startsWith('.') ? path.resolve(path.dirname(file), specifier) : path.resolve('../../../' + specifier);
if (path.extname(resolved) === '') {
if (fs.existsSync(resolved + '.tsx')) {
resolved += '.tsx';
} else if (fs.existsSync(resolved + '.ts')) {
resolved += '.ts';
}
}
deps.add(resolved);
}
return deps;
}
function getExampleFiles(file: string, contents: string, type: string | undefined) {
let npmDeps = {};
let urls = {};
let fileDeps = parseFile(file, contents, npmDeps, urls);
let {files} = getFiles([...fileDeps], type, npmDeps);
return {files, deps: npmDeps, urls};
}
let packageVersionCache = new Map<string, string>();
function getPackageVersion(pkg: string) {
let version = packageVersionCache.get(pkg);
if (version) {
return version;
}
let p = findPackageJSON(pkg, __filename);
if (p) {
let json = JSON.parse(fs.readFileSync(p, 'utf8'));
packageVersionCache.set(pkg, json.version);
return json.version;
} else {
throw new Error('Could not find package.json for ' + pkg);
}
}
function resolveUrl(specifier: string, file: string) {
let relative = path.relative(path.resolve('pages'), path.dirname(file)).split(/[/\\]/);
let cur = assets;
for (let part of [...relative, ...specifier.slice(2).split('/')]) {
let p = part.split('.');
cur = cur[p[0]];
if (!cur) {
throw new Error('Could not resolve URL ' + specifier);
}
if (p[1]) {
cur = cur[p[1]];
}
}
let publicUrl = process.env.PUBLIC_URL || 'http://localhost:1234';
return publicUrl + cur;
}