From e613d96ddf75cb35462b3500cf9d11f87bc32020 Mon Sep 17 00:00:00 2001 From: AramJonghu Date: Tue, 21 Jul 2026 23:53:24 +0200 Subject: [PATCH] chore(nesting): reduced nesting --- frontend/src/components/Header.tsx | 5 +- frontend/src/components/MdProcessor.tsx | 483 +++++++++++------------- 2 files changed, 218 insertions(+), 270 deletions(-) diff --git a/frontend/src/components/Header.tsx b/frontend/src/components/Header.tsx index 16ec2ef..d83526d 100644 --- a/frontend/src/components/Header.tsx +++ b/frontend/src/components/Header.tsx @@ -52,7 +52,10 @@ export default function Header({ theme }: HeaderProps) {
-
diff --git a/frontend/src/components/MdProcessor.tsx b/frontend/src/components/MdProcessor.tsx index 1e06466..c38951e 100644 --- a/frontend/src/components/MdProcessor.tsx +++ b/frontend/src/components/MdProcessor.tsx @@ -1,6 +1,5 @@ import { useEffect, - useRef, useState, useMemo, type ComponentPropsWithoutRef, @@ -12,6 +11,25 @@ import rehypeRaw from "rehype-raw"; import rehypeSanitize from "rehype-sanitize"; import highlighter from "../utils/highlighter"; +function CopyButton({ code }: { code: string }): ReactElement { + return ( + + ); +} + const toRawUrl = (repoUrl: string): string => { if (!repoUrl) return repoUrl; @@ -112,6 +130,200 @@ export default function MdProcessor({ const loading = useRemote && fetchState.url !== repoUrl; const displayContent = directContent ?? fetchState.content; + const markdownComponents = { + code({ + className, + children, + ...props + }: ComponentPropsWithoutRef<"code">): ReactElement { + const isBlock = + className || + (typeof children === "string" && children.includes("\n")); + + if (isBlock) { + return ( + + {children} + + ); + } + + return ( + + {children} + + ); + }, + + pre({ children }: ComponentPropsWithoutRef<"pre">): ReactElement { + const codeChild = Array.isArray(children) ? children[0] : children; + + let lang = ""; + let code = ""; + + if ( + codeChild && + typeof codeChild === "object" && + "props" in codeChild + ) { + const el = codeChild as { + props: { className?: string; children?: string }; + }; + const cls = el.props?.className; + const match = cls?.match(/language-(\w+)/); + if (match) lang = match[1]; + if (typeof el.props?.children === "string") { + code = el.props.children; + } + } + + const highlighted = + lang && code + ? highlighter.codeToHtml(code, { + lang, + themes: { + dark: "catppuccin-macchiato", + light: "catppuccin-latte", + }, + defaultColor: false, + }) + : null; + + if (highlighted) { + return ( +
+ {lang && ( + + {lang} + + )} + +
+
+ ); + } + + return ( +
+ +
+                        {children}
+                    
+
+ ); + }, + + h1({ children }: ComponentPropsWithoutRef<"h1">): ReactElement { + return ( +

+ {children} +

+ ); + }, + + h2({ children }: ComponentPropsWithoutRef<"h2">): ReactElement { + return ( +

+ {children} +

+ ); + }, + + h3({ children }: ComponentPropsWithoutRef<"h3">): ReactElement { + return ( +

+ {children} +

+ ); + }, + + p({ children }: ComponentPropsWithoutRef<"p">): ReactElement { + return

{children}

; + }, + + ul({ children }: ComponentPropsWithoutRef<"ul">): ReactElement { + return
    {children}
; + }, + + ol({ children }: ComponentPropsWithoutRef<"ol">): ReactElement { + return
    {children}
; + }, + + li({ children }: ComponentPropsWithoutRef<"li">): ReactElement { + return
  • {children}
  • ; + }, + + a({ children, href }: ComponentPropsWithoutRef<"a">): ReactElement { + return ( + + {children} + + ); + }, + + blockquote({ + children, + }: ComponentPropsWithoutRef<"blockquote">): ReactElement { + return ( +
    + {children} +
    + ); + }, + + table({ children }: ComponentPropsWithoutRef<"table">): ReactElement { + return ( +
    + + {children} +
    +
    + ); + }, + + th({ children }: ComponentPropsWithoutRef<"th">): ReactElement { + return ( + + {children} + + ); + }, + + td({ children }: ComponentPropsWithoutRef<"td">): ReactElement { + return ( + + {children} + + ); + }, + + img({ src, alt }: ComponentPropsWithoutRef<"img">): ReactElement { + const resolved = resolveImageSrc(src || "", base); + + return ( + {alt} + ); + }, + }; + return (
    {loading ? ( @@ -125,274 +337,7 @@ export default function MdProcessor({ ): ReactElement { - const isBlock = - className || - (typeof children === "string" && - children.includes("\n")); - - if (isBlock) { - return ( - - {children} - - ); - } - - return ( - - {children} - - ); - }, - - pre({ - children, - }: ComponentPropsWithoutRef<"pre">): ReactElement { - const codeChild = Array.isArray(children) - ? children[0] - : children; - - let lang = ""; - let code = ""; - - if ( - codeChild && - typeof codeChild === "object" && - "props" in codeChild - ) { - const el = codeChild as { - props: { - className?: string; - children?: string; - }; - }; - const cls = el.props?.className; - const match = cls?.match(/language-(\w+)/); - if (match) lang = match[1]; - if ( - typeof el.props?.children === "string" - ) { - code = el.props.children; - } - } - - const highlighted = - lang && code - ? highlighter.codeToHtml(code, { - lang, - themes: { - dark: "catppuccin-macchiato", - light: "catppuccin-latte", - }, - defaultColor: false, - }) - : null; - - const copyRef = useRef(null); - - if (highlighted) { - return ( -
    - {lang && ( - - {lang} - - )} - -
    -
    - ); - } - - return ( -
    -                                        {children}
    -                                    
    - ); - }, - - h1({ - children, - }: ComponentPropsWithoutRef<"h1">): ReactElement { - return ( -

    - {children} -

    - ); - }, - - h2({ - children, - }: ComponentPropsWithoutRef<"h2">): ReactElement { - return ( -

    - {children} -

    - ); - }, - - h3({ - children, - }: ComponentPropsWithoutRef<"h3">): ReactElement { - return ( -

    - {children} -

    - ); - }, - - p({ - children, - }: ComponentPropsWithoutRef<"p">): ReactElement { - return ( -

    - {children} -

    - ); - }, - - ul({ - children, - }: ComponentPropsWithoutRef<"ul">): ReactElement { - return ( -
      - {children} -
    - ); - }, - - ol({ - children, - }: ComponentPropsWithoutRef<"ol">): ReactElement { - return ( -
      - {children} -
    - ); - }, - - li({ - children, - }: ComponentPropsWithoutRef<"li">): ReactElement { - return
  • {children}
  • ; - }, - - a({ - children, - href, - }: ComponentPropsWithoutRef<"a">): ReactElement { - return ( - - {children} - - ); - }, - - blockquote({ - children, - }: ComponentPropsWithoutRef<"blockquote">): ReactElement { - return ( -
    - {children} -
    - ); - }, - - table({ - children, - }: ComponentPropsWithoutRef<"table">): ReactElement { - return ( -
    - - {children} -
    -
    - ); - }, - - th({ - children, - }: ComponentPropsWithoutRef<"th">): ReactElement { - return ( - - {children} - - ); - }, - - td({ - children, - }: ComponentPropsWithoutRef<"td">): ReactElement { - return ( - - {children} - - ); - }, - - img({ - src, - alt, - }: ComponentPropsWithoutRef<"img">): ReactElement { - const resolved = resolveImageSrc( - src || "", - base, - ); - - return ( - {alt} - ); - }, - }} + components={markdownComponents} > {displayContent}