|
255 | 255 | '</div>'; |
256 | 256 | }; |
257 | 257 |
|
258 | | - // DETECTION: Check for square brackets [Path 1] [Path 2] first, then fall back to pipes |
| 258 | + // SUPER ROBUST DETECTION & NORMALIZATION |
| 259 | + // 1. Convert all variations to standard chars |
| 260 | + var cleanRec = rec.replace(/[||]/g, '|') |
| 261 | + .replace(/[[[]/g, '[') |
| 262 | + .replace(/[]]]/g, ']') |
| 263 | + .replace(/[/g, '[') |
| 264 | + .replace(/]/g, ']') |
| 265 | + .replace(/[/g, '[') |
| 266 | + .replace(/]/g, ']'); |
| 267 | + |
259 | 268 | var rawPaths = []; |
260 | | - var bracketMatches = rec.match(/\[([^\]]+)\]/g); |
| 269 | + // 2. Bracket Split: look for the gap between brackets: ] [ or ][ |
| 270 | + if (cleanRec.indexOf('[') !== -1 && cleanRec.indexOf(']') !== -1) { |
| 271 | + rawPaths = cleanRec.split(/\]\s*\[/).map(function(p) { |
| 272 | + return p.replace(/[\[\]]/g, '').trim(); |
| 273 | + }).filter(Boolean); |
| 274 | + } |
261 | 275 |
|
262 | | - if (bracketMatches && bracketMatches.length > 1) { |
263 | | - rawPaths = bracketMatches.map(function(m) { |
264 | | - return m.replace(/[\[\]]/g, '').trim(); |
| 276 | + // 3. Fallback to Pipe Split: |
| 277 | + if (rawPaths.length <= 1) { |
| 278 | + rawPaths = cleanRec.split(/[||]/).map(function (s) { |
| 279 | + return s.replace(/[\[\]]/g, '').trim(); |
265 | 280 | }).filter(Boolean); |
266 | | - } else { |
267 | | - rawPaths = rec.split(/[||]/).map(function (s) { return s.trim(); }).filter(Boolean); |
268 | 281 | } |
269 | 282 |
|
270 | 283 | var pathHTML = '<div class="enhancement-tree">'; |
|
273 | 286 | // BRANCHING LOGIC |
274 | 287 | // 1. Parse all paths into arrays of steps |
275 | 288 | var processedPaths = rawPaths.map(function(p) { |
276 | | - return p.split(/[>;;]/).map(function(s) { return s.trim(); }).filter(Boolean); |
| 289 | + return p.split(/[>;;]/).map(function(s) { |
| 290 | + return s.replace(/[\[\]]/g, '').trim(); |
| 291 | + }).filter(Boolean); |
277 | 292 | }); |
278 | 293 |
|
279 | 294 | // 2. Identify common root (assume first item of first path as root candidate) |
280 | | - var rootName = processedPaths[0][0]; |
281 | | - var allShareRoot = processedPaths.every(function(p) { |
| 295 | + var rootName = (processedPaths[0] && processedPaths[0][0]) || ''; |
| 296 | + var allShareRoot = rootName && processedPaths.every(function(p) { |
282 | 297 | return p.length > 0 && p[0].toLowerCase() === rootName.toLowerCase(); |
283 | 298 | }); |
284 | 299 |
|
285 | 300 | if (allShareRoot) { |
286 | | - // Render Shared Root Node |
287 | 301 | pathHTML += renderNode(rootName, 'base', name); |
288 | 302 | pathHTML += '<div class="enhancement-arrow">↓</div>'; |
289 | | - |
290 | | - // Remove root from each path for branching display |
291 | 303 | processedPaths.forEach(function(p) { p.shift(); }); |
292 | 304 | } |
293 | 305 |
|
|
298 | 310 | pathHTML += '<div class="enhancement-branch">'; |
299 | 311 | steps.forEach(function(sName, idx) { |
300 | 312 | var rank = (idx === steps.length - 1) ? 'max' : 'up'; |
301 | | - // If we didn't have a shared root, the first item of each branch might be 'base' |
302 | 313 | if (!allShareRoot && idx === 0) rank = 'base'; |
303 | | - |
304 | 314 | pathHTML += renderNode(sName, rank, name); |
305 | 315 | if (idx < steps.length - 1) pathHTML += '<div class="enhancement-arrow">↓</div>'; |
306 | 316 | }); |
|
310 | 320 | pathHTML += '</div>'; |
311 | 321 | } else { |
312 | 322 | // LINEAR LOGIC |
313 | | - var steps = rec.split(/[>;||]/).map(function (s) { return s.trim(); }).filter(Boolean); |
| 323 | + var steps = cleanRec.split(/[>;||]/).map(function (s) { |
| 324 | + return s.replace(/[\[\]]/g, '').trim(); |
| 325 | + }).filter(Boolean); |
314 | 326 | steps.forEach(function (stepName, idx) { |
315 | 327 | var rank = "up"; |
316 | 328 | if (idx === 0) rank = "base"; |
317 | 329 | else if (idx === steps.length - 1 && steps.length > 1) rank = "max"; |
318 | | - |
319 | 330 | pathHTML += renderNode(stepName, rank, name); |
320 | 331 | if (idx < steps.length - 1) pathHTML += '<div class="enhancement-arrow">↓</div>'; |
321 | 332 | }); |
|
0 commit comments