Fix note unfiling and relative image paths (#222)

This commit is contained in:
Yuri Karamian
2026-07-20 02:03:15 +02:00
parent 28ef37f354
commit fd55c00015
3 changed files with 63 additions and 11 deletions
+28
View File
@@ -0,0 +1,28 @@
function pathParts(path: string): string[] {
return path.replace(/\\/g, '/').split('/').filter(Boolean);
}
function pathRoot(path: string): string {
const normalized = path.replace(/\\/g, '/');
const drive = normalized.match(/^([A-Za-z]:)\//);
if (drive) return drive[1].toLowerCase();
return normalized.startsWith('/') ? '/' : '';
}
export function relativePath(fromDirectory: string, targetPath: string): string {
const from = pathParts(fromDirectory);
const target = pathParts(targetPath);
if (pathRoot(fromDirectory) !== pathRoot(targetPath)) return targetPath.replace(/\\/g, '/');
const windowsPath = /^[A-Za-z]:[\\/]/.test(fromDirectory) || /^[A-Za-z]:[\\/]/.test(targetPath);
const equal = windowsPath
? (left: string, right: string) => left.toLowerCase() === right.toLowerCase()
: (left: string, right: string) => left === right;
let common = 0;
while (common < from.length && common < target.length && equal(from[common], target[common])) {
common++;
}
const result = [...Array(from.length - common).fill('..'), ...target.slice(common)];
return result.join('/') || '.';
}