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
9435d204
Commit
9435d204
authored
Aug 14, 2024
by
Timothy J. Baek
Browse files
refac: more robust mermaid chart rendering
parent
609a42c2
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
33 additions
and
16 deletions
+33
-16
src/lib/components/chat/Messages/CodeBlock.svelte
src/lib/components/chat/Messages/CodeBlock.svelte
+29
-15
src/lib/components/chat/Messages/MarkdownTokens.svelte
src/lib/components/chat/Messages/MarkdownTokens.svelte
+1
-1
src/routes/(app)/+layout.svelte
src/routes/(app)/+layout.svelte
+3
-0
No files found.
src/lib/components/chat/Messages/CodeBlock.svelte
View file @
9435d204
...
...
@@ -3,7 +3,7 @@
import { loadPyodide } from 'pyodide';
import mermaid from 'mermaid';
import { getContext, getAllContexts } from 'svelte';
import { getContext, getAllContexts
, onMount
} from 'svelte';
import { copyToClipboard } from '$lib/utils';
import 'highlight.js/styles/github-dark.min.css';
...
...
@@ -18,6 +18,8 @@
export let lang = '';
export let code = '';
let mermaidHtml = null;
let highlightedCode = null;
let executing = false;
...
...
@@ -213,17 +215,14 @@ __builtins__.input = input`);
$: if (code) {
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);
(async () => {
try {
const { svg } = await mermaid.render(`mermaid-${id}`, code);
mermaidHtml = svg;
} catch (error) {
console.error('Error:', error);
}
})();
} else {
// Function to perform the code highlighting
const highlightCode = () => {
...
...
@@ -236,13 +235,28 @@ __builtins__.input = input`);
debounceTimeout = setTimeout(highlightCode, 10);
}
}
onMount(async () => {
await mermaid.initialize({ startOnLoad: true });
if (lang === 'mermaid' && (token?.raw ?? '').endsWith('```')) {
try {
const { svg } = await mermaid.render(`mermaid-${id}`, code);
mermaidHtml = svg;
} catch (error) {
console.error('Error:', error);
}
}
});
</script>
<div class="my-2" dir="ltr">
{#if lang === 'mermaid'}
{#key code}
<pre class="mermaid-{id}">{code}</pre>
{/key}
{#if mermaidHtml}
{@html mermaidHtml}
{:else}
<pre class=" mermaid-{id}">{code}</pre>
{/if}
{:else}
<div
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 @
9435d204
...
...
@@ -26,7 +26,7 @@
</svelte:element>
{:else if token.type === 'code'}
<CodeBlock
{
id}
id
={`${id}-${tokenIdx}`
}
{token}
lang={token?.lang ?? ''}
code={revertSanitizedResponseContent(token?.text ?? '')}
...
...
src/routes/(app)/+layout.svelte
View file @
9435d204
...
...
@@ -3,6 +3,8 @@
import { onMount, tick, getContext } from 'svelte';
import { openDB, deleteDB } from 'idb';
import fileSaver from 'file-saver';
import mermaid from 'mermaid';
const { saveAs } = fileSaver;
import { goto } from '$app/navigation';
...
...
@@ -178,6 +180,7 @@
await tick();
}
await mermaid.initialize({ startOnLoad: false });
loaded = true;
});
</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