Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
canifa_note
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Vũ Hoàng Anh
canifa_note
Commits
f44b0850
Commit
f44b0850
authored
Sep 16, 2025
by
Steven
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: enhance CodeBlock component with theme-aware syntax highlighting
parent
c3d4f8e9
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
11 additions
and
5 deletions
+11
-5
CodeBlock.tsx
web/src/components/MemoContent/CodeBlock.tsx
+11
-5
No files found.
web/src/components/MemoContent/CodeBlock.tsx
View file @
f44b0850
...
...
@@ -2,9 +2,11 @@ import copy from "copy-to-clipboard";
import
DOMPurify
from
"dompurify"
;
import
hljs
from
"highlight.js"
;
import
{
CopyIcon
}
from
"lucide-react"
;
import
{
observer
}
from
"mobx-react-lite"
;
import
{
useEffect
,
useMemo
}
from
"react"
;
import
toast
from
"react-hot-toast"
;
import
{
cn
}
from
"@/lib/utils"
;
import
{
workspaceStore
}
from
"@/store"
;
import
MermaidBlock
from
"./MermaidBlock"
;
import
{
BaseProps
}
from
"./types"
;
...
...
@@ -87,6 +89,9 @@ const CodeBlock: React.FC<Props> = ({ language, content }: Props) => {
return
<
MermaidBlock
content=
{
content
}
/>;
}
const
appTheme
=
workspaceStore
.
state
.
theme
;
const
isDarkTheme
=
appTheme
.
includes
(
"dark"
);
useEffect
(()
=>
{
const
dynamicImportStyle
=
async
()
=>
{
// Remove any existing highlight.js style
...
...
@@ -96,13 +101,14 @@ const CodeBlock: React.FC<Props> = ({ language, content }: Props) => {
}
try
{
// Always use the github light theme
const
cssModule
=
await
import
(
"highlight.js/styles/github.css?inline"
);
const
cssModule
=
isDarkTheme
?
await
import
(
"highlight.js/styles/github-dark-dimmed.css?inline"
)
:
await
import
(
"highlight.js/styles/github.css?inline"
);
// Create and inject the style
const
style
=
document
.
createElement
(
"style"
);
style
.
textContent
=
cssModule
.
default
;
style
.
setAttribute
(
"data-hljs-theme"
,
"light"
);
style
.
setAttribute
(
"data-hljs-theme"
,
isDarkTheme
?
"dark"
:
"light"
);
document
.
head
.
appendChild
(
style
);
}
catch
(
error
)
{
console
.
warn
(
"Failed to load highlight.js theme:"
,
error
);
...
...
@@ -110,7 +116,7 @@ const CodeBlock: React.FC<Props> = ({ language, content }: Props) => {
};
dynamicImportStyle
();
},
[]);
},
[
appTheme
,
isDarkTheme
]);
const
highlightedCode
=
useMemo
(()
=>
{
try
{
...
...
@@ -154,4 +160,4 @@ const CodeBlock: React.FC<Props> = ({ language, content }: Props) => {
);
};
export
default
CodeBlock
;
export
default
observer
(
CodeBlock
)
;
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment