Skip to content

Commit 4395754

Browse files
fix. path overlap issue in html viewer (#1318)
1 parent e4c712d commit 4395754

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

src/lib/run.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,48 @@ async function run(
290290
pathParts.shift();
291291
}
292292

293-
const fullPath = Url.join(rootFolder, pathParts.join("/"));
293+
function removePrefix(str, prefix) {
294+
if (str.startsWith(prefix)) {
295+
return str.slice(prefix.length);
296+
}
297+
return str;
298+
}
299+
300+
function findOverlap(a, b) {
301+
// Start with the smallest possible overlap (1 character) and increase
302+
let maxOverlap = "";
303+
304+
// Check all possible overlapping lengths
305+
for (let i = 1; i <= Math.min(a.length, b.length); i++) {
306+
// Get the ending substring of a with length i
307+
const endOfA = a.slice(-i);
308+
// Get the starting substring of b with length i
309+
const startOfB = b.slice(0, i);
310+
311+
// If they match, we have a potential overlap
312+
if (endOfA === startOfB) {
313+
maxOverlap = endOfA;
314+
}
315+
}
316+
317+
return maxOverlap;
318+
}
319+
320+
console.log(`RootFolder ${rootFolder}`);
321+
console.log(`PARTS ${pathParts.join("/")}`);
322+
const overlap = findOverlap(rootFolder, pathParts.join("/"));
323+
324+
let fullPath;
325+
if (overlap !== "") {
326+
fullPath = Url.join(
327+
rootFolder,
328+
removePrefix(pathParts.join("/"), overlap),
329+
);
330+
} else {
331+
fullPath = Url.join(rootFolder, pathParts.join("/"));
332+
}
333+
334+
console.log(`Full PATH ${fullPath}`);
294335

295336
// Add back the query if present
296337
url = query ? `${fullPath}?${query}` : fullPath;

0 commit comments

Comments
 (0)