2026-02-26 15:40:58 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 解析相对路径为绝对路径
|
|
|
|
|
|
* 支持 ./ 和 ../ 语法
|
|
|
|
|
|
* @param base - 基准路径(通常是当前 markdown 文件的路径)
|
|
|
|
|
|
* @param relative - 相对路径或绝对路径
|
|
|
|
|
|
* @returns 解析后的路径
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function resolvePath(base: string, relative: string): string {
|
2026-03-02 17:54:40 +08:00
|
|
|
|
if(!relative) return relative;
|
2026-02-26 15:40:58 +08:00
|
|
|
|
if (relative.startsWith('/')) {
|
|
|
|
|
|
return relative;
|
|
|
|
|
|
}
|
2026-02-27 21:22:13 +08:00
|
|
|
|
// if starts with http(s)://
|
|
|
|
|
|
if(relative.startsWith('http://') || relative.startsWith('https://')){
|
|
|
|
|
|
return relative;
|
|
|
|
|
|
}
|
2026-02-26 15:40:58 +08:00
|
|
|
|
|
|
|
|
|
|
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('/');
|
|
|
|
|
|
}
|
2026-03-01 11:52:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
2026-03-02 17:54:40 +08:00
|
|
|
|
// export function loadElementSrc(element?: { textContent: string, closest: (arg0: string) => HTMLElement }){
|
|
|
|
|
|
export function loadElementSrc(element?: any | HTMLElement){
|
2026-03-01 11:52:09 +08:00
|
|
|
|
const rawSrc = element?.textContent;
|
|
|
|
|
|
if(element) element.textContent = "";
|
|
|
|
|
|
|
|
|
|
|
|
const articleEl = element?.closest('article[data-src]');
|
|
|
|
|
|
const articlePath = articleEl?.getAttribute('data-src') || '';
|
|
|
|
|
|
|
|
|
|
|
|
return { articlePath, rawSrc };
|
|
|
|
|
|
}
|