From 34c2f9e3463c06086978ca6c75197915f01abd08 Mon Sep 17 00:00:00 2001 From: hypercross Date: Thu, 26 Feb 2026 14:07:58 +0800 Subject: [PATCH] fix: app routing --- src/App.tsx | 26 +++++++++++--------------- src/data-loader/index.ts | 23 ++++++++++------------- src/main.tsx | 25 ++++++++++++++----------- 3 files changed, 35 insertions(+), 39 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 7dc4dab..cb53e1e 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,26 +1,22 @@ -import { Component, createSignal, onMount } from 'solid-js'; -import { useLocation } from '@solidjs/router'; +import { Component, createSignal, onMount } from "solid-js"; +import { useLocation } from "@solidjs/router"; // 导入组件以注册自定义元素 -import './components'; -import { Article } from './components'; +import "./components"; +import { Article } from "./components"; const App: Component = () => { const location = useLocation(); - const [currentPath, setCurrentPath] = createSignal(''); + const [currentPath, setCurrentPath] = createSignal(""); onMount(() => { // 根据路由加载对应的 markdown 文件 - 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`; - } + let path = decodeURIComponent(location.pathname); + + if (!path) path = "/content/"; + if (path.endsWith("/")) path += "index"; + if (!path.endsWith(".md")) path += ".md"; + setCurrentPath(path); }); diff --git a/src/data-loader/index.ts b/src/data-loader/index.ts index 5c33853..2c3e660 100644 --- a/src/data-loader/index.ts +++ b/src/data-loader/index.ts @@ -2,13 +2,6 @@ let dataIndex: Record | null = null; let isDevIndexLoaded = false; let isCliIndexLoaded = false; -/** - * 设置全局数据索引(CLI 环境使用) - */ -export function setDataIndex(index: Record) { - dataIndex = index; -} - /** * 从索引获取数据 */ @@ -27,13 +20,16 @@ async function loadDevIndex(): Promise { isDevIndexLoaded = true; // @ts-ignore - import.meta.glob 在构建时会被处理 - if (typeof import.meta !== 'undefined' && import.meta.glob) { + if (typeof import.meta !== "undefined" && import.meta.glob) { try { // @ts-ignore - 只加载 .csv 和 .md 文件 - const modules = import.meta.glob('../../content/**/*.{csv,md}', { as: 'raw', eager: true }); + const modules = import.meta.glob("../../content/**/*.{csv,md}", { + as: "raw", + eager: true, + }); const index: Record = {}; for (const [path, content] of Object.entries(modules)) { - const normalizedPath = path.replace('/src/', '/'); + const normalizedPath = path.replace("/src/", "/"); index[normalizedPath] = content as string; } dataIndex = { ...dataIndex, ...index }; @@ -51,9 +47,9 @@ async function loadCliIndex(): Promise { isCliIndexLoaded = true; try { - const response = await fetch('/__CONTENT_INDEX.json'); + const response = await fetch("/__CONTENT_INDEX.json"); if (!response.ok) { - throw new Error('Failed to fetch content index'); + throw new Error("Failed to fetch content index"); } const index = await response.json(); dataIndex = { ...dataIndex, ...index }; @@ -81,6 +77,7 @@ export async function fetchData(path: string): Promise { } // 索引不存在时,使用 fetch 加载 + if (dataIndex !== null) throw new Error(`no data in index: ${path}`); try { const response = await fetch(path); if (!response.ok) { @@ -88,7 +85,7 @@ export async function fetchData(path: string): Promise { } return await response.text(); } catch (error) { - console.error('fetchData error:', error); + console.error("fetchData error:", error); throw error; } } diff --git a/src/main.tsx b/src/main.tsx index 3fb7d77..b2e3d96 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -1,15 +1,18 @@ -import { render } from 'solid-js/web'; -import { Router, Route } from '@solidjs/router'; -import App from './App'; -import './styles.css'; +import { render } from "solid-js/web"; +import { Router, Route } from "@solidjs/router"; +import App from "./App"; +import "./styles.css"; -const root = document.getElementById('root'); +const root = document.getElementById("root"); if (root) { - render(() => ( - - - - - ), root); + render( + () => ( + + + + + ), + root, + ); }