Skip to content

Commit 6c6690d

Browse files
fix(repl): Add syntax highlighting and fix styling for Python REPL
Integrated react-syntax-highlighter into PythonREPL to ensure code blocks within the REPL are properly highlighted and consistent with the site theme. Also cleaned up whitespace in the MDX source.
1 parent 28041b6 commit 6c6690d

4 files changed

Lines changed: 136 additions & 9 deletions

File tree

package-lock.json

Lines changed: 113 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"react-dom": "^19.2.0",
2323
"react-py": "^1.11.5",
2424
"react-router-dom": "^7.13.0",
25+
"react-syntax-highlighter": "^16.1.0",
2526
"rehype-highlight": "^7.0.2",
2627
"rehype-katex": "^7.0.1",
2728
"remark-gfm": "^4.0.1",
@@ -35,6 +36,7 @@
3536
"@types/react": "^19.2.5",
3637
"@types/react-dom": "^19.2.3",
3738
"@types/react-router-dom": "^5.3.3",
39+
"@types/react-syntax-highlighter": "^15.5.13",
3840
"@vitejs/plugin-react": "^5.1.1",
3941
"eslint": "^9.39.1",
4042
"eslint-plugin-react": "^7.37.5",

src/components/PythonREPL.tsx

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import React, { useEffect, useState } from 'react';
22
import { usePython } from 'react-py';
33
import { Box, Button, CircularProgress, Typography } from '@mui/material';
44
import PlayArrowIcon from '@mui/icons-material/PlayArrow';
5+
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
6+
import { atomDark } from 'react-syntax-highlighter/dist/esm/styles/prism';
57

68
interface PythonREPLProps {
79
code: string;
@@ -21,7 +23,7 @@ export const PythonREPL: React.FC<PythonREPLProps> = ({ code: initialCode, packa
2123
}, [stderr]);
2224

2325
return (
24-
<Box sx={{ my: 4, border: '1px solid #444', borderRadius: 2, overflow: 'hidden' }}>
26+
<Box sx={{ my: 4, border: '1px solid #444', borderRadius: 2, overflow: 'hidden', bgcolor: '#1e1e1e' }}>
2527
<Box sx={{ bgcolor: '#2d2d2d', p: 1, borderBottom: '1px solid #444', display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
2628
<Typography variant="caption" sx={{ color: '#aaa', ml: 1 }}>Python 3 (WASM)</Typography>
2729
<Button
@@ -35,18 +37,29 @@ export const PythonREPL: React.FC<PythonREPLProps> = ({ code: initialCode, packa
3537
}}
3638
disabled={isLoading || isRunning}
3739
>
38-
Run
40+
{isRunning ? 'Running...' : 'Run'}
3941
</Button>
4042
</Box>
41-
<Box component="pre" sx={{ m: 0, p: 2, bgcolor: '#1e1e1e', overflowX: 'auto', fontSize: '0.9rem', fontFamily: 'monospace' }}>
42-
<code>{initialCode}</code>
43-
</Box>
43+
44+
<SyntaxHighlighter
45+
language="python"
46+
style={atomDark}
47+
customStyle={{
48+
margin: 0,
49+
padding: '1.5rem',
50+
fontSize: '0.95rem',
51+
backgroundColor: 'transparent'
52+
}}
53+
>
54+
{initialCode.trim()}
55+
</SyntaxHighlighter>
56+
4457
{output.length > 0 && (
4558
<Box sx={{ p: 2, bgcolor: '#000', borderTop: '1px solid #444' }}>
4659
<Typography variant="overline" color="gray">Output:</Typography>
47-
<pre style={{ margin: 0, whiteSpace: 'pre-wrap', color: '#4caf50' }}>
60+
<Box component="pre" sx={{ m: 0, mt: 1, whiteSpace: 'pre-wrap', color: '#4caf50', fontFamily: 'monospace', fontSize: '0.9rem' }}>
4861
{output.join('\n')}
49-
</pre>
62+
</Box>
5063
</Box>
5164
)}
5265
</Box>

src/content/notes/rendering-math.mdx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ point = np.array([10, 10, 20])
2121
projected = perspective_projection(point)
2222
2323
print(f"3D Point: {point}")
24-
print(f"2D Projected: {projected}")
25-
`}
24+
print(f"2D Projected: {projected}")`}
2625
/>
2726

2827
The formula used above is a simplified version of the perspective projection matrix used in WebGL:

0 commit comments

Comments
 (0)