Skip to content

Commit

Permalink
fix site issue and description issue
Browse files Browse the repository at this point in the history
  • Loading branch information
sunwei committed Jan 6, 2025
1 parent 04e0562 commit d928f11
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 92 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.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",
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.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": {
Expand Down
13 changes: 3 additions & 10 deletions src/fileinfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

// 获取文件内容
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -165,6 +158,6 @@ export class FileInfo {
}

getDescription(): string {
return this.content;
return this.content.replace(/[\r\n]+/g, ' ');
}
}
154 changes: 76 additions & 78 deletions src/hugoverse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export class Hugoverse {

// 创建站点,进度设置为 10%
const siteId = await this.createSite();
if (siteId === "") {
if (siteId === "" || siteId === undefined) {
return "";
}
callback(10); // 进度更新为10%
Expand Down Expand Up @@ -205,45 +205,45 @@ export class Hugoverse {
}

async sendSiteRequest(action: string, siteId: string): Promise<string> {
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<string> {
Expand All @@ -256,51 +256,49 @@ export class Hugoverse {


async createSite(): Promise<string> {
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<string> {
Expand Down
4 changes: 3 additions & 1 deletion src/svelte/BuildDeploy.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
<div class="card is-selected">
<p>You can preview your site by clicking the link below:</p>
<a href={previewLink} target="_blank">{previewLink}</a>
<p class="dns-info">Please note that it may take a few minutes for the DNS to propagate and become active.</p>
</div>
{/if}

Expand All @@ -122,10 +123,11 @@
</div>

<!-- 部署完成后显示链接 -->
{#if deploySuccess}
{#if deploySuccess && previewFilename === fileInfo.name}
<div class="card is-selected">
<p>Congratulations! Your site has been deployed. Click the link below to view it:</p>
<a href={deployLink} target="_blank">{deployLink}</a>
<p class="dns-info">Please note that it may take a few minutes for the DNS to propagate and become active.</p>
</div>
{/if}

Expand Down
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit d928f11

Please sign in to comment.