enzostvs HF Staff commited on
Commit
3a30111
·
1 Parent(s): fb9c1a7

try a new update things, to have a better accuracy

Browse files
Files changed (1) hide show
  1. app/api/ask/route.ts +114 -21
app/api/ask/route.ts CHANGED
@@ -285,11 +285,35 @@ export async function PUT(request: NextRequest) {
285
  return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
286
  };
287
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
288
  const createFlexibleHtmlRegex = (searchBlock: string) => {
289
- let searchRegex = escapeRegExp(searchBlock)
290
- .replace(/\s+/g, '\\s*')
 
 
 
 
291
  .replace(/>\s*</g, '>\\s*<')
292
- .replace(/\s*>/g, '\\s*>');
 
293
 
294
  return new RegExp(searchRegex, 'g');
295
  };
@@ -418,17 +442,49 @@ export async function PUT(request: NextRequest) {
418
  updatedLines.push([1, replaceBlock.split("\n").length]);
419
  } else {
420
  const regex = createFlexibleHtmlRegex(searchBlock);
421
- const match = regex.exec(pageHtml);
 
 
 
422
 
423
  if (match) {
424
- const matchedText = match[0];
425
- const beforeText = pageHtml.substring(0, match.index);
426
- const startLineNumber = beforeText.split("\n").length;
427
- const replaceLines = replaceBlock.split("\n").length;
428
- const endLineNumber = startLineNumber + replaceLines - 1;
429
-
430
- updatedLines.push([startLineNumber, endLineNumber]);
431
- pageHtml = pageHtml.replace(matchedText, replaceBlock);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
432
  }
433
  }
434
 
@@ -507,17 +563,54 @@ export async function PUT(request: NextRequest) {
507
  updatedLines.push([1, replaceBlock.split("\n").length]);
508
  } else {
509
  const regex = createFlexibleHtmlRegex(searchBlock);
510
- const match = regex.exec(newHtml);
 
 
 
 
 
 
 
 
 
511
 
512
  if (match) {
513
- const matchedText = match[0];
514
- const beforeText = newHtml.substring(0, match.index);
515
- const startLineNumber = beforeText.split("\n").length;
516
- const replaceLines = replaceBlock.split("\n").length;
517
- const endLineNumber = startLineNumber + replaceLines - 1;
518
-
519
- updatedLines.push([startLineNumber, endLineNumber]);
520
- newHtml = newHtml.replace(matchedText, replaceBlock);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
521
  }
522
  }
523
 
 
285
  return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
286
  };
287
 
288
+ const normalizeHtml = (html: string): string => {
289
+ return html
290
+ // Normalize whitespace within tags
291
+ .replace(/\s+/g, ' ')
292
+ // Remove spaces before closing >
293
+ .replace(/\s+>/g, '>')
294
+ // Remove spaces before />
295
+ .replace(/\s+\/>/g, '/>')
296
+ // Normalize spaces around = in attributes
297
+ .replace(/\s*=\s*/g, '=')
298
+ // Normalize quotes (convert single to double)
299
+ .replace(/='([^']*)'/g, '="$1"')
300
+ // Remove trailing spaces in opening/closing tags
301
+ .replace(/<([^>]*?)\s+>/g, '<$1>')
302
+ // Normalize self-closing tags
303
+ .replace(/\/\s*>/g, '/>')
304
+ .trim();
305
+ };
306
+
307
  const createFlexibleHtmlRegex = (searchBlock: string) => {
308
+ // Normalize both the search block for comparison
309
+ const normalizedSearch = normalizeHtml(searchBlock);
310
+
311
+ // Escape regex special characters
312
+ let searchRegex = escapeRegExp(normalizedSearch)
313
+ // Make whitespace flexible (but only between elements, not within tags)
314
  .replace(/>\s*</g, '>\\s*<')
315
+ // Make line breaks and spaces around content flexible
316
+ .replace(/>\s*([^<]+)\s*</g, '>\\s*$1\\s*<');
317
 
318
  return new RegExp(searchRegex, 'g');
319
  };
 
442
  updatedLines.push([1, replaceBlock.split("\n").length]);
443
  } else {
444
  const regex = createFlexibleHtmlRegex(searchBlock);
445
+
446
+ // Normalize the pageHtml for matching
447
+ const normalizedPageHtml = normalizeHtml(pageHtml);
448
+ const match = regex.exec(normalizedPageHtml);
449
 
450
  if (match) {
451
+ // Find the original match in the non-normalized HTML
452
+ const normalizedSearch = normalizeHtml(searchBlock);
453
+ const originalMatchIndex = pageHtml.indexOf(searchBlock);
454
+
455
+ if (originalMatchIndex !== -1) {
456
+ const beforeText = pageHtml.substring(0, originalMatchIndex);
457
+ const startLineNumber = beforeText.split("\n").length;
458
+ const replaceLines = replaceBlock.split("\n").length;
459
+ const endLineNumber = startLineNumber + replaceLines - 1;
460
+
461
+ updatedLines.push([startLineNumber, endLineNumber]);
462
+ pageHtml = pageHtml.replace(searchBlock, replaceBlock);
463
+ } else {
464
+ // Fallback: try to find similar pattern in the original HTML
465
+ const flexibleRegex = new RegExp(
466
+ escapeRegExp(searchBlock)
467
+ .replace(/\s+/g, '\\s+')
468
+ .replace(/\s*=\s*/g, '\\s*=\\s*')
469
+ .replace(/'\s*([^']*)\s*'/g, "'\\s*$1\\s*'")
470
+ .replace(/"\s*([^"]*)\s*"/g, '"\\s*$1\\s*"')
471
+ .replace(/\s*>/g, '\\s*>')
472
+ .replace(/\s*\/>/g, '\\s*/>'),
473
+ 'g'
474
+ );
475
+
476
+ const flexibleMatch = flexibleRegex.exec(pageHtml);
477
+ if (flexibleMatch) {
478
+ const matchedText = flexibleMatch[0];
479
+ const beforeText = pageHtml.substring(0, flexibleMatch.index);
480
+ const startLineNumber = beforeText.split("\n").length;
481
+ const replaceLines = replaceBlock.split("\n").length;
482
+ const endLineNumber = startLineNumber + replaceLines - 1;
483
+
484
+ updatedLines.push([startLineNumber, endLineNumber]);
485
+ pageHtml = pageHtml.replace(matchedText, replaceBlock);
486
+ }
487
+ }
488
  }
489
  }
490
 
 
563
  updatedLines.push([1, replaceBlock.split("\n").length]);
564
  } else {
565
  const regex = createFlexibleHtmlRegex(searchBlock);
566
+
567
+ // Get the main page HTML (first page or index page)
568
+ const mainPage = updatedPages.find(p => p.path === '/' || p.path === '/index' || p.path === 'index') || updatedPages[0];
569
+ if (!mainPage) continue;
570
+
571
+ newHtml = mainPage.html;
572
+
573
+ // Normalize the newHtml for matching
574
+ const normalizedNewHtml = normalizeHtml(newHtml);
575
+ const match = regex.exec(normalizedNewHtml);
576
 
577
  if (match) {
578
+ // Find the original match in the non-normalized HTML
579
+ const originalMatchIndex = newHtml.indexOf(searchBlock);
580
+
581
+ if (originalMatchIndex !== -1) {
582
+ const beforeText = newHtml.substring(0, originalMatchIndex);
583
+ const startLineNumber = beforeText.split("\n").length;
584
+ const replaceLines = replaceBlock.split("\n").length;
585
+ const endLineNumber = startLineNumber + replaceLines - 1;
586
+
587
+ updatedLines.push([startLineNumber, endLineNumber]);
588
+ newHtml = newHtml.replace(searchBlock, replaceBlock);
589
+ } else {
590
+ // Fallback: try to find similar pattern in the original HTML
591
+ const flexibleRegex = new RegExp(
592
+ escapeRegExp(searchBlock)
593
+ .replace(/\s+/g, '\\s+')
594
+ .replace(/\s*=\s*/g, '\\s*=\\s*')
595
+ .replace(/'\s*([^']*)\s*'/g, "'\\s*$1\\s*'")
596
+ .replace(/"\s*([^"]*)\s*"/g, '"\\s*$1\\s*"')
597
+ .replace(/\s*>/g, '\\s*>')
598
+ .replace(/\s*\/>/g, '\\s*/>'),
599
+ 'g'
600
+ );
601
+
602
+ const flexibleMatch = flexibleRegex.exec(newHtml);
603
+ if (flexibleMatch) {
604
+ const matchedText = flexibleMatch[0];
605
+ const beforeText = newHtml.substring(0, flexibleMatch.index);
606
+ const startLineNumber = beforeText.split("\n").length;
607
+ const replaceLines = replaceBlock.split("\n").length;
608
+ const endLineNumber = startLineNumber + replaceLines - 1;
609
+
610
+ updatedLines.push([startLineNumber, endLineNumber]);
611
+ newHtml = newHtml.replace(matchedText, replaceBlock);
612
+ }
613
+ }
614
  }
615
  }
616