Skip to content

Commit

Permalink
modify: follow obsidian best practices
Browse files Browse the repository at this point in the history
  • Loading branch information
sunwei committed Nov 13, 2024
1 parent 572fa9b commit 0a4022f
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 98 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "mdfriday",
"name": "Friday",
"version": "0.1.1",
"version": "0.1.2",
"minAppVersion": "0.15.0",
"description": "Notes to Website. Friday helps you turn Markdown documents into websites in minutes.",
"author": "sunwei",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-friday-plugin",
"version": "0.1.1",
"version": "0.1.2",
"description": "Friday is an Obsidian plugin that empowers users to focus on content creation by writing Markdown files, while we handle the distribution. From creating websites to content deployment, Friday serves as a creative output assistant, helping users turn their work into publishable sites with ease.",
"main": "main.js",
"scripts": {
Expand Down
2 changes: 0 additions & 2 deletions src/fileinfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export class FileInfo {
this.content = this.extractContentWithoutFrontmatter(fileContent, this.frontMatter); // 存储去掉 frontmatter 的内容

callback(this); // 通知外部异步操作已完成
console.log("fileInfo updated and callback");
} else {
callback(this); // 没有文件的情况,直接回调
}
Expand Down Expand Up @@ -100,7 +99,6 @@ export class FileInfo {
hasContentConfigured(): boolean {
const contentPath = this.getContentFolder();
if (contentPath !== FM_CONTENT_EMPTY && contentPath !== null && this.isContentFolderExists) {
console.log("has content configured: ", contentPath);
return true
}
return false
Expand Down
29 changes: 5 additions & 24 deletions src/hugoverse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ export class Hugoverse {
projectDirPath(filepath: string): string {
const projDirPath = path.join(path.dirname(filepath), "MDFriday");

console.log("MDFriday projectDirPath: ", projDirPath);

return projDirPath;
}

Expand All @@ -50,7 +48,6 @@ export class Hugoverse {
if (await this.plugin.app.vault.adapter.exists(manifestPath)) {
const data = await this.app.vault.adapter.read(manifestPath);
this.manifestConfig = JSON.parse(data);
console.log('Manifest configuration loaded successfully.');
}
} catch (error) {
console.error('Failed to load manifest.json', error);
Expand Down Expand Up @@ -190,8 +187,6 @@ export class Hugoverse {
// 定义请求的URL
const url = `${this.apiUrl}/api/${action}?type=Site&id=${siteId}`;

console.log(`${action} URL: `, url);

// 创建 FormData 实例并添加 siteId 字段
let body = new FormData();
body.append("site", `${siteId}`);
Expand All @@ -218,10 +213,7 @@ export class Hugoverse {

// 解析返回的 JSON 数据,提取 ID
const responseData = JSON.parse(response.text);
const actionUrl = responseData.data[0];
console.log(`Site ${action} generated successfully. URL:`, actionUrl);

return actionUrl;
return responseData.data[0];
} catch (error) {
console.error(`Error generating site ${action}:`, error);
new Notice(`Failed to ${action} site.`, 5000);
Expand Down Expand Up @@ -252,7 +244,6 @@ export class Hugoverse {
body.append("Params", this.plugin.fileInfo.getParams());
body.append("working_dir", "");

console.log("create site url: ", createSiteUrl);
// 将 FormData 转换为 ArrayBuffer
const boundary = "----WebKitFormBoundary" + Math.random().toString(36).substring(2, 9);
const arrayBufferBody = await this.formDataToArrayBuffer(body, boundary);
Expand All @@ -276,8 +267,6 @@ export class Hugoverse {
const responseData = JSON.parse(response.text);
const siteId = responseData.data[0].id; // 假设`data`数组的第一个元素包含所需的`id`

console.log("Site created successfully. Site ID:", siteId);

return siteId;
} catch (error) {
console.error("Failed to create site:", error.toString());
Expand All @@ -302,7 +291,6 @@ export class Hugoverse {
body.append("post", `/api/content?type=Post&id=${postId}`);
body.append("path", path);

console.log("create site url: ", url);
// 将 FormData 转换为 ArrayBuffer
const boundary = "----WebKitFormBoundary" + Math.random().toString(36).substring(2, 9);
const arrayBufferBody = await this.formDataToArrayBuffer(body, boundary);
Expand All @@ -324,10 +312,8 @@ export class Hugoverse {

// 解析返回的 JSON 数据,提取 ID
const responseData = JSON.parse(response.text);
const sitePostId = responseData.data[0].id; // 假设`data`数组的第一个元素包含所需的`id`
console.log("Site post created successfully. SitePost Id: ", sitePostId);

return sitePostId;
// 假设`data`数组的第一个元素包含所需的`id`
return responseData.data[0].id;
} catch (error) {
console.error("Error creating site post:", error);
new Notice("Failed to create site post.", 5000);
Expand Down Expand Up @@ -412,7 +398,6 @@ export class Hugoverse {
imageResults.forEach((result, index) => {
if (result) {
body.append(`assets.${index}`, result.blob, result.imagePath);
console.log("image index:", index, result.imagePath, "Size:", result.blob.size);
}
});

Expand All @@ -432,17 +417,13 @@ export class Hugoverse {

// 检查响应状态
if (response.status !== 200) {
console.log("create post response: ", response.text, file.name);
throw new Error(`Post creation failed: ${response.text}`);
}

// 解析返回的 JSON 数据,提取 ID
const responseData = JSON.parse(response.text);
const postId = responseData.data[0].id; // 假设`data`数组的第一个元素包含所需的`id`

console.log("Post created successfully. Post ID:", postId, file.name);

return postId;
// 假设`data`数组的第一个元素包含所需的`id`
return responseData.data[0].id;
} catch (error) {
console.error("Failed to create post:", error.toString());
new Notice("Failed to create post.", 5000);
Expand Down
68 changes: 41 additions & 27 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import {App, Notice, Plugin, TFile, TFolder, PluginSettingTab, Setting, FileSystemAdapter} from 'obsidian';
import { getDefaultFrontMatter } from './frontmatter';
import ServerView, { FRIDAY_SERVER_VIEW_TYPE } from './server';
import {App, Notice, Plugin, PluginSettingTab, Setting, TFile, TFolder} from 'obsidian';
import {getDefaultFrontMatter} from './frontmatter';
import ServerView, {FRIDAY_SERVER_VIEW_TYPE} from './server';
import {User} from "./user";
import {Hugoverse} from "./hugoverse";
import {FileInfo} from "./fileinfo";

interface MyPluginSettings {
mySetting: string;
interface FridaySettings {
netlifyToken: string;
}

const DEFAULT_SETTINGS: MyPluginSettings = {
mySetting: 'default'
const DEFAULT_SETTINGS: FridaySettings = {
netlifyToken: ''
}

export const FRIDAY_ICON = 'dice-5';
export const API_URL_DEV = 'http://127.0.0.1:1314';
export const API_URL_PRO = 'https://mdfriday.sunwei.xyz';

export default class FridayPlugin extends Plugin {
settings: MyPluginSettings;
settings: FridaySettings;
statusBar: HTMLElement

fileInfo: FileInfo;
Expand All @@ -37,16 +37,14 @@ export default class FridayPlugin extends Plugin {

this.statusBar = this.addStatusBarItem();

this.addSettingTab(new SampleSettingTab(this.app, this));
this.addSettingTab(new FridaySettingTab(this.app, this));

this.registerView(FRIDAY_SERVER_VIEW_TYPE, leaf => new ServerView(leaf, this))
if (this.app.workspace.layoutReady) this.initLeaf()
else this.app.workspace.onLayoutReady(() => this.initLeaf())
}

async initFriday(): Promise<void> {
console.log("Init Friday...")

this.fileInfo = new FileInfo()
this.apiUrl = process.env.NODE_ENV === 'production' ? API_URL_PRO : API_URL_DEV;
this.user = new User(this);
Expand Down Expand Up @@ -82,10 +80,7 @@ export default class FridayPlugin extends Plugin {
: this.app.fileManager.getNewFileParent(this.app.workspace.getActiveFile()?.path || '');

try {
const fNote: TFile = await (this.app.fileManager as any).createNewMarkdownFile(
targetFolder,
'Untitled Friday Site'
);
const fNote: TFile = await this.createUniqueMarkdownFile(targetFolder.path, 'Untitled Friday Site');

await this.app.vault.modify(fNote, getDefaultFrontMatter());
await this.app.workspace.getLeaf().openFile(fNote);
Expand All @@ -95,18 +90,37 @@ export default class FridayPlugin extends Plugin {
}
}

async status(text: string) {
this.statusBar.setText(text)
async createUniqueMarkdownFile(targetFolder: string, baseFileName: string): Promise<TFile> {
let fileIndex = 0;
let newFile: TFile | null = null;

while (!newFile) {
// 动态生成文件名:如 Untitled Friday Site, Untitled Friday Site 1, Untitled Friday Site 2
const fileName = fileIndex === 0 ? `${baseFileName}.md` : `${baseFileName} ${fileIndex}.md`;
const filePath = `${targetFolder}/${fileName}`;

try {
// 尝试创建文件
newFile = await this.app.vault.create(filePath, ''); // 创建空文件
} catch (error) {
// 如果文件已存在,则递增 fileIndex 并重试
if (error.message.includes("File already exists")) {
fileIndex++;
} else {
throw error; // 其他错误直接抛出
}
}
}

return newFile;
}

async loadUser() {
const response = await fetch('https://api.github.com/users/mdfriday');
const data = await response.json();
return data
async status(text: string) {
this.statusBar.setText(text)
}
}

class SampleSettingTab extends PluginSettingTab {
class FridaySettingTab extends PluginSettingTab {
plugin: FridayPlugin;

constructor(app: App, plugin: FridayPlugin) {
Expand All @@ -120,13 +134,13 @@ class SampleSettingTab extends PluginSettingTab {
containerEl.empty();

new Setting(containerEl)
.setName('Setting #1')
.setDesc('It\'s a secret')
.setName('Netlify Token')
.setDesc('Deploy to your own account')
.addText(text => text
.setPlaceholder('Enter your secret')
.setValue(this.plugin.settings.mySetting)
.setPlaceholder('Enter your netlify token')
.setValue(this.plugin.settings.netlifyToken)
.onChange(async (value) => {
this.plugin.settings.mySetting = value;
this.plugin.settings.netlifyToken = value;
await this.plugin.saveSettings();
}));
}
Expand Down
31 changes: 10 additions & 21 deletions src/preview.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Modal, App } from 'obsidian';
import {Modal, App} from 'obsidian';

export class WebPreviewModal extends Modal {
private readonly url: string;
Expand All @@ -9,34 +9,23 @@ export class WebPreviewModal extends Modal {
}

onOpen() {
const { contentEl, modalEl } = this;
const {contentEl, modalEl} = this;

// 设置 Modal 的宽度和高度
modalEl.style.width = '900px';
modalEl.style.height = `${window.innerHeight * 0.9}px`; // 设置 Modal 高度为屏幕的 90%
// Apply CSS class to modal
modalEl.classList.add('my-modal');

// 创建 iframe 元素
// Create and style the iframe with a CSS class
const iframe = document.createElement('iframe');
iframe.src = this.url; // 设置 iframe 的链接
iframe.src = this.url;
iframe.classList.add('my-iframe');

// 设置 iframe 的样式
iframe.style.width = '100%'; // 让 iframe 填满 Modal 的宽度
iframe.style.height = '100%'; // 让 iframe 填满 Modal 的高度
iframe.style.border = 'none'; // 去除边框
iframe.style.backgroundColor = 'white'; // 设置背景色为白色

// 将 iframe 插入到 modal 内容中
// Insert the iframe into the modal content
contentEl.appendChild(iframe);

// 动态调整 iframe 的高度
window.addEventListener('resize', () => {
modalEl.style.height = `${window.innerHeight * 0.9}px`; // 监听窗口调整事件,动态调整 Modal 高度
iframe.style.height = `${window.innerHeight * 0.9}px`; // 同时调整 iframe 的高度
});
}


onClose() {
const { contentEl } = this;
const {contentEl} = this;
contentEl.empty(); // 关闭时清空内容
}
}
5 changes: 2 additions & 3 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ItemView, WorkspaceLeaf} from 'obsidian';
import {ItemView, WorkspaceLeaf, TFile} from 'obsidian';
import FridayPlugin, {FRIDAY_ICON} from "./main";
import Server from './svelte/Server.svelte';
import {FileInfo} from "./fileinfo";
Expand Down Expand Up @@ -46,7 +46,7 @@ export default class ServerView extends ItemView {
async onOpen(): Promise<void> {
await this.updateFileInfo(); // 初始化时获取 fileInfo
this._app = new Server({
target: (this as any).contentEl,
target: this.contentEl,
props: {
fileInfo: this.plugin.fileInfo,
app: this.app,
Expand All @@ -61,7 +61,6 @@ export default class ServerView extends ItemView {
await fileInfo.updateFileInfo(this.app, (updatedFileInfo) => {
this.plugin.fileInfo = updatedFileInfo; // 更新 fileInfo
if (this._app) {
// 重新设置 Svelte 组件的 props
this._app.$set({ fileInfo: this.plugin.fileInfo });
}
});
Expand Down
Loading

0 comments on commit 0a4022f

Please sign in to comment.