Skip to content

Commit 4a4365e

Browse files
committed
fix: code syntax highlighting to support all other languages
1 parent 85a07fd commit 4a4365e

File tree

4 files changed

+164
-24
lines changed

4 files changed

+164
-24
lines changed

package.json

+5
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,18 @@
2424
"@codemirror/lang-html": "^6.4.9",
2525
"@codemirror/lang-java": "^6.0.1",
2626
"@codemirror/lang-javascript": "^6.2.2",
27+
"@codemirror/lang-json": "^6.0.1",
2728
"@codemirror/lang-php": "^6.0.1",
2829
"@codemirror/lang-python": "^6.1.6",
30+
"@codemirror/lang-rust": "^6.0.1",
2931
"@codemirror/lang-sql": "^6.8.0",
32+
"@codemirror/lang-xml": "^6.1.0",
3033
"@langchain/anthropic": "^0.3.5",
3134
"@langchain/core": "^0.3.14",
3235
"@langchain/langgraph": "^0.2.18",
3336
"@langchain/langgraph-sdk": "^0.0.17",
3437
"@langchain/openai": "^0.3.11",
38+
"@nextjournal/lang-clojure": "^1.0.0",
3539
"@radix-ui/react-avatar": "^1.1.0",
3640
"@radix-ui/react-checkbox": "^1.1.2",
3741
"@radix-ui/react-dialog": "^1.1.2",
@@ -45,6 +49,7 @@
4549
"@radix-ui/react-slot": "^1.1.0",
4650
"@radix-ui/react-toast": "^1.2.1",
4751
"@radix-ui/react-tooltip": "^1.1.2",
52+
"@replit/codemirror-lang-csharp": "^6.2.0",
4853
"@supabase/ssr": "^0.5.1",
4954
"@supabase/supabase-js": "^2.45.5",
5055
"@types/react-syntax-highlighter": "^15.5.13",

src/components/artifacts/CodeRenderer.tsx

+41-20
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@ import { cpp } from "@codemirror/lang-cpp";
66
import { java } from "@codemirror/lang-java";
77
import { php } from "@codemirror/lang-php";
88
import { python } from "@codemirror/lang-python";
9-
import styles from "./CodeRenderer.module.css";
10-
import { cleanContent } from "@/lib/normalize_string";
119
import { html } from "@codemirror/lang-html";
1210
import { sql } from "@codemirror/lang-sql";
11+
import { json } from "@codemirror/lang-json";
12+
import { rust } from "@codemirror/lang-rust";
13+
import { xml } from "@codemirror/lang-xml";
14+
import { clojure } from "@nextjournal/lang-clojure";
15+
import { csharp } from "@replit/codemirror-lang-csharp";
16+
import styles from "./CodeRenderer.module.css";
17+
import { cleanContent } from "@/lib/normalize_string";
1318
import { cn } from "@/lib/utils";
1419

1520
export interface CodeRendererProps {
@@ -22,25 +27,41 @@ export interface CodeRendererProps {
2227
setUpdateRenderedArtifactRequired: Dispatch<SetStateAction<boolean>>;
2328
}
2429

25-
export function CodeRenderer(props: Readonly<CodeRendererProps>) {
26-
let extensions: any[] = [];
27-
if (props.artifactContent.language === "javascript") {
28-
extensions = [javascript({ jsx: true, typescript: false })];
29-
} else if (props.artifactContent.language === "typescript") {
30-
extensions = [javascript({ jsx: true, typescript: true })];
31-
} else if (props.artifactContent.language === "cpp") {
32-
extensions = [cpp()];
33-
} else if (props.artifactContent.language === "java") {
34-
extensions = [java()];
35-
} else if (props.artifactContent.language === "php") {
36-
extensions = [php()];
37-
} else if (props.artifactContent.language === "python") {
38-
extensions = [python()];
39-
} else if (props.artifactContent.language === "html") {
40-
extensions = [html()];
41-
} else if (props.artifactContent.language === "sql") {
42-
extensions = [sql()];
30+
const getLanguageExtension = (language: string) => {
31+
switch (language) {
32+
case "javascript":
33+
return javascript({ jsx: true, typescript: false });
34+
case "typescript":
35+
return javascript({ jsx: true, typescript: true });
36+
case "cpp":
37+
return cpp();
38+
case "java":
39+
return java();
40+
case "php":
41+
return php();
42+
case "python":
43+
return python();
44+
case "html":
45+
return html();
46+
case "sql":
47+
return sql();
48+
case "json":
49+
return json();
50+
case "rust":
51+
return rust();
52+
case "xml":
53+
return xml();
54+
case "clojure":
55+
return clojure();
56+
case "csharp":
57+
return csharp();
58+
default:
59+
return [];
4360
}
61+
};
62+
63+
export function CodeRenderer(props: Readonly<CodeRendererProps>) {
64+
const extensions = [getLanguageExtension(props.artifactContent.language)];
4465

4566
useEffect(() => {
4667
if (props.updateRenderedArtifactRequired) {

src/types.ts

+25
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ export type ProgrammingLanguageOptions =
9595
| "python"
9696
| "html"
9797
| "sql"
98+
| "json"
99+
| "rust"
100+
| "xml"
101+
| "clojure"
102+
| "csharp"
98103
| "other";
99104

100105
export const PROGRAMMING_LANGUAGES: Array<{
@@ -133,6 +138,26 @@ export const PROGRAMMING_LANGUAGES: Array<{
133138
language: "sql",
134139
label: "SQL",
135140
},
141+
{
142+
language: "json",
143+
label: "JSON",
144+
},
145+
{
146+
language: "rust",
147+
label: "Rust",
148+
},
149+
{
150+
language: "xml",
151+
label: "XML",
152+
},
153+
{
154+
language: "clojure",
155+
label: "Clojure",
156+
},
157+
{
158+
language: "csharp",
159+
label: "C#",
160+
},
136161
{
137162
language: "other",
138163
label: "Other",

yarn.lock

+93-4
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,14 @@
263263
"@lezer/common" "^1.0.0"
264264
"@lezer/javascript" "^1.0.0"
265265

266+
"@codemirror/lang-json@^6.0.1":
267+
version "6.0.1"
268+
resolved "https://registry.yarnpkg.com/@codemirror/lang-json/-/lang-json-6.0.1.tgz#0a0be701a5619c4b0f8991f9b5e95fe33f462330"
269+
integrity sha512-+T1flHdgpqDDlJZ2Lkil/rLiRy684WMLc74xUnjJH48GQdfJo/pudlTRreZmKwzP8/tGdKf83wlbAdOCzlJOGQ==
270+
dependencies:
271+
"@codemirror/language" "^6.0.0"
272+
"@lezer/json" "^1.0.0"
273+
266274
"@codemirror/lang-php@^6.0.1":
267275
version "6.0.1"
268276
resolved "https://registry.yarnpkg.com/@codemirror/lang-php/-/lang-php-6.0.1.tgz#fa34cc75562178325861a5731f79bd621f57ffaa"
@@ -285,6 +293,14 @@
285293
"@lezer/common" "^1.2.1"
286294
"@lezer/python" "^1.1.4"
287295

296+
"@codemirror/lang-rust@^6.0.1":
297+
version "6.0.1"
298+
resolved "https://registry.yarnpkg.com/@codemirror/lang-rust/-/lang-rust-6.0.1.tgz#d6829fc7baa39a15bcd174a41a9e0a1bf7cf6ba8"
299+
integrity sha512-344EMWFBzWArHWdZn/NcgkwMvZIWUR1GEBdwG8FEp++6o6vT6KL9V7vGs2ONsKxxFUPXKI0SPcWhyYyl2zPYxQ==
300+
dependencies:
301+
"@codemirror/language" "^6.0.0"
302+
"@lezer/rust" "^1.0.0"
303+
288304
"@codemirror/lang-sql@^6.8.0":
289305
version "6.8.0"
290306
resolved "https://registry.yarnpkg.com/@codemirror/lang-sql/-/lang-sql-6.8.0.tgz#1ae68ad49f378605ff88a4cc428ba667ce056068"
@@ -297,6 +313,18 @@
297313
"@lezer/highlight" "^1.0.0"
298314
"@lezer/lr" "^1.0.0"
299315

316+
"@codemirror/lang-xml@^6.1.0":
317+
version "6.1.0"
318+
resolved "https://registry.yarnpkg.com/@codemirror/lang-xml/-/lang-xml-6.1.0.tgz#e3e786e1a89fdc9520efe75c1d6d3de1c40eb91c"
319+
integrity sha512-3z0blhicHLfwi2UgkZYRPioSgVTo9PV5GP5ducFH6FaHy0IAJRg+ixj5gTR1gnT/glAIC8xv4w2VL1LoZfs+Jg==
320+
dependencies:
321+
"@codemirror/autocomplete" "^6.0.0"
322+
"@codemirror/language" "^6.4.0"
323+
"@codemirror/state" "^6.0.0"
324+
"@codemirror/view" "^6.0.0"
325+
"@lezer/common" "^1.0.0"
326+
"@lezer/xml" "^1.0.0"
327+
300328
"@codemirror/language@^6.0.0", "@codemirror/language@^6.4.0", "@codemirror/language@^6.6.0", "@codemirror/language@^6.8.0":
301329
version "6.10.3"
302330
resolved "https://registry.yarnpkg.com/@codemirror/language/-/language-6.10.3.tgz#eb25fc5ade19032e7bf1dcaa957804e5f1660585"
@@ -755,6 +783,15 @@
755783
"@lezer/highlight" "^1.1.3"
756784
"@lezer/lr" "^1.3.0"
757785

786+
"@lezer/json@^1.0.0":
787+
version "1.0.2"
788+
resolved "https://registry.yarnpkg.com/@lezer/json/-/json-1.0.2.tgz#bdc849e174113e2d9a569a5e6fb1a27e2f703eaf"
789+
integrity sha512-xHT2P4S5eeCYECyKNPhr4cbEL9tc8w83SPwRC373o9uEdrvGKTZoJVAGxpOsZckMlEh9W23Pc72ew918RWQOBQ==
790+
dependencies:
791+
"@lezer/common" "^1.2.0"
792+
"@lezer/highlight" "^1.0.0"
793+
"@lezer/lr" "^1.0.0"
794+
758795
"@lezer/lr@^1.0.0", "@lezer/lr@^1.1.0", "@lezer/lr@^1.3.0":
759796
version "1.4.2"
760797
resolved "https://registry.yarnpkg.com/@lezer/lr/-/lr-1.4.2.tgz#931ea3dea8e9de84e90781001dae30dea9ff1727"
@@ -780,6 +817,24 @@
780817
"@lezer/highlight" "^1.0.0"
781818
"@lezer/lr" "^1.0.0"
782819

820+
"@lezer/rust@^1.0.0":
821+
version "1.0.2"
822+
resolved "https://registry.yarnpkg.com/@lezer/rust/-/rust-1.0.2.tgz#cc9a75605d67182a0e799ac40b1965a61dcc6ef0"
823+
integrity sha512-Lz5sIPBdF2FUXcWeCu1//ojFAZqzTQNRga0aYv6dYXqJqPfMdCAI0NzajWUd4Xijj1IKJLtjoXRPMvTKWBcqKg==
824+
dependencies:
825+
"@lezer/common" "^1.2.0"
826+
"@lezer/highlight" "^1.0.0"
827+
"@lezer/lr" "^1.0.0"
828+
829+
"@lezer/xml@^1.0.0":
830+
version "1.0.5"
831+
resolved "https://registry.yarnpkg.com/@lezer/xml/-/xml-1.0.5.tgz#4bb7fd3e527f41b78372477aa753f035b41c3846"
832+
integrity sha512-VFouqOzmUWfIg+tfmpcdV33ewtK+NSwd4ngSe1aG7HFb4BN0ExyY1b8msp+ndFrnlG4V4iC8yXacjFtrwERnaw==
833+
dependencies:
834+
"@lezer/common" "^1.2.0"
835+
"@lezer/highlight" "^1.0.0"
836+
"@lezer/lr" "^1.0.0"
837+
783838
"@mantine/core@^7.10.1":
784839
version "7.13.3"
785840
resolved "https://registry.yarnpkg.com/@mantine/core/-/core-7.13.3.tgz#ad31f00de6d6eb52f0bc2ce1b95f79ac7ab25a67"
@@ -859,6 +914,21 @@
859914
resolved "https://registry.yarnpkg.com/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-14.2.7.tgz#75e1d90758cb10a547e1cdfb878871da28123682"
860915
integrity sha512-pxsI23gKWRt/SPHFkDEsP+w+Nd7gK37Hpv0ngc5HpWy2e7cKx9zR/+Q2ptAUqICNTecAaGWvmhway7pj/JLEWA==
861916

917+
"@nextjournal/lang-clojure@^1.0.0":
918+
version "1.0.0"
919+
resolved "https://registry.yarnpkg.com/@nextjournal/lang-clojure/-/lang-clojure-1.0.0.tgz#0efbd594769e606eea532758519a239f0d38959d"
920+
integrity sha512-gOCV71XrYD0DhwGoPMWZmZ0r92/lIHsqQu9QWdpZYYBwiChNwMO4sbVMP7eTuAqffFB2BTtCSC+1skSH9d3bNg==
921+
dependencies:
922+
"@codemirror/language" "^6.0.0"
923+
"@nextjournal/lezer-clojure" "1.0.0"
924+
925+
"@nextjournal/lezer-clojure@1.0.0":
926+
version "1.0.0"
927+
resolved "https://registry.yarnpkg.com/@nextjournal/lezer-clojure/-/lezer-clojure-1.0.0.tgz#0e7ff75f8d0fabed36d26b9f6b5f00d8a9f385e6"
928+
integrity sha512-VZyuGu4zw5mkTOwQBTaGVNWmsOZAPw5ZRxu1/Knk/Xfs7EDBIogwIs5UXTYkuECX5ZQB8eOB+wKA2pc7VyqaZQ==
929+
dependencies:
930+
"@lezer/lr" "^1.0.0"
931+
862932
"@nodelib/fs.scandir@2.1.5":
863933
version "2.1.5"
864934
resolved "https://registry.yarnpkg.com/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz#7619c2eb21b25483f6d167548b4cfd5a7488c3d5"
@@ -1422,6 +1492,11 @@
14221492
resolved "https://registry.yarnpkg.com/@remirror/core-constants/-/core-constants-3.0.0.tgz#96fdb89d25c62e7b6a5d08caf0ce5114370e3b8f"
14231493
integrity sha512-42aWfPrimMfDKDi4YegyS7x+/0tlzaqwPQCULLanv3DMIlu96KTJR0fM5isWX2UViOqlGnX6YFgqWepcX+XMNg==
14241494

1495+
"@replit/codemirror-lang-csharp@^6.2.0":
1496+
version "6.2.0"
1497+
resolved "https://registry.yarnpkg.com/@replit/codemirror-lang-csharp/-/codemirror-lang-csharp-6.2.0.tgz#bd652f5788ad93579ee0dcab5b163ed2674b974f"
1498+
integrity sha512-6utbaWkoymhoAXj051mkRp+VIJlpwUgCX9Toevz3YatiZsz512fw3OVCedXQx+WcR0wb6zVHjChnuxqfCLtFVQ==
1499+
14251500
"@rtsao/scc@^1.1.0":
14261501
version "1.1.0"
14271502
resolved "https://registry.yarnpkg.com/@rtsao/scc/-/scc-1.1.0.tgz#927dd2fae9bc3361403ac2c7a00c32ddce9ad7e8"
@@ -6952,8 +7027,16 @@ streamsearch@^1.1.0:
69527027
resolved "https://registry.yarnpkg.com/streamsearch/-/streamsearch-1.1.0.tgz#404dd1e2247ca94af554e841a8ef0eaa238da764"
69537028
integrity sha512-Mcc5wHehp9aXz1ax6bZUyY5afg9u2rv5cqQI3mRrYkGC8rW2hM02jWuwjtL++LS5qinSyhj2QfLyNsuc+VsExg==
69547029

6955-
"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.1.0:
6956-
name string-width-cjs
7030+
"string-width-cjs@npm:string-width@^4.2.0":
7031+
version "4.2.3"
7032+
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
7033+
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
7034+
dependencies:
7035+
emoji-regex "^8.0.0"
7036+
is-fullwidth-code-point "^3.0.0"
7037+
strip-ansi "^6.0.1"
7038+
7039+
string-width@^4.1.0:
69577040
version "4.2.3"
69587041
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
69597042
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
@@ -7041,8 +7124,14 @@ stringify-entities@^4.0.0:
70417124
character-entities-html4 "^2.0.0"
70427125
character-entities-legacy "^3.0.0"
70437126

7044-
"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
7045-
name strip-ansi-cjs
7127+
"strip-ansi-cjs@npm:strip-ansi@^6.0.1":
7128+
version "6.0.1"
7129+
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
7130+
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
7131+
dependencies:
7132+
ansi-regex "^5.0.1"
7133+
7134+
strip-ansi@^6.0.0, strip-ansi@^6.0.1:
70467135
version "6.0.1"
70477136
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
70487137
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==

0 commit comments

Comments
 (0)