Skip to content

Commit dab7277

Browse files
committed
perf: pre-lex details inner content at tokenize time
1 parent 66501c8 commit dab7277

2 files changed

Lines changed: 12 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';
@@ -398,7 +398,7 @@
398398
<div class="mb-1.5" slot="content">
399399
<svelte:self
400400
id={`${id}-${tokenIdx}-${detailIdx}-d`}
401-
tokens={marked.lexer(decode(detailToken.text))}
401+
tokens={detailToken.tokens ?? []}
402402
attributes={detailToken?.attributes}
403403
{done}
404404
{editCodeBlock}
@@ -445,7 +445,7 @@
445445
<div class=" mb-1.5" slot="content">
446446
<svelte:self
447447
id={`${id}-${tokenIdx}-d`}
448-
tokens={marked.lexer(decode(token.text))}
448+
tokens={token.tokens ?? []}
449449
attributes={token?.attributes}
450450
{done}
451451
{editCodeBlock}

src/lib/utils/marked/extension.ts

Lines changed: 9 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,15 @@ function detailsTokenizer(src: string) {
4952
content = content.slice(summaryMatch[0].length).trim();
5053
}
5154

55+
const tokens: any[] = [];
56+
this.lexer.blockTokens(decode(content), tokens);
57+
5258
return {
5359
type: 'details',
5460
raw: fullMatch,
5561
summary: summary,
5662
text: content,
63+
tokens,
5764
attributes: attributes // Include extracted attributes from <details>
5865
};
5966
}
@@ -89,6 +96,6 @@ function detailsExtension() {
8996

9097
export default function (options = {}) {
9198
return {
92-
extensions: [detailsExtension(options)]
99+
extensions: [detailsExtension()]
93100
};
94101
}

0 commit comments

Comments
 (0)