From a4e46e6e18140afbf376ef3baa26aa5e90c27d94 Mon Sep 17 00:00:00 2001
From: Jay V <air@live.ca>
Date: Fri, 30 May 2025 13:58:32 -0400
Subject: share page diff

---
 app/packages/web/src/components/CodeBlock.tsx | 47 +++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)
 create mode 100644 app/packages/web/src/components/CodeBlock.tsx

(limited to 'app/packages/web/src/components/CodeBlock.tsx')

diff --git a/app/packages/web/src/components/CodeBlock.tsx b/app/packages/web/src/components/CodeBlock.tsx
new file mode 100644
index 000000000..17559ece1
--- /dev/null
+++ b/app/packages/web/src/components/CodeBlock.tsx
@@ -0,0 +1,47 @@
+import {
+  type JSX,
+  onCleanup,
+  splitProps,
+  createEffect,
+  createResource,
+} from "solid-js"
+import { codeToHtml } from "shiki"
+import { transformerNotationDiff } from '@shikijs/transformers'
+
+interface CodeBlockProps extends JSX.HTMLAttributes<HTMLDivElement> {
+  code: string
+  lang?: string
+}
+function CodeBlock(props: CodeBlockProps) {
+  const [local, rest] = splitProps(props, ["code", "lang"])
+  let containerRef!: HTMLDivElement
+
+  const [html] = createResource(async () => {
+    return (await codeToHtml(local.code, {
+      lang: local.lang || "text",
+      themes: {
+        light: 'github-light',
+        dark: 'github-dark',
+      },
+      transformers: [
+        transformerNotationDiff(),
+      ],
+    })) as string
+  })
+
+  onCleanup(() => {
+    if (containerRef) containerRef.innerHTML = ""
+  })
+
+  createEffect(() => {
+    if (html() && containerRef) {
+      containerRef.innerHTML = html() as string
+    }
+  })
+
+  return (
+    <div ref={containerRef} {...rest}></div>
+  )
+}
+
+export default CodeBlock
-- 
cgit v1.2.3

