Skip to content

Commit b7dd856

Browse files
committed
Optimize output
1 parent 0277ffe commit b7dd856

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

beta/src/styles/sandpack.css

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,12 @@ html.dark .sandpack--playground .sp-overlay {
233233
padding: 0;
234234
}
235235

236-
.sandpack--codeblock .cm-line {
236+
.sandpack--codeblock .hl-line {
237237
margin-left: -20px;
238238
padding-left: 20px;
239239
padding-right: 20px;
240+
@apply bg-github-highlight;
241+
@apply dark:bg-opacity-10;
240242
}
241243

242244
/**

beta/src/utils/prepareMDX.tsx

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ if (typeof window !== 'undefined') {
1717
}
1818

1919
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20-
export const PREPARE_MDX_CACHE_BREAKER = 4;
20+
export const PREPARE_MDX_CACHE_BREAKER = 5;
2121
// !!! IMPORTANT !!! Bump this if you change any logic.
2222
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2323

@@ -98,6 +98,7 @@ function highlightCodeBlocksRecursively(
9898
parentPath[parentPath.length - 1] === 'pre' && // Don't highlight inline text
9999
parentPath[parentPath.length - 2] !== 'Sandpack' // Interactive snippets highlight on the client
100100
) {
101+
overrideProps.children = undefined;
101102
overrideProps.highlightedCode = prepareCodeBlockChildren(
102103
props.children,
103104
props.meta
@@ -217,7 +218,7 @@ function prepareCodeBlockChildren(
217218
let buffer = '';
218219
let lineIndex = 0;
219220
let lineOutput = [];
220-
let finalOutput = [];
221+
let finalOutput: Array<React.ReactNode> = [];
221222
for (let i = 0; i < code.length; i++) {
222223
if (tokenEnds.has(i)) {
223224
if (!currentToken) {
@@ -277,30 +278,35 @@ function prepareCodeBlockChildren(
277278
}
278279
}
279280
if (code[i] === '\n') {
280-
lineOutput.push(buffer);
281+
lineOutput.push(buffer, <br />);
281282
buffer = '';
282-
finalOutput.push(
283-
<div
284-
key={lineIndex}
285-
className={'cm-line ' + (highlightedLines.get(lineIndex) ?? '')}>
286-
{lineOutput}
287-
<br />
288-
</div>
289-
);
283+
if (highlightedLines.has(lineIndex)) {
284+
finalOutput.push(
285+
<div
286+
key={lineIndex + 'l'}
287+
className={highlightedLines.get(lineIndex)}>
288+
{lineOutput}
289+
</div>
290+
);
291+
} else {
292+
finalOutput = [...finalOutput, ...lineOutput];
293+
}
290294
lineOutput = [];
291295
lineIndex++;
292296
} else {
293297
buffer += code[i];
294298
}
295299
}
296300
lineOutput.push(buffer);
297-
finalOutput.push(
298-
<div
299-
key={lineIndex}
300-
className={'cm-line ' + (highlightedLines.get(lineIndex) ?? '')}>
301-
{lineOutput}
302-
</div>
303-
);
301+
if (highlightedLines.has(lineIndex)) {
302+
finalOutput.push(
303+
<div key={lineIndex + 'l'} className={highlightedLines.get(lineIndex)}>
304+
{lineOutput}
305+
</div>
306+
);
307+
} else {
308+
finalOutput = [...finalOutput, ...lineOutput];
309+
}
304310
return finalOutput;
305311
}
306312

@@ -371,7 +377,7 @@ function getLineDecorators(
371377
const linesToHighlight = getHighlightLines(meta);
372378
const highlightedLineConfig = linesToHighlight.map((line) => {
373379
return {
374-
className: 'bg-github-highlight dark:bg-opacity-10',
380+
className: 'hl-line',
375381
line,
376382
};
377383
});

0 commit comments

Comments
 (0)