Skip to content

Commit 54028c9

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. Test plan: pages reported in #7994 no longer crash on the next PR Closes #7994
1 parent 2a9ef2d commit 54028c9

File tree

1 file changed

+75
-4
lines changed

1 file changed

+75
-4
lines changed

src/components/MDX/Sandpack/createFileMap.ts

Lines changed: 75 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,18 @@ 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+
console.log(props.meta, tokens);
95+
const name = tokens.find(
96+
(token) => token.includes('/') || token.includes('.')
97+
);
98+
if (name) {
99+
filePath = name.startsWith('/') ? name : `/${name}`;
100+
}
101+
if (tokens.includes('hidden')) {
43102
fileHidden = true;
44103
}
45-
if (params.includes('active')) {
104+
if (tokens.includes('active')) {
46105
fileActive = true;
47106
}
48107
} else {
@@ -57,6 +116,18 @@ export const createFileMap = (codeSnippets: any) => {
57116
}
58117
}
59118

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

0 commit comments

Comments
 (0)