-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcustome-code-blocks.tsx
43 lines (39 loc) · 1.03 KB
/
custome-code-blocks.tsx
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"use client";
import { ModifiedPre } from "components/code";
import React, { useState, useEffect } from "react";
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
const CustomCodeBlock = ({
children,
}: {
children: React.ReactNode | string;
}) => {
const [numberLines, setNumberLines] = useState<number>(0);
useEffect(() => {
const text = typeof children === "string" ? children : children?.toString();
const lines = text?.split("\n").filter(Boolean).length;
setNumberLines(lines ?? 0);
}, [children]);
if (numberLines === 1) {
return (
<div className="nx-inline-flex">
<SyntaxHighlighter
id="code-single"
language="python"
style={{
margin: "0px",
}}
wrapLines={true}
wrapLongLines={true}
>
{children as string}
</SyntaxHighlighter>
</div>
);
}
return (
<div className="nx-py-4">
<ModifiedPre>{children}</ModifiedPre>
</div>
);
};
export default CustomCodeBlock;