Skip to content

Commit a309336

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

2 files changed

Lines changed: 11 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: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { decode } from 'html-entities';
2+
13
// Helper function to find matching closing tag
24
function findMatchingClosingTag(src: string, openTag: string, closeTag: string): number {
35
let depth = 1;
@@ -26,7 +28,7 @@ function parseAttributes(tag: string): { [key: string]: string } {
2628
return attributes;
2729
}
2830

29-
function detailsTokenizer(src: string) {
31+
function detailsTokenizer(this: any, src: string) {
3032
// Updated regex to capture attributes inside <details>
3133
const detailsRegex = /^<details(\s+[^>]*)?>\n/;
3234
const summaryRegex = /^<summary>(.*?)<\/summary>\n/;
@@ -49,11 +51,15 @@ function detailsTokenizer(src: string) {
4951
content = content.slice(summaryMatch[0].length).trim();
5052
}
5153

54+
const tokens: any[] = [];
55+
this.lexer.blockTokens(decode(content), tokens);
56+
5257
return {
5358
type: 'details',
5459
raw: fullMatch,
5560
summary: summary,
5661
text: content,
62+
tokens,
5763
attributes: attributes // Include extracted attributes from <details>
5864
};
5965
}
@@ -89,6 +95,6 @@ function detailsExtension() {
8995

9096
export default function (options = {}) {
9197
return {
92-
extensions: [detailsExtension(options)]
98+
extensions: [detailsExtension()]
9399
};
94100
}

0 commit comments

Comments
 (0)