waveterm/frontend/app/element/markdown.tsx

103 lines
3.3 KiB
TypeScript
Raw Normal View History

2024-05-14 18:37:41 +02:00
// Copyright 2024, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
2024-07-03 23:32:55 +02:00
import { CopyButton } from "@/app/element/copybutton";
2024-05-28 21:12:28 +02:00
import { clsx } from "clsx";
2024-07-03 23:32:55 +02:00
import React from "react";
2024-05-14 18:37:41 +02:00
import ReactMarkdown from "react-markdown";
2024-07-03 23:32:55 +02:00
import rehypeRaw from "rehype-raw";
2024-05-14 18:37:41 +02:00
import remarkGfm from "remark-gfm";
import "./markdown.less";
2024-07-03 23:32:55 +02:00
const Link = ({ href, children }: { href: string; children: React.ReactNode }) => {
const newUrl = "https://extern?" + encodeURIComponent(href);
2024-05-14 18:37:41 +02:00
return (
2024-07-03 23:32:55 +02:00
<a href={newUrl} target="_blank" rel="noopener">
{children}
2024-05-14 18:37:41 +02:00
</a>
);
2024-07-03 23:32:55 +02:00
};
const Header = ({ children, hnum }: { children: React.ReactNode; hnum: number }) => {
return <div className={clsx("title", `is-${hnum}`)}>{children}</div>;
};
const Code = ({ children }: { children: React.ReactNode }) => {
return <code>{children}</code>;
};
type CodeBlockProps = {
children: React.ReactNode;
onClickExecute?: (cmd: string) => void;
};
const CodeBlock = ({ children, onClickExecute }: CodeBlockProps) => {
const getTextContent = (children: any): string => {
if (typeof children === "string") {
return children;
} else if (Array.isArray(children)) {
return children.map(getTextContent).join("");
} else if (children.props && children.props.children) {
return getTextContent(children.props.children);
}
return "";
};
const handleCopy = async (e: React.MouseEvent) => {
let textToCopy = getTextContent(children);
textToCopy = textToCopy.replace(/\n$/, ""); // remove trailing newline
await navigator.clipboard.writeText(textToCopy);
};
const handleExecute = (e: React.MouseEvent) => {
let textToCopy = getTextContent(children);
textToCopy = textToCopy.replace(/\n$/, ""); // remove trailing newline
if (onClickExecute) {
onClickExecute(textToCopy);
return;
}
2024-05-14 18:37:41 +02:00
};
2024-07-03 23:32:55 +02:00
return (
<pre className="codeblock">
{children}
<div className="codeblock-actions">
<CopyButton className="copy-button" onClick={handleCopy} title="Copy" />
{onClickExecute && <i className="fa-regular fa-square-terminal" onClick={handleExecute}></i>}
</div>
</pre>
);
};
type MarkdownProps = {
text: string;
style?: React.CSSProperties;
className?: string;
onClickExecute?: (cmd: string) => void;
};
const Markdown = ({ text, style, className, onClickExecute }: MarkdownProps) => {
const markdownComponents = {
a: Link,
h1: (props: any) => <Header {...props} hnum={1} />,
h2: (props: any) => <Header {...props} hnum={2} />,
h3: (props: any) => <Header {...props} hnum={3} />,
h4: (props: any) => <Header {...props} hnum={4} />,
h5: (props: any) => <Header {...props} hnum={5} />,
h6: (props: any) => <Header {...props} hnum={6} />,
code: Code,
pre: (props: any) => <CodeBlock {...props} onClickExecute={onClickExecute} />,
};
2024-05-14 18:37:41 +02:00
return (
2024-07-03 23:32:55 +02:00
<div className={clsx("markdown content", className)} style={style}>
<ReactMarkdown remarkPlugins={[remarkGfm]} rehypePlugins={[rehypeRaw]} components={markdownComponents}>
2024-05-14 18:37:41 +02:00
{text}
</ReactMarkdown>
</div>
);
2024-07-03 23:32:55 +02:00
};
2024-05-14 18:37:41 +02:00
export { Markdown };