Skip to content

Commit eb46c5a

Browse files
author
白云苍狗
committed
🦄 refactor: 参数名调整
1 parent cc4cd74 commit eb46c5a

File tree

1 file changed

+37
-22
lines changed
  • packages/vitepress-theme-async/plugin

1 file changed

+37
-22
lines changed

packages/vitepress-theme-async/plugin/page.ts

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,27 @@ const generatePages = (len: number, pageSize: number, paramKey: string, otherPag
8484
fitstPageName = fitstPageName.replace(/^\//, '');
8585
const count = Math.ceil(len / pageSize);
8686
return new Array(count).fill(null).map((_, i) => {
87-
return i === 0 ? { params: { [paramKey]: fitstPageName } } : { params: { [paramKey]: joinPath(otherPageName, (i + 1).toString()) } };
87+
return i === 0
88+
? {
89+
params: {
90+
[paramKey]: fitstPageName,
91+
index: fitstPageName,
92+
},
93+
}
94+
: {
95+
params: {
96+
[paramKey]: joinPath(otherPageName, (i + 1).toString()),
97+
index: joinPath(otherPageName, (i + 1).toString()),
98+
},
99+
};
88100
});
89101
};
90102

91103
/**
92104
* 通用分页生成
93105
* @param files 文章路径集合
94106
* @param config 主题配置
95-
* @param paramKey [paramName].md 动态文件参数名
107+
* @param pageType [paramName].md 动态文件参数名
96108
* @param processFn 子级分页数据处理
97109
* @returns
98110
*
@@ -104,20 +116,23 @@ const generatePages = (len: number, pageSize: number, paramKey: string, otherPag
104116
* | /xxx/ | /xxx/index.html | /xxx/page/2.html | /xxx/子级.html | /xxx/子级/page/2.html |
105117
* | /xxx/index | /xxx/index.html | /xxx/page/2.html | /xxx/子级.html | /xxx/子级/page/2.html |
106118
*/
107-
const generatePagesByType = async (files: string[], config: UserConfig<AsyncThemeConfig>, paramKey: AsyncTheme.PageType, processFn?: ProcessFn) => {
119+
const generatePagesByType = async (files: string[], config: UserConfig<AsyncThemeConfig>, pageType: AsyncTheme.PageType, processFn?: ProcessFn) => {
108120
const raw = getMeta(files, config.themeConfig?.timeZone || 8);
109-
const pageSize = (paramKey === 'index' ? config.themeConfig?.indexGenerator?.perPage : config.themeConfig?.archiveGenerator?.perPage) || 10;
110-
const type = paramKey;
121+
const pageSize = (pageType === 'index' ? config.themeConfig?.indexGenerator?.perPage : config.themeConfig?.archiveGenerator?.perPage) || 10;
122+
const type = pageType; // 当前页类型在 Frontmatter 使用的 [属性]
123+
const paramKey = pageType; // 动态路径参数名 - 默认与 layout 值保持一致
111124

112-
const baseName = config.themeConfig?.page?.[type] || type; // 获取用户配置路径
125+
const baseName = config.themeConfig?.page?.[pageType] || pageType; // 获取用户配置路径
113126
const basePageName = baseName.replace(/(\/index$|^index$)/, ''); // 去掉路径最后一层的 index
114127

115128
// 默认分页
116129
let firstPageName = baseName;
117130
if (/\/$/.test(firstPageName) || firstPageName === '') {
118131
firstPageName = joinPath(firstPageName, 'index');
119132
}
120-
const len = ['index', 'archives'].includes(type) ? files.length : raw.filter(item => item[type].length).length;
133+
134+
// 首页、归档默认分页取全部数据 标签、分类默认分页需要根据 Frontmatter [属性] 过滤数据
135+
const len = ['index', 'archives'].includes(pageType) ? files.length : raw.filter(item => item[type].length).length;
121136
const defpage = generatePages(len, pageSize, paramKey, joinPath(basePageName, 'page'), firstPageName);
122137

123138
// 子级分页
@@ -137,40 +152,40 @@ const generatePagesByType = async (files: string[], config: UserConfig<AsyncThem
137152
};
138153

139154
/** 首页生成 */
140-
const pageIndex = async (files: string[], config: UserConfig<AsyncThemeConfig>, paramKey: AsyncTheme.PageType) => {
141-
return generatePagesByType(files, config, paramKey);
155+
const pageIndex = async (files: string[], config: UserConfig<AsyncThemeConfig>, pageType: AsyncTheme.PageType) => {
156+
return generatePagesByType(files, config, pageType);
142157
};
143158

144159
/** 标签页生成 */
145-
const tagPageIndex = async (files: string[], config: UserConfig<AsyncThemeConfig>, paramKey: AsyncTheme.PageType) => {
160+
const tagPageIndex = async (files: string[], config: UserConfig<AsyncThemeConfig>, pageType: AsyncTheme.PageType) => {
146161
const processFn: ProcessFn = raw => sortBy(groupBy(raw, 'tags'), { 1: -1 });
147-
return generatePagesByType(files, config, paramKey, processFn);
162+
return generatePagesByType(files, config, pageType, processFn);
148163
};
149164

150165
/** 分类页生成 */
151-
const categoriePageIndex = async (files: string[], config: UserConfig<AsyncThemeConfig>, paramKey: AsyncTheme.PageType) => {
166+
const categoriePageIndex = async (files: string[], config: UserConfig<AsyncThemeConfig>, pageType: AsyncTheme.PageType) => {
152167
const processFn: ProcessFn = raw => sortBy(groupBy(raw, 'categories'), { 1: -1 });
153-
return generatePagesByType(files, config, paramKey, processFn);
168+
return generatePagesByType(files, config, pageType, processFn);
154169
};
155170

156171
/** 归档页生成 */
157-
const archivePageIndex = async (files: string[], config: UserConfig<AsyncThemeConfig>, paramKey: AsyncTheme.PageType) => {
172+
const archivePageIndex = async (files: string[], config: UserConfig<AsyncThemeConfig>, pageType: AsyncTheme.PageType) => {
158173
const processFn: ProcessFn = raw =>
159174
sortBy(
160175
groupBy(raw, 'date', date => formatDate(date, config.themeConfig?.archiveGenerator?.dateFmt || 'YYYY')),
161176
{ 0: -1 },
162177
);
163-
return generatePagesByType(files, config, paramKey, processFn);
178+
return generatePagesByType(files, config, pageType, processFn);
164179
};
165180

166181
/**
167182
* 动态路由生成
168183
* @param config 主题配置信息
169-
* @param type
184+
* @param pageType 页面 layout 类型
170185
* @param root 根目录
171186
* @returns
172187
*/
173-
export const dynamicPages = async (config: UserConfig<AsyncThemeConfig>, type: AsyncTheme.PageType, root?: string) => {
188+
export const dynamicPages = async (config: UserConfig<AsyncThemeConfig>, pageType: AsyncTheme.PageType, root?: string) => {
174189
if (!root) {
175190
const argv = process.argv.slice(2);
176191
const command = argv[0];
@@ -182,18 +197,18 @@ export const dynamicPages = async (config: UserConfig<AsyncThemeConfig>, type: A
182197
const files = await getFiles(normalizePath(`${srcDir}/${config.themeConfig?.postDir ?? 'posts'}`));
183198

184199
let paths: DynamicPages[] = [];
185-
switch (type) {
200+
switch (pageType) {
186201
case 'index':
187-
paths = await pageIndex(files, config, type);
202+
paths = await pageIndex(files, config, pageType);
188203
break;
189204
case 'tags':
190-
paths = await tagPageIndex(files, config, type);
205+
paths = await tagPageIndex(files, config, pageType);
191206
break;
192207
case 'archives':
193-
paths = await archivePageIndex(files, config, type);
208+
paths = await archivePageIndex(files, config, pageType);
194209
break;
195210
case 'categories':
196-
paths = await categoriePageIndex(files, config, type);
211+
paths = await categoriePageIndex(files, config, pageType);
197212
break;
198213
default:
199214
break;

0 commit comments

Comments
 (0)