Skip to content

Commit

Permalink
fix(main.ts): import missing functions from utils file to avoid undef…
Browse files Browse the repository at this point in the history
…ined errors

fix(main.ts): handle case when activeView or activeView.file is null in getActiveView() function
fix(main.ts): handle error when publishing post and display error message
feat(main.ts): update file content with new front matter and body after publishing post
fix(utils.ts): import FrontMatterCache type from obsidian to fix type error
feat(utils.ts): add functions to remove front matter, extract content body, and create new front matter
  • Loading branch information
anpigon committed Mar 29, 2024
1 parent 236c198 commit 1cd7391
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 16 deletions.
41 changes: 25 additions & 16 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ import { DEFAULT_SETTINGS, SteemitSettingTab } from './settings';
import { SteemitClient } from './steemit-client';
import { SteemitPluginSettings, SteemitPost } from './types';
import { SubmitConfirmModal } from './ui/submit_confirm_modal';
import { makeDefaultPermlink, removeObsidianComments, stripFrontmatter } from './utils';
import {
createNewFrontMatter,
extractContentBody,
makeDefaultPermlink,
removeObsidianComments,
stripFrontmatter,
} from './utils';
export default class SteemitPlugin extends Plugin {
private _settings?: SteemitPluginSettings;
readonly appName = `${this.manifest.id}/${this.manifest.version}`;
Expand Down Expand Up @@ -97,7 +103,7 @@ export default class SteemitPlugin extends Plugin {

getActiveView() {
const activeView = this.app.workspace.getActiveViewOfType(MarkdownView);
if (!activeView) {
if (!activeView || !activeView.file) {
throw new Error('There is no editor view found.');
}
return activeView;
Expand All @@ -123,9 +129,9 @@ export default class SteemitPlugin extends Plugin {
const response = await this.client.publishPost(post, postOptions);
await this.updateFileContent(post);
new Notice(`Post published successfully! ${response.id}`);
} catch (e: any) {
console.warn(e);
new Notice(e.toString());
} catch (err: any) {
console.warn(err);
new Notice(err.toString());
}
}
}).open();
Expand All @@ -136,23 +142,26 @@ export default class SteemitPlugin extends Plugin {

async updateFileContent(post: SteemitPost) {
try {
const activeView = this.getActiveView();
if (!activeView || !activeView.file) {
throw new Error('There is no active file.');
}
const file = this.getActiveView().file!;

const fileContent = await this.app.vault.read(file!);
const frontMatter = await this.processFrontMatter(file!);

const frontMatter = stringifyYaml({
...(await this.processFrontMatter(activeView.file)),
category: post.category === '0' ? '' : post.category,
const contentBody = extractContentBody(fileContent);
const newFrontMatter = createNewFrontMatter(frontMatter, {
category: post.category,
title: post.title,
permlink: post.permlink,
tags: post.tags,
});

await this.app.vault.modify(activeView.file, `---\n${frontMatter}---\n${post.body}`);
} catch (ex: any) {
console.warn(ex);
new Notice(ex.toString());
const newFileContent = `---\n${stringifyYaml(newFrontMatter)}---\n${contentBody}`;

// 전체 내용을 파일에 쓴다.
return await this.app.vault.modify(file, newFileContent);
} catch (err: any) {
console.warn(err);
new Notice(err.toString());
}
}
}
39 changes: 39 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { FrontMatterCache } from 'obsidian';

export const frontmatterRegex = /^---\n(?:((?!---)(.|\n)*?)\n)?---(\n|$)/;

export function makeDefaultPermlink() {
Expand All @@ -14,3 +16,40 @@ export function stripFrontmatter(content: string) {
export function removeObsidianComments(content: string) {
return content.replace(/^\n?%%(.+?)%%\n?$/gms, '');
}

export const removeFrontmatter = (markdownContent: string) => {
const frontmatterRegex = /^---\s*\n([\s\S]*?)\n---\s*\n/;
const match = markdownContent.match(frontmatterRegex);

if (match) {
// Extract frontmatter and remove it from the content
const frontmatterString = match[0];
const contentWithoutFrontmatter = markdownContent.replace(frontmatterRegex, '');

return { frontmatterString, contentWithoutFrontmatter };
} else {
// No frontmatter found
return { frontmatterString: '', contentWithoutFrontmatter: markdownContent };
}
};

// 파일 내용에서 프론트매터를 제거하는 함수
export const extractContentBody = (fileContent: string): string => {
const { contentWithoutFrontmatter } = removeFrontmatter(fileContent);
return contentWithoutFrontmatter;
};

// 새로운 프론트매터를 생성하는 함수
export const createNewFrontMatter = (
existingFrontMatter: FrontMatterCache,
additions: FrontMatterCache,
): FrontMatterCache => {
const newFrontMatter = { ...existingFrontMatter, ...additions };
// 값이 없는 키 삭제
Object.entries(newFrontMatter).forEach(([key, value]) => {
if (!value) {
delete newFrontMatter[key];
}
});
return newFrontMatter;
};

0 comments on commit 1cd7391

Please sign in to comment.