-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Expand file tree
/
Copy pathcreateFileMap.ts
More file actions
152 lines (134 loc) · 3.73 KB
/
createFileMap.ts
File metadata and controls
152 lines (134 loc) · 3.73 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
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*/
import type {SandpackFile} from '@codesandbox/sandpack-react/unstyled';
import type {PropsWithChildren, ReactElement, HTMLAttributes} from 'react';
export const AppJSPath = `/src/App.js`;
export const StylesCSSPath = `/src/styles.css`;
export const SUPPORTED_FILES = [AppJSPath, StylesCSSPath];
/**
* Tokenize meta attributes while ignoring brace-wrapped metadata (e.g. {expectedErrors: …}).
*/
function splitMeta(meta: string): string[] {
const tokens: string[] = [];
let current = '';
let depth = 0;
const trimmed = meta.trim();
for (let ii = 0; ii < trimmed.length; ii++) {
const char = trimmed[ii];
if (char === '{') {
if (depth === 0 && current) {
tokens.push(current);
current = '';
}
depth += 1;
continue;
}
if (char === '}') {
if (depth > 0) {
depth -= 1;
}
if (depth === 0) {
current = '';
}
if (depth < 0) {
throw new Error(`Unexpected closing brace in meta: ${meta}`);
}
continue;
}
if (depth > 0) {
continue;
}
if (/\s/.test(char)) {
if (current) {
tokens.push(current);
current = '';
}
continue;
}
current += char;
}
if (current) {
tokens.push(current);
}
if (depth !== 0) {
throw new Error(`Unclosed brace in meta: ${meta}`);
}
return tokens;
}
export const createFileMap = (codeSnippets: any) => {
return codeSnippets.reduce(
(result: Record<string, SandpackFile>, codeSnippet: React.ReactElement) => {
if (
(codeSnippet.type as any).mdxName !== 'pre' &&
codeSnippet.type !== 'pre'
) {
return result;
}
const {props} = (
codeSnippet.props as PropsWithChildren<{
children: ReactElement<
HTMLAttributes<HTMLDivElement> & {meta?: string}
>;
}>
).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 tokens = splitMeta(props.meta);
const name = tokens.find(
(token) => token.includes('/') || token.includes('.')
);
if (name) {
filePath = name.startsWith('/') ? name : `/${name}`;
}
if (tokens.includes('hidden')) {
fileHidden = true;
}
if (tokens.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 (!filePath) {
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;
},
{}
);
};