File size: 8,342 Bytes
55bd140
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import { unified } from 'unified';
import remarkParse from 'remark-parse';
import remarkGfm from 'remark-gfm';
import remarkMath from 'remark-math';

type ParseMetrics = {
  totalTime: number;
  parseTime: number;
  convertTime: number;
  tokenCount: number;
  nodeCount: number;
  contentStats: {
    codeBlocks: number;
    tables: number;
    lists: number;
    headings: number;
    links: number;
    images: number;
    inlineMath: number;
    mathBlocks: number;
  };
};

type ParseResult =
  | {
      success: true;
      tokens: any[];
      metrics: ParseMetrics;
    }
  | {
    success: false;
    error: any;
    metrics: ParseMetrics;
  };

/**
 * Unified-based parser (same parser family LibreChat uses via react-markdown)
 * Parses markdown to MDAST (remark) and converts to the token shape expected by our Svelte renderers.
 * Final version: no console logging. Includes math (inlineMath, math) mapping to inlineKatex/blockKatex.
 */
// Comprehensive heuristic to detect a wide range of markdown features.
const MARKDOWN_QUICK_SCAN_REGEX =
	/^(?:#|>|\*|-|\d+\.|\s*\|)\s|```|---|\[.+\]\(.+\)|!\[.*\]\(.*\)|\[.+\]:.*|\$.+\$|<(?:div|table|details|pre|code)|(?:\*\*|__|_|~~|`|\[\^.*\])/m;

// Regex for hard-block segmentation
const FENCED_CODE_REGEX = /^(\s*)(`{3,}|~{3,})([^\n]*)\n([\s\S]+?)\n\s*\2\s*$/gm;
const BLOCK_MATH_REGEX = /^\$\$\n([\s\S]+?)\n\$\$\s*$/gm;
const BLOCK_HTML_REGEX = /^<(details|div|table|pre|code)[\s>][\s\S]*?<\/\1>\s*$/gm;
// Extract horizontal rules to prevent them from creating Setext headings
const HORIZONTAL_RULE_REGEX = /^(\s*)([-*_])\2{2,}\s*$/gm;

export function parseMarkdownToTokens(
	content: string,
	messageId: string,
	useHybrid = false
): ParseResult {
	const perfStart = performance.now();

	const metrics: ParseMetrics = {
		totalTime: 0,
		parseTime: 0,
		convertTime: 0,
		tokenCount: 0,
		nodeCount: 0,
		contentStats: {
			codeBlocks: 0,
			tables: 0,
			lists: 0,
			headings: 0,
			links: 0,
			images: 0,
			inlineMath: 0,
			mathBlocks: 0
		}
	};

	try {
		const parseStart = performance.now();
		const tree: any = unified().use(remarkParse).use(remarkGfm).use(remarkMath).parse(content);
		const parseEnd = performance.now();
		metrics.parseTime = parseEnd - parseStart;
		metrics.nodeCount = countNodes(tree);

		metrics.contentStats = analyzeContent(tree);

		const convertStart = performance.now();
		const tokens = convertMdastToMarkedTokens(tree);
		const convertEnd = performance.now();
		metrics.convertTime = convertEnd - convertStart;
		metrics.tokenCount = tokens.length;

		metrics.totalTime = performance.now() - perfStart;

		return { success: true, tokens, metrics };
	} catch (error: any) {
		metrics.totalTime = performance.now() - perfStart;
		return { success: false, error, metrics };
	}
}

/**
 * Convert remark MDAST tree into a minimal "marked-like" token array compatible with existing render pipeline.
 * Includes mapping for math:
 *  - inlineMath -> { type: 'inlineKatex', text, displayMode: false }
 *  - math       -> { type: 'blockKatex',  text, displayMode: true  }
 */
function convertMdastToMarkedTokens(tree: any): any[] {
  const tokens: any[] = [];
  const children: any[] = Array.isArray(tree?.children) ? tree.children : [];

  for (const node of children) {
    const token = convertNode(node);
    if (token) tokens.push(token);
  }
  return tokens;
}

function convertNode(node: any): any {
  switch (node?.type) {
    case 'heading':
      return {
        type: 'heading',
        depth: node.depth,
        text: extractText(node),
        tokens: convertChildren(node)
      };

    case 'paragraph':
      return {
        type: 'paragraph',
        text: extractText(node),
        tokens: convertChildren(node)
      };

    case 'code':
      return {
        type: 'code',
        lang: node.lang || '',
        text: node.value || '',
        raw: `\`\`\`${node.lang || ''}\n${node.value || ''}\n\`\`\``
      };

    case 'inlineCode':
      return {
        type: 'codespan',
        text: node.value || '',
        raw: '`' + (node.value || '') + '`'
      };

    case 'list':
      return {
        type: 'list',
        ordered: !!node.ordered,
        start: node.start || 1,
        loose: false,
        items: (node.children || []).map((item: any) => ({
          type: 'list_item',
          task: item.checked != null,
          checked: !!item.checked,
          loose: false,
          tokens: convertChildren(item)
        }))
      };

    case 'blockquote':
      return {
        type: 'blockquote',
        text: extractText(node),
        tokens: convertChildren(node)
      };

    case 'table': {
      const rows = node.children || [];
      const headerRow = rows[0] || { children: [] };
      const bodyRows = rows.slice(1);

      return {
        type: 'table',
        header: (headerRow.children || []).map((cell: any) => ({
          text: extractText(cell),
          tokens: convertChildren(cell)
        })),
        rows: bodyRows.map((row: any) =>
          (row.children || []).map((cell: any) => ({
            text: extractText(cell),
            tokens: convertChildren(cell)
          }))
        ),
        align: node.align || []
      };
    }

    case 'thematicBreak':
      return { type: 'hr' };

    case 'html':
      // Keep as raw HTML; renderer handles HtmlToken fallback
      return {
        type: 'html',
        text: node.value || '',
        raw: node.value || ''
      };

    case 'text':
      return {
        type: 'text',
        text: node.value || '',
        raw: node.value || ''
      };

    case 'emphasis':
      return {
        type: 'em',
        text: extractText(node),
        tokens: convertChildren(node)
      };

    case 'strong':
      return {
        type: 'strong',
        text: extractText(node),
        tokens: convertChildren(node)
      };

    case 'link':
      return {
        type: 'link',
        href: node.url || '',
        title: node.title || '',
        text: extractText(node),
        tokens: convertChildren(node)
      };

    case 'image':
      return {
        type: 'image',
        href: node.url || '',
        title: node.title || '',
        text: node.alt || ''
      };

    case 'break':
      return { type: 'br' };

    case 'delete':
      return {
        type: 'del',
        text: extractText(node),
        tokens: convertChildren(node)
      };

    // MATH SUPPORT (remark-math)
    case 'inlineMath':
      return {
        type: 'inlineKatex',
        text: node.value || '',
        displayMode: false
      };

    case 'math':
      return {
        type: 'blockKatex',
        text: node.value || '',
        displayMode: true
      };

    default:
      // Unhandled nodes are ignored to avoid breaking renderers
      return null;
  }
}

function convertChildren(node: any): any[] {
  const out: any[] = [];
  const kids: any[] = Array.isArray(node?.children) ? node.children : [];
  for (const child of kids) {
    const t = convertNode(child);
    if (t) out.push(t);
  }
  return out;
}

function extractText(node: any): string {
  if (!node) return '';
  if (typeof node === 'string') return node;
  if (node.value) return String(node.value);
  if (Array.isArray(node.children)) {
    return node.children.map(extractText).join('');
  }
  return '';
}

function countNodes(tree: any): number {
  let count = 0;
  function walk(n: any) {
    if (!n) return;
    count += 1;
    const kids: any[] = Array.isArray(n.children) ? n.children : [];
    for (const c of kids) walk(c);
  }
  walk(tree);
  return count;
}

function analyzeContent(tree: any) {
  const stats = {
    codeBlocks: 0,
    tables: 0,
    lists: 0,
    headings: 0,
    links: 0,
    images: 0,
    inlineMath: 0,
    mathBlocks: 0
  };

  function walk(n: any) {
    if (!n) return;
    switch (n.type) {
      case 'code': stats.codeBlocks++; break;
      case 'table': stats.tables++; break;
      case 'list': stats.lists++; break;
      case 'heading': stats.headings++; break;
      case 'link': stats.links++; break;
      case 'image': stats.images++; break;
      case 'inlineMath': stats.inlineMath++; break;
      case 'math': stats.mathBlocks++; break;
    }
    const kids: any[] = Array.isArray(n.children) ? n.children : [];
    for (const c of kids) walk(c);
  }

  walk(tree);
  return stats;
}