|
1 | 1 | import path from 'node:path';
|
2 |
| -import { isObject } from '@vueuse/core'; |
3 |
| -import directoryTree from 'directory-tree'; |
4 | 2 | import fs from 'fs-extra';
|
5 | 3 | import matter from 'gray-matter';
|
6 | 4 | import { glob, type GlobOptions } from 'tinyglobby';
|
7 | 5 | import { createMarkdownRenderer, type SiteConfig } from 'vitepress';
|
| 6 | +import { handleAutoSidebar } from '../utils/loader/auto.sidebar'; |
8 | 7 | import { dateToUnixTimestamp } from '../utils/node/date';
|
9 | 8 | import { getLastCommitInfo } from '../utils/node/git';
|
10 |
| -import { ensureIndexMd, getPattern, normalizePath } from '../utils/node/path'; |
| 9 | +import { getPattern, normalizePath } from '../utils/node/path'; |
11 | 10 |
|
12 | 11 | export interface ContentData {
|
13 | 12 | url: string
|
@@ -71,116 +70,6 @@ export interface ContentOptions<T = ContentData[]> {
|
71 | 70 | globOptions?: GlobOptions
|
72 | 71 | }
|
73 | 72 |
|
74 |
| -function filterHiddenItems(items: any[]): any[] { |
75 |
| - return items |
76 |
| - .filter(item => !item.hide) // 过滤掉 hide 为 true 的对象 |
77 |
| - .map((item: any) => { |
78 |
| - if (item.items) { |
79 |
| - // 如果有子项,递归过滤子项 |
80 |
| - return { |
81 |
| - ...item, |
82 |
| - items: filterHiddenItems(item.items), |
83 |
| - }; |
84 |
| - } |
85 |
| - return item; |
86 |
| - }); |
87 |
| -} |
88 |
| - |
89 |
| -// sidebar排序, 越小越靠前 |
90 |
| -function sortSidebar(sidebar: any[]) { |
91 |
| - sidebar.forEach(item => { |
92 |
| - if (item.items) { |
93 |
| - item.items = sortSidebar(item.items); |
94 |
| - } |
95 |
| - }); |
96 |
| - |
97 |
| - // 排序函数 |
98 |
| - return sidebar.sort((a, b) => { |
99 |
| - // 判断 path 是否以 index.md 结尾 |
100 |
| - const aIsIndex = a.path.endsWith('index.md'); |
101 |
| - const bIsIndex = b.path.endsWith('index.md'); |
102 |
| - |
103 |
| - if (aIsIndex && !bIsIndex) { |
104 |
| - return -1; |
105 |
| - } |
106 |
| - if (!aIsIndex && bIsIndex) { |
107 |
| - return 1; |
108 |
| - } |
109 |
| - |
110 |
| - if (a.sort === undefined && b.sort !== undefined) { |
111 |
| - return 1; |
112 |
| - } |
113 |
| - else if (a.sort !== undefined && b.sort === undefined) { |
114 |
| - return -1; |
115 |
| - } |
116 |
| - |
117 |
| - // 根据 sort 排序 |
118 |
| - if (a.sort !== b.sort) { |
119 |
| - return a.sort - b.sort; |
120 |
| - } |
121 |
| - |
122 |
| - // 根据 text 的首个字符排序 |
123 |
| - return a.text.localeCompare(b.text); |
124 |
| - }); |
125 |
| -} |
126 |
| -// Handle auto sidebar |
127 |
| -function formatSidebarItems(item: any, PATH: string, config: SiteConfig, data: Map<string, ContentData>) { |
128 |
| - const link = `/${ |
129 |
| - normalizePath(path.relative(config.srcDir, PATH)) |
130 |
| - .replace(/(^|\/)index\.md$/, '$1') |
131 |
| - .replace(/\.md$/, config.cleanUrls ? '' : '.html')}`; |
132 |
| - const filename = link.split('/')[link.split('/').length - 1].split('.')[0] || 'index'; |
133 |
| - const article = data.get(ensureIndexMd(PATH)); |
134 |
| - const content = matter(article?.src || '').content; |
135 |
| - const match = content.match(/^(#+)\s+(.+)/m); |
136 |
| - const title = match?.[2] || filename; |
137 |
| - const { sidebar, publish } = article?.frontmatter || {}; |
138 |
| - |
139 |
| - item.sort = sidebar?.sort; |
140 |
| - item.hide = sidebar === false; |
141 |
| - |
142 |
| - if (!item.children) { |
143 |
| - item.link = link; |
144 |
| - item.text = sidebar?.text || title; |
145 |
| - item.hide = publish === false; |
146 |
| - } |
147 |
| - else { |
148 |
| - item.items = item.children; |
149 |
| - item.text = sidebar?.title || title; |
150 |
| - item.collapsed = sidebar?.collapsed; |
151 |
| - delete item.children; |
152 |
| - } |
153 |
| -} |
154 |
| - |
155 |
| -function handleAutoSidebar(config: SiteConfig, data: Map<string, ContentData>) { |
156 |
| - const sidebar: any = (global as any).VITEPRESS_CONFIG.userConfig.themeConfig?.sidebar; |
157 |
| - const autoPaths: string[] = []; |
158 |
| - const autoSidebar: Record<string, any> = {}; |
159 |
| - if (isObject(sidebar)) { |
160 |
| - Object.keys(sidebar).forEach(key => { |
161 |
| - if (((sidebar as any)[key] as string) === 'auto') { |
162 |
| - autoPaths.push(key); |
163 |
| - } |
164 |
| - }); |
165 |
| - autoPaths.forEach(item => { |
166 |
| - const dirPath = path.join(config.srcDir, item); |
167 |
| - const filteredTree: any = directoryTree(dirPath, { |
168 |
| - exclude: /(node_modules|\.vitepress)$/, |
169 |
| - extensions: /\.md/, |
170 |
| - normalizePath: true |
171 |
| - }, (item, PATH) => formatSidebarItems(item, PATH, config, data), (item, PATH) => formatSidebarItems(item, PATH, config, data)); |
172 |
| - |
173 |
| - if (!data.get(ensureIndexMd(filteredTree.path))) { |
174 |
| - autoSidebar[item] = sortSidebar(filterHiddenItems(filteredTree.items)); |
175 |
| - } |
176 |
| - else { |
177 |
| - autoSidebar[item] = sortSidebar(filterHiddenItems([filteredTree])); |
178 |
| - } |
179 |
| - }); |
180 |
| - } |
181 |
| - return autoSidebar; |
182 |
| -} |
183 |
| - |
184 | 73 | export function createBaseDataLoader<T = {
|
185 | 74 | list: ContentData[]
|
186 | 75 | autoSidebar: any
|
|
0 commit comments