-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathcreateFileMap.ts
More file actions
58 lines (52 loc) · 1.7 KB
/
createFileMap.ts
File metadata and controls
58 lines (52 loc) · 1.7 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
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*/
import type {SandpackFile} from '@codesandbox/sandpack-react/unstyled';
export const AppJSPath = `/src/App.js`;
export const StylesCSSPath = `/src/styles.css`;
export const SUPPORTED_FILES = [AppJSPath, StylesCSSPath];
export const createFileMap = (codeSnippets: any) => {
return codeSnippets.reduce(
(result: Record<string, SandpackFile>, codeSnippet: React.ReactElement) => {
if ((codeSnippet.type as any).mdxName !== 'pre') {
return result;
}
const {props} = codeSnippet.props.children;
let filePath; // path in the folder structure
let fileHidden = false; // if the file is available as a tab
let fileActive = false; // if the file tab is shown by default
if (props.meta) {
const [name, ...params] = props.meta.split(' ');
filePath = '/' + name;
if (params.includes('hidden')) {
fileHidden = true;
}
if (params.includes('active')) {
fileActive = true;
}
} else {
if (props.className === 'language-js') {
filePath = AppJSPath;
} else if (props.className === 'language-css') {
filePath = StylesCSSPath;
} else {
throw new Error(
`Code block is missing a filename: ${props.children}`
);
}
}
if (result[filePath]) {
throw new Error(
`File ${filePath} was defined multiple times. Each file snippet should have a unique path name`
);
}
result[filePath] = {
code: (props.children || '') as string,
hidden: fileHidden,
active: fileActive,
};
return result;
},
{}
);
};