mirror of
https://github.com/olcubo/obsidian-cubox.git
synced 2026-07-22 05:43:25 +00:00
perf: skip article content requests when not needed in templates
This commit is contained in:
parent
7182d2452a
commit
e82a0f969e
2 changed files with 28 additions and 8 deletions
18
src/main.ts
18
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(
|
||||
|
|
|
|||
|
|
@ -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}}}');
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue