mirror of
https://github.com/woodchen-ink/obsidian-publish-to-discourse.git
synced 2025-07-18 05:42:05 +08:00
重构标签处理逻辑,移除配置中的固定标签设置
- 从配置中删除 selectedTags 字段 - 在 ActiveFile 接口中新增 tags 属性 - 调整标签处理流程,从文件的 Front Matter 获取标签 - 更新发布和更新帖子时的标签处理方式 - 优化标签选择和保存机制
This commit is contained in:
parent
cc1e06ec5c
commit
862c42da61
@ -7,15 +7,13 @@ export interface DiscourseSyncSettings {
|
|||||||
apiKey: string;
|
apiKey: string;
|
||||||
disUser: string;
|
disUser: string;
|
||||||
category: number;
|
category: number;
|
||||||
selectedTags: string[];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const DEFAULT_SETTINGS: DiscourseSyncSettings = {
|
export const DEFAULT_SETTINGS: DiscourseSyncSettings = {
|
||||||
baseUrl: "https://yourforum.example.com",
|
baseUrl: "https://yourforum.example.com",
|
||||||
apiKey: "apikey",
|
apiKey: "apikey",
|
||||||
disUser: "DiscourseUsername",
|
disUser: "DiscourseUsername",
|
||||||
category: 1,
|
category: 1
|
||||||
selectedTags: []
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export class DiscourseSyncSettingsTab extends PluginSettingTab {
|
export class DiscourseSyncSettingsTab extends PluginSettingTab {
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import { App, TFile } from 'obsidian';
|
import { App, TFile } from 'obsidian';
|
||||||
import { expandEmbeds } from './expand-embeds';
|
|
||||||
import { NotifyUser } from './notification';
|
import { NotifyUser } from './notification';
|
||||||
import { DiscourseAPI } from './api';
|
import { DiscourseAPI } from './api';
|
||||||
import { isImageFile } from './utils';
|
import { isImageFile } from './utils';
|
||||||
@ -69,9 +68,4 @@ export class EmbedHandler {
|
|||||||
});
|
});
|
||||||
return processedContent;
|
return processedContent;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理文件内容,展开嵌入内容
|
|
||||||
async expandFileContent(file: TFile): Promise<string> {
|
|
||||||
return await expandEmbeds(this.app, file);
|
|
||||||
}
|
|
||||||
}
|
}
|
67
src/main.ts
67
src/main.ts
@ -46,10 +46,10 @@ export default class PublishToDiscourse extends Plugin implements PluginInterfac
|
|||||||
|
|
||||||
// 添加发布命令
|
// 添加发布命令
|
||||||
this.addCommand({
|
this.addCommand({
|
||||||
id: "category-modal",
|
id: "publish-to-discourse",
|
||||||
name: t('PUBLISH_TO_DISCOURSE'),
|
name: t('PUBLISH_TO_DISCOURSE'),
|
||||||
callback: () => {
|
callback: () => {
|
||||||
this.openCategoryModal();
|
this.publishToDiscourse();
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -76,51 +76,45 @@ export default class PublishToDiscourse extends Plugin implements PluginInterfac
|
|||||||
const syncDiscourse = (item: MenuItem) => {
|
const syncDiscourse = (item: MenuItem) => {
|
||||||
item.setTitle(t('PUBLISH_TO_DISCOURSE'));
|
item.setTitle(t('PUBLISH_TO_DISCOURSE'));
|
||||||
item.onClick(async () => {
|
item.onClick(async () => {
|
||||||
const content = await expandEmbeds(this.app, file);
|
await this.publishToDiscourse(file);
|
||||||
const fm = getFrontMatter(content);
|
|
||||||
this.activeFile = {
|
|
||||||
name: file.basename,
|
|
||||||
content: content,
|
|
||||||
postId: fm?.discourse_post_id
|
|
||||||
};
|
|
||||||
await this.syncToDiscourse();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
menu.addItem(syncDiscourse)
|
menu.addItem(syncDiscourse)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 打开分类选择模态框
|
// 同步到Discourse - 统一入口点
|
||||||
private async openCategoryModal() {
|
private async publishToDiscourse(file?: TFile) {
|
||||||
// 每次都重新获取 activeFile 的最新内容,不使用缓存
|
// 如果提供了特定文件,使用该文件;否则使用当前活动文件
|
||||||
const activeFile = this.app.workspace.getActiveFile();
|
const targetFile = file || this.app.workspace.getActiveFile();
|
||||||
if (!activeFile) {
|
if (!targetFile) {
|
||||||
new NotifyUser(this.app, t('NO_ACTIVE_FILE')).open();
|
new NotifyUser(this.app, t('NO_ACTIVE_FILE')).open();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 使用expandEmbeds处理嵌入内容,而不是直接读取文件内容
|
// 使用expandEmbeds处理嵌入内容
|
||||||
const content = await expandEmbeds(this.app, activeFile);
|
const content = await expandEmbeds(this.app, targetFile);
|
||||||
const fm = getFrontMatter(content);
|
const fm = getFrontMatter(content);
|
||||||
|
|
||||||
|
// 初始化activeFile对象
|
||||||
this.activeFile = {
|
this.activeFile = {
|
||||||
name: activeFile.basename,
|
name: targetFile.basename,
|
||||||
content: content,
|
content: content,
|
||||||
postId: fm?.discourse_post_id
|
postId: fm?.discourse_post_id,
|
||||||
|
// 从frontmatter中获取标签,如果没有则使用空数组
|
||||||
|
tags: fm?.discourse_tags || []
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// 获取分类和标签列表
|
||||||
const [categories, tags] = await Promise.all([
|
const [categories, tags] = await Promise.all([
|
||||||
this.api.fetchCategories(),
|
this.api.fetchCategories(),
|
||||||
this.api.fetchTags()
|
this.api.fetchTags()
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (categories.length > 0) {
|
if (categories.length > 0) {
|
||||||
new SelectCategoryModal(this.app, this, categories, tags).open();
|
new SelectCategoryModal(this.app, this, categories, tags).open();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 同步到Discourse
|
|
||||||
private async syncToDiscourse() {
|
|
||||||
await this.openCategoryModal();
|
|
||||||
}
|
|
||||||
|
|
||||||
// 在Discourse中打开
|
// 在Discourse中打开
|
||||||
private async openInDiscourse() {
|
private async openInDiscourse() {
|
||||||
const activeFile = this.app.workspace.getActiveFile();
|
const activeFile = this.app.workspace.getActiveFile();
|
||||||
@ -165,6 +159,9 @@ export default class PublishToDiscourse extends Plugin implements PluginInterfac
|
|||||||
const topicId = frontMatter?.discourse_topic_id;
|
const topicId = frontMatter?.discourse_topic_id;
|
||||||
const isUpdate = postId !== undefined;
|
const isUpdate = postId !== undefined;
|
||||||
|
|
||||||
|
// 获取当前选择的标签
|
||||||
|
const currentTags = this.activeFile.tags || [];
|
||||||
|
|
||||||
// 发布或更新帖子
|
// 发布或更新帖子
|
||||||
let result;
|
let result;
|
||||||
try {
|
try {
|
||||||
@ -176,20 +173,25 @@ export default class PublishToDiscourse extends Plugin implements PluginInterfac
|
|||||||
(frontMatter?.title ? frontMatter?.title : this.activeFile.name),
|
(frontMatter?.title ? frontMatter?.title : this.activeFile.name),
|
||||||
content,
|
content,
|
||||||
this.settings.category,
|
this.settings.category,
|
||||||
this.settings.selectedTags || []
|
currentTags
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// 如果更新成功,更新Front Matter
|
||||||
|
if (result.success) {
|
||||||
|
await this.updateFrontMatter(postId, topicId, currentTags);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// 创建新帖子
|
// 创建新帖子
|
||||||
result = await this.api.createPost(
|
result = await this.api.createPost(
|
||||||
(frontMatter?.title ? frontMatter?.title : this.activeFile.name),
|
(frontMatter?.title ? frontMatter?.title : this.activeFile.name),
|
||||||
content,
|
content,
|
||||||
this.settings.category,
|
this.settings.category,
|
||||||
this.settings.selectedTags || []
|
currentTags
|
||||||
);
|
);
|
||||||
|
|
||||||
// 如果创建成功,更新Front Matter
|
// 如果创建成功,更新Front Matter
|
||||||
if (result.success && result.postId && result.topicId) {
|
if (result.success && result.postId && result.topicId) {
|
||||||
await this.updateFrontMatter(result.postId, result.topicId);
|
await this.updateFrontMatter(result.postId, result.topicId, currentTags);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -205,7 +207,7 @@ export default class PublishToDiscourse extends Plugin implements PluginInterfac
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 更新Front Matter
|
// 更新Front Matter
|
||||||
private async updateFrontMatter(postId: number, topicId: number) {
|
private async updateFrontMatter(postId: number, topicId: number, tags: string[]) {
|
||||||
try {
|
try {
|
||||||
// 获取当前活动文件
|
// 获取当前活动文件
|
||||||
const activeFile = this.app.workspace.getActiveFile();
|
const activeFile = this.app.workspace.getActiveFile();
|
||||||
@ -224,7 +226,8 @@ export default class PublishToDiscourse extends Plugin implements PluginInterfac
|
|||||||
...fm,
|
...fm,
|
||||||
discourse_post_id: postId,
|
discourse_post_id: postId,
|
||||||
discourse_topic_id: topicId,
|
discourse_topic_id: topicId,
|
||||||
discourse_url: discourseUrl
|
discourse_url: discourseUrl,
|
||||||
|
discourse_tags: tags
|
||||||
};
|
};
|
||||||
newContent = content.replace(/^---\n[\s\S]*?\n---\n/, `---\n${yaml.stringify(updatedFm)}---\n`);
|
newContent = content.replace(/^---\n[\s\S]*?\n---\n/, `---\n${yaml.stringify(updatedFm)}---\n`);
|
||||||
} else {
|
} else {
|
||||||
@ -232,7 +235,8 @@ export default class PublishToDiscourse extends Plugin implements PluginInterfac
|
|||||||
const newFm = {
|
const newFm = {
|
||||||
discourse_post_id: postId,
|
discourse_post_id: postId,
|
||||||
discourse_topic_id: topicId,
|
discourse_topic_id: topicId,
|
||||||
discourse_url: discourseUrl
|
discourse_url: discourseUrl,
|
||||||
|
discourse_tags: tags
|
||||||
};
|
};
|
||||||
newContent = `---\n${yaml.stringify(newFm)}---\n${content}`;
|
newContent = `---\n${yaml.stringify(newFm)}---\n${content}`;
|
||||||
}
|
}
|
||||||
@ -242,7 +246,8 @@ export default class PublishToDiscourse extends Plugin implements PluginInterfac
|
|||||||
this.activeFile = {
|
this.activeFile = {
|
||||||
name: activeFile.basename,
|
name: activeFile.basename,
|
||||||
content: newContent,
|
content: newContent,
|
||||||
postId: postId
|
postId: postId,
|
||||||
|
tags: tags
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
new NotifyUser(this.app, t('UPDATE_FAILED')).open();
|
new NotifyUser(this.app, t('UPDATE_FAILED')).open();
|
||||||
|
@ -4,6 +4,7 @@ export interface ActiveFile {
|
|||||||
name: string;
|
name: string;
|
||||||
content: string;
|
content: string;
|
||||||
postId?: number;
|
postId?: number;
|
||||||
|
tags?: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface PluginInterface {
|
export interface PluginInterface {
|
||||||
|
@ -63,8 +63,8 @@ export class SelectCategoryModal extends Modal {
|
|||||||
const selectedTags = new Set<string>();
|
const selectedTags = new Set<string>();
|
||||||
|
|
||||||
// 初始化已选标签
|
// 初始化已选标签
|
||||||
if (this.plugin.settings.selectedTags && this.plugin.settings.selectedTags.length > 0) {
|
if (this.plugin.activeFile.tags && this.plugin.activeFile.tags.length > 0) {
|
||||||
this.plugin.settings.selectedTags.forEach(tag => selectedTags.add(tag));
|
this.plugin.activeFile.tags.forEach(tag => selectedTags.add(tag));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新标签显示
|
// 更新标签显示
|
||||||
@ -209,9 +209,8 @@ export class SelectCategoryModal extends Modal {
|
|||||||
const noticeContainer = buttonArea.createEl('div', { cls: 'notice-container' });
|
const noticeContainer = buttonArea.createEl('div', { cls: 'notice-container' });
|
||||||
|
|
||||||
submitButton.onclick = async () => {
|
submitButton.onclick = async () => {
|
||||||
// 保存选中的标签
|
// 保存当前选择的标签到activeFile对象
|
||||||
this.plugin.settings.selectedTags = Array.from(selectedTags);
|
this.plugin.activeFile.tags = Array.from(selectedTags);
|
||||||
await this.plugin.saveSettings();
|
|
||||||
|
|
||||||
// 禁用提交按钮,显示加载状态
|
// 禁用提交按钮,显示加载状态
|
||||||
submitButton.disabled = true;
|
submitButton.disabled = true;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user