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 `
`; }; // Open all links in a new tab/window (from https://github.com/markedjs/marked/issues/655#issuecomment-383226346) @@ -30,24 +89,31 @@ const html = origLinkRenderer.call(renderer, href, title, text); return html.replace(/^ -{#each tokens as token, tokenIdx (`${id}-${tokenIdx}`)} - {#if token.type === 'code'} - {#if token.lang === 'mermaid'} -
{revertSanitizedResponseContent(token.text)}
- {:else} - - {/if} - - - {:else} - {@html marked.parse(token.raw, { - ...defaults, - gfm: true, - breaks: true, - renderer - })} - {/if} -{/each} + {:else} + {@html marked.parse(token.raw, { + ...defaults, + gfm: true, + breaks: true, + renderer + })} + {/if} + {/each} +
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" - /> -
+ +