refactor: path handling

This commit is contained in:
hypercross 2026-02-26 15:40:58 +08:00
parent 2ab6ed9687
commit e4bdd06182
4 changed files with 41 additions and 19 deletions

View File

@ -2,6 +2,7 @@ import { customElement, noShadowDOM } from "solid-element";
import { createSignal, onCleanup } from "solid-js"; import { createSignal, onCleanup } from "solid-js";
import { render } from "solid-js/web"; import { render } from "solid-js/web";
import { Article } from "./Article"; import { Article } from "./Article";
import { resolvePath } from "../utils/path";
customElement("md-link", {}, (props, { element }) => { customElement("md-link", {}, (props, { element }) => {
noShadowDOM(); noShadowDOM();
@ -27,15 +28,7 @@ customElement("md-link", {}, (props, { element }) => {
const articleEl = element?.closest('article[data-src]'); const articleEl = element?.closest('article[data-src]');
const articlePath = articleEl?.getAttribute('data-src') || ''; const articlePath = articleEl?.getAttribute('data-src') || '';
// 解析相对路径(相对于 markdown 文件所在目录) // 解析相对路径
const resolvePath = (base: string, relative: string): string => {
if (relative.startsWith('/')) {
return relative;
}
const baseDir = base.substring(0, base.lastIndexOf('/') + 1);
return baseDir + relative;
};
const linkSrc = resolvePath(articlePath, path); const linkSrc = resolvePath(articlePath, path);
// 查找包含此元素的 p 标签 // 查找包含此元素的 p 标签

View File

@ -2,6 +2,7 @@ import { customElement, noShadowDOM } from 'solid-element';
import { createSignal, For, Show, createEffect, createMemo, createResource } from 'solid-js'; import { createSignal, For, Show, createEffect, createMemo, createResource } from 'solid-js';
import { parse } from 'csv-parse/browser/esm/sync'; import { parse } from 'csv-parse/browser/esm/sync';
import { marked } from '../markdown'; import { marked } from '../markdown';
import { resolvePath } from '../utils/path';
interface TableRow { interface TableRow {
label: string; label: string;
@ -32,15 +33,7 @@ customElement('md-table', { roll: false, remix: false }, (props, { element }) =>
const articleEl = element?.closest('article[data-src]'); const articleEl = element?.closest('article[data-src]');
const articlePath = articleEl?.getAttribute('data-src') || ''; const articlePath = articleEl?.getAttribute('data-src') || '';
// 解析相对路径(相对于 markdown 文件所在目录) // 解析相对路径
const resolvePath = (base: string, relative: string): string => {
if (relative.startsWith('/')) {
return relative;
}
const baseDir = base.substring(0, base.lastIndexOf('/') + 1);
return baseDir + relative;
};
const resolvedSrc = resolvePath(articlePath, src); const resolvedSrc = resolvePath(articlePath, src);
// 加载 CSV 文件的函数 // 加载 CSV 文件的函数

1
src/utils/index.ts Normal file
View File

@ -0,0 +1 @@
export { resolvePath } from './path';

35
src/utils/path.ts Normal file
View File

@ -0,0 +1,35 @@
/**
*
* ./ ../
* @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('/');
}