diff --git a/manifest.json b/manifest.json index 312cf0d..b2b8056 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "mdfriday", "name": "Friday", - "version": "0.2.1", + "version": "0.2.2", "minAppVersion": "0.15.0", "description": "Notes to Website. Friday helps you turn Markdown documents into websites in minutes.", "author": "sunwei", diff --git a/package.json b/package.json index 66872f3..913328e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "obsidian-friday-plugin", - "version": "0.2.1", + "version": "0.2.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": { diff --git a/src/fileinfo.ts b/src/fileinfo.ts index a3ee697..d189b9d 100644 --- a/src/fileinfo.ts +++ b/src/fileinfo.ts @@ -36,11 +36,7 @@ export class FileInfo { const metadata = app.metadataCache.getFileCache(activeFile); this.frontMatter = metadata?.frontmatter ?? null; - if (await app.vault.adapter.exists(this.getContentFolder())) { - this.isContentFolderExists = true - } else { - this.isContentFolderExists = false - } + this.isContentFolderExists = await app.vault.adapter.exists(this.getContentFolder()); this.isReadyForBuild = this.hasFridayPluginEnabled() && this.hasThemeConfigured() && this.hasContentConfigured() // 获取文件内容 @@ -106,10 +102,7 @@ export class FileInfo { hasContentConfigured(): boolean { const contentPath = this.getContentFolder(); - if (contentPath !== FM_CONTENT_EMPTY && contentPath !== null && this.isContentFolderExists) { - return true - } - return false + return contentPath !== FM_CONTENT_EMPTY && contentPath !== null && this.isContentFolderExists; } getContentFolder(): string { @@ -165,6 +158,6 @@ export class FileInfo { } getDescription(): string { - return this.content; + return this.content.replace(/[\r\n]+/g, ' '); } } diff --git a/src/hugoverse.ts b/src/hugoverse.ts index 96dd5af..10e09b3 100644 --- a/src/hugoverse.ts +++ b/src/hugoverse.ts @@ -107,7 +107,7 @@ export class Hugoverse { // 创建站点,进度设置为 10% const siteId = await this.createSite(); - if (siteId === "") { + if (siteId === "" || siteId === undefined) { return ""; } callback(10); // 进度更新为10% @@ -205,45 +205,45 @@ export class Hugoverse { } async sendSiteRequest(action: string, siteId: string): Promise { - try { - // 定义请求的URL - const url = `${this.apiUrl}/api/${action}?type=Site&id=${siteId}`; - - // 创建 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); - const arrayBufferBody = await this.formDataToArrayBuffer(body, boundary); - - // 发送请求 - const response: RequestUrlResponse = await requestUrl({ - url: url, - method: "POST", - headers: { - "Authorization": `Bearer ${await this.user.getToken()}`, - "Content-Type": `multipart/form-data; boundary=${boundary}`, - }, - body: arrayBufferBody, - }); + // 定义请求的URL + const url = `${this.apiUrl}/api/${action}?type=Site&id=${siteId}`; + + // 创建 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); + const arrayBufferBody = await this.formDataToArrayBuffer(body, boundary); + + // 发送请求 + const response: RequestUrlResponse = await requestUrl({ + url: url, + method: "POST", + headers: { + "Authorization": `Bearer ${await this.user.getToken()}`, + "Content-Type": `multipart/form-data; boundary=${boundary}`, + }, + body: arrayBufferBody, + }); - // 检查响应状态 - if (response.status !== 200) { - throw new Error(`Failed to ${action} site: ${response.text}`); + // 检查响应状态 + if (response.status !== 200) { + let errMsg = `Failed to ${action} site.`; + if (response.status === 409) { + errMsg = "Domain is already taken. Please choose a different one by changing the note name."; } - - // 解析返回的 JSON 数据,提取 ID - return response.json.data[0]; - } catch (error) { - console.error(`Error generating site ${action}:`, error); - new Notice(`Failed to ${action} site.`, 5000); + console.error(`Error generating site ${action}:`, response.text); + new Notice(errMsg, 5000); return ""; } + + // 解析返回的 JSON 数据,提取 ID + return response.json.data[0]; } async previewSite(siteId: string): Promise { @@ -256,51 +256,49 @@ export class Hugoverse { async createSite(): Promise { - try { - const createSiteUrl = `${this.apiUrl}/api/content?type=Site`; - - let body: FormData = new FormData(); - body.append("title", this.plugin.fileInfo.getBaseName()); - body.append("description", this.plugin.fileInfo.getDescription()); - body.append("base_url", "/"); - body.append("theme", this.plugin.fileInfo.getThemeName()); - body.append("owner", this.plugin.user.getName()); - body.append("Params", this.plugin.fileInfo.getParams()); - body.append("working_dir", ""); - - this.plugin.fileInfo.languages.forEach((lang, index) => { - body.append(`languages.${index}`, lang); - }); - this.plugin.fileInfo.menus.forEach((menu, index) => { - body.append(`menus.${index}`, menu); - }) - - // 将 FormData 转换为 ArrayBuffer - const boundary = "----WebKitFormBoundary" + Math.random().toString(36).substring(2, 9); - const arrayBufferBody = await this.formDataToArrayBuffer(body, boundary); - - const response: RequestUrlResponse = await requestUrl({ - url: createSiteUrl, - method: "POST", - headers: { - "Authorization": `Bearer ${await this.user.getToken()}`, - "Content-Type": `multipart/form-data; boundary=${boundary}`, - }, - body: arrayBufferBody, - }); - - // 检查响应状态 - if (response.status !== 200) { - throw new Error(`Site creation failed: ${response.text}`); - } + const createSiteUrl = `${this.apiUrl}/api/content?type=Site`; + + let body: FormData = new FormData(); + body.append("title", this.plugin.fileInfo.getBaseName()); + body.append("description", this.plugin.fileInfo.getDescription()); + body.append("base_url", "/"); + body.append("theme", this.plugin.fileInfo.getThemeName()); + body.append("owner", this.plugin.user.getName()); + body.append("Params", this.plugin.fileInfo.getParams()); + body.append("working_dir", ""); + + this.plugin.fileInfo.languages.forEach((lang, index) => { + body.append(`languages.${index}`, lang); + }); + this.plugin.fileInfo.menus.forEach((menu, index) => { + body.append(`menus.${index}`, menu); + }) + + // 将 FormData 转换为 ArrayBuffer + const boundary = "----WebKitFormBoundary" + Math.random().toString(36).substring(2, 9); + const arrayBufferBody = await this.formDataToArrayBuffer(body, boundary); + + const response: RequestUrlResponse = await requestUrl({ + url: createSiteUrl, + method: "POST", + headers: { + "Authorization": `Bearer ${await this.user.getToken()}`, + "Content-Type": `multipart/form-data; boundary=${boundary}`, + }, + body: arrayBufferBody, + }); - // 解析返回的 JSON 数据,提取 ID - // 假设`data`数组的第一个元素包含所需的`id` - return response.json.data[0].id; - } catch (error) { - console.error("Failed to create site:", error.toString()); - new Notice("Failed to create site.", 5000); + // 检查响应状态 + if (response.status !== 200) { + const err = new Error(`Site creation failed: ${response.text}`); + console.error("Failed to create site:", err.toString()); + new Notice(err.toString(), 5000); + return ""; } + + // 解析返回的 JSON 数据,提取 ID + // 假设`data`数组的第一个元素包含所需的`id` + return response.json.data[0].id; } async createSitePost(siteId: string, postId: string, file: TFile): Promise { diff --git a/src/svelte/BuildDeploy.svelte b/src/svelte/BuildDeploy.svelte index 0f384f6..e68ced2 100644 --- a/src/svelte/BuildDeploy.svelte +++ b/src/svelte/BuildDeploy.svelte @@ -104,6 +104,7 @@

You can preview your site by clicking the link below:

{previewLink} +

Please note that it may take a few minutes for the DNS to propagate and become active.

{/if} @@ -122,10 +123,11 @@ -{#if deploySuccess} +{#if deploySuccess && previewFilename === fileInfo.name}

Congratulations! Your site has been deployed. Click the link below to view it:

{deployLink} +

Please note that it may take a few minutes for the DNS to propagate and become active.

{/if} diff --git a/versions.json b/versions.json index e5c3b49..ada3b66 100644 --- a/versions.json +++ b/versions.json @@ -4,5 +4,6 @@ "0.1.7": "0.15.0", "0.1.8": "0.15.0", "0.2.0": "0.15.0", - "0.2.1": "0.15.0" + "0.2.1": "0.15.0", + "0.2.2": "0.15.0" } \ No newline at end of file