refactor: 改为从 docs 目录动态读取 md 文件,支持目录层级

This commit is contained in:
2026-03-18 13:43:45 +08:00
parent 351557a19b
commit c5649b3eba
20 changed files with 390 additions and 500 deletions

View File

@@ -3,9 +3,16 @@ import { DocTree } from './DocTree'
import { DocContent } from './DocContent'
import { parseMarkdown, buildFileTree } from '@/lib/parser'
import type { DocFile, ParsedDoc } from '@/lib/types'
import { docs } from '@/data/docs'
import { config } from '@/config'
const DOCS_FILES: Record<string, string> = docs
const modules = import.meta.glob('../docs/**/*.md', { as: 'raw', eager: true })
const DOCS_FILES: Record<string, string> = Object.fromEntries(
Object.entries(modules).map(([path, content]) => {
const relativePath = path.replace('../docs/', '')
return [relativePath, content as string]
})
)
export const ApiDocViewer = () => {
const [fileTree, setFileTree] = useState<DocFile[]>([])
@@ -14,8 +21,7 @@ export const ApiDocViewer = () => {
useEffect(() => {
const files = Object.keys(DOCS_FILES)
const basePath = '/docs'
const tree = buildFileTree(files, basePath)
const tree = buildFileTree(files, '/')
setFileTree(tree)
if (files.length > 0) {
@@ -41,7 +47,7 @@ export const ApiDocViewer = () => {
const handleReferenceClick = useCallback((ref: string) => {
const normalizedRef = ref.replace('.md', '')
const allFiles = Object.keys(DOCS_FILES).map(k => k.replace('/docs/', ''))
const allFiles = Object.keys(DOCS_FILES)
const match = allFiles.find(f => f.replace('.md', '') === normalizedRef)
if (match) {
setSelectedPath(match)
@@ -52,7 +58,7 @@ export const ApiDocViewer = () => {
<div className="flex h-screen bg-[#1e1e1e]">
<aside className="w-64 bg-[#252526] border-r border-[#3c3c3c] flex flex-col">
<div className="p-3 border-b border-[#3c3c3c]">
<h2 className="text-sm font-semibold text-gray-200">API </h2>
<h2 className="text-sm font-semibold text-gray-200">{config.sidebarTitle}</h2>
</div>
<div className="flex-1 overflow-hidden">
<DocTree