fix: md path

This commit is contained in:
hypercross 2026-02-26 01:15:40 +08:00
parent 3f61e63709
commit 3098328a27
2 changed files with 14 additions and 5 deletions

View File

@ -12,10 +12,19 @@ const App: Component = () => {
onMount(async () => { onMount(async () => {
// 根据路由加载对应的 markdown 文件 // 根据路由加载对应的 markdown 文件
const path = location.pathname.slice(1) || 'index'; let path = decodeURIComponent(location.pathname.slice(1));
if (!path) {
path = '/content/index.md';
} else if (!path.startsWith('/content/')) {
path = `/content/${path}`;
}
// 确保有 .md 扩展名
if (!path.endsWith('.md')) {
path = `${path}.md`;
}
setCurrentPath(path); setCurrentPath(path);
try { try {
const response = await fetch(`/content/${path}.md`); const response = await fetch(path);
if (response.ok) { if (response.ok) {
const text = await response.text(); const text = await response.text();
setContent(text); setContent(text);

View File

@ -17,11 +17,11 @@ customElement('md-table', { roll: false, remix: false }, (props, { element }) =>
// 从 element 的 textContent 获取 CSV 路径 // 从 element 的 textContent 获取 CSV 路径
const src = element?.textContent?.trim() || ''; const src = element?.textContent?.trim() || '';
// 从父节点 article 的 data-src 获取当前 markdown 文件路径 // 从父节点 article 的 data-src 获取当前 markdown 文件完整路径
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 => { const resolvePath = (base: string, relative: string): string => {
if (relative.startsWith('/')) { if (relative.startsWith('/')) {
return relative; return relative;
@ -30,7 +30,7 @@ customElement('md-table', { roll: false, remix: false }, (props, { element }) =>
return baseDir + relative; return baseDir + relative;
}; };
const resolvedSrc = resolvePath(`/content/${articlePath}`, src); const resolvedSrc = resolvePath(articlePath, src);
// 解析 CSV 内容 // 解析 CSV 内容
const parseCSV = (content: string) => { const parseCSV = (content: string) => {