11<script lang =" ts" >
2- import type { HTMLAttributes } from ' svelte/elements' ;
2+ import type { ClassValue , HTMLAttributes } from ' svelte/elements' ;
33 import Highlight , { type LanguageType } from ' svelte-highlight' ;
4- import ' svelte-highlight/styles/github.css' ;
4+ import ' svelte-highlight/styles/atelier-dune-light.css' ;
5+ // import 'svelte-highlight/styles/github.css';
6+ import { clsx } from ' clsx' ;
57
68 interface Props extends HTMLAttributes <HTMLDivElement > {
79 language: LanguageType <string >;
1315 let { language, code, title, copyable = true , class : cssClass, ... restProps }: Props = $props ();
1416
1517 let copied = $state (false );
18+ let codeNode = $state <HTMLElement | null >(null );
19+
20+ $effect (() => {
21+ const root = codeNode ?.querySelector (' .hljs' );
22+ if (! root ) return ;
23+ styleDirectTextChildren (root , ' orphan-text-node' );
24+ });
25+
26+ function styleDirectTextChildren(root : Node , cssClass : ClassValue ) {
27+ for (const node of root .childNodes ) {
28+ if (node .nodeType === Node .TEXT_NODE && node .nodeValue ?.trim ()) {
29+ const span = document .createElement (' span' );
30+ span .textContent = node .nodeValue ;
31+ span .classList .add (clsx (cssClass ));
32+ root .replaceChild (span , node );
33+ }
34+ }
35+ }
1636
1737 async function copyToClipboard() {
1838 try {
4969 {/if }
5070 </div >
5171 {/if }
52- <div class =" code-snippet-content" >
72+ <div bind:this ={ codeNode } class =" code-snippet-content" >
5373 <Highlight {language } {code } />
5474 </div >
5575</div >
5676
57- <!--
58- @component
59- # CodeSnippet
60- A syntax-highlighted code block with optional title and copy functionality.
61-
62- ## Props
63- - `language` (LanguageType): The language for syntax highlighting
64- - `code` (string): The code content to display
65- - `title` (string, optional): Optional title for the code snippet
66- - `copyable` (boolean, default: true): Whether to show the copy button
67-
68- ## Usage
69- ```svelte
70- <script>
71- import CodeSnippet from './lib/CodeSnippet.svelte';
72- import typescript from "svelte-highlight/languages/typescript";
73- </script>
74-
75- <CodeSnippet
76- language={typescript}
77- code="console.log('Hello, world!');"
78- title="Example"
79- />
80- ```
81- -->
82-
8377<style >
8478 .code-snippet {
8579 border : 1px solid var (--bs-border-color );
@@ -99,6 +93,10 @@ A syntax-highlighted code block with optional title and copy functionality.
9993 font-size : 0.875rem ;
10094 }
10195
96+ :global([data-bs-theme = ' dark' ]) .code-snippet-header {
97+ background-color : var (--bs-gray-800 );
98+ }
99+
102100 .code-snippet-title {
103101 font-weight : 600 ;
104102 color : var (--bs-secondary );
@@ -134,8 +132,11 @@ A syntax-highlighted code block with optional title and copy functionality.
134132 /* Dark mode support */
135133 @media (prefers-color-scheme: dark) {
136134 .code-snippet-header {
137- background-color : var (--bs-dark );
138- border-bottom-color : var (--bs-secondary );
135+ background-color : var (--bs-gray-800 );
139136 }
140137 }
138+
139+ :global .orphan-text-node {
140+ color : var (--bs-body-color );
141+ }
141142 </style >
0 commit comments