File size: 1,469 Bytes
2c972ff
9577634
e3c83d9
ace9a3e
d852bfd
8cbfe1a
3c71c00
 
 
 
8cbfe1a
ace9a3e
 
 
 
d852bfd
ace9a3e
 
d852bfd
2c972ff
 
8aab1a5
2c972ff
 
 
 
 
 
 
3c71c00
e3c83d9
2c972ff
d852bfd
 
 
 
 
 
 
2c972ff
 
 
 
 
 
690e2ca
 
2c972ff
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<script lang="ts">
	import { marked } from 'marked';
	import type { Message } from '$lib/types/Message';

	import CodeBlock from '../CodeBlock.svelte';

	function sanitizeMd(md: string) {
		return md.replaceAll('<', '&lt;');
	}

	export let message: Message;
	let el: HTMLElement;

	const options: marked.MarkedOptions = {
		...marked.getDefaults(),
		gfm: true
	};

	$: tokens = marked.lexer(sanitizeMd(message.content));
</script>

{#if message.from === 'assistant'}
	<div class="flex items-start justify-start gap-4 leading-relaxed">
		<img
			alt=""
			src="https://huggingface.co/avatars/2edb18bd0206c16b433841a47f53fa8e.svg"
			class="mt-5 w-3 h-3 flex-none rounded-full shadow-lg"
		/>
		<div
			class="prose dark:prose-invert :prose-pre:bg-gray-100 dark:prose-pre:bg-gray-950 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 text-gray-600 dark:text-gray-300"
			bind:this={el}
		>
			{#each tokens as token}
				{#if token.type === 'code'}
					<CodeBlock lang={token.lang} code={token.text} />
				{:else}
					{@html marked.parser([token], options)}
				{/if}
			{/each}
		</div>
	</div>
{/if}
{#if message.from === 'user'}
	<div class="flex items-start justify-start gap-4">
		<div class="mt-5 w-3 h-3 flex-none rounded-full" />
		<div class="rounded-2xl px-5 py-3.5 text-gray-500 dark:text-gray-400 whitespace-break-spaces">
			{message.content.trim()}
		</div>
	</div>
{/if}