Adrien Denat commited on
Commit
4a0bbdf
1 Parent(s): 7f827cf

Adrien syntax highlighting ssr (#32)

Browse files

* only show copy to clipboard button on hover of code block

* only highlight code on client-side to support parsed markdown on SSR

src/lib/components/chat/ChatMessage.svelte CHANGED
@@ -2,6 +2,7 @@
2
  import { marked } from 'marked';
3
  import type { Message } from '$lib/types/Message';
4
  import { afterUpdate } from 'svelte';
 
5
 
6
  import CopyToClipBoardBtn from '../CopyToClipBoardBtn.svelte';
7
 
@@ -14,7 +15,7 @@
14
  // Add wrapper to code blocks
15
  renderer.code = (code, lang) => {
16
  return `
17
- <div class="code-block">
18
  <pre>
19
  <code class="language-${lang}">${code}</code>
20
  </pre>
@@ -48,7 +49,9 @@
48
  renderer
49
  };
50
 
51
- $: marked(message.content, options, handleParsed);
 
 
52
 
53
  afterUpdate(() => {
54
  if (el) {
@@ -62,7 +65,8 @@
62
  target: block,
63
  props: {
64
  value: (block as HTMLElement).innerText ?? '',
65
- classNames: 'absolute top-2 right-2'
 
66
  }
67
  });
68
  block.classList.add('has-copy-btn');
@@ -79,7 +83,7 @@
79
  class="mt-5 w-3 h-3 flex-none rounded-full shadow-lg"
80
  />
81
  <div
82
- class="group relative rounded-2xl px-5 py-3.5 border border-gray-100 bg-gradient-to-br from-gray-50 dark:from-gray-800/40 dark:border-gray-800 prose text-gray-600 dark:text-gray-300"
83
  bind:this={el}
84
  >
85
  {@html html}
 
2
  import { marked } from 'marked';
3
  import type { Message } from '$lib/types/Message';
4
  import { afterUpdate } from 'svelte';
5
+ import { browser } from '$app/environment';
6
 
7
  import CopyToClipBoardBtn from '../CopyToClipBoardBtn.svelte';
8
 
 
15
  // Add wrapper to code blocks
16
  renderer.code = (code, lang) => {
17
  return `
18
+ <div class="group code-block">
19
  <pre>
20
  <code class="language-${lang}">${code}</code>
21
  </pre>
 
49
  renderer
50
  };
51
 
52
+ $: browser && marked(message.content, options, handleParsed);
53
+
54
+ html = marked(message.content, options);
55
 
56
  afterUpdate(() => {
57
  if (el) {
 
65
  target: block,
66
  props: {
67
  value: (block as HTMLElement).innerText ?? '',
68
+ classNames:
69
+ 'absolute top-2 right-2 invisible opacity-0 group-hover:visible group-hover:opacity-100'
70
  }
71
  });
72
  block.classList.add('has-copy-btn');
 
83
  class="mt-5 w-3 h-3 flex-none rounded-full shadow-lg"
84
  />
85
  <div
86
+ class="relative rounded-2xl px-5 py-3.5 border border-gray-100 bg-gradient-to-br from-gray-50 dark:from-gray-800/40 dark:border-gray-800 prose text-gray-600 dark:text-gray-300"
87
  bind:this={el}
88
  >
89
  {@html html}