Skip to content

Commit 94055c0

Browse files
committed
git commit -m "fix(StringProcessor): normalize whitespace in multi-line code labels
- Replace newlines and tabs with spaces before escaping - Collapse multiple consecutive spaces into single space - Fixes 'Unsupported markdown' error when visualizing Promise/async code - Resolves issue with multi-line nested structures in node labels
1 parent 1c9a519 commit 94055c0

File tree

1 file changed

+10
-21
lines changed

1 file changed

+10
-21
lines changed

src/core/utils/StringProcessor.ts

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
import { FlowchartEdge, FlowchartNode } from "../../ir/ir";
2-
31
export class StringProcessor {
42
private static escapeCache = new Map<string, string>();
53
private static readonly MAX_CACHE_SIZE = 1000;
64

7-
// Precompiled regex for better performance
85
private static readonly escapeRegex = /"|\\|\n|<|>|`/g;
96
private static readonly colonRegex = /:$/;
107
private static readonly escapeMap: Record<string, string> = {
118
'"': "#quot;",
129
"\\": "\\\\",
13-
"\n": " ",
10+
"\n": " ",
1411
"<": "#60;",
1512
">": "#62;",
1613
"`": "#96;",
@@ -22,24 +19,29 @@ export class StringProcessor {
2219
// Check cache first
2320
const cached = this.escapeCache.get(str);
2421
if (cached !== undefined) {
25-
// Move to end for LRU behavior
2622
this.escapeCache.delete(str);
2723
this.escapeCache.set(str, cached);
2824
return cached;
2925
}
3026

31-
// Use LRU eviction instead of clearing entire cache
27+
// LRU eviction
3228
if (this.escapeCache.size >= this.MAX_CACHE_SIZE) {
3329
const firstKey = this.escapeCache.keys().next().value;
3430
if (firstKey !== undefined) {
3531
this.escapeCache.delete(firstKey);
3632
}
3733
}
34+
let processed = str;
35+
36+
processed = processed.replace(/[\r\n\t]+/g, ' ');
37+
processed = processed.replace(/\s+/g, ' ');
38+
processed = processed.trim();
3839

39-
let escaped = str.replace(
40+
let escaped = processed.replace(
4041
this.escapeRegex,
4142
(match) => this.escapeMap[match]
4243
);
44+
4345
escaped = escaped.replace(this.colonRegex, "").trim();
4446

4547
// Length limiting for readability
@@ -55,17 +57,4 @@ export class StringProcessor {
5557
static clearCache(): void {
5658
this.escapeCache.clear();
5759
}
58-
}
59-
60-
export interface ProcessResult {
61-
nodes: FlowchartNode[];
62-
edges: FlowchartEdge[];
63-
entryNodeId?: string;
64-
exitPoints: { id: string; label?: string }[];
65-
nodesConnectedToExit: Set<string>;
66-
}
67-
68-
export interface LoopContext {
69-
breakTargetId: string;
70-
continueTargetId: string;
71-
}
60+
}

0 commit comments

Comments
 (0)