Skip to content

Commit

Permalink
Refactor file content update logic
Browse files Browse the repository at this point in the history
  • Loading branch information
anpigon committed Jan 24, 2024
1 parent eb19767 commit 925a4fd
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,18 @@ export default class SteemitPlugin extends Plugin {
this.client = new SteemitClient(this.settings.username, this.settings.password);

const activeView = this.getActiveView();
const post = await this.parsePostData(activeView.file!);
const file = activeView.file;
if (!file) {
throw new Error('There is no active file.');
}

const post = await this.parsePostData(file);
if (!post.body) {
throw new Error('Content is empty.');
}

new SubmitConfirmModal(this, post, async (post, postOptions) => {
await this.publishAndUpdate(post, postOptions, activeView.file!);
await this.publishAndUpdate(post, postOptions, file);
}).open();
} catch (e: any) {
new Notice(e.toString());
Expand All @@ -208,28 +213,17 @@ export default class SteemitPlugin extends Plugin {
}

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);
}

createNewFileContent(frontMatter: any, contentBody: string, post: SteemitPost): string {
const fileContent = await this.app.vault.read(file);
const frontMatter = await this.processFrontMatter(file);
const contentBody = extractContentBody(fileContent);
const newFrontMatter = createNewFrontMatter(frontMatter, {
category: post.category,
title: post.title,
permlink: post.permlink,
tags: post.tags,
});

return `---\n${stringifyYaml(newFrontMatter)}---\n${contentBody}`;
}

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 };
const newFileContent = `---\n${stringifyYaml(newFrontMatter)}---\n${contentBody}`;
return await this.app.vault.modify(file, newFileContent);
}
}

0 comments on commit 925a4fd

Please sign in to comment.