From e82a0f969ec0ded4ed80b4ff3de34ab41b65eac4 Mon Sep 17 00:00:00 2001 From: delphi Date: Fri, 28 Mar 2025 15:24:32 +0800 Subject: [PATCH] perf: skip article content requests when not needed in templates --- src/main.ts | 18 ++++++++++-------- src/templateProcessor.ts | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/main.ts b/src/main.ts index a1f1f9d..d8af2cf 100644 --- a/src/main.ts +++ b/src/main.ts @@ -117,6 +117,9 @@ export default class CuboxSyncPlugin extends Plugin { ); const { articles, hasMore: moreArticles} = result; + + // 检查内容模板是否需要文章内容 + const needsContent = this.templateProcessor.needsArticleContent(this.settings.contentTemplate); if (articles.length === 0) { break; @@ -125,15 +128,14 @@ export default class CuboxSyncPlugin extends Plugin { // 处理每篇文章 for (const article of articles) { try { - // 获取文章内容 - const content = await this.cuboxApi.getArticleDetail(article.id); - if (content === null) continue; + let fullArticle = {...article}; - // 合并文章基本信息、内容和高亮 - const fullArticle = { - ...article, - content: content - }; + if (needsContent) { + const content = await this.cuboxApi.getArticleDetail(article.id); + if (content === null) continue; + + fullArticle.content = content; + } // 处理文件名和内容 const filename = this.templateProcessor.processFilenameTemplate( diff --git a/src/templateProcessor.ts b/src/templateProcessor.ts index 5a9cc7f..836e02b 100644 --- a/src/templateProcessor.ts +++ b/src/templateProcessor.ts @@ -313,4 +313,22 @@ export class TemplateProcessor { private getArticleProperty(article: CuboxArticle, propertyName: string): unknown { return (article as unknown as Record)[propertyName]; } + + /** + * 检查模板是否需要文章内容 + * @param template 内容模板 + * @returns 是否需要请求文章内容 + */ + needsArticleContent(template: string): boolean { + // 如果没有模板,但默认模板会输出文章内容,所以需要内容 + if (!template) { + return true; + } + + // 检查模板中是否包含需要文章内容的变量 + return template.includes('{{content}}') || + template.includes('{{{content}}}') || + template.includes('{{content_highlighted}}') || + template.includes('{{{content_highlighted}}}'); + } } \ No newline at end of file