File size: 4,957 Bytes
676fc08 | 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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | import dynamic from "next/dynamic";
import { useMemo, memo } from "react";
import ReactMarkdown, { type Options, type Components } from "react-markdown";
import rehypeRaw from "rehype-raw";
import remarkGfm from "remark-gfm";
import remarkMath from "remark-math";
import remarkBreaks from "remark-breaks";
import rehypeHighlight from "rehype-highlight";
import rehypeKatex from "rehype-katex";
import { clsx } from "clsx";
import { omit } from "radash";
import "katex/dist/katex.min.css";
import "./style.css";
const Code = dynamic(() => import("./Code"));
const Mermaid = dynamic(() => import("./Mermaid"));
export type MarkdownProps = {
id?: string;
className?: string;
children: string;
components?: Partial<Components>;
};
function MarkdownBlock({ children: content, ...rest }: Options) {
const remarkPlugins = useMemo(
() => rest.remarkPlugins ?? [],
[rest.remarkPlugins]
);
const rehypePlugins = useMemo(
() => rest.rehypePlugins ?? [],
[rest.rehypePlugins]
);
const components = useMemo(() => rest.components ?? {}, [rest.components]);
return (
<ReactMarkdown
{...rest}
remarkPlugins={[remarkGfm, remarkMath, remarkBreaks, ...remarkPlugins]}
rehypePlugins={[
rehypeRaw,
[rehypeHighlight, { detect: true, ignoreMissing: true }],
rehypeKatex,
...rehypePlugins,
]}
disallowedElements={["script", "form"]}
components={{
pre: (props) => {
const { children, className, ...rest } = props;
return (
<pre
{...omit(rest, ["node"])}
className={clsx("my-4 not-prose", className)}
>
{children}
</pre>
);
},
code: (props) => {
const { children, className, ...rest } = props;
const isInline =
!props.node?.position?.start.line ||
props.node?.position?.start.line === props.node?.position?.end.line;
if (isInline) {
return (
<span
className={clsx(
"bg-primary-foreground rounded-sm px-1 font-mono text-sm",
className
)}
{...props}
>
{children}
</span>
);
}
if (className?.includes("hljs")) {
const lang = /language-(\w+)/.exec(className || "");
if (lang && lang[1] === "mermaid") {
return <Mermaid>{children}</Mermaid>;
}
return (
<Code lang={lang ? lang[1] : "plaintext"}>
<code
{...omit(rest, ["node"])}
className={clsx("break-all", className)}
>
{children}
</code>
</Code>
);
} else {
return (
<code
{...omit(rest, ["node"])}
className={clsx("break-all", className)}
>
{children}
</code>
);
}
},
a: (props) => {
const { children, className, href = "", target, ...rest } = props;
if (/\.(aac|mp3|opus|wav)$/.test(href)) {
return (
<figure>
<audio controls src={href}></audio>
</figure>
);
}
if (/\.(3gp|3g2|webm|ogv|mpeg|mp4|avi)$/.test(href)) {
return (
<video controls width="99.9%">
<source src={href} />
</video>
);
}
const isInternal = /^\/#/i.test(href);
const isReferenceLink = /^[0-9]*$/.test(children?.toString() || "");
return (
<a
{...omit(rest, ["node"])}
className={clsx("break-all", className, {
reference: isReferenceLink,
})}
href={href}
target={isInternal ? "_self" : target ?? "_blank"}
>
{children}
</a>
);
},
img: (props) => {
const { className, src, alt, ...rest } = props;
return (
<picture
className={clsx(
"my-2 flex justify-center items-center w-4/5 max-sm:w-full h-[50vw] max-sm:h-80 m-auto",
className
)}
>
<img
className="size-full object-cover rounded transition-all duration-200 ease-out"
{...omit(rest, ["node"])}
src={src}
alt={alt}
title={alt}
referrerPolicy="no-referrer"
rel="noopener noreferrer"
/>
</picture>
);
},
...components,
}}
>
{content}
</ReactMarkdown>
);
}
export default memo(MarkdownBlock);
|