MarkdownTokens.svelte 3.29 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
<script lang="ts">
Timothy J. Baek's avatar
Timothy J. Baek committed
2
	import { marked } from 'marked';
Timothy J. Baek's avatar
Timothy J. Baek committed
3
	import type { Token } from 'marked';
Timothy J. Baek's avatar
Timothy J. Baek committed
4
	import { revertSanitizedResponseContent, unescapeHtml } from '$lib/utils';
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
5

Timothy J. Baek's avatar
Timothy J. Baek committed
6
	import { onMount } from 'svelte';
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
7
8
9
10

	import Image from '$lib/components/common/Image.svelte';
	import CodeBlock from '$lib/components/chat/Messages/CodeBlock.svelte';

Timothy J. Baek's avatar
Timothy J. Baek committed
11
12
13
14
15
16
	import MarkdownInlineTokens from '$lib/components/chat/Messages/MarkdownInlineTokens.svelte';

	export let id: string;
	export let tokens: Token[];
	export let top = true;

Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
17
18
	let containerElement;

Timothy J. Baek's avatar
Timothy J. Baek committed
19
20
21
22
	const headerComponent = (depth: number) => {
		return 'h' + depth;
	};

Timothy J. Baek's avatar
Timothy J. Baek committed
23
24
25
26
27
28
	const renderer = new marked.Renderer();
	// For code blocks with simple backticks
	renderer.codespan = (code) => {
		return `<code class="codespan">${code.replaceAll('&amp;', '&')}</code>`;
	};

Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
29
	let codes = [];
Timothy J. Baek's avatar
Timothy J. Baek committed
30
	renderer.code = (code, lang) => {
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
31
32
33
34
35
36
37
38
		codes.push({
			code: code,
			lang: lang
		});
		codes = codes;
		const codeId = `${id}-${codes.length}`;

		const interval = setInterval(() => {
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
39
40
			const codeElement = document.getElementById(`code-${codeId}`);
			if (codeElement) {
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
41
				clearInterval(interval);
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
42
43
44
45
				// If the code is already loaded, don't load it again
				if (codeElement.innerHTML) {
					return;
				}
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
46
47

				new CodeBlock({
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
48
					target: codeElement,
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
49
50
51
52
53
54
55
56
57
58
59
					props: {
						id: `${id}-${codes.length}`,
						lang: lang,
						code: revertSanitizedResponseContent(code)
					},
					hydrate: true,
					$$inline: true
				});
			}
		}, 10);

Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
60
		return `<div id="code-${id}-${codes.length}"></div>`;
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
61
62
63
64
65
66
67
68
69
70
71
72
73
	};

	let images = [];
	renderer.image = (href, title, text) => {
		images.push({
			href: href,
			title: title,
			text: text
		});
		images = images;

		const imageId = `${id}-${images.length}`;
		const interval = setInterval(() => {
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
74
75
			const imageElement = document.getElementById(`image-${imageId}`);
			if (imageElement) {
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
76
				clearInterval(interval);
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
77
78
79
80
81
82
83

				// If the image is already loaded, don't load it again
				if (imageElement.innerHTML) {
					return;
				}

				console.log('image', href, text);
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
84
				new Image({
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
85
					target: imageElement,
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
86
87
88
89
					props: {
						src: href,
						alt: text
					},
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
90
					$$inline: true
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
91
92
93
94
				});
			}
		}, 10);

Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
95
		return `<div id="image-${id}-${images.length}"></div>`;
Timothy J. Baek's avatar
Timothy J. Baek committed
96
97
98
99
100
101
102
103
	};

	// Open all links in a new tab/window (from https://github.com/markedjs/marked/issues/655#issuecomment-383226346)
	const origLinkRenderer = renderer.link;
	renderer.link = (href, title, text) => {
		const html = origLinkRenderer.call(renderer, href, title, text);
		return html.replace(/^<a /, '<a target="_blank" rel="nofollow" ');
	};
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
104

Timothy J. Baek's avatar
Timothy J. Baek committed
105
106
107
108
	const { extensions, ...defaults } = marked.getDefaults() as marked.MarkedOptions & {
		// eslint-disable-next-line @typescript-eslint/no-explicit-any
		extensions: any;
	};
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
109
110
111
112
113

	$: if (tokens) {
		images = [];
		codes = [];
	}
Timothy J. Baek's avatar
Timothy J. Baek committed
114
115
</script>

Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<div bind:this={containerElement} class="flex flex-col">
	{#each tokens as token, tokenIdx (`${id}-${tokenIdx}`)}
		{#if token.type === 'code'}
			{#if token.lang === 'mermaid'}
				<pre class="mermaid">{revertSanitizedResponseContent(token.text)}</pre>
			{:else}
				<CodeBlock
					id={`${id}-${tokenIdx}`}
					lang={token?.lang ?? ''}
					code={revertSanitizedResponseContent(token?.text ?? '')}
				/>
			{/if}
		{:else}
			{@html marked.parse(token.raw, {
				...defaults,
				gfm: true,
				breaks: true,
				renderer
			})}
		{/if}
	{/each}
</div>