ttrpg-tools/src/components/utils/path.ts

36 lines
898 B
TypeScript
Raw Normal View History

2026-02-26 15:40:58 +08:00
/**
*
* ./ ../
* @param base - markdown
* @param relative -
* @returns
*/
export function resolvePath(base: string, relative: string): string {
if (relative.startsWith('/')) {
return relative;
}
const baseParts = base.split('/').filter(Boolean);
// 移除文件名,保留目录路径
if (baseParts.length > 0 && !base.endsWith('/')) {
baseParts.pop();
}
const relativeParts = relative.split('/');
for (const part of relativeParts) {
if (part === '.' || part === '') {
// 跳过 . 和空字符串
continue;
} else if (part === '..') {
// 向上一级
baseParts.pop();
} else {
// 普通路径段
baseParts.push(part);
}
}
return '/' + baseParts.join('/');
}