From ce8b608b8cec8cddb83ce7f40b4f2145dc9f1c2b Mon Sep 17 00:00:00 2001 From: wood chen Date: Thu, 6 Mar 2025 21:52:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=B8=96=E5=AD=90=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E9=80=BB=E8=BE=91=EF=BC=8C=E6=94=AF=E6=8C=81=E5=90=8C?= =?UTF-8?q?=E6=97=B6=E6=9B=B4=E6=96=B0=E5=B8=96=E5=AD=90=E5=86=85=E5=AE=B9?= =?UTF-8?q?=E5=92=8C=E4=B8=BB=E9=A2=98=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 分离帖子内容和主题更新流程 - 添加对主题标题和标签的单独更新支持 - 改进错误处理和响应处理机制 - 在标签选择模态框中添加更新时的标签初始化 --- src/main.ts | 167 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 119 insertions(+), 48 deletions(-) diff --git a/src/main.ts b/src/main.ts index 9870453..bc4b86b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -142,6 +142,7 @@ export default class DiscourseSyncPlugin extends Plugin { // Check if frontmatter contains post ID const frontMatter = this.getFrontMatter(content); const postId = frontMatter?.discourse_post_id; + const topicId = frontMatter?.discourse_topic_id; // Filter out note properties section content = content.replace(/^---[\s\S]*?---\n/, ''); @@ -156,32 +157,94 @@ export default class DiscourseSyncPlugin extends Plugin { }); const isUpdate = postId !== undefined; - const endpoint = isUpdate ? `${this.settings.baseUrl}/posts/${postId}` : url; - const method = isUpdate ? "PUT" : "POST"; + + if (isUpdate) { + // For updating a post + const postEndpoint = `${this.settings.baseUrl}/posts/${postId}`; + const topicEndpoint = `${this.settings.baseUrl}/t/${topicId}`; + + // First update the post content + try { + const postResponse = await requestUrl({ + url: postEndpoint, + method: "PUT", + contentType: "application/json", + body: JSON.stringify({ + raw: content, + edit_reason: "Updated from Obsidian" + }), + headers, + throw: false + }); + + if (postResponse.status !== 200) { + return { + message: "Error", + error: `${t('UPDATE_FAILED')} (${postResponse.status})` + }; + } + + // Then update the topic (for title and tags) + const topicResponse = await requestUrl({ + url: topicEndpoint, + method: "PUT", + contentType: "application/json", + body: JSON.stringify({ + title: (frontMatter?.title ? frontMatter?.title : this.activeFile.name), + tags: this.settings.selectedTags || [] + }), + headers, + throw: false + }); + + if (topicResponse.status === 200) { + return { message: "Success" }; + } else { + try { + const errorResponse = topicResponse.json; + if (errorResponse.errors && errorResponse.errors.length > 0) { + return { + message: "Error", + error: errorResponse.errors.join('\n') + }; + } + if (errorResponse.error) { + return { + message: "Error", + error: errorResponse.error + }; + } + } catch (parseError) { + return { + message: "Error", + error: `${t('UPDATE_FAILED')} (${topicResponse.status})` + }; + } + } + } catch (error) { + return { + message: "Error", + error: `${t('UPDATE_FAILED')}: ${error.message || t('UNKNOWN_ERROR')}` + }; + } + } else { + // For creating a new post + try { + const response = await requestUrl({ + url, + method: "POST", + contentType: "application/json", + body: JSON.stringify({ + title: (frontMatter?.title ? frontMatter?.title : this.activeFile.name), + raw: content, + category: this.settings.category, + tags: this.settings.selectedTags || [] + }), + headers, + throw: false + }); - const body = JSON.stringify(isUpdate ? { - raw: content, - edit_reason: "Updated from Obsidian", - tags: this.settings.selectedTags || [] - } : { - title: (frontMatter?.title ? frontMatter?.title : this.activeFile.name), - raw: content, - category: this.settings.category, - tags: this.settings.selectedTags || [] - }); - - try { - const response = await requestUrl({ - url: endpoint, - method: method, - contentType: "application/json", - body, - headers, - throw: false - }); - - if (response.status === 200) { - if (!isUpdate) { + if (response.status === 200) { try { // Get new post ID and topic ID const responseData = response.json; @@ -199,35 +262,35 @@ export default class DiscourseSyncPlugin extends Plugin { error: t('SAVE_POST_ID_ERROR') }; } - } - return { message: "Success" }; - } else { - try { - const errorResponse = response.json; - if (errorResponse.errors && errorResponse.errors.length > 0) { - return { - message: "Error", - error: errorResponse.errors.join('\n') - }; - } - if (errorResponse.error) { + return { message: "Success" }; + } else { + try { + const errorResponse = response.json; + if (errorResponse.errors && errorResponse.errors.length > 0) { + return { + message: "Error", + error: errorResponse.errors.join('\n') + }; + } + if (errorResponse.error) { + return { + message: "Error", + error: errorResponse.error + }; + } + } catch (parseError) { return { message: "Error", - error: errorResponse.error + error: `${t('PUBLISH_FAILED')} (${response.status})` }; } - } catch (parseError) { - return { - message: "Error", - error: `${isUpdate ? t('UPDATE_FAILED') : t('PUBLISH_FAILED')} (${response.status})` - }; } + } catch (error) { + return { + message: "Error", + error: `${t('PUBLISH_FAILED')}: ${error.message || t('UNKNOWN_ERROR')}` + }; } - } catch (error) { - return { - message: "Error", - error: `${isUpdate ? t('UPDATE_FAILED') : t('PUBLISH_FAILED')}: ${error.message || t('UNKNOWN_ERROR')}` - }; } return { message: "Error", error: `${isUpdate ? t('UPDATE_FAILED') : t('PUBLISH_FAILED')}, ${t('TRY_AGAIN')}` }; } @@ -527,6 +590,11 @@ export class SelectCategoryModal extends Modal { // Selected tags display area const selectedTagsContainer = tagSelectArea.createEl('div', { cls: 'selected-tags' }); const selectedTags = new Set(); + + // Initialize with existing tags if updating + if (isUpdate && this.plugin.settings.selectedTags && this.plugin.settings.selectedTags.length > 0) { + this.plugin.settings.selectedTags.forEach(tag => selectedTags.add(tag)); + } // Update selected tags display const updateSelectedTags = () => { @@ -546,6 +614,9 @@ export class SelectCategoryModal extends Modal { }; }); }; + + // Initialize tag display + updateSelectedTags(); // Create tag input container const tagInputContainer = tagSelectArea.createEl('div', { cls: 'tag-input-container' });