fix: heading

This commit is contained in:
hypercross 2026-02-26 15:10:03 +08:00
parent c923d80d30
commit 53faa775be
1 changed files with 6 additions and 13 deletions

View File

@ -1,4 +1,4 @@
import { Component, createSignal, onMount, Show } from "solid-js";
import { Component, createMemo, createSignal, onMount, Show } from "solid-js";
import { generateToc, type FileNode, type TocNode } from "../data-loader";
import { useLocation } from "@solidjs/router";
import { FileTreeNode, HeadingNode } from "./FileTree";
@ -17,25 +17,18 @@ export const Sidebar: Component<SidebarProps> = (props) => {
const [pathHeadings, setPathHeadings] = createSignal<
Record<string, TocNode[]>
>({});
const [currentFileHeadings, setCurrentFileHeadings] = createSignal<TocNode[]>(
[],
);
// 加载目录数据
onMount(async () => {
const toc = await generateToc();
setFileTree(toc.fileTree);
setPathHeadings(toc.pathHeadings);
});
// 根据当前路径更新当前文件的标题列表
onMount(() => {
const updateHeadings = () => {
// 响应式获取当前文件的标题列表
const currentFileHeadings = createMemo(() => {
const pathname = location.pathname;
const headings =
pathHeadings()[pathname] || pathHeadings()[`${pathname}.md`];
setCurrentFileHeadings(headings || []);
};
updateHeadings();
return pathHeadings()[pathname] || pathHeadings()[`${pathname}.md`] || [];
});
return (