Skip to content

Commit 685adee

Browse files
committed
perf: pre-lex details inner content at tokenize time
1 parent 86efc62 commit 685adee

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

src/lib/components/chat/Messages/Markdown/MarkdownTokens.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import fileSaver from 'file-saver';
77
const { saveAs } = fileSaver;
88
9-
import { marked, type Token } from 'marked';
9+
import { type Token } from 'marked';
1010
import { copyToClipboard, unescapeHtml } from '$lib/utils';
1111
1212
import { WEBUI_BASE_URL } from '$lib/constants';
@@ -399,7 +399,7 @@
399399
<div class="mb-1.5" slot="content">
400400
<svelte:self
401401
id={`${id}-${tokenIdx}-${detailIdx}-d`}
402-
tokens={marked.lexer(decode(detailToken.text))}
402+
tokens={detailToken.tokens ?? []}
403403
attributes={detailToken?.attributes}
404404
{done}
405405
{editCodeBlock}
@@ -447,7 +447,7 @@
447447
<div class=" mb-1.5" slot="content">
448448
<svelte:self
449449
id={`${id}-${tokenIdx}-d`}
450-
tokens={marked.lexer(decode(token.text))}
450+
tokens={token.tokens ?? []}
451451
attributes={token?.attributes}
452452
{done}
453453
{editCodeBlock}

src/lib/utils/marked/extension.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { decode } from 'html-entities';
2+
import { type TokenizerThis } from 'marked';
3+
14
// Helper function to find matching closing tag
25
function findMatchingClosingTag(src: string, openTag: string, closeTag: string): number {
36
let depth = 1;
@@ -26,7 +29,7 @@ function parseAttributes(tag: string): { [key: string]: string } {
2629
return attributes;
2730
}
2831

29-
function detailsTokenizer(src: string) {
32+
function detailsTokenizer(this: TokenizerThis, src: string) {
3033
// Updated regex to capture attributes inside <details>
3134
const detailsRegex = /^<details(\s+[^>]*)?>\n/;
3235
const summaryRegex = /^<summary>(.*?)<\/summary>\n/;
@@ -49,11 +52,17 @@ function detailsTokenizer(src: string) {
4952
content = content.slice(summaryMatch[0].length).trim();
5053
}
5154

55+
const tokens: any[] = [];
56+
if (attributes?.type !== 'tool_calls') {
57+
this.lexer.blockTokens(decode(content), tokens);
58+
}
59+
5260
return {
5361
type: 'details',
5462
raw: fullMatch,
5563
summary: summary,
5664
text: content,
65+
tokens,
5766
attributes: attributes // Include extracted attributes from <details>
5867
};
5968
}
@@ -89,6 +98,6 @@ function detailsExtension() {
8998

9099
export default function (options = {}) {
91100
return {
92-
extensions: [detailsExtension(options)]
101+
extensions: [detailsExtension()]
93102
};
94103
}

0 commit comments

Comments
 (0)