Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
feat(markdown): move highlighting to worker (#1741)
Browse files
src/lib/components/CodeBlock.svelte
CHANGED
|
@@ -1,24 +1,19 @@
|
|
| 1 |
<script lang="ts">
|
| 2 |
import CopyToClipBoardBtn from "./CopyToClipBoardBtn.svelte";
|
| 3 |
import DOMPurify from "isomorphic-dompurify";
|
| 4 |
-
import hljs from "highlight.js";
|
| 5 |
|
| 6 |
interface Props {
|
| 7 |
code?: string;
|
| 8 |
lang?: string;
|
| 9 |
}
|
| 10 |
|
| 11 |
-
let { code = ""
|
| 12 |
-
|
| 13 |
-
let highlightedCode = $derived(hljs.highlightAuto(code, hljs.getLanguage(lang)?.aliases).value);
|
| 14 |
</script>
|
| 15 |
|
| 16 |
<div class="group relative my-4 rounded-lg">
|
| 17 |
<pre
|
| 18 |
class="scrollbar-custom overflow-auto px-5 font-mono scrollbar-thumb-gray-500 hover:scrollbar-thumb-gray-400 dark:scrollbar-thumb-white/10 dark:hover:scrollbar-thumb-white/20"><code
|
| 19 |
-
><!-- eslint-disable svelte/no-at-html-tags -->{@html DOMPurify.sanitize(
|
| 20 |
-
highlightedCode
|
| 21 |
-
)}</code
|
| 22 |
></pre>
|
| 23 |
<CopyToClipBoardBtn
|
| 24 |
classNames="btn rounded-lg border border-gray-200 px-2 py-2 text-sm shadow-sm transition-all hover:border-gray-300 active:shadow-inner dark:border-gray-700 dark:hover:border-gray-500 absolute top-2 right-2 invisible opacity-0 group-hover:visible group-hover:opacity-100 dark:text-gray-700 text-gray-200"
|
|
|
|
| 1 |
<script lang="ts">
|
| 2 |
import CopyToClipBoardBtn from "./CopyToClipBoardBtn.svelte";
|
| 3 |
import DOMPurify from "isomorphic-dompurify";
|
|
|
|
| 4 |
|
| 5 |
interface Props {
|
| 6 |
code?: string;
|
| 7 |
lang?: string;
|
| 8 |
}
|
| 9 |
|
| 10 |
+
let { code = "" }: Props = $props();
|
|
|
|
|
|
|
| 11 |
</script>
|
| 12 |
|
| 13 |
<div class="group relative my-4 rounded-lg">
|
| 14 |
<pre
|
| 15 |
class="scrollbar-custom overflow-auto px-5 font-mono scrollbar-thumb-gray-500 hover:scrollbar-thumb-gray-400 dark:scrollbar-thumb-white/10 dark:hover:scrollbar-thumb-white/20"><code
|
| 16 |
+
><!-- eslint-disable svelte/no-at-html-tags -->{@html DOMPurify.sanitize(code)}</code
|
|
|
|
|
|
|
| 17 |
></pre>
|
| 18 |
<CopyToClipBoardBtn
|
| 19 |
classNames="btn rounded-lg border border-gray-200 px-2 py-2 text-sm shadow-sm transition-all hover:border-gray-300 active:shadow-inner dark:border-gray-700 dark:hover:border-gray-500 absolute top-2 right-2 invisible opacity-0 group-hover:visible group-hover:opacity-100 dark:text-gray-700 text-gray-200"
|
src/lib/components/chat/MarkdownRenderer.svelte
CHANGED
|
@@ -62,6 +62,6 @@
|
|
| 62 |
{@html DOMPurify.sanitize(html)}
|
| 63 |
{/await}
|
| 64 |
{:else if token.type === "code"}
|
| 65 |
-
<CodeBlock
|
| 66 |
{/if}
|
| 67 |
{/each}
|
|
|
|
| 62 |
{@html DOMPurify.sanitize(html)}
|
| 63 |
{/await}
|
| 64 |
{:else if token.type === "code"}
|
| 65 |
+
<CodeBlock code={token.code} />
|
| 66 |
{/if}
|
| 67 |
{/each}
|
src/lib/utils/marked.ts
CHANGED
|
@@ -3,6 +3,7 @@ import "katex/dist/contrib/mhchem.mjs";
|
|
| 3 |
import { Marked } from "marked";
|
| 4 |
import type { Tokens, TokenizerExtension, RendererExtension } from "marked";
|
| 5 |
import type { WebSearchSource } from "$lib/types/WebSearch";
|
|
|
|
| 6 |
|
| 7 |
interface katexBlockToken extends Tokens.Generic {
|
| 8 |
type: "katexBlock";
|
|
@@ -188,7 +189,7 @@ export async function processTokens(content: string, sources: WebSearchSource[])
|
|
| 188 |
return {
|
| 189 |
type: "code" as const,
|
| 190 |
lang: token.lang,
|
| 191 |
-
code: token.text,
|
| 192 |
};
|
| 193 |
} else {
|
| 194 |
return {
|
|
@@ -207,7 +208,11 @@ export function processTokensSync(content: string, sources: WebSearchSource[]):
|
|
| 207 |
const tokens = marked.lexer(content);
|
| 208 |
return tokens.map((token) => {
|
| 209 |
if (token.type === "code") {
|
| 210 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
}
|
| 212 |
return { type: "text" as const, html: marked.parse(token.raw) };
|
| 213 |
});
|
|
|
|
| 3 |
import { Marked } from "marked";
|
| 4 |
import type { Tokens, TokenizerExtension, RendererExtension } from "marked";
|
| 5 |
import type { WebSearchSource } from "$lib/types/WebSearch";
|
| 6 |
+
import hljs from "highlight.js";
|
| 7 |
|
| 8 |
interface katexBlockToken extends Tokens.Generic {
|
| 9 |
type: "katexBlock";
|
|
|
|
| 189 |
return {
|
| 190 |
type: "code" as const,
|
| 191 |
lang: token.lang,
|
| 192 |
+
code: hljs.highlightAuto(token.text, hljs.getLanguage(token.lang)?.aliases).value,
|
| 193 |
};
|
| 194 |
} else {
|
| 195 |
return {
|
|
|
|
| 208 |
const tokens = marked.lexer(content);
|
| 209 |
return tokens.map((token) => {
|
| 210 |
if (token.type === "code") {
|
| 211 |
+
return {
|
| 212 |
+
type: "code" as const,
|
| 213 |
+
lang: token.lang,
|
| 214 |
+
code: hljs.highlightAuto(token.text, hljs.getLanguage(token.lang)?.aliases).value,
|
| 215 |
+
};
|
| 216 |
}
|
| 217 |
return { type: "text" as const, html: marked.parse(token.raw) };
|
| 218 |
});
|