From 94055c0c4fd81ea4278952f429d3970029c502e5 Mon Sep 17 00:00:00 2001 From: ducphamngoc08 Date: Sat, 15 Nov 2025 11:09:57 +0700 Subject: [PATCH 1/2] 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 --- src/core/utils/StringProcessor.ts | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/src/core/utils/StringProcessor.ts b/src/core/utils/StringProcessor.ts index 7c5ce83..19c72aa 100644 --- a/src/core/utils/StringProcessor.ts +++ b/src/core/utils/StringProcessor.ts @@ -1,16 +1,13 @@ -import { FlowchartEdge, FlowchartNode } from "../../ir/ir"; - export class StringProcessor { private static escapeCache = new Map(); private static readonly MAX_CACHE_SIZE = 1000; - // Precompiled regex for better performance private static readonly escapeRegex = /"|\\|\n|<|>|`/g; private static readonly colonRegex = /:$/; private static readonly escapeMap: Record = { '"': "#quot;", "\\": "\\\\", - "\n": " ", + "\n": " ", "<": "#60;", ">": "#62;", "`": "#96;", @@ -22,24 +19,29 @@ export class StringProcessor { // Check cache first const cached = this.escapeCache.get(str); if (cached !== undefined) { - // Move to end for LRU behavior this.escapeCache.delete(str); this.escapeCache.set(str, cached); return cached; } - // Use LRU eviction instead of clearing entire cache + // LRU eviction if (this.escapeCache.size >= this.MAX_CACHE_SIZE) { const firstKey = this.escapeCache.keys().next().value; if (firstKey !== undefined) { this.escapeCache.delete(firstKey); } } + let processed = str; + + processed = processed.replace(/[\r\n\t]+/g, ' '); + processed = processed.replace(/\s+/g, ' '); + processed = processed.trim(); - let escaped = str.replace( + let escaped = processed.replace( this.escapeRegex, (match) => this.escapeMap[match] ); + escaped = escaped.replace(this.colonRegex, "").trim(); // Length limiting for readability @@ -55,17 +57,4 @@ export class StringProcessor { static clearCache(): void { this.escapeCache.clear(); } -} - -export interface ProcessResult { - nodes: FlowchartNode[]; - edges: FlowchartEdge[]; - entryNodeId?: string; - exitPoints: { id: string; label?: string }[]; - nodesConnectedToExit: Set; -} - -export interface LoopContext { - breakTargetId: string; - continueTargetId: string; -} +} \ No newline at end of file From 22c7e875358991af66023573fea32a77409c7654 Mon Sep 17 00:00:00 2001 From: ducphamngoc08 Date: Sat, 15 Nov 2025 11:19:10 +0700 Subject: [PATCH 2/2] update StringProcessor.ts --- src/core/utils/StringProcessor.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/core/utils/StringProcessor.ts b/src/core/utils/StringProcessor.ts index 19c72aa..8572bc6 100644 --- a/src/core/utils/StringProcessor.ts +++ b/src/core/utils/StringProcessor.ts @@ -1,7 +1,8 @@ +import { FlowchartEdge, FlowchartNode } from "../../ir/ir"; + export class StringProcessor { private static escapeCache = new Map(); private static readonly MAX_CACHE_SIZE = 1000; - private static readonly escapeRegex = /"|\\|\n|<|>|`/g; private static readonly colonRegex = /:$/; private static readonly escapeMap: Record = { @@ -57,4 +58,16 @@ export class StringProcessor { static clearCache(): void { this.escapeCache.clear(); } +} +export interface ProcessResult { + nodes: FlowchartNode[]; + edges: FlowchartEdge[]; + entryNodeId?: string; + exitPoints: { id: string; label?: string }[]; + nodesConnectedToExit: Set; +} + +export interface LoopContext { + breakTargetId: string; + continueTargetId: string; } \ No newline at end of file