From b2490cc32b5ce1480d87c5349616c0003ac58ce3 Mon Sep 17 00:00:00 2001 From: anpigon Date: Fri, 29 Dec 2023 22:06:01 +0900 Subject: [PATCH] fix(main.ts): import SteemitPostOptions from types to fix missing type error refactor(main.ts): extract publishAndUpdate method to improve code readability and reusability refactor(main.ts): extract updateFileContent method to improve code readability and reusability refactor(main.ts): extract readFileAndProcessFrontMatter method to improve code readability and reusability --- src/main.ts | 65 ++++++++++++++++++++++++++++------------------------- 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/src/main.ts b/src/main.ts index 9288373..62b0cf5 100755 --- a/src/main.ts +++ b/src/main.ts @@ -4,7 +4,7 @@ import { FrontMatterCache, MarkdownView, Notice, Plugin, stringifyYaml, TFile } import { DEFAULT_SETTINGS, SteemitSettingTab } from './settings'; import { SteemitClient } from './steemit-client'; -import { SteemitPluginSettings, SteemitPost } from './types'; +import { SteemitPluginSettings, SteemitPost, SteemitPostOptions } from './types'; import { SubmitConfirmModal } from './ui/submit_confirm_modal'; import { createNewFrontMatter, @@ -124,44 +124,49 @@ export default class SteemitPlugin extends Plugin { } new SubmitConfirmModal(this, post, async (post, postOptions) => { - if (this.client) { - try { - const response = await this.client.publishPost(post, postOptions); - await this.updateFileContent(post); - new Notice(`Post published successfully! ${response.id}`); - } catch (err: any) { - console.warn(err); - new Notice(err.toString()); - } - } + await this.publishAndUpdate(post, postOptions, activeView.file!); }).open(); } catch (e: any) { new Notice(e.toString()); } } - async updateFileContent(post: SteemitPost) { - try { - const file = this.getActiveView().file!; + async publishAndUpdate(post: SteemitPost, postOptions: SteemitPostOptions, file: TFile) { + if (this.client) { + try { + const response = await this.client.publishPost(post, postOptions); + await this.updateFileContent(post, file); + new Notice(`Post published successfully! ${response.id}`); + } catch (err: any) { + console.warn(err); + new Notice(err.toString()); + } + } + } - const fileContent = await this.app.vault.read(file!); - const frontMatter = await this.processFrontMatter(file!); + async updateFileContent(post: SteemitPost, file: TFile) { + const { frontMatter, contentBody } = await this.readFileAndProcessFrontMatter(file); + const newFileContent = this.createNewFileContent(frontMatter, contentBody, post); + return await this.app.vault.modify(file, newFileContent); + } - const contentBody = extractContentBody(fileContent); - const newFrontMatter = createNewFrontMatter(frontMatter, { - category: post.category, - title: post.title, - permlink: post.permlink, - tags: post.tags, - }); + createNewFileContent(frontMatter: any, contentBody: string, post: SteemitPost): string { + const newFrontMatter = createNewFrontMatter(frontMatter, { + category: post.category, + title: post.title, + permlink: post.permlink, + tags: post.tags, + }); - const newFileContent = `---\n${stringifyYaml(newFrontMatter)}---\n${contentBody}`; + return `---\n${stringifyYaml(newFrontMatter)}---\n${contentBody}`; + } - // 전체 내용을 파일에 쓴다. - return await this.app.vault.modify(file, newFileContent); - } catch (err: any) { - console.warn(err); - new Notice(err.toString()); - } + async readFileAndProcessFrontMatter( + file: TFile, + ): Promise<{ fileContent: string; frontMatter: FrontMatterCache; contentBody: string }> { + const fileContent = await this.app.vault.read(file); + const frontMatter = await this.processFrontMatter(file); + const contentBody = extractContentBody(fileContent); + return { fileContent, frontMatter, contentBody }; } }