Skip to content

Commit 5f46bfb

Browse files
committed
Ignore braces when building Sandpack file map
Previously, `createFileMap` split the MDX meta string on spaces and assumed the first token was the filename. Once we prefixed code fences with `{expectedErrors: …}`, it would incorrectly parse the meta and crash. This PR updates createFileMap to skip tokens in the meta containing a start and end brace pair (using a stack to ensure we close on the correct brace) while tokenizing the meta string as expected. I also added a fallback that was previously not handled if the filepath was undefined. Test plan: pages reported in #7994 no longer crash on the next PR Closes #7994
1 parent 2a9ef2d commit 5f46bfb

File tree

1 file changed

+74
-4
lines changed

1 file changed

+74
-4
lines changed

src/components/MDX/Sandpack/createFileMap.ts

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,59 @@ export const AppJSPath = `/src/App.js`;
1616
export const StylesCSSPath = `/src/styles.css`;
1717
export const SUPPORTED_FILES = [AppJSPath, StylesCSSPath];
1818

19+
/**
20+
* Tokenize meta attributes while ignoring brace-wrapped metadata (e.g. {expectedErrors: …}).
21+
*/
22+
function splitMeta(meta: string): string[] {
23+
const tokens: string[] = [];
24+
let current = '';
25+
let depth = 0;
26+
const trimmed = meta.trim();
27+
28+
for (let index = 0; index < trimmed.length; index += 1) {
29+
const char = trimmed[index];
30+
31+
if (char === '{') {
32+
if (depth === 0 && current) {
33+
tokens.push(current);
34+
current = '';
35+
}
36+
depth += 1;
37+
continue;
38+
}
39+
40+
if (char === '}') {
41+
if (depth > 0) {
42+
depth -= 1;
43+
}
44+
if (depth === 0) {
45+
current = '';
46+
}
47+
continue;
48+
}
49+
50+
if (depth > 0) {
51+
continue;
52+
}
53+
54+
if (/\s/.test(char)) {
55+
if (current) {
56+
tokens.push(current);
57+
current = '';
58+
}
59+
continue;
60+
}
61+
62+
current += char;
63+
}
64+
65+
if (current) {
66+
tokens.push(current);
67+
}
68+
69+
return tokens;
70+
}
71+
1972
export const createFileMap = (codeSnippets: any) => {
2073
return codeSnippets.reduce(
2174
(result: Record<string, SandpackFile>, codeSnippet: React.ReactElement) => {
@@ -37,12 +90,17 @@ export const createFileMap = (codeSnippets: any) => {
3790
let fileActive = false; // if the file tab is shown by default
3891

3992
if (props.meta) {
40-
const [name, ...params] = props.meta.split(' ');
41-
filePath = '/' + name;
42-
if (params.includes('hidden')) {
93+
const tokens = splitMeta(props.meta);
94+
const name = tokens.find(
95+
(token) => token.includes('/') || token.includes('.')
96+
);
97+
if (name) {
98+
filePath = name.startsWith('/') ? name : `/${name}`;
99+
}
100+
if (tokens.includes('hidden')) {
43101
fileHidden = true;
44102
}
45-
if (params.includes('active')) {
103+
if (tokens.includes('active')) {
46104
fileActive = true;
47105
}
48106
} else {
@@ -57,6 +115,18 @@ export const createFileMap = (codeSnippets: any) => {
57115
}
58116
}
59117

118+
if (!filePath) {
119+
if (props.className === 'language-js') {
120+
filePath = AppJSPath;
121+
} else if (props.className === 'language-css') {
122+
filePath = StylesCSSPath;
123+
} else {
124+
throw new Error(
125+
`Code block is missing a filename: ${props.children}`
126+
);
127+
}
128+
}
129+
60130
if (result[filePath]) {
61131
throw new Error(
62132
`File ${filePath} was defined multiple times. Each file snippet should have a unique path name`

0 commit comments

Comments
 (0)