From 3098328a27f5a24324438a038a96e3e54a33e9bb Mon Sep 17 00:00:00 2001 From: hypercross Date: Thu, 26 Feb 2026 01:15:40 +0800 Subject: [PATCH] fix: md path --- src/App.tsx | 13 +++++++++++-- src/components/table.tsx | 6 +++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 89cf106..9a33738 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -12,10 +12,19 @@ const App: Component = () => { onMount(async () => { // 根据路由加载对应的 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); try { - const response = await fetch(`/content/${path}.md`); + const response = await fetch(path); if (response.ok) { const text = await response.text(); setContent(text); diff --git a/src/components/table.tsx b/src/components/table.tsx index 0e6ecb7..4bcdd94 100644 --- a/src/components/table.tsx +++ b/src/components/table.tsx @@ -17,11 +17,11 @@ customElement('md-table', { roll: false, remix: false }, (props, { element }) => // 从 element 的 textContent 获取 CSV 路径 const src = element?.textContent?.trim() || ''; - // 从父节点 article 的 data-src 获取当前 markdown 文件路径 + // 从父节点 article 的 data-src 获取当前 markdown 文件完整路径 const articleEl = element?.closest('article[data-src]'); const articlePath = articleEl?.getAttribute('data-src') || ''; - // 解析相对路径 + // 解析相对路径(相对于 markdown 文件所在目录) const resolvePath = (base: string, relative: string): string => { if (relative.startsWith('/')) { return relative; @@ -30,7 +30,7 @@ customElement('md-table', { roll: false, remix: false }, (props, { element }) => return baseDir + relative; }; - const resolvedSrc = resolvePath(`/content/${articlePath}`, src); + const resolvedSrc = resolvePath(articlePath, src); // 解析 CSV 内容 const parseCSV = (content: string) => {