waveterm/frontend/app/element/markdown.tsx

404 lines
14 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";
import { createContentBlockPlugin } from "@/app/element/markdown-contentblock-plugin";
2024-12-17 01:04:07 +01:00
import {
MarkdownContentBlockType,
resolveRemoteFile,
resolveSrcSet,
transformBlocks,
} from "@/app/element/markdown-util";
import { boundNumber, useAtomValueSafe } from "@/util/util";
2024-05-28 21:12:28 +02:00
import { clsx } from "clsx";
import { Atom } from "jotai";
import { OverlayScrollbarsComponent, OverlayScrollbarsComponentRef } from "overlayscrollbars-react";
2024-09-07 20:20:28 +02:00
import { useEffect, useMemo, useRef, useState } from "react";
2024-09-07 20:10:10 +02:00
import ReactMarkdown, { Components } from "react-markdown";
import rehypeHighlight from "rehype-highlight";
2024-07-03 23:32:55 +02:00
import rehypeRaw from "rehype-raw";
import rehypeSanitize, { defaultSchema } from "rehype-sanitize";
import rehypeSlug from "rehype-slug";
import RemarkFlexibleToc, { TocItem } from "remark-flexible-toc";
import remarkGfm from "remark-gfm";
2024-09-07 02:30:19 +02:00
import { openLink } from "../store/global";
import { IconButton } from "./iconbutton";
import "./markdown.scss";
2024-05-14 18:37:41 +02:00
2024-09-07 20:10:10 +02:00
const Link = ({
setFocusedHeading,
props,
}: {
props: React.AnchorHTMLAttributes<HTMLAnchorElement>;
setFocusedHeading: (href: string) => void;
}) => {
2024-09-07 02:30:19 +02:00
const onClick = (e: React.MouseEvent) => {
e.preventDefault();
2024-09-07 20:10:10 +02:00
if (props.href.startsWith("#")) {
setFocusedHeading(props.href);
} else {
openLink(props.href);
}
2024-09-07 02:30:19 +02:00
};
2024-05-14 18:37:41 +02:00
return (
2024-09-07 20:10:10 +02:00
<a href={props.href} onClick={onClick}>
{props.children}
2024-05-14 18:37:41 +02:00
</a>
);
2024-07-03 23:32:55 +02:00
};
2024-09-07 20:18:38 +02:00
const Heading = ({ props, hnum }: { props: React.HTMLAttributes<HTMLHeadingElement>; hnum: number }) => {
return (
2024-09-07 20:18:38 +02:00
<div id={props.id} className={clsx("heading", `is-${hnum}`)}>
{props.children}
</div>
);
2024-07-03 23:32:55 +02:00
};
const Code = ({ className, children }: { className: string; children: React.ReactNode }) => {
return <code className={className}>{children}</code>;
2024-07-03 23:32:55 +02:00
};
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 onClick={handleCopy} title="Copy" />
{onClickExecute && (
<IconButton
decl={{
elemtype: "iconbutton",
icon: "regular@square-terminal",
click: handleExecute,
}}
/>
)}
2024-07-03 23:32:55 +02:00
</div>
</pre>
);
};
2024-12-17 01:04:07 +01:00
const MarkdownSource = ({
props,
resolveOpts,
}: {
props: React.HTMLAttributes<HTMLSourceElement> & {
srcSet?: string;
media?: string;
};
resolveOpts: MarkdownResolveOpts;
}) => {
const [resolvedSrcSet, setResolvedSrcSet] = useState<string>(props.srcSet);
const [resolving, setResolving] = useState<boolean>(true);
useEffect(() => {
const resolvePath = async () => {
const resolved = await resolveSrcSet(props.srcSet, resolveOpts);
setResolvedSrcSet(resolved);
setResolving(false);
};
resolvePath();
}, [props.srcSet]);
if (resolving) {
return null;
}
return <source srcSet={resolvedSrcSet} media={props.media} />;
};
interface WaveBlockProps {
blockkey: string;
blockmap: Map<string, MarkdownContentBlockType>;
}
const WaveBlock: React.FC<WaveBlockProps> = (props) => {
const { blockkey, blockmap } = props;
const block = blockmap.get(blockkey);
if (block == null) {
return null;
}
const sizeInKB = Math.round((block.content.length / 1024) * 10) / 10;
const displayName = block.id.replace(/^"|"$/g, "");
return (
<div className="waveblock">
<div className="wave-block-content">
<div className="wave-block-icon">
<i className="fas fa-file-code"></i>
</div>
<div className="wave-block-info">
<span className="wave-block-filename">{displayName}</span>
<span className="wave-block-size">{sizeInKB} KB</span>
</div>
</div>
</div>
);
};
const MarkdownImg = ({
props,
resolveOpts,
}: {
2024-09-07 20:01:46 +02:00
props: React.ImgHTMLAttributes<HTMLImageElement>;
resolveOpts: MarkdownResolveOpts;
}) => {
2024-09-07 00:19:15 +02:00
const [resolvedSrc, setResolvedSrc] = useState<string>(props.src);
2024-12-17 01:04:07 +01:00
const [resolvedSrcSet, setResolvedSrcSet] = useState<string>(props.srcSet);
2024-09-07 00:19:15 +02:00
const [resolvedStr, setResolvedStr] = useState<string>(null);
const [resolving, setResolving] = useState<boolean>(true);
2024-09-07 00:19:15 +02:00
useEffect(() => {
if (props.src.startsWith("data:image/")) {
setResolving(false);
setResolvedSrc(props.src);
setResolvedStr(null);
return;
}
if (resolveOpts == null) {
setResolving(false);
setResolvedSrc(null);
setResolvedStr(`[img:${props.src}]`);
return;
}
2024-12-17 01:04:07 +01:00
const resolveFn = async () => {
2024-12-17 01:04:07 +01:00
const [resolvedSrc, resolvedSrcSet] = await Promise.all([
resolveRemoteFile(props.src, resolveOpts),
resolveSrcSet(props.srcSet, resolveOpts),
]);
setResolvedSrc(resolvedSrc);
setResolvedSrcSet(resolvedSrcSet);
setResolvedStr(null);
setResolving(false);
};
resolveFn();
2024-12-17 01:04:07 +01:00
}, [props.src, props.srcSet]);
if (resolving) {
return null;
}
if (resolvedStr != null) {
return <span>{resolvedStr}</span>;
}
if (resolvedSrc != null) {
2024-12-17 01:04:07 +01:00
return <img {...props} src={resolvedSrc} srcSet={resolvedSrcSet} />;
}
return <span>[img]</span>;
};
2024-07-03 23:32:55 +02:00
type MarkdownProps = {
text?: string;
textAtom?: Atom<string> | Atom<Promise<string>>;
showTocAtom?: Atom<boolean>;
2024-07-03 23:32:55 +02:00
style?: React.CSSProperties;
className?: string;
onClickExecute?: (cmd: string) => void;
resolveOpts?: MarkdownResolveOpts;
scrollable?: boolean;
rehype?: boolean;
2024-12-17 01:04:07 +01:00
fontSizeOverride?: number;
fixedFontSizeOverride?: number;
2024-07-03 23:32:55 +02:00
};
const Markdown = ({
text,
textAtom,
showTocAtom,
style,
className,
resolveOpts,
2024-12-17 01:04:07 +01:00
fontSizeOverride,
fixedFontSizeOverride,
scrollable = true,
rehype = true,
onClickExecute,
}: MarkdownProps) => {
const textAtomValue = useAtomValueSafe<string>(textAtom);
const tocRef = useRef<TocItem[]>([]);
const showToc = useAtomValueSafe(showTocAtom) ?? false;
const contentsOsRef = useRef<OverlayScrollbarsComponentRef>(null);
const [focusedHeading, setFocusedHeading] = useState<string>(null);
// Ensure uniqueness of ids between MD preview instances.
const [idPrefix] = useState<string>(crypto.randomUUID());
text = textAtomValue ?? text;
const transformedOutput = transformBlocks(text);
const transformedText = transformedOutput.content;
const contentBlocksMap = transformedOutput.blocks;
useEffect(() => {
if (focusedHeading && contentsOsRef.current && contentsOsRef.current.osInstance()) {
const { viewport } = contentsOsRef.current.osInstance().elements();
const heading = document.getElementById(idPrefix + focusedHeading.slice(1));
if (heading) {
const headingBoundingRect = heading.getBoundingClientRect();
const viewportBoundingRect = viewport.getBoundingClientRect();
const headingTop = headingBoundingRect.top - viewportBoundingRect.top;
viewport.scrollBy({ top: headingTop });
}
}
}, [focusedHeading]);
2024-09-07 20:10:10 +02:00
const markdownComponents: Partial<Components> = {
a: (props: React.HTMLAttributes<HTMLAnchorElement>) => (
<Link props={props} setFocusedHeading={setFocusedHeading} />
),
p: (props: React.HTMLAttributes<HTMLParagraphElement>) => <div className="paragraph" {...props} />,
2024-09-07 20:18:38 +02:00
h1: (props: React.HTMLAttributes<HTMLHeadingElement>) => <Heading props={props} hnum={1} />,
h2: (props: React.HTMLAttributes<HTMLHeadingElement>) => <Heading props={props} hnum={2} />,
h3: (props: React.HTMLAttributes<HTMLHeadingElement>) => <Heading props={props} hnum={3} />,
h4: (props: React.HTMLAttributes<HTMLHeadingElement>) => <Heading props={props} hnum={4} />,
h5: (props: React.HTMLAttributes<HTMLHeadingElement>) => <Heading props={props} hnum={5} />,
h6: (props: React.HTMLAttributes<HTMLHeadingElement>) => <Heading props={props} hnum={6} />,
2024-09-07 20:01:46 +02:00
img: (props: React.HTMLAttributes<HTMLImageElement>) => <MarkdownImg props={props} resolveOpts={resolveOpts} />,
2024-12-17 01:04:07 +01:00
source: (props: React.HTMLAttributes<HTMLSourceElement>) => (
<MarkdownSource props={props} resolveOpts={resolveOpts} />
),
2024-07-03 23:32:55 +02:00
code: Code,
2024-09-07 20:01:46 +02:00
pre: (props: React.HTMLAttributes<HTMLPreElement>) => (
<CodeBlock children={props.children} onClickExecute={onClickExecute} />
),
2024-07-03 23:32:55 +02:00
};
markdownComponents["waveblock"] = (props: any) => <WaveBlock {...props} blockmap={contentBlocksMap} />;
2024-07-03 23:32:55 +02:00
2024-09-07 01:32:29 +02:00
const toc = useMemo(() => {
if (showToc && tocRef.current.length > 0) {
return tocRef.current.map((item) => {
return (
<a
key={item.href}
className="toc-item"
style={{ "--indent-factor": item.depth } as React.CSSProperties}
2024-09-07 20:20:28 +02:00
onClick={() => setFocusedHeading(item.href)}
2024-09-07 01:32:29 +02:00
>
{item.value}
</a>
);
});
}
}, [showToc, tocRef]);
let rehypePlugins = null;
if (rehype) {
rehypePlugins = [
rehypeRaw,
rehypeHighlight,
() =>
rehypeSanitize({
...defaultSchema,
attributes: {
...defaultSchema.attributes,
span: [
...(defaultSchema.attributes?.span || []),
// Allow all class names starting with `hljs-`.
["className", /^hljs-./],
2024-12-17 01:04:07 +01:00
["srcset"],
["media"],
["type"],
// Alternatively, to allow only certain class names:
// ['className', 'hljs-number', 'hljs-title', 'hljs-variable']
],
waveblock: [["blockkey"]],
},
2024-12-17 01:04:07 +01:00
tagNames: [...(defaultSchema.tagNames || []), "span", "waveblock", "picture", "source"],
}),
() => rehypeSlug({ prefix: idPrefix }),
];
}
const remarkPlugins: any = [
remarkGfm,
[RemarkFlexibleToc, { tocRef: tocRef.current }],
[createContentBlockPlugin, { blocks: contentBlocksMap }],
];
const ScrollableMarkdown = () => {
return (
<OverlayScrollbarsComponent
ref={contentsOsRef}
className="content"
options={{ scrollbars: { autoHide: "leave" } }}
>
<ReactMarkdown
remarkPlugins={remarkPlugins}
rehypePlugins={rehypePlugins}
components={markdownComponents}
>
{transformedText}
</ReactMarkdown>
</OverlayScrollbarsComponent>
);
};
const NonScrollableMarkdown = () => {
return (
<div className="content non-scrollable">
<ReactMarkdown
remarkPlugins={remarkPlugins}
rehypePlugins={rehypePlugins}
components={markdownComponents}
>
{transformedText}
</ReactMarkdown>
</div>
);
};
2024-12-17 01:04:07 +01:00
const mergedStyle = { ...style };
if (fontSizeOverride != null) {
mergedStyle["--markdown-font-size"] = `${boundNumber(fontSizeOverride, 6, 64)}px`;
2024-12-17 01:04:07 +01:00
}
if (fixedFontSizeOverride != null) {
mergedStyle["--markdown-fixed-font-size"] = `${boundNumber(fixedFontSizeOverride, 6, 64)}px`;
2024-12-17 01:04:07 +01:00
}
return (
2024-12-17 01:04:07 +01:00
<div className={clsx("markdown", className)} style={mergedStyle}>
{scrollable ? <ScrollableMarkdown /> : <NonScrollableMarkdown />}
2024-09-07 01:32:29 +02:00
{toc && (
<OverlayScrollbarsComponent className="toc" options={{ scrollbars: { autoHide: "leave" } }}>
<div className="toc-inner">
<h4>Table of Contents</h4>
2024-09-07 01:32:29 +02:00
{toc}
</div>
</OverlayScrollbarsComponent>
)}
2024-05-14 18:37:41 +02:00
</div>
);
2024-07-03 23:32:55 +02:00
};
2024-05-14 18:37:41 +02:00
export { Markdown };