update at 2025-10-08 09:18:20
This commit is contained in:
118
src/main.ts
118
src/main.ts
@@ -1,23 +1,10 @@
|
||||
/*
|
||||
* Copyright (c) 2024-2025 Sun Booshi
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
/**
|
||||
* 文件:main.ts
|
||||
* 入口:Obsidian 插件主类,负责:
|
||||
* - 视图注册 / 右键菜单扩展
|
||||
* - 微信公众号与小红书发布入口调度
|
||||
* - 设置加载与保存
|
||||
* - 与 NotePreview / 批量发布 / 小红书登录流程衔接
|
||||
*/
|
||||
|
||||
import { Plugin, WorkspaceLeaf, App, PluginManifest, Menu, Notice, TAbstractFile, TFile, TFolder } from 'obsidian';
|
||||
@@ -28,6 +15,9 @@ import AssetsManager from './assets';
|
||||
import { setVersion, uevent } from './utils';
|
||||
import { WidgetsModal } from './widgets-modal';
|
||||
import { BatchPublishModal } from './batch-publish-modal';
|
||||
import { XiaohongshuLoginModal } from './xiaohongshu/login-modal';
|
||||
import { XiaohongshuContentAdapter } from './xiaohongshu/adapter';
|
||||
import { XiaohongshuAPIManager } from './xiaohongshu/api';
|
||||
|
||||
/**
|
||||
* NoteToMpPlugin
|
||||
@@ -115,6 +105,7 @@ export default class NoteToMpPlugin extends Plugin {
|
||||
// 监听右键菜单
|
||||
this.registerEvent(
|
||||
this.app.workspace.on('file-menu', (menu, file) => {
|
||||
// 发布到微信公众号
|
||||
menu.addItem((item) => {
|
||||
item
|
||||
.setTitle('发布到公众号')
|
||||
@@ -134,6 +125,22 @@ export default class NoteToMpPlugin extends Plugin {
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// 发布到小红书(新增)
|
||||
menu.addItem((item) => {
|
||||
item
|
||||
.setTitle('发布到小红书')
|
||||
.setIcon('lucide-heart')
|
||||
.onClick(async () => {
|
||||
if (file instanceof TFile) {
|
||||
if (file.extension.toLowerCase() !== 'md') {
|
||||
new Notice('只能发布 Markdown 文件');
|
||||
return;
|
||||
}
|
||||
await this.publishToXiaohongshu(file);
|
||||
}
|
||||
});
|
||||
});
|
||||
})
|
||||
);
|
||||
}
|
||||
@@ -174,4 +181,75 @@ export default class NoteToMpPlugin extends Plugin {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发布到小红书
|
||||
*/
|
||||
async publishToXiaohongshu(file: TFile) {
|
||||
try {
|
||||
console.log('开始发布到小红书...', file.name);
|
||||
new Notice('开始发布到小红书...');
|
||||
|
||||
// 获取API实例
|
||||
const api = XiaohongshuAPIManager.getInstance(true);
|
||||
|
||||
// 检查登录状态,如果未登录则显示登录对话框
|
||||
console.log('检查登录状态...');
|
||||
// 暂时总是显示登录对话框进行测试
|
||||
const isLoggedIn = false; // await api.checkLoginStatus();
|
||||
console.log('登录状态:', isLoggedIn);
|
||||
|
||||
if (!isLoggedIn) {
|
||||
console.log('用户未登录,显示登录对话框...');
|
||||
new Notice('需要登录小红书账户');
|
||||
|
||||
let loginSuccess = false;
|
||||
|
||||
const loginModal = new XiaohongshuLoginModal(this.app, () => {
|
||||
console.log('登录成功回调被调用');
|
||||
loginSuccess = true;
|
||||
});
|
||||
|
||||
console.log('打开登录模态窗口...');
|
||||
await new Promise<void>((resolve) => {
|
||||
const originalClose = loginModal.close;
|
||||
loginModal.close = () => {
|
||||
console.log('登录窗口关闭');
|
||||
originalClose.call(loginModal);
|
||||
resolve();
|
||||
};
|
||||
loginModal.open();
|
||||
});
|
||||
|
||||
console.log('登录结果:', loginSuccess);
|
||||
if (!loginSuccess) {
|
||||
new Notice('登录失败,无法发布到小红书');
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 读取文件内容
|
||||
const content = await this.app.vault.read(file);
|
||||
|
||||
// 转换内容格式
|
||||
const adapter = new XiaohongshuContentAdapter();
|
||||
const xiaohongshuPost = adapter.adaptMarkdownToXiaohongshu(content, {
|
||||
generateTitle: true,
|
||||
addStyle: true
|
||||
});
|
||||
|
||||
// 发布文章
|
||||
const result = await api.createPost(xiaohongshuPost);
|
||||
|
||||
if (result.success) {
|
||||
new Notice('文章已成功发布到小红书!');
|
||||
} else {
|
||||
new Notice('发布失败: ' + result.message);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('发布到小红书失败:', error);
|
||||
new Notice('发布失败: ' + (error instanceof Error ? error.message : String(error)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user