-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathtest_markdown_streaming.js
More file actions
80 lines (47 loc) · 1.65 KB
/
test_markdown_streaming.js
File metadata and controls
80 lines (47 loc) · 1.65 KB
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
const testMarkdownWithExcessiveNewlines = `# Test Response
This is a test response with excessive newlines.
Here's some content after multiple empty lines.
## Section Header
More content here.
### Subsection
Final content with lots of spacing.
The end.`;
const testStreamingTokens = [
"# Test Response\n\n",
"This is a test response",
" with excessive newlines.\n\n\n\n",
"Here's some content after",
" multiple empty lines.\n\n\n\n\n",
"## Section Header\n\n",
"More content here.\n\n\n\n\n\n\n",
"### Subsection\n\n",
"Final content with lots",
" of spacing.\n\n\n\n\n",
"The end."
];
function currentCleanup(text) {
return text.replace(/\n{3,}/g, '\n\n');
}
function improvedCleanup(text) {
text = text.replace(/\n{3,}/g, '\n\n');
text = text.replace(/[ \t]+$/gm, '');
text = text.replace(/[ \t]{3,}/g, ' ');
text = text.replace(/[ \t]*\n[ \t]*\n[ \t]*\n/g, '\n\n');
text = text.trim();
return text;
}
console.log("=== ORIGINAL TEXT ===");
console.log(JSON.stringify(testMarkdownWithExcessiveNewlines));
console.log("\n=== CURRENT CLEANUP ===");
console.log(JSON.stringify(currentCleanup(testMarkdownWithExcessiveNewlines)));
console.log("\n=== IMPROVED CLEANUP ===");
console.log(JSON.stringify(improvedCleanup(testMarkdownWithExcessiveNewlines)));
console.log("\n=== STREAMING SIMULATION ===");
let streamedText = "";
testStreamingTokens.forEach((token, i) => {
streamedText += token;
console.log(`Token ${i + 1}: "${token}"`);
console.log(`Accumulated (current): "${currentCleanup(streamedText)}"`);
console.log(`Accumulated (improved): "${improvedCleanup(streamedText)}"`);
console.log("---");
});