From 64cf4c6ab0223ab78024e65c7eaf1916d0dcfdfe Mon Sep 17 00:00:00 2001 From: LetTTGACO <1225751694@qq.com> Date: Mon, 20 May 2024 00:55:45 +0800 Subject: [PATCH] feat(yuque): yuque optimize --- playground/plugin-from-yuque-pwd/src/YuqueApi.ts | 2 +- playground/plugin-from-yuque-pwd/src/YuqueClient.ts | 13 ++++++------- playground/plugin-from-yuque-token/src/YuqueApi.ts | 2 +- .../plugin-from-yuque-token/src/YuqueClient.ts | 9 ++++----- 4 files changed, 12 insertions(+), 14 deletions(-) diff --git a/playground/plugin-from-yuque-pwd/src/YuqueApi.ts b/playground/plugin-from-yuque-pwd/src/YuqueApi.ts index 3078c647..f130ea8c 100644 --- a/playground/plugin-from-yuque-pwd/src/YuqueApi.ts +++ b/playground/plugin-from-yuque-pwd/src/YuqueApi.ts @@ -92,7 +92,7 @@ export default class YuqueApi extends ElogBaseContext { /** * 获取目录信息(已排序) */ - async getSortedInfoList() { + async getSortedDocList() { try { const res = await this.requestInternal( `${this.config.login}/${this.config.repo}`, diff --git a/playground/plugin-from-yuque-pwd/src/YuqueClient.ts b/playground/plugin-from-yuque-pwd/src/YuqueClient.ts index 83ac18d0..c7788357 100644 --- a/playground/plugin-from-yuque-pwd/src/YuqueClient.ts +++ b/playground/plugin-from-yuque-pwd/src/YuqueClient.ts @@ -22,11 +22,11 @@ export default class YuqueClient extends ElogFromContext { await this.api.login(); this.ctx.info('正在获取文档列表,请稍等...'); // 获取已排序目录信息 - const sortedInfoList = await this.api.getSortedInfoList(); + const sortedDocList = await this.api.getSortedDocList(); // 获取文档列表 let yuqueBaseDocList = await this.api.getDocList(); // 根据目录排序文档顺序,处理文档目录 - yuqueBaseDocList = sortedInfoList + yuqueBaseDocList = sortedDocList .filter((item) => { return item.type === 'DOC'; }) @@ -35,7 +35,7 @@ export default class YuqueClient extends ElogFromContext { let catalogPath: DocStructure[] = []; let parentId = item.parent_uuid; for (let i = 0; i < item.level; i++) { - const current = sortedInfoList.find((item) => item.uuid === parentId)!; + const current = sortedDocList.find((item) => item.uuid === parentId)!; parentId = current.parent_uuid; catalogPath.push({ id: item.url, @@ -72,7 +72,6 @@ export default class YuqueClient extends ElogFromContext { process.exit(); } this.ctx.info('待下载数', String(needUpdateDocList.length)); - let updatedDocList: DocDetail[]; const promise = async (doc: YuqueDoc) => { this.ctx.info(`下载文档 ${doc._index}/${needUpdateDocList.length} `, doc.title); let articleStr = await this.api.getDocString(doc.slug); @@ -90,9 +89,9 @@ export default class YuqueClient extends ElogFromContext { }; return docDetail; }; - updatedDocList = await this.asyncPool(this.config.limit || 3, needUpdateDocList, promise); + const docDetailList = await this.asyncPool(this.config.limit || 3, needUpdateDocList, promise); // 更新缓存里的文章 - this.updateCache(updatedDocList, idMap); + this.updateCache(docDetailList, idMap); this.ctx.info('已下载数', String(needUpdateDocList.length)); // 写入缓存 this.writeCache({ @@ -101,6 +100,6 @@ export default class YuqueClient extends ElogFromContext { title: item.title, })), }); - return updatedDocList; + return docDetailList; } } diff --git a/playground/plugin-from-yuque-token/src/YuqueApi.ts b/playground/plugin-from-yuque-token/src/YuqueApi.ts index 3a218de9..0bbc04b3 100644 --- a/playground/plugin-from-yuque-token/src/YuqueApi.ts +++ b/playground/plugin-from-yuque-token/src/YuqueApi.ts @@ -58,7 +58,7 @@ export default class YuqueApi extends ElogBaseContext { /** * 获取目录 */ - async getToc() { + async getSortedDocList() { return this.requestInternal( `repos/${this.config.login}/${this.config.repo}/toc`, { diff --git a/playground/plugin-from-yuque-token/src/YuqueClient.ts b/playground/plugin-from-yuque-token/src/YuqueClient.ts index 702effaf..2cf0da8b 100644 --- a/playground/plugin-from-yuque-token/src/YuqueClient.ts +++ b/playground/plugin-from-yuque-token/src/YuqueClient.ts @@ -20,11 +20,11 @@ export default class YuqueClient extends ElogFromContext { async getDocDetailList() { this.ctx.info('正在获取文档列表,请稍等...'); // 获取目录 - const catalogList = await this.api.getToc(); + const sortedDocList = await this.api.getSortedDocList(); // 获取文档列表 let yuqueBaseDocList = await this.api.getDocList(); // 根据目录排序文档顺序,处理文档目录 - yuqueBaseDocList = catalogList + yuqueBaseDocList = sortedDocList .filter((item) => { return item.type === 'DOC'; }) @@ -33,7 +33,7 @@ export default class YuqueClient extends ElogFromContext { let catalogPath: DocStructure[] = []; let parentId = item.parent_uuid; for (let i = 0; i < item.level; i++) { - const current = catalogList.find((item) => item.uuid === parentId)!; + const current = sortedDocList.find((item) => item.uuid === parentId)!; parentId = current.parent_uuid; catalogPath.push({ id: item.slug, @@ -70,7 +70,6 @@ export default class YuqueClient extends ElogFromContext { process.exit(); } this.ctx.info('待下载数', String(needUpdateDocList.length)); - let docDetailList: DocDetail[]; const promise = async (doc: YuqueDoc) => { this.ctx.info(`下载文档 ${doc._index}/${needUpdateDocList.length} `, doc.title); let article = await this.api.getDocDetail(doc.slug); @@ -88,7 +87,7 @@ export default class YuqueClient extends ElogFromContext { }; return docDetail; }; - docDetailList = await this.asyncPool(this.config.limit || 3, needUpdateDocList, promise); + const docDetailList = await this.asyncPool(this.config.limit || 3, needUpdateDocList, promise); // 更新缓存里的文章 this.updateCache(docDetailList, idMap); this.ctx.info('已下载数', String(needUpdateDocList.length));