Skip to content

Commit

Permalink
fix: handle edge cases syntax highlight (#3969)
Browse files Browse the repository at this point in the history
  • Loading branch information
urmauur authored Nov 7, 2024
1 parent fb1fcc5 commit 8d4734c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 22 deletions.
2 changes: 1 addition & 1 deletion web/screens/LocalServer/LocalServerRightPanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ const LocalServerRightPanel = () => {
<div className="mt-2">
<Input
value={selectedModel?.id || ''}
className="cursor-pointer text-[hsla(var(--text-secondary))]"
className="cursor-pointer text-[hsla(var(--text-secondary))] hover:border-[hsla(var(--app-border))] focus-visible:outline-0 focus-visible:ring-0"
readOnly
onClick={() => {
clipboard.copy(selectedModel?.id)
Expand Down
22 changes: 8 additions & 14 deletions web/screens/Thread/ThreadCenterPanel/ChatInput/RichTextEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,6 @@ const RichTextEditor = ({
if (Editor.isBlock(editor, node) && node.type === 'paragraph') {
node.children.forEach((child: { text: any }, childIndex: number) => {
const text = child.text
const { selection } = editor

if (selection) {
const selectedNode = Editor.node(editor, selection)

if (Editor.isBlock(editor, selectedNode[0] as CustomElement)) {
const isNodeEmpty = Editor.string(editor, selectedNode[1]) === ''

if (isNodeEmpty) {
// Reset language when a node is cleared
currentLanguage.current = 'plaintext'
}
}
}

// Match code block start and end
const startMatch = text.match(/^```(\w*)$/)
Expand Down Expand Up @@ -346,6 +332,14 @@ const RichTextEditor = ({
.join('\n')

setCurrentPrompt(combinedText)
if (combinedText.trim() === '') {
currentLanguage.current = 'plaintext'
}
const hasCodeBlockStart = combinedText.match(/^```(\w*)/m)
// Set language to plaintext if no code block with language identifier is found
if (!hasCodeBlockStart) {
currentLanguage.current = 'plaintext'
}
}}
>
<Editable
Expand Down
14 changes: 7 additions & 7 deletions web/styles/components/code-block.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.hljs-comment,
.hljs-quote {
color: #d4d0ab;
color: var(--hljs-comment);
}

/* Red */
Expand All @@ -12,7 +12,7 @@
.hljs-selector-class,
.hljs-regexp,
.hljs-deletion {
color: #ffa07a;
color: var(--hljs-variable);
}

/* Orange */
Expand All @@ -24,32 +24,32 @@
.hljs-params,
.hljs-meta,
.hljs-link {
color: #f5ab35;
color: var(--hljs-number);
}

/* Yellow */
.hljs-attribute {
color: #ffd700;
color: var(--hljs-attribute);
}

/* Green */
.hljs-string,
.hljs-symbol,
.hljs-bullet,
.hljs-addition {
color: #abe338;
color: var(--hljs-string);
}

/* Blue */
.hljs-title,
.hljs-section {
color: #00e0e0;
color: var(--hljs-title);
}

/* Purple */
.hljs-keyword,
.hljs-selector-tag {
color: #dcc6e0;
color: var(--hljs-keyword);
}

.hljs {
Expand Down
16 changes: 16 additions & 0 deletions web/styles/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@
--text-secondary: 0, 0%, 0%, 0.6;
--text-tertiary: 0, 0%, 0%, 0.4;
--text-quaternary: 0, 0%, 0%, 0.2;

--hljs-comment: #6e7781;
--hljs-variable: #cf222e;
--hljs-number: #bc4c00;
--hljs-attribute: #b58407;
--hljs-string: #116329;
--hljs-title: #0550ae;
--hljs-keyword: #8250df;
}

html.dark {
Expand Down Expand Up @@ -68,4 +76,12 @@ html.dark {
--text-secondary: 0, 0%, 68%, 1;
--text-tertiary: 0, 0%, 68%, 0.4;
--text-quaternary: 0, 0%, 68%, 0.2;

--hljs-comment: #8b949e;
--hljs-variable: #ff7b72;
--hljs-number: #f0883e;
--hljs-attribute: #ffa657;
--hljs-string: #7ee787;
--hljs-title: #79c0ff;
--hljs-keyword: #d2a8ff;
}

0 comments on commit 8d4734c

Please sign in to comment.