| 470 | |
| 471 | export const IndentationFlexibleReplacer: Replacer = function* (content, find) { |
| 472 | const removeIndentation = (text: string) => { |
| 473 | const lines = text.split("\n") |
| 474 | const nonEmptyLines = lines.filter((line) => line.trim().length > 0) |
| 475 | if (nonEmptyLines.length === 0) return text |
| 476 | |
| 477 | const minIndent = Math.min( |
| 478 | ...nonEmptyLines.map((line) => { |
| 479 | const match = line.match(/^(\s*)/) |
| 480 | return match ? match[1].length : 0 |
| 481 | }), |
| 482 | ) |
| 483 | |
| 484 | return lines.map((line) => (line.trim().length === 0 ? line : line.slice(minIndent))).join("\n") |
| 485 | } |
| 486 | |
| 487 | const normalizedFind = removeIndentation(find) |
| 488 | const contentLines = content.split("\n") |