Skip to content

Commit 044168c

Browse files
feat: Integrate CodeMirror with VS Code theme
- Installs CodeMirror and the `@uiw/codemirror-theme-vscode` package to provide enhanced, colorful syntax highlighting for code blocks. - Implements a client-side script that dynamically replaces all `<pre>` tags with CodeMirror instances. - Adds support for dynamic language loading for a variety of common languages. - Correctly loads the script as an ES module in the Astro layout. - Verifies the visual changes with Playwright to ensure the theme is applied correctly.
1 parent 378ada3 commit 044168c

File tree

5 files changed

+49
-47
lines changed

5 files changed

+49
-47
lines changed

dev_server.log

Lines changed: 0 additions & 16 deletions
This file was deleted.

package-lock.json

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"@astrojs/mdx": "^4.2.1",
1414
"@astrojs/rss": "^4.0.11",
1515
"@astrojs/sitemap": "^3.3.0",
16+
"@babel/runtime": "^7.28.4",
1617
"@codemirror/lang-css": "^6.3.1",
1718
"@codemirror/lang-html": "^6.4.11",
1819
"@codemirror/lang-javascript": "^6.2.4",
@@ -23,6 +24,7 @@
2324
"@codemirror/state": "^6.5.3",
2425
"@codemirror/view": "^6.39.9",
2526
"@tailwindcss/vite": "^4.1.18",
27+
"@uiw/codemirror-theme-vscode": "^4.25.4",
2628
"astro": "^5.5.2",
2729
"codemirror": "^6.0.2",
2830
"lucide-astro": "^0.556.0",

src/layouts/BlogPost.astro

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ const tagList: string[] =
119119
</article>
120120
</main>
121121
<Footer />
122-
<script src="../scripts/codemirror-loader.js"></script>
122+
<script>
123+
import "../scripts/codemirror-loader.js";
124+
</script>
123125
</body>
124126
</html>

src/scripts/codemirror-loader.js

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
import { EditorView, basicSetup } from "codemirror";
33
import { EditorState } from "@codemirror/state";
4+
import { vscodeDark } from '@uiw/codemirror-theme-vscode';
45

56
async function main() {
67
const codeblocks = document.querySelectorAll('pre');
@@ -66,36 +67,7 @@ async function main() {
6667
extensions: [
6768
...extensions,
6869
EditorView.editable.of(false),
69-
EditorView.theme({
70-
"&": {
71-
color: "var(--slate)",
72-
backgroundColor: "var(--light-navy)",
73-
borderRadius: "0.5rem",
74-
border: "1px solid var(--lightest-navy)",
75-
padding: "0.5rem 0",
76-
},
77-
".cm-content": {
78-
padding: "0 1rem",
79-
},
80-
"&.cm-focused .cm-cursor": {
81-
borderLeftColor: "#fff"
82-
},
83-
"&.cm-focused .cm-selectionBackground, ::selection": {
84-
backgroundColor: "#222"
85-
},
86-
".cm-gutters": {
87-
backgroundColor: "var(--light-navy)",
88-
color: "var(--slate)",
89-
border: "none",
90-
padding: "0 0.5rem"
91-
},
92-
".cm-scroller": {
93-
fontFamily: "var(--font-mono)"
94-
},
95-
".cm-line": {
96-
padding: "0"
97-
}
98-
})
70+
vscodeDark
9971
]
10072
});
10173

0 commit comments

Comments
 (0)