sidebar都正确了 · codeagles/CoderWithAI@fbba9d1 · GitHub
Skip to content

Commit fbba9d1

Browse files
committed
sidebar都正确了
1 parent b28d978 commit fbba9d1

1,187 files changed

Lines changed: 108 additions & 44 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎app/[language]/page.tsx‎

Lines changed: 35 additions & 38 deletions

‎components/TutorialSidebar.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ function TutorialItem({ item, language, depth = 0 }: { item: Tutorial, language:
3636
<Link
3737
href={`/${encodeURIComponent(language)}/${encodeURIComponent(item.path)}`}
3838
onClick={toggleOpen}
39-
className={`hover:text-blue-600 transition-colors duration-200 ${depth === 0 ? 'font-semibold' : ''} ${isActive ? 'text-blue-600' : ''}`}
39+
className={`hover:text-blue-600 transition-colors duration-200 ${depth === 0 ? 'font-semibold' : ''} ${isActive ? 'text-blue-600' : ''} ${item.isOutline ? 'font-bold' : ''}`}
4040
>
4141
{item.title}
4242
</Link>
4343
</div>
4444
{hasItems && isOpen && (
4545
<ul>
46-
{item.items.map((subItem: any) => (
46+
{item.items.map((subItem: Tutorial) => (
4747
<TutorialItem key={subItem.path} item={subItem} language={language} depth={depth + 1} />
4848
))}
4949
</ul>

‎lib/tutorials.ts‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ export interface Tutorial {
77
title: string;
88
path: string;
99
items?: Tutorial[];
10+
isOutline?: boolean;
11+
sortOrder: number;
1012
}
1113

1214
export function sortLanguages(languages: string[]): string[] {
@@ -56,31 +58,50 @@ function getDirectoryStructure(dirPath: string, basePath: string): Tutorial[] {
5658
const items = fs.readdirSync(dirPath, { withFileTypes: true })
5759
const structure = items.map(item => {
5860
const itemPath = path.join(dirPath, item.name)
61+
const relativePath = path.relative(path.join(tutorialsDirectory, basePath), itemPath)
62+
const sortOrderMatch = item.name.match(/^(\d+)/)
63+
const sortOrder = sortOrderMatch ? parseInt(sortOrderMatch[1], 10) : Infinity
64+
5965
if (item.isDirectory()) {
66+
const children = getDirectoryStructure(itemPath, basePath)
6067
return {
6168
title: item.name,
62-
path: path.relative(path.join(tutorialsDirectory, basePath), itemPath),
63-
items: getDirectoryStructure(itemPath, basePath)
69+
path: relativePath,
70+
items: children,
71+
sortOrder: sortOrder,
72+
isOutline: false
6473
}
6574
} else if (item.isFile() && item.name.endsWith('.md')) {
6675
try {
6776
const fileContents = fs.readFileSync(itemPath, 'utf8')
6877
const { data } = matter(fileContents)
78+
const isOutline = !relativePath.includes(path.sep) || item.name.toLowerCase().includes('大纲')
6979
return {
7080
title: data.title || item.name.replace('.md', ''),
71-
path: path.relative(path.join(tutorialsDirectory, basePath), itemPath).replace('.md', '')
81+
path: relativePath.replace('.md', ''),
82+
isOutline: isOutline,
83+
sortOrder: isOutline ? -1 : sortOrder
7284
}
7385
} catch (error) {
7486
console.error(`Error parsing file ${itemPath}:`, error)
7587
return {
7688
title: item.name.replace('.md', ''),
77-
path: path.relative(path.join(tutorialsDirectory, basePath), itemPath).replace('.md', '')
89+
path: relativePath.replace('.md', ''),
90+
isOutline: false,
91+
sortOrder: sortOrder
7892
}
7993
}
8094
}
8195
return null
8296
}).filter((item): item is Tutorial => item !== null)
8397

98+
// 修改排序逻辑
99+
structure.sort((a, b) => {
100+
if (a.isOutline && !b.isOutline) return -1
101+
if (!a.isOutline && b.isOutline) return 1
102+
return a.sortOrder - b.sortOrder
103+
})
104+
84105
return structure
85106
}
86107

‎renamefold.js‎

Lines changed: 46 additions & 0 deletions

0 commit comments

Comments
 (0)