Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
open-webui
Commits
609a42c2
Commit
609a42c2
authored
Aug 14, 2024
by
Timothy J. Baek
Browse files
refac: mermaid chart rendering
parent
6a1e7ab0
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
31 additions
and
19 deletions
+31
-19
src/lib/components/chat/Messages/CodeBlock.svelte
src/lib/components/chat/Messages/CodeBlock.svelte
+30
-11
src/lib/components/chat/Messages/MarkdownTokens.svelte
src/lib/components/chat/Messages/MarkdownTokens.svelte
+1
-0
src/lib/components/chat/Messages/ResponseMessage.svelte
src/lib/components/chat/Messages/ResponseMessage.svelte
+0
-8
No files found.
src/lib/components/chat/Messages/CodeBlock.svelte
View file @
609a42c2
<script lang="ts">
<script lang="ts">
import hljs from 'highlight.js';
import hljs from 'highlight.js';
import { loadPyodide } from 'pyodide';
import { loadPyodide } from 'pyodide';
import mermaid from 'mermaid';
import { getContext, getAllContexts } from 'svelte';
import { getContext, getAllContexts } from 'svelte';
import { copyToClipboard } from '$lib/utils';
import { copyToClipboard } from '$lib/utils';
...
@@ -12,6 +14,7 @@
...
@@ -12,6 +14,7 @@
export let id = '';
export let id = '';
export let token;
export let lang = '';
export let lang = '';
export let code = '';
export let code = '';
...
@@ -207,23 +210,39 @@ __builtins__.input = input`);
...
@@ -207,23 +210,39 @@ __builtins__.input = input`);
};
};
let debounceTimeout;
let debounceTimeout;
$: if (code) {
// Function to perform the code highlighting
const highlightCode = () => {
highlightedCode = hljs.highlightAuto(code, hljs.getLanguage(lang)?.aliases).value || code;
};
// Clear the previous timeout if it exists
clearTimeout(debounceTimeout);
// Set a new timeout to debounce the code highlighting
$: if (code) {
debounceTimeout = setTimeout(highlightCode, 10);
if (lang === 'mermaid' && (token?.raw ?? '').endsWith('```')) {
// Function to perform the code highlighting
const renderMermaid = async () => {
// mermaid.initialize({ startOnLoad: true });
await mermaid.run({
querySelector: `.mermaid-${id}`
});
};
// Clear the previous timeout if it exists
clearTimeout(debounceTimeout);
// Set a new timeout to debounce the code highlighting
debounceTimeout = setTimeout(renderMermaid, 50);
} else {
// Function to perform the code highlighting
const highlightCode = () => {
highlightedCode = hljs.highlightAuto(code, hljs.getLanguage(lang)?.aliases).value || code;
};
// Clear the previous timeout if it exists
clearTimeout(debounceTimeout);
// Set a new timeout to debounce the code highlighting
debounceTimeout = setTimeout(highlightCode, 10);
}
}
}
</script>
</script>
<div class="my-2" dir="ltr">
<div class="my-2" dir="ltr">
{#if lang === 'mermaid'}
{#if lang === 'mermaid'}
<pre class="mermaid">{code}</pre>
{#key code}
<pre class="mermaid-{id}">{code}</pre>
{/key}
{:else}
{:else}
<div
<div
class="flex justify-between bg-[#202123] text-white text-xs px-4 pt-1 pb-0.5 rounded-t-lg overflow-x-auto"
class="flex justify-between bg-[#202123] text-white text-xs px-4 pt-1 pb-0.5 rounded-t-lg overflow-x-auto"
...
...
src/lib/components/chat/Messages/MarkdownTokens.svelte
View file @
609a42c2
...
@@ -27,6 +27,7 @@
...
@@ -27,6 +27,7 @@
{:else if token.type === 'code'}
{:else if token.type === 'code'}
<CodeBlock
<CodeBlock
{id}
{id}
{token}
lang={token?.lang ?? ''}
lang={token?.lang ?? ''}
code={revertSanitizedResponseContent(token?.text ?? '')}
code={revertSanitizedResponseContent(token?.text ?? '')}
/>
/>
...
...
src/lib/components/chat/Messages/ResponseMessage.svelte
View file @
609a42c2
...
@@ -2,7 +2,6 @@
...
@@ -2,7 +2,6 @@
import { toast } from 'svelte-sonner';
import { toast } from 'svelte-sonner';
import dayjs from 'dayjs';
import dayjs from 'dayjs';
import { marked } from 'marked';
import { marked } from 'marked';
import mermaid from 'mermaid';
import { fade } from 'svelte/transition';
import { fade } from 'svelte/transition';
import { createEventDispatcher } from 'svelte';
import { createEventDispatcher } from 'svelte';
...
@@ -277,18 +276,11 @@
...
@@ -277,18 +276,11 @@
$: if (!edit) {
$: if (!edit) {
(async () => {
(async () => {
await tick();
await tick();
await mermaid.run({
querySelector: '.mermaid'
});
})();
})();
}
}
onMount(async () => {
onMount(async () => {
await tick();
await tick();
await mermaid.run({
querySelector: '.mermaid'
});
});
});
</script>
</script>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment