From e6b3de310bb3f1c55ff2c085ce24a64347787d26 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Wed, 7 Aug 2024 01:55:37 +0200 Subject: [PATCH] refac --- .../chat/Messages/MarkdownTokens.svelte | 115 ++++++++++++++---- src/lib/components/common/Image.svelte | 23 ++-- 2 files changed, 100 insertions(+), 38 deletions(-) diff --git a/src/lib/components/chat/Messages/MarkdownTokens.svelte b/src/lib/components/chat/Messages/MarkdownTokens.svelte index a182e5cd5..b9dd6bfd4 100644 --- a/src/lib/components/chat/Messages/MarkdownTokens.svelte +++ b/src/lib/components/chat/Messages/MarkdownTokens.svelte @@ -2,14 +2,20 @@ import { marked } from 'marked'; import type { Token } from 'marked'; import { revertSanitizedResponseContent, unescapeHtml } from '$lib/utils'; - import CodeBlock from '$lib/components/chat/Messages/CodeBlock.svelte'; + import { onMount } from 'svelte'; + + import Image from '$lib/components/common/Image.svelte'; + import CodeBlock from '$lib/components/chat/Messages/CodeBlock.svelte'; + import MarkdownInlineTokens from '$lib/components/chat/Messages/MarkdownInlineTokens.svelte'; export let id: string; export let tokens: Token[]; export let top = true; + let containerElement; + const headerComponent = (depth: number) => { return 'h' + depth; }; @@ -20,8 +26,61 @@ return `${code.replaceAll('&', '&')}`; }; + let codes = []; renderer.code = (code, lang) => { - return `
${code}
`; + codes.push({ + code: code, + lang: lang + }); + codes = codes; + const codeId = `${id}-${codes.length}`; + + const interval = setInterval(() => { + if (document.getElementById(`code-${codeId}`)) { + clearInterval(interval); + + new CodeBlock({ + target: document.getElementById(`code-${codeId}`), + props: { + id: `${id}-${codes.length}`, + lang: lang, + code: revertSanitizedResponseContent(code) + }, + hydrate: true, + $$inline: true + }); + } + }, 10); + + return `
`; + }; + + 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(() => { + if (document.getElementById(`image-${imageId}`)) { + clearInterval(interval); + new Image({ + target: document.getElementById(`image-${imageId}`), + props: { + src: href, + alt: text + }, + hydrate: true + }); + } + }, 10); + + return ` diff --git a/src/lib/components/common/Image.svelte b/src/lib/components/common/Image.svelte index 76340f414..53305b836 100644 --- a/src/lib/components/common/Image.svelte +++ b/src/lib/components/common/Image.svelte @@ -13,18 +13,13 @@ let showImagePreview = false; -
- - - { - showImagePreview = true; - }} - src={_src} - {alt} - class=" rounded-lg cursor-pointer" - draggable="false" - data-cy="image" - /> -
+ +