💄 syntax highlighting without monaco
This commit is contained in:
parent
2b81745177
commit
0fe93c7c4f
10 changed files with 633 additions and 478 deletions
810
gui/package-lock.json
generated
810
gui/package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
|
@ -27,9 +27,6 @@
|
|||
"downshift": "^7.6.0",
|
||||
"lodash": "^4.17.21",
|
||||
"minisearch": "^6.3.0",
|
||||
"monaco-editor-textmate": "^4.0.0",
|
||||
"monaco-textmate": "^3.0.1",
|
||||
"monaco-vscode-textmate-theme-converter": "^0.1.7",
|
||||
"onigasm": "^2.2.5",
|
||||
"posthog-js": "^1.58.0",
|
||||
"prismjs": "^1.29.0",
|
||||
|
|
@ -38,7 +35,6 @@
|
|||
"react-error-boundary": "^4.0.11",
|
||||
"react-hook-form": "^7.47.0",
|
||||
"react-markdown": "^9.0.1",
|
||||
"react-monaco-editor": "^0.55.0",
|
||||
"react-redux": "^8.0.5",
|
||||
"react-remark": "^2.1.0",
|
||||
"react-router-dom": "^6.14.2",
|
||||
|
|
@ -47,6 +43,7 @@
|
|||
"react-tooltip": "^5.18.0",
|
||||
"redux-persist": "^6.0.0",
|
||||
"redux-persist-transform-filter": "^0.0.22",
|
||||
"rehype-highlight": "^7.0.0",
|
||||
"rehype-katex": "^7.0.0",
|
||||
"rehype-wrap-all": "^1.1.0",
|
||||
"remark-math": "^6.0.0",
|
||||
|
|
@ -71,7 +68,6 @@
|
|||
"postcss": "^8.4.21",
|
||||
"tailwindcss": "^3.2.7",
|
||||
"typescript": "^4.9.3",
|
||||
"vite": "^4.1.0",
|
||||
"vite-plugin-monaco-editor": "^1.1.0"
|
||||
"vite": "^4.1.0"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,8 +14,8 @@ import MonacoPage from "./pages/monaco";
|
|||
import SettingsPage from "./pages/settings";
|
||||
|
||||
import { ContextSubmenuItem } from "core";
|
||||
import { useMonacoTheme } from "./hooks/useMonacoTheme";
|
||||
import useSubmenuContextProviders from "./hooks/useSubmenuContextProviders";
|
||||
import { useVscTheme } from "./hooks/useVscTheme";
|
||||
|
||||
const router = createMemoryRouter([
|
||||
{
|
||||
|
|
@ -78,24 +78,24 @@ export const SubmenuContextProvidersContext = createContext<{
|
|||
addItem: () => {},
|
||||
});
|
||||
|
||||
export const MonacoThemeContext = createContext<any>(undefined);
|
||||
export const VscThemeContext = createContext<any>(undefined);
|
||||
|
||||
function App() {
|
||||
const dispatch = useDispatch();
|
||||
|
||||
useSetup(dispatch);
|
||||
|
||||
const monacoTheme = useMonacoTheme();
|
||||
const vscTheme = useVscTheme();
|
||||
const submenuContextProvidersMethods = useSubmenuContextProviders();
|
||||
|
||||
return (
|
||||
<MonacoThemeContext.Provider value={monacoTheme}>
|
||||
<VscThemeContext.Provider value={vscTheme}>
|
||||
<SubmenuContextProvidersContext.Provider
|
||||
value={submenuContextProvidersMethods}
|
||||
>
|
||||
<RouterProvider router={router} />
|
||||
</SubmenuContextProvidersContext.Provider>
|
||||
</MonacoThemeContext.Provider>
|
||||
</VscThemeContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ const SecondDiv = styled.div`
|
|||
right: 4px;
|
||||
display: flex;
|
||||
gap: 4px;
|
||||
background-color: ${vscEditorBackground};
|
||||
`;
|
||||
|
||||
interface CodeBlockToolBarProps {
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ import { getFontSize } from "../../util";
|
|||
import { postToIde } from "../../util/ide";
|
||||
import FileIcon from "../FileIcon";
|
||||
import HeaderButtonWithText from "../HeaderButtonWithText";
|
||||
import { MonacoCodeBlock } from "./MonacoCodeBlock";
|
||||
import StyledMarkdownPreview from "./StyledMarkdownPreview";
|
||||
|
||||
const PreviewMarkdownDiv = styled.div<{
|
||||
borderColor?: string;
|
||||
|
|
@ -58,10 +58,18 @@ const StyledHeaderButtonWithText = styled(HeaderButtonWithText)<{
|
|||
|
||||
const MAX_PREVIEW_HEIGHT = 300;
|
||||
|
||||
// Pre-compile the regular expression outside of the function
|
||||
const backticksRegex = /`{3,}/gm;
|
||||
|
||||
function CodeSnippetPreview(props: CodeSnippetPreviewProps) {
|
||||
const [collapsed, setCollapsed] = React.useState(true);
|
||||
const [hovered, setHovered] = React.useState(false);
|
||||
|
||||
const fence = React.useMemo(() => {
|
||||
const backticks = props.item.content.match(backticksRegex);
|
||||
return backticks ? backticks.sort().at(-1) + "`" : "```";
|
||||
}, [props.item.content]);
|
||||
|
||||
const codeBlockRef = React.useRef<HTMLDivElement>(null);
|
||||
|
||||
return (
|
||||
|
|
@ -141,12 +149,12 @@ function CodeSnippetPreview(props: CodeSnippetPreviewProps) {
|
|||
overflow: collapsed ? "hidden" : "auto",
|
||||
}}
|
||||
>
|
||||
<MonacoCodeBlock
|
||||
codeString={props.item.content}
|
||||
preProps={{}}
|
||||
showBorder={false}
|
||||
language={getMarkdownLanguageTagForFile(props.item.description)}
|
||||
></MonacoCodeBlock>
|
||||
<StyledMarkdownPreview
|
||||
source={`${fence}${getMarkdownLanguageTagForFile(
|
||||
props.item.description
|
||||
)}\n${props.item.content}\n${fence}`}
|
||||
showCodeBorder={false}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{codeBlockRef.current?.scrollHeight > MAX_PREVIEW_HEIGHT && (
|
||||
|
|
|
|||
|
|
@ -1,20 +1,50 @@
|
|||
import { useState } from "react";
|
||||
import { debounce } from "lodash";
|
||||
import { useEffect, useState } from "react";
|
||||
import CodeBlockToolBar from "./CodeBlockToolbar";
|
||||
|
||||
function PreWithToolbar(props: { copyvalue: string; children: any }) {
|
||||
function childToText(child: any) {
|
||||
if (typeof child === "string") {
|
||||
return child;
|
||||
} else if (child?.props) {
|
||||
return childToText(child.props?.children);
|
||||
} else if (Array.isArray(child)) {
|
||||
return childrenToText(child);
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
function childrenToText(children: any) {
|
||||
return children.map((child: any) => childToText(child)).join("");
|
||||
}
|
||||
|
||||
function PreWithToolbar(props: { children: any }) {
|
||||
const [hovering, setHovering] = useState(false);
|
||||
|
||||
const [copyValue, setCopyValue] = useState("");
|
||||
|
||||
useEffect(() => {
|
||||
const debouncedEffect = debounce(() => {
|
||||
setCopyValue(childrenToText(props.children.props.children));
|
||||
}, 100);
|
||||
|
||||
debouncedEffect();
|
||||
|
||||
return () => {
|
||||
debouncedEffect.cancel();
|
||||
};
|
||||
}, [props.children]);
|
||||
|
||||
return (
|
||||
<pre
|
||||
{...props}
|
||||
<div
|
||||
style={{ padding: "0px" }}
|
||||
className="relative"
|
||||
onMouseEnter={() => setHovering(true)}
|
||||
onMouseLeave={() => setHovering(false)}
|
||||
>
|
||||
{hovering && <CodeBlockToolBar text={props.copyvalue}></CodeBlockToolBar>}
|
||||
{hovering && <CodeBlockToolBar text={copyValue}></CodeBlockToolBar>}
|
||||
{props.children}
|
||||
</pre>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,9 @@ import React, { memo, useEffect } from "react";
|
|||
import { useRemark } from "react-remark";
|
||||
// import rehypeKatex from "rehype-katex";
|
||||
// import remarkMath from "remark-math";
|
||||
import rehypeHighlight from "rehype-highlight";
|
||||
import styled from "styled-components";
|
||||
import { visit } from "unist-util-visit";
|
||||
import {
|
||||
defaultBorderRadius,
|
||||
vscBackground,
|
||||
|
|
@ -10,8 +12,8 @@ import {
|
|||
vscForeground,
|
||||
} from "..";
|
||||
import { getFontSize } from "../../util";
|
||||
import { MonacoCodeBlock } from "./MonacoCodeBlock";
|
||||
import PreWithToolbar from "./PreWithToolbar";
|
||||
import { SyntaxHighlightedPre } from "./SyntaxHighlightedPre";
|
||||
import "./markdown.css";
|
||||
|
||||
const StyledMarkdown = styled.div<{
|
||||
|
|
@ -24,8 +26,7 @@ const StyledMarkdown = styled.div<{
|
|||
|
||||
max-width: calc(100vw - 24px);
|
||||
overflow-x: scroll;
|
||||
|
||||
font-size: 12px;
|
||||
overflow-y: hidden;
|
||||
|
||||
${(props) => {
|
||||
if (props.showBorder) {
|
||||
|
|
@ -41,7 +42,6 @@ const StyledMarkdown = styled.div<{
|
|||
span.line:empty {
|
||||
display: none;
|
||||
}
|
||||
color: #f78383;
|
||||
word-wrap: break-word;
|
||||
border-radius: ${defaultBorderRadius};
|
||||
background-color: ${vscEditorBackground};
|
||||
|
|
@ -51,6 +51,7 @@ const StyledMarkdown = styled.div<{
|
|||
|
||||
code:not(pre > code) {
|
||||
font-family: monospace;
|
||||
color: #f78383;
|
||||
}
|
||||
|
||||
background-color: ${vscBackground};
|
||||
|
|
@ -114,6 +115,20 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview(
|
|||
const [reactContent, setMarkdownSource] = useRemark({
|
||||
// remarkPlugins: [remarkMath],
|
||||
// rehypePlugins: [rehypeKatex as any, {}],
|
||||
remarkPlugins: [
|
||||
() => {
|
||||
return (tree) => {
|
||||
visit(tree, "code", (node: any) => {
|
||||
if (!node.lang) {
|
||||
node.lang === "javascript";
|
||||
} else if (node.lang.includes(".")) {
|
||||
node.lang = node.lang.split(".").slice(-1)[0];
|
||||
}
|
||||
});
|
||||
};
|
||||
},
|
||||
],
|
||||
rehypePlugins: [rehypeHighlight as any, {}],
|
||||
rehypeReactOptions: {
|
||||
components: {
|
||||
a: ({ node, ...props }) => {
|
||||
|
|
@ -124,30 +139,39 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview(
|
|||
);
|
||||
},
|
||||
pre: ({ node, ...preProps }) => {
|
||||
const codeString =
|
||||
preProps.children?.[0]?.props?.children?.[0].trim() || "";
|
||||
const monacoEditor = (
|
||||
<MonacoCodeBlock
|
||||
showBorder={props.showCodeBorder}
|
||||
language={
|
||||
preProps.children?.[0]?.props?.className?.split("-")[1] ||
|
||||
"typescript"
|
||||
}
|
||||
preProps={preProps}
|
||||
codeString={codeString}
|
||||
/>
|
||||
);
|
||||
return props.showCodeBorder ? (
|
||||
<PreWithToolbar copyvalue={codeString}>
|
||||
{monacoEditor}
|
||||
<PreWithToolbar>
|
||||
<SyntaxHighlightedPre {...preProps}></SyntaxHighlightedPre>
|
||||
</PreWithToolbar>
|
||||
) : (
|
||||
monacoEditor
|
||||
<SyntaxHighlightedPre {...preProps}></SyntaxHighlightedPre>
|
||||
);
|
||||
},
|
||||
// p: ({ node, ...props }) => {
|
||||
// return <FadeInWords {...props}></FadeInWords>;
|
||||
// },
|
||||
// pre: ({ node, ...preProps }) => {
|
||||
// const codeString =
|
||||
// preProps.children?.[0]?.props?.children?.[0].trim() || "";
|
||||
// const monacoEditor = (
|
||||
// <MonacoCodeBlock
|
||||
// showBorder={props.showCodeBorder}
|
||||
// language={
|
||||
// preProps.children?.[0]?.props?.className?.split("-")[1] ||
|
||||
// "typescript"
|
||||
// }
|
||||
// preProps={preProps}
|
||||
// codeString={codeString}
|
||||
// />
|
||||
// );
|
||||
// return props.showCodeBorder ? (
|
||||
// <PreWithToolbar copyvalue={codeString}>
|
||||
// <SyntaxHighlightedPre {...preProps}></SyntaxHighlightedPre>
|
||||
// </PreWithToolbar>
|
||||
// ) : (
|
||||
// <SyntaxHighlightedPre {...preProps}></SyntaxHighlightedPre>
|
||||
// );
|
||||
// },
|
||||
// // p: ({ node, ...props }) => {
|
||||
// // return <FadeInWords {...props}></FadeInWords>;
|
||||
// // },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
|
|
|||
27
gui/src/components/markdown/SyntaxHighlightedPre.tsx
Normal file
27
gui/src/components/markdown/SyntaxHighlightedPre.tsx
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import { useContext } from "react";
|
||||
import styled from "styled-components";
|
||||
import { vscForeground } from "..";
|
||||
import { VscThemeContext } from "../../App";
|
||||
|
||||
const StyledPre = styled.pre<{ theme: any }>`
|
||||
& .hljs {
|
||||
color: ${vscForeground};
|
||||
}
|
||||
|
||||
${(props) =>
|
||||
Object.keys(props.theme)
|
||||
.map((key, index) => {
|
||||
return `
|
||||
& ${key} {
|
||||
color: ${props.theme[key]};
|
||||
}
|
||||
`;
|
||||
})
|
||||
.join("")}
|
||||
`;
|
||||
|
||||
export const SyntaxHighlightedPre = (props: any) => {
|
||||
const currentTheme = useContext(VscThemeContext);
|
||||
|
||||
return <StyledPre {...props} theme={currentTheme} />;
|
||||
};
|
||||
|
|
@ -1,23 +0,0 @@
|
|||
// import { convertTheme } from 'monaco-vscode-textmate-theme-converter/lib/cjs';
|
||||
import { Registry } from "monaco-textmate";
|
||||
// import { wireTmGrammars } from 'monaco-editor-textmate';
|
||||
|
||||
const registry = new Registry({
|
||||
getGrammarDefinition: async (scopeName) => {
|
||||
if (scopeName == "source.ts") {
|
||||
return {
|
||||
format: "json",
|
||||
content: await (await fetch("./TypeScript.tmLanguage.json")).text(),
|
||||
};
|
||||
} else if (scopeName == "source.js") {
|
||||
return {
|
||||
format: "json",
|
||||
content: await (await fetch("./JavaScript.tmLanguage.json")).text(),
|
||||
};
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
export function useMonacoTheme() {}
|
||||
98
gui/src/hooks/useVscTheme.ts
Normal file
98
gui/src/hooks/useVscTheme.ts
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
import { useEffect, useState } from "react";
|
||||
|
||||
const hljsToTextMate = {
|
||||
".hljs-comment": ["comment"],
|
||||
".hljs-tag": ["tag"],
|
||||
|
||||
".hljs-doctag": ["keyword"],
|
||||
".hljs-keyword": ["keyword"],
|
||||
".hljs-meta .hljs-keyword": ["keyword"],
|
||||
".hljs-template-tag": ["keyword"],
|
||||
".hljs-template-variable": ["keyword"],
|
||||
".hljs-type": ["keyword"],
|
||||
".hljs-variable.language_": ["keyword"],
|
||||
|
||||
".hljs-title": ["title", "function", "class"],
|
||||
".hljs-title.class_": ["title", "function", "class", "variable"],
|
||||
".hljs-title.class_.inherited__": ["title", "function", "class", "variable"],
|
||||
".hljs-title.function_": [
|
||||
"support.function",
|
||||
"entity.name.function",
|
||||
"title",
|
||||
"function",
|
||||
"class",
|
||||
],
|
||||
|
||||
".hljs-built_in": [
|
||||
"support.function",
|
||||
"entity.name.function",
|
||||
"title",
|
||||
"function",
|
||||
"class",
|
||||
],
|
||||
|
||||
".hljs-name": ["constant"],
|
||||
|
||||
".hljs-attr": ["variable", "operator", "number"],
|
||||
".hljs-attribute": ["attribute", "variable", "operator", "number"],
|
||||
".hljs-literal": ["variable", "operator", "number"],
|
||||
".hljs-meta": ["variable", "operator", "number"],
|
||||
".hljs-number": ["constant.numeric", "number", "variable", "operator"],
|
||||
".hljs-operator": ["variable", "operator", "number"],
|
||||
".hljs-variable": ["variable", "operator", "number"],
|
||||
".hljs-selector-attr": ["variable", "operator", "number"],
|
||||
".hljs-selector-class": ["variable", "operator", "number"],
|
||||
".hljs-selector-id": ["variable", "operator", "number"],
|
||||
|
||||
".hljs-regexp": ["string"],
|
||||
".hljs-string": ["string"],
|
||||
".hljs-meta .hljs-string": ["string"],
|
||||
|
||||
".hljs-params": ["variable", "operator", "number"],
|
||||
};
|
||||
|
||||
function constructTheme(tmTheme: any): any {
|
||||
const rules = tmTheme["rules"] || [];
|
||||
|
||||
const tokenToForeground = {};
|
||||
rules.forEach(({ token, foreground }) => {
|
||||
if (!foreground || !token) {
|
||||
return;
|
||||
}
|
||||
tokenToForeground[token] = foreground;
|
||||
});
|
||||
|
||||
const theme = {};
|
||||
Object.keys(hljsToTextMate).forEach((className) => {
|
||||
const tokens = hljsToTextMate[className];
|
||||
for (const scope of tokens) {
|
||||
if (tokenToForeground[scope]) {
|
||||
theme[className] = tokenToForeground[scope];
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return theme;
|
||||
}
|
||||
|
||||
export function useVscTheme() {
|
||||
const [theme, setTheme] = useState<any>(
|
||||
constructTheme((window as any).fullColorTheme || {})
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
const listener = (e) => {
|
||||
if (e.data.type === "setTheme") {
|
||||
(window as any).fullColorTheme = e.data.theme;
|
||||
setTheme(constructTheme(e.data.theme));
|
||||
}
|
||||
};
|
||||
window.addEventListener("message", listener);
|
||||
return () => {
|
||||
window.removeEventListener("message", listener);
|
||||
};
|
||||
}, []);
|
||||
|
||||
return theme;
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue