Skip to content

Commit c4929f6

Browse files
committed
refactor(Markdown.tsx): simplify customComponents type definition
Remove unnecessary type imports from react-markdown and simplify the type definition for customComponents to use a more generic Record type. This change reduces complexity and improves readability. fix(Markdown.tsx): add type check for src before window.open Ensure that src is a string before attempting to open it in a new window. This prevents potential runtime errors if src is undefined or not a string. fix(Markdown.tsx): remove unused inline parameter from code component Remove the unused inline parameter from the code component to clean up the code and avoid confusion. fix(Markdown.tsx): initialize copyTimeout with undefined Explicitly initialize copyTimeout with undefined to ensure clarity and prevent potential issues with uninitialized variables.
1 parent d1afda0 commit c4929f6

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

src/components/global/Markdown.tsx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import { useCallback, useEffect, useRef, useState } from "react";
44
import ReactMarkdown from "react-markdown";
5-
import { type SpecialComponents } from "react-markdown/lib/ast-to-react";
6-
import { type NormalComponents } from "react-markdown/lib/complex-types";
75
import rehypeKatex from "rehype-katex";
86
import rehypePrism from "rehype-prism-plus";
97
import rehypeRaw from "rehype-raw";
@@ -22,9 +20,7 @@ type Props = {
2220
content: string;
2321
view?: "markdown" | "raw";
2422
className?: string;
25-
customComponents?: Partial<
26-
Omit<NormalComponents, keyof SpecialComponents> & SpecialComponents
27-
>;
23+
customComponents?: Record<string, React.ComponentType<Record<string, unknown>>>;
2824
};
2925
export default function Markdown({
3026
content,
@@ -78,7 +74,9 @@ export default function Markdown({
7874
className,
7975
)}
8076
onClick={() => {
81-
window.open(src, "_blank");
77+
if (typeof src === "string") {
78+
window.open(src, "_blank");
79+
}
8280
}}
8381
{...props}
8482
/>
@@ -113,7 +111,7 @@ export default function Markdown({
113111
</p>
114112
),
115113
pre: PreComponent,
116-
code: ({ inline: _inline, className, children, ...props }) => (
114+
code: ({ className, children, ...props }) => (
117115
<code
118116
className={cn(
119117
`whitespace-pre-wrap! break-words font-mono text-sm!`,
@@ -141,7 +139,7 @@ function PreComponent(props: React.HTMLAttributes<HTMLPreElement>) {
141139
const [copyStatus, setCopyStatus] = useState<"idle" | "copied" | "error">(
142140
"idle",
143141
);
144-
const copyTimeout = useRef<NodeJS.Timeout>();
142+
const copyTimeout = useRef<NodeJS.Timeout | undefined>(undefined);
145143

146144
const handleCopy = useCallback(async () => {
147145
if (!codeRef.current) return;

0 commit comments

Comments
 (0)