Adrien Denat commited on
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
1 unmodified line2345676 unmodified lines1415161718192027 unmodified lines484950515253547 unmodified lines6263646566676810 unmodified lines798081828384851 unmodified lineimport { marked } from 'marked';import type { Message } from '$lib/types/Message';import { afterUpdate } from 'svelte';import CopyToClipBoardBtn from '../CopyToClipBoardBtn.svelte';6 unmodified lines// Add wrapper to code blocksrenderer.code = (code, lang) => {return `<div class="code-block"><pre><code class="language-${lang}">${code}</code></pre>27 unmodified linesrenderer};$: marked(message.content, options, handleParsed);afterUpdate(() => {if (el) {7 unmodified linestarget: block,props: {value: (block as HTMLElement).innerText ?? '',classNames: 'absolute top-2 right-2'}});block.classList.add('has-copy-btn');10 unmodified linesclass="mt-5 w-3 h-3 flex-none rounded-full shadow-lg"/><divclass="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"bind:this={el}>{@html html}1 unmodified line23456786 unmodified lines1516171819202127 unmodified lines4950515253545556577 unmodified lines656667686970717210 unmodified lines838485868788891 unmodified lineimport { marked } from 'marked';import type { Message } from '$lib/types/Message';import { afterUpdate } from 'svelte';import { browser } from '$app/environment';import CopyToClipBoardBtn from '../CopyToClipBoardBtn.svelte';6 unmodified lines// Add wrapper to code blocksrenderer.code = (code, lang) => {return `<div class="group code-block"><pre><code class="language-${lang}">${code}</code></pre>27 unmodified linesrenderer};$: browser && marked(message.content, options, handleParsed);html = marked(message.content, options);afterUpdate(() => {if (el) {7 unmodified linestarget: block,props: {value: (block as HTMLElement).innerText ?? '',classNames:'absolute top-2 right-2 invisible opacity-0 group-hover:visible group-hover:opacity-100'}});block.classList.add('has-copy-btn');10 unmodified linesclass="mt-5 w-3 h-3 flex-none rounded-full shadow-lg"/><divclass="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"bind:this={el}>{@html html}
src/lib/components/chat/ChatMessage.svelte+8-4