forked from rocicorp/zero-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeBlock.tsx
More file actions
29 lines (24 loc) · 789 Bytes
/
CodeBlock.tsx
File metadata and controls
29 lines (24 loc) · 789 Bytes
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
'use client';
import React, {useEffect} from 'react';
import hljs from 'highlight.js';
type CodeBlockProps = {
code: string;
language: string; // Highlight.js language name (e.g., "javascript", "typescript")
};
const CodeBlock: React.FC<CodeBlockProps> = ({code, language}) => {
const highlightedCode = React.useMemo(() => {
// Pre-highlight the code before rendering
const validLanguage = hljs.getLanguage(language) ? language : 'plaintext';
return hljs.highlight(code, {language: validLanguage}).value;
}, [code, language]);
return (
<pre>
{/* Render pre-highlighted HTML */}
<code
className={`hljs language-${language}`}
dangerouslySetInnerHTML={{__html: highlightedCode}}
/>
</pre>
);
};
export default CodeBlock;