From 0b496ba5821f6cca16c772c3b10e0f6052867f36 Mon Sep 17 00:00:00 2001 From: seaHi Date: Tue, 22 Sep 2026 21:00:47 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20Obsidian=20=E6=8F=92=E4=BB=B6=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=8C=89=E5=88=86=E7=B1=BB=E7=9B=AE=E5=BD=95=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=E6=96=87=E7=AB=A0=E5=B9=B6=E5=A2=9E=E5=8A=A0=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E6=B5=8B=E8=AF=95=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/obsidian-plugin/src/api.ts | 1 + packages/obsidian-plugin/src/main.ts | 20 ++++++++++++----- packages/obsidian-plugin/src/pull_modal.ts | 12 ++++++++++- packages/obsidian-plugin/src/settings.ts | 25 +++++++++++++++++++++- 4 files changed, 51 insertions(+), 7 deletions(-) diff --git a/packages/obsidian-plugin/src/api.ts b/packages/obsidian-plugin/src/api.ts index 4a4c82a..bfe0209 100644 --- a/packages/obsidian-plugin/src/api.ts +++ b/packages/obsidian-plugin/src/api.ts @@ -20,6 +20,7 @@ export interface PostMeta { title: string; published_at: string; category_slug?: string; + category_name?: string; } export interface PostDetail extends PostMeta { diff --git a/packages/obsidian-plugin/src/main.ts b/packages/obsidian-plugin/src/main.ts index 433b961..a36ef12 100644 --- a/packages/obsidian-plugin/src/main.ts +++ b/packages/obsidian-plugin/src/main.ts @@ -97,7 +97,7 @@ export default class BlogFlarePlugin extends Plugin { this.addCommand({ id: 'publish-post', - name: '发布文章 (push)', + name: '发布文章', checkCallback: (checking: boolean) => { const activeView = this.app.workspace.getActiveViewOfType(MarkdownView); const file = activeView?.file; @@ -164,7 +164,7 @@ export default class BlogFlarePlugin extends Plugin { this.addCommand({ id: 'pull-post', - name: '拉取文章 (pull)', + name: '拉取文章', callback: () => { this.api.getPosts().then(items => { if (items.length === 0) { @@ -181,7 +181,7 @@ export default class BlogFlarePlugin extends Plugin { this.addCommand({ id: 'init-sync', - name: '全量拉取初始化 (init sync)', + name: '全量拉取初始化', callback: () => { const mdFiles = this.app.vault.getMarkdownFiles(); if (mdFiles.length > 0) { @@ -219,7 +219,17 @@ export default class BlogFlarePlugin extends Plugin { const fileContent = frontmatterLines.join('\n') + '\n\n' + (detail.content || ''); - const fileName = `${detail.title.replace(/[/\\:]/g, '-')}.md`; + const baseName = detail.title.replace(/[/\\:]/g, '-'); + let dirPath = ''; + const categoryDir = detail.category_name || detail.category_slug; + if (categoryDir) { + dirPath = categoryDir.replace(/[/\\:]/g, '-'); + const folder = this.app.vault.getAbstractFileByPath(dirPath); + if (!folder) { + await this.app.vault.createFolder(dirPath); + } + } + const fileName = dirPath ? `${dirPath}/${baseName}.md` : `${baseName}.md`; await this.app.vault.create(fileName, fileContent); successCount++; } catch (err) { @@ -238,7 +248,7 @@ export default class BlogFlarePlugin extends Plugin { this.addCommand({ id: 'delete-post', - name: '下线云端文章 (delete)', + name: '下线云端文章', checkCallback: (checking: boolean) => { const activeView = this.app.workspace.getActiveViewOfType(MarkdownView); const file = activeView?.file; diff --git a/packages/obsidian-plugin/src/pull_modal.ts b/packages/obsidian-plugin/src/pull_modal.ts index caa1b09..6a8a7fd 100644 --- a/packages/obsidian-plugin/src/pull_modal.ts +++ b/packages/obsidian-plugin/src/pull_modal.ts @@ -40,7 +40,17 @@ export class PullSuggestModal extends FuzzySuggestModal { const fileContent = frontmatterLines.join('\n') + '\n\n' + (detail.content || ''); // 决定文件名 (过滤掉非法字符) - const fileName = `${detail.title.replace(/[/\\:]/g, '-')}.md`; + const baseName = detail.title.replace(/[/\\:]/g, '-'); + let dirPath = ''; + const categoryDir = detail.category_name || detail.category_slug; + if (categoryDir) { + dirPath = categoryDir.replace(/[/\\:]/g, '-'); + const folder = this.app.vault.getAbstractFileByPath(dirPath); + if (!folder) { + await this.app.vault.createFolder(dirPath); + } + } + const fileName = dirPath ? `${dirPath}/${baseName}.md` : `${baseName}.md`; // 检查是否已有该文件 let file = this.app.vault.getAbstractFileByPath(fileName); diff --git a/packages/obsidian-plugin/src/settings.ts b/packages/obsidian-plugin/src/settings.ts index 595abb1..7ccce26 100644 --- a/packages/obsidian-plugin/src/settings.ts +++ b/packages/obsidian-plugin/src/settings.ts @@ -1,4 +1,4 @@ -import { App, PluginSettingTab, Setting } from 'obsidian'; +import { App, PluginSettingTab, Setting, Notice } from 'obsidian'; import BlogFlarePlugin from './main'; export interface BlogFlareSettings { @@ -44,5 +44,28 @@ export class BlogFlareSettingTab extends PluginSettingTab { this.plugin.settings.apiToken = value; await this.plugin.saveSettings(); })); + + new Setting(containerEl) + .setName('测试连接') + .setDesc('验证当前配置的 Endpoint 与 API 令牌是否能正确连接到 Blogflare 后端') + .addButton((button) => { + button + .setButtonText('测试') + .setCta() + .onClick(async () => { + button.setButtonText('测试中...'); + button.setDisabled(true); + try { + await this.plugin.api.getCategories(); + new Notice('✅ 连接成功!API 令牌有效。'); + } catch (error) { + console.error(error); + new Notice('❌ 连接失败:API 令牌无效或网络错误,请检查设置。'); + } finally { + button.setButtonText('测试'); + button.setDisabled(false); + } + }); + }); } }