|
1 | 1 | <script> |
2 | | - function getFileName(line) { |
3 | | - // Skips "|", "└", "├" found in file tree |
4 | | - const index = line.search(/[a-zA-Z0-9]/); |
5 | | - return line.substring(index).trim(); |
| 2 | + function getFileName(element) { |
| 3 | + const indentSize = 4; |
| 4 | + let path = ""; |
| 5 | + let prevIndentLevel = null; |
| 6 | +
|
| 7 | + while (element) { |
| 8 | + const line = element.textContent; |
| 9 | + const index = line.search(/[a-zA-Z0-9_.-]/); |
| 10 | + const indentLevel = index / indentSize; |
| 11 | +
|
| 12 | + // Stop when we reach or go above the top-level directory |
| 13 | + if (indentLevel <= 1) { |
| 14 | + break; |
| 15 | + } |
| 16 | +
|
| 17 | + // Only include directories that are one level above the previous |
| 18 | + if (prevIndentLevel === null || indentLevel === prevIndentLevel - 1) { |
| 19 | + const fileName = line.substring(index).trim(); |
| 20 | + path = fileName + path; |
| 21 | + prevIndentLevel = indentLevel; |
| 22 | + } |
| 23 | +
|
| 24 | + element = element.previousElementSibling; |
| 25 | + } |
| 26 | +
|
| 27 | + return path; |
6 | 28 | } |
7 | 29 |
|
8 | 30 | function toggleFile(element) { |
9 | 31 | const patternInput = document.getElementById("pattern"); |
10 | 32 | const patternFiles = patternInput.value ? patternInput.value.split(",").map(item => item.trim()) : []; |
11 | 33 |
|
12 | | - if (element.textContent.includes("Directory structure:")) { |
| 34 | + const directoryContainer = document.getElementById("directory-structure-container"); |
| 35 | + const treeLineElements = Array.from(directoryContainer.children).filter(child => child.tagName === "PRE"); |
| 36 | +
|
| 37 | + // Skip the first two tree lines (header and repository name) |
| 38 | + if (treeLineElements[0] === element || treeLineElements[1] === element) { |
13 | 39 | return; |
14 | 40 | } |
15 | 41 |
|
16 | 42 | element.classList.toggle('line-through'); |
17 | 43 | element.classList.toggle('text-gray-500'); |
18 | 44 |
|
19 | | - const fileName = getFileName(element.textContent); |
| 45 | + const fileName = getFileName(element); |
20 | 46 | const fileIndex = patternFiles.indexOf(fileName); |
21 | 47 |
|
22 | 48 | if (fileIndex !== -1) { |
|
0 commit comments