💄 Shiki for syntax highlighting

This commit is contained in:
Nate Sesti 2024-01-26 21:10:14 -08:00
parent 5c999b6c7c
commit 4db11b1e54
5 changed files with 4768 additions and 1885 deletions

5471
gui/package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -13,6 +13,7 @@
"@heroicons/react": "^2.0.18",
"@monaco-editor/react": "^4.6.0",
"@reduxjs/toolkit": "^1.9.3",
"@shikijs/rehype": "^1.0.0-beta.0",
"@tiptap/extension-dropcursor": "^2.1.16",
"@tiptap/extension-image": "^2.1.16",
"@tiptap/extension-mention": "^2.1.13",
@ -22,7 +23,6 @@
"@tiptap/starter-kit": "^2.1.13",
"@tiptap/suggestion": "^2.1.13",
"@types/vscode-webview": "^1.57.1",
"@uiw/react-markdown-preview": "^4.1.13",
"core": "file:../core",
"dompurify": "^3.0.6",
"downshift": "^7.6.0",
@ -34,13 +34,17 @@
"react-dom": "^18.2.0",
"react-error-boundary": "^4.0.11",
"react-hook-form": "^7.47.0",
"react-markdown": "^9.0.1",
"react-redux": "^8.0.5",
"react-remark": "^2.1.0",
"react-router-dom": "^6.14.2",
"react-switch": "^7.0.0",
"react-syntax-highlighter": "^15.5.0",
"react-tooltip": "^5.18.0",
"redux-persist": "^6.0.0",
"redux-persist-transform-filter": "^0.0.22",
"rehype-shikiji": "^0.10.2",
"remark-shiki-twoslash": "^3.1.3",
"seti-file-icons": "^0.0.8",
"socket.io-client": "^4.7.2",
"styled-components": "^5.3.6",

View file

@ -1,5 +1,5 @@
import React, { isValidElement, useEffect } from "react";
import { ReactMarkdown } from "react-markdown/lib/react-markdown";
import ReactMarkdown from "react-markdown";
import styled from "styled-components";
import {
VSC_BACKGROUND_VAR,

View file

@ -1,5 +1,6 @@
import MarkdownPreview from "@uiw/react-markdown-preview";
import React, { memo, useCallback } from "react";
import { memo, useEffect } from "react";
import { useRemark } from "react-remark";
import rehypeShikiji from "rehype-shikiji";
import styled from "styled-components";
import {
defaultBorderRadius,
@ -9,77 +10,21 @@ import {
vscForeground,
} from "..";
import { getFontSize } from "../../util";
import LinkableCode from "./LinkableCode";
import "./markdown.css";
const tokenColorMap = (window as any).tokenColorMap;
const StyledMarkdownPreviewComponent = styled(MarkdownPreview)<{
light?: boolean;
const StyledMarkdown = styled.div<{
fontSize?: number;
maxHeight?: number;
showBorder?: boolean;
}>`
pre {
background-color: ${(props) =>
props.light ? vscBackground : vscEditorBackground};
background-color: ${vscEditorBackground};
border-radius: ${defaultBorderRadius};
max-width: calc(100vw - 24px);
}
overflow-x: scroll;
code {
color: #f78383;
word-wrap: break-word;
border-radius: ${defaultBorderRadius};
background-color: ${vscEditorBackground};
}
* .token.builtin {
color: ${tokenColorMap["support.function"] || tokenColorMap.keyword};
}
* .token.number {
color: ${tokenColorMap["constant.numeric"] || tokenColorMap.constant};
}
* .token.operator {
color: ${tokenColorMap["keyword.operator"] ||
tokenColorMap.operator ||
tokenColorMap.keyword};
}
* .token.function {
color: ${tokenColorMap.function || tokenColorMap["support.function"]};
}
* .token.selector {
color: ${tokenColorMap.selector ||
tokenColorMap["source.css support.function"]};
}
* .token.class-name {
color: ${tokenColorMap["entity.name.class"] ||
tokenColorMap["entity.name.type"] ||
tokenColorMap.class};
}
* .token.macro {
color: ${tokenColorMap.macro || tokenColorMap["entity.name.function"]};
}
${tokenColorMap &&
Object.keys(tokenColorMap).map((tokenType) => {
return `
* .${tokenType} {
color: ${tokenColorMap[tokenType]};
}
`;
})}
pre > code {
background-color: ${vscEditorBackground};
color: ${vscForeground};
padding: ${(props) => (props.showBorder ? "12px" : "0px 2px")};
font-size: 12px;
border-radius: ${defaultBorderRadius};
${(props) => {
@ -89,7 +34,7 @@ const StyledMarkdownPreviewComponent = styled(MarkdownPreview)<{
`;
}
}}
padding: ${(props) => (props.showBorder ? "12px" : "0px 2px")};
${(props) => {
if (props.maxHeight) {
return `
@ -100,7 +45,15 @@ const StyledMarkdownPreviewComponent = styled(MarkdownPreview)<{
}}
}
background-color: ${(props) => (props.light ? "transparent" : vscBackground)};
code {
color: #f78383;
word-wrap: break-word;
border-radius: ${defaultBorderRadius};
background-color: ${vscEditorBackground};
font-size: 12px;
}
background-color: ${vscBackground};
font-family:
system-ui,
-apple-system,
@ -114,8 +67,13 @@ const StyledMarkdownPreviewComponent = styled(MarkdownPreview)<{
"Helvetica Neue",
sans-serif;
font-size: ${(props) => props.fontSize || getFontSize()}px;
padding: 8px;
padding-left: 8px;
padding-right: 8px;
color: ${vscForeground};
p {
line-height: 1.5;
}
`;
interface StyledMarkdownPreviewProps {
@ -125,24 +83,18 @@ interface StyledMarkdownPreviewProps {
showCodeBorder?: boolean;
}
const MemoizedCode = React.memo(({ node, ...codeProps }: any) => {
return <code {...codeProps}></code>;
});
const StyledMarkdownPreview = memo(function StyledMarkdownPreview(
props: StyledMarkdownPreviewProps
) {
const renderCode = useCallback(({ node, ...codeProps }) => {
return codeProps.inline ? (
<LinkableCode {...codeProps}></LinkableCode>
) : (
<MemoizedCode {...codeProps}></MemoizedCode>
);
}, []);
return (
<StyledMarkdownPreviewComponent
components={{
const [reactContent, setMarkdownSource] = useRemark({
rehypePlugins: [
[
rehypeShikiji as any,
{ theme: (window as any).fullColorTheme || "nord" },
],
],
rehypeReactOptions: {
components: {
a: ({ node, ...props }) => {
return (
<a {...props} target="_blank">
@ -150,17 +102,22 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview(
</a>
);
},
code: renderCode,
}}
className={props.className}
},
},
});
useEffect(() => {
setMarkdownSource(props.source || "");
}, [props.source]);
return (
<StyledMarkdown
maxHeight={props.maxHeight}
fontSize={getFontSize()}
source={props.source || ""}
wrapperElement={{
"data-color-mode": "dark",
}}
showBorder={props.showCodeBorder}
/>
>
{reactContent}
</StyledMarkdown>
);
});

File diff suppressed because it is too large Load diff