Skip to content

Commit

Permalink
fix(main.ts): import SteemitPostOptions from types to fix missing typ…
Browse files Browse the repository at this point in the history
…e 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
  • Loading branch information
anpigon committed Mar 29, 2024
1 parent 1cd7391 commit b2490cc
Showing 1 changed file with 35 additions and 30 deletions.
65 changes: 35 additions & 30 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 };
}
}

0 comments on commit b2490cc

Please sign in to comment.