perf: skip article content requests when not needed in templates

This commit is contained in:
delphi 2025-03-28 15:24:32 +08:00
parent 7182d2452a
commit e82a0f969e
2 changed files with 28 additions and 8 deletions

View file

@ -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(

View file

@ -313,4 +313,22 @@ export class TemplateProcessor {
private getArticleProperty(article: CuboxArticle, propertyName: string): unknown {
return (article as unknown as Record<string, unknown>)[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}}}');
}
}