Skip to content

Commit

Permalink
support customize domain
Browse files Browse the repository at this point in the history
  • Loading branch information
sunwei committed Dec 9, 2024
1 parent 78754a3 commit 7acdf39
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 21 deletions.
2 changes: 1 addition & 1 deletion esbuild.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ if you want to view the source, please visit the github repository of this plugi
`

const prod = process.argv[2] === 'production'
const outputFile = prod ? 'main.js' : '/Users/sunwei/Documents/Obsidian Vault/.obsidian/plugins/obsidian-friday-plugin/main.js';
const outputFile = prod ? 'main.js' : '/Users/sunwei/Documents/Obsidian/.obsidian/plugins/mdfriday/main.js';

esbuild
.build({
Expand Down
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.8",
"version": "0.2.0",
"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.8",
"version": "0.2.0",
"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
6 changes: 5 additions & 1 deletion src/hugoverse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ export class Hugoverse {
}
callback(100); // 预览完成,进度达到100%

const modal = new WebPreviewModal(this.app, preUrl); // 创建一个 WebPreviewModal 实例
const newUrl = preUrl.replace("app.mdfriday.com", "netlify.app");
const modal = new WebPreviewModal(this.app, newUrl); // 创建一个 WebPreviewModal 实例
modal.open();

return preUrl;
Expand All @@ -213,6 +214,9 @@ export class Hugoverse {
// 创建 FormData 实例并添加 siteId 字段
let body = new FormData();
body.append("site", `${siteId}`);
body.append("domain", this.plugin.settings.rootDomain);
body.append("host_name", "Netlify");
body.append("host_token", this.plugin.settings.netlifyToken);

// 将 FormData 转换为 ArrayBuffer
const boundary = "----WebKitFormBoundary" + Math.random().toString(36).substring(2, 9);
Expand Down
70 changes: 54 additions & 16 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,25 @@ interface FridaySettings {
username: string;
password: string;
userToken: string;

rootDomain: string
netlifyToken: string
}

const DEFAULT_SETTINGS: FridaySettings = {
username: '',
password: '',
userToken: '',
rootDomain: '',
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';

const FRIDAY_ROOT_FOLDER = 'MDFriday';

export default class FridayPlugin extends Plugin {
settings: FridaySettings;
statusBar: HTMLElement
Expand Down Expand Up @@ -75,12 +82,10 @@ export default class FridayPlugin extends Plugin {
}

async newNote(folder?: TFolder) {
const targetFolder = folder
? folder
: this.app.fileManager.getNewFileParent(this.app.workspace.getActiveFile()?.path || '');
await this.ensureRootFolderExists();

try {
const fNote: TFile = await this.createUniqueMarkdownFile(targetFolder.path, 'Untitled Friday Site');
const fNote: TFile = await this.createUniqueMarkdownFile(FRIDAY_ROOT_FOLDER, 'Untitled Friday Site');

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

async ensureRootFolderExists() {
if (!(await this.app.vault.adapter.exists(FRIDAY_ROOT_FOLDER))) {
await this.app.vault.createFolder(FRIDAY_ROOT_FOLDER);
}
}

async createUniqueMarkdownFile(targetFolder: string, baseFileName: string): Promise<TFile> {
let fileIndex = 0;
let newFile: TFile | null = null;
Expand Down Expand Up @@ -133,12 +144,12 @@ class FridaySettingTab extends PluginSettingTab {

containerEl.empty();

const { username, password, userToken } = this.plugin.settings;
const {username, password, userToken} = this.plugin.settings;

if (userToken) {
// 用户已登录的界面
containerEl.createEl("h2", { text: "Welcome Back!" });
containerEl.createEl("p", { text: `Logged in as: ${username}` });
containerEl.createEl("h2", {text: "Welcome Back!"});
containerEl.createEl("p", {text: `Logged in as: ${username}`});

new Setting(containerEl)
.addButton((button) =>
Expand All @@ -152,8 +163,8 @@ class FridaySettingTab extends PluginSettingTab {
);
} else {
// 用户未登录的界面
containerEl.createEl("h2", { text: "Welcome!" });
containerEl.createEl("p", { text: "Please enter your credentials." });
containerEl.createEl("h2", {text: "Welcome!"});
containerEl.createEl("p", {text: "Please enter your credentials."});

// Email 输入框
new Setting(containerEl)
Expand Down Expand Up @@ -195,15 +206,42 @@ class FridaySettingTab extends PluginSettingTab {
this.display(); // 刷新界面
})
).addButton((button) =>
button
.setButtonText("Login")
.setCta()
.onClick(async () => {
await this.plugin.user.login(); // 处理登录逻辑
this.display(); // 刷新界面
})
button
.setButtonText("Login")
.setCta()
.onClick(async () => {
await this.plugin.user.login(); // 处理登录逻辑
this.display(); // 刷新界面
})
);
}

containerEl.createEl("h2", {text: "Deployment"});
new Setting(containerEl)
.setName("Root Domain")
.setDesc("Set your custom root domain (e.g., mdfriday.com). This will be used for generated links.")
.addText(text =>
text
.setPlaceholder("Enter your root domain")
.setValue(this.plugin.settings.rootDomain || "")
.onChange(async (value) => {
this.plugin.settings.rootDomain = value;
await this.plugin.saveSettings();
})
);

new Setting(containerEl)
.setName("Netlify Token")
.setDesc("Set your Netlify personal access token here.")
.addText(text =>
text
.setPlaceholder("Enter your Netlify Token")
.setValue(this.plugin.settings.netlifyToken || "")
.onChange(async (value) => {
this.plugin.settings.netlifyToken = value;
await this.plugin.saveSettings();
})
);

}
}
31 changes: 31 additions & 0 deletions src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,46 @@ export class WebPreviewModal extends Modal {
// Apply CSS class to modal
modalEl.classList.add('friday-preview-modal');

this.showLoadingMessage()
setTimeout(() => {
this.loadIframe();
}, 1000);
}

showLoadingMessage() {
const { contentEl } = this;
contentEl.empty(); // 清空内容

const loadingMessage = document.createElement('p');
loadingMessage.textContent = "Loading...";
loadingMessage.classList.add('friday-preview-loading');
contentEl.appendChild(loadingMessage);
}

loadIframe() {
const { contentEl } = this;

// Create and style the iframe with a CSS class
const iframe = document.createElement('iframe');
iframe.src = this.url;
iframe.classList.add('friday-preview-iframe');
iframe.onload = () => {
setTimeout(() => {
this.onIframeLoaded(contentEl, iframe);
}, 500);
};

// Insert the iframe into the modal content
contentEl.appendChild(iframe);
}

onIframeLoaded(contentEl: HTMLElement, iframe: HTMLIFrameElement) {
// iframe 加载完成后,隐藏加载提示
const loadingMessage = contentEl.querySelector('.friday-preview-loading');
if (loadingMessage) {
loadingMessage.remove(); // 移除加载提示
}
}

onClose() {
const {contentEl} = this;
Expand Down
8 changes: 8 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@
border: none;
background-color: white;
}

.friday-preview-loading {
text-align: center;
margin-top: 20px;
font-size: 18px;
color: black;
height: 100%;
}
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"0.1.5": "0.15.0",
"0.1.6": "0.15.0",
"0.1.7": "0.15.0",
"0.1.8": "0.15.0"
"0.1.8": "0.15.0",
"0.2.0": "0.15.0"
}

0 comments on commit 7acdf39

Please sign in to comment.