2024-08-17 20:21:23 +02:00
|
|
|
import { readFile, mkdir, writeFile } from "node:fs/promises";
|
2024-08-04 16:15:09 +02:00
|
|
|
import { join } from "node:path";
|
|
|
|
|
import { md } from "$lib/utils";
|
2024-08-05 14:38:44 +02:00
|
|
|
import type { Actions, PageServerLoad } from "./$types";
|
2024-08-19 20:33:23 +02:00
|
|
|
import { API_BASE_PREFIX } from "$lib/server/utils";
|
2024-08-17 19:29:10 +02:00
|
|
|
import { render } from "svelte/server";
|
|
|
|
|
import BlogIndex from "$lib/templates/blog/BlogIndex.svelte";
|
|
|
|
|
import BlogArticle from "$lib/templates/blog/BlogArticle.svelte";
|
2024-08-18 18:17:59 +02:00
|
|
|
import DocsIndex from "$lib/templates/docs/DocsIndex.svelte";
|
2024-08-27 16:39:29 +02:00
|
|
|
import DocsArticle from "$lib/templates/docs/DocsArticle.svelte";
|
2024-08-19 20:33:23 +02:00
|
|
|
import { dev } from "$app/environment";
|
2024-08-03 18:07:27 +02:00
|
|
|
|
2024-09-07 13:04:09 +02:00
|
|
|
interface WebsiteData {
|
|
|
|
|
id: string;
|
|
|
|
|
content_type: "Blog" | "Docs";
|
|
|
|
|
favicon_image: string | null;
|
|
|
|
|
title: string;
|
|
|
|
|
logo_type: "text" | "image";
|
|
|
|
|
logo_text: string | null;
|
|
|
|
|
logo_image: string | null;
|
|
|
|
|
main_content: string;
|
|
|
|
|
additional_text: string;
|
|
|
|
|
accent_color_light_theme: string;
|
|
|
|
|
accent_color_dark_theme: string;
|
|
|
|
|
articles: {
|
|
|
|
|
cover_image: string | null;
|
|
|
|
|
title: string;
|
|
|
|
|
publication_date: string;
|
|
|
|
|
meta_description: string;
|
|
|
|
|
main_content: string;
|
|
|
|
|
}[];
|
|
|
|
|
categorized_articles: {
|
|
|
|
|
[key: string]: { title: string; publication_date: string; meta_description: string }[];
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-20 19:17:05 +02:00
|
|
|
export const load: PageServerLoad = async ({ params, fetch, cookies, parent }) => {
|
2024-08-14 19:33:41 +02:00
|
|
|
const websiteOverviewData = await fetch(
|
|
|
|
|
`${API_BASE_PREFIX}/website_overview?id=eq.${params.websiteId}`,
|
|
|
|
|
{
|
|
|
|
|
method: "GET",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
Authorization: `Bearer ${cookies.get("session_token")}`,
|
|
|
|
|
Accept: "application/vnd.pgrst.object+json"
|
|
|
|
|
}
|
2024-08-04 16:15:09 +02:00
|
|
|
}
|
2024-08-14 19:33:41 +02:00
|
|
|
);
|
2024-08-04 16:15:09 +02:00
|
|
|
|
|
|
|
|
const websiteOverview = await websiteOverviewData.json();
|
2024-08-20 19:17:05 +02:00
|
|
|
const { website } = await parent();
|
2024-08-04 16:15:09 +02:00
|
|
|
|
2024-08-07 19:13:39 +02:00
|
|
|
generateStaticFiles(websiteOverview);
|
2024-08-04 17:46:41 +02:00
|
|
|
|
2024-09-01 13:37:28 +02:00
|
|
|
const websitePreviewUrl = `${
|
|
|
|
|
dev
|
|
|
|
|
? "http://localhost:18000"
|
|
|
|
|
: process.env.ORIGIN
|
|
|
|
|
? process.env.ORIGIN
|
|
|
|
|
: "http://localhost:18000"
|
2024-09-07 18:22:58 +02:00
|
|
|
}/previews/${websiteOverview.id}/`;
|
2024-08-10 22:20:57 +02:00
|
|
|
|
2024-09-07 15:07:31 +02:00
|
|
|
const websiteProdUrl = dev
|
2024-09-07 18:22:58 +02:00
|
|
|
? `http://localhost:18000/${websiteOverview.id}/`
|
2024-09-07 15:07:31 +02:00
|
|
|
: process.env.ORIGIN
|
|
|
|
|
? process.env.ORIGIN.replace("//", `//${websiteOverview.id}.`)
|
2024-09-07 18:22:58 +02:00
|
|
|
: `http://localhost:18000/${websiteOverview.id}/`;
|
2024-09-07 15:07:31 +02:00
|
|
|
|
2024-08-04 17:46:41 +02:00
|
|
|
return {
|
2024-08-10 22:20:57 +02:00
|
|
|
websiteOverview,
|
2024-08-20 19:17:05 +02:00
|
|
|
websitePreviewUrl,
|
2024-09-07 15:07:31 +02:00
|
|
|
websiteProdUrl,
|
2024-08-20 19:17:05 +02:00
|
|
|
website
|
2024-08-04 17:46:41 +02:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2024-08-05 14:38:44 +02:00
|
|
|
export const actions: Actions = {
|
2024-08-27 16:39:29 +02:00
|
|
|
publishWebsite: async ({ fetch, params, cookies }) => {
|
|
|
|
|
const websiteOverviewData = await fetch(
|
|
|
|
|
`${API_BASE_PREFIX}/website_overview?id=eq.${params.websiteId}`,
|
|
|
|
|
{
|
|
|
|
|
method: "GET",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
Authorization: `Bearer ${cookies.get("session_token")}`,
|
|
|
|
|
Accept: "application/vnd.pgrst.object+json"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
2024-08-04 17:46:41 +02:00
|
|
|
|
2024-08-27 16:39:29 +02:00
|
|
|
const websiteOverview = await websiteOverviewData.json();
|
2024-08-07 19:13:39 +02:00
|
|
|
generateStaticFiles(websiteOverview, false);
|
2024-08-10 22:20:57 +02:00
|
|
|
|
2024-08-30 15:48:15 +02:00
|
|
|
const res = await fetch(`${API_BASE_PREFIX}/website?id=eq.${params.websiteId}`, {
|
|
|
|
|
method: "PATCH",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
Authorization: `Bearer ${cookies.get("session_token")}`
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
is_published: true
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!res.ok) {
|
|
|
|
|
const response = await res.json();
|
|
|
|
|
return { success: false, message: response.message };
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-10 22:20:57 +02:00
|
|
|
return { success: true, message: "Successfully published website" };
|
2024-08-04 17:46:41 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-09-07 13:04:09 +02:00
|
|
|
const generateStaticFiles = async (websiteData: WebsiteData, isPreview: boolean = true) => {
|
2024-08-18 18:17:59 +02:00
|
|
|
let head = "";
|
|
|
|
|
let body = "";
|
|
|
|
|
|
|
|
|
|
switch (websiteData.content_type) {
|
|
|
|
|
case "Blog":
|
|
|
|
|
{
|
|
|
|
|
({ head, body } = render(BlogIndex, {
|
|
|
|
|
props: {
|
2024-08-25 16:31:12 +02:00
|
|
|
favicon: websiteData.favicon_image
|
|
|
|
|
? `${API_BASE_PREFIX}/rpc/retrieve_file?id=${websiteData.favicon_image}`
|
|
|
|
|
: "",
|
2024-08-18 18:17:59 +02:00
|
|
|
title: websiteData.title,
|
|
|
|
|
logoType: websiteData.logo_type,
|
2024-08-25 16:31:12 +02:00
|
|
|
logo:
|
|
|
|
|
websiteData.logo_type === "text"
|
2024-09-07 13:04:09 +02:00
|
|
|
? (websiteData.logo_text ?? "")
|
2024-08-25 16:31:12 +02:00
|
|
|
: `${API_BASE_PREFIX}/rpc/retrieve_file?id=${websiteData.logo_image}`,
|
2024-08-24 20:34:06 +02:00
|
|
|
mainContent: md(websiteData.main_content ?? "", false),
|
2024-08-18 18:17:59 +02:00
|
|
|
articles: websiteData.articles ?? [],
|
2024-08-25 15:06:55 +02:00
|
|
|
footerAdditionalText: md(websiteData.additional_text ?? "")
|
2024-08-18 18:17:59 +02:00
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case "Docs":
|
|
|
|
|
{
|
|
|
|
|
({ head, body } = render(DocsIndex, {
|
|
|
|
|
props: {
|
2024-08-27 16:39:29 +02:00
|
|
|
favicon: websiteData.favicon_image
|
|
|
|
|
? `${API_BASE_PREFIX}/rpc/retrieve_file?id=${websiteData.favicon_image}`
|
|
|
|
|
: "",
|
2024-08-18 18:17:59 +02:00
|
|
|
title: websiteData.title,
|
|
|
|
|
logoType: websiteData.logo_type,
|
2024-08-27 16:39:29 +02:00
|
|
|
logo:
|
|
|
|
|
websiteData.logo_type === "text"
|
2024-09-07 13:04:09 +02:00
|
|
|
? (websiteData.logo_text ?? "")
|
2024-08-27 16:39:29 +02:00
|
|
|
: `${API_BASE_PREFIX}/rpc/retrieve_file?id=${websiteData.logo_image}`,
|
2024-08-24 20:34:06 +02:00
|
|
|
mainContent: md(websiteData.main_content ?? "", false),
|
2024-08-28 17:30:32 +02:00
|
|
|
categorizedArticles: websiteData.categorized_articles ?? [],
|
2024-08-25 15:06:55 +02:00
|
|
|
footerAdditionalText: md(websiteData.additional_text ?? "")
|
2024-08-18 18:17:59 +02:00
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2024-08-17 19:29:10 +02:00
|
|
|
|
2024-09-07 16:45:20 +02:00
|
|
|
const indexFileContents = `
|
|
|
|
|
<!DOCTYPE html>
|
|
|
|
|
<html lang="en">
|
|
|
|
|
<head>
|
|
|
|
|
${head}
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
${body}
|
|
|
|
|
</body>
|
|
|
|
|
</html>`;
|
2024-08-04 17:46:41 +02:00
|
|
|
|
|
|
|
|
let uploadDir = "";
|
|
|
|
|
|
|
|
|
|
if (isPreview) {
|
2024-08-18 13:48:36 +02:00
|
|
|
uploadDir = join("/", "var", "www", "archtika-websites", "previews", websiteData.id);
|
2024-08-04 17:46:41 +02:00
|
|
|
} else {
|
2024-08-18 13:48:36 +02:00
|
|
|
uploadDir = join("/", "var", "www", "archtika-websites", websiteData.id);
|
2024-08-04 17:46:41 +02:00
|
|
|
}
|
2024-08-04 16:15:09 +02:00
|
|
|
|
|
|
|
|
await mkdir(uploadDir, { recursive: true });
|
|
|
|
|
await writeFile(join(uploadDir, "index.html"), indexFileContents);
|
2024-08-27 16:39:29 +02:00
|
|
|
await mkdir(join(uploadDir, "articles"), {
|
2024-08-18 18:17:59 +02:00
|
|
|
recursive: true
|
|
|
|
|
});
|
2024-08-04 16:15:09 +02:00
|
|
|
|
2024-08-08 20:31:38 +02:00
|
|
|
for (const article of websiteData.articles ?? []) {
|
2024-08-04 16:15:09 +02:00
|
|
|
const articleFileName = article.title.toLowerCase().split(" ").join("-");
|
|
|
|
|
|
2024-08-18 18:17:59 +02:00
|
|
|
let head = "";
|
|
|
|
|
let body = "";
|
|
|
|
|
|
|
|
|
|
switch (websiteData.content_type) {
|
|
|
|
|
case "Blog":
|
|
|
|
|
{
|
|
|
|
|
({ head, body } = render(BlogArticle, {
|
|
|
|
|
props: {
|
2024-08-25 16:31:12 +02:00
|
|
|
favicon: websiteData.favicon_image
|
|
|
|
|
? `${API_BASE_PREFIX}/rpc/retrieve_file?id=${websiteData.favicon_image}`
|
|
|
|
|
: "",
|
2024-08-18 18:17:59 +02:00
|
|
|
title: article.title,
|
|
|
|
|
logoType: websiteData.logo_type,
|
2024-08-25 16:31:12 +02:00
|
|
|
logo:
|
|
|
|
|
websiteData.logo_type === "text"
|
2024-09-07 13:04:09 +02:00
|
|
|
? (websiteData.logo_text ?? "")
|
2024-08-25 16:31:12 +02:00
|
|
|
: `${API_BASE_PREFIX}/rpc/retrieve_file?id=${websiteData.logo_image}`,
|
2024-08-18 18:17:59 +02:00
|
|
|
coverImage: article.cover_image
|
2024-08-19 19:55:06 +02:00
|
|
|
? `${API_BASE_PREFIX}/rpc/retrieve_file?id=${article.cover_image}`
|
2024-08-18 18:17:59 +02:00
|
|
|
: "",
|
|
|
|
|
publicationDate: article.publication_date,
|
2024-08-24 20:34:06 +02:00
|
|
|
mainContent: md(article.main_content ?? ""),
|
2024-09-07 16:45:20 +02:00
|
|
|
footerAdditionalText: md(websiteData.additional_text ?? ""),
|
|
|
|
|
metaDescription: article.meta_description
|
2024-08-18 18:17:59 +02:00
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case "Docs":
|
|
|
|
|
{
|
2024-08-27 16:39:29 +02:00
|
|
|
({ head, body } = render(DocsArticle, {
|
2024-08-18 18:17:59 +02:00
|
|
|
props: {
|
2024-08-27 16:39:29 +02:00
|
|
|
favicon: websiteData.favicon_image
|
|
|
|
|
? `${API_BASE_PREFIX}/rpc/retrieve_file?id=${websiteData.favicon_image}`
|
|
|
|
|
: "",
|
2024-08-18 18:17:59 +02:00
|
|
|
title: article.title,
|
|
|
|
|
logoType: websiteData.logo_type,
|
2024-08-27 16:39:29 +02:00
|
|
|
logo:
|
|
|
|
|
websiteData.logo_type === "text"
|
2024-09-07 13:04:09 +02:00
|
|
|
? (websiteData.logo_text ?? "")
|
2024-08-27 16:39:29 +02:00
|
|
|
: `${API_BASE_PREFIX}/rpc/retrieve_file?id=${websiteData.logo_image}`,
|
2024-08-24 20:34:06 +02:00
|
|
|
mainContent: md(article.main_content ?? ""),
|
2024-08-28 17:30:32 +02:00
|
|
|
categorizedArticles: websiteData.categorized_articles ?? [],
|
2024-09-07 16:45:20 +02:00
|
|
|
footerAdditionalText: md(websiteData.additional_text ?? ""),
|
|
|
|
|
metaDescription: article.meta_description
|
2024-08-18 18:17:59 +02:00
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2024-08-17 19:29:10 +02:00
|
|
|
|
2024-09-07 16:45:20 +02:00
|
|
|
const articleFileContents = `
|
|
|
|
|
<!DOCTYPE html>
|
|
|
|
|
<html lang="en">
|
|
|
|
|
<head>
|
|
|
|
|
${head}
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
${body}
|
|
|
|
|
</body>
|
|
|
|
|
</html>`;
|
2024-08-03 18:07:27 +02:00
|
|
|
|
2024-08-27 16:39:29 +02:00
|
|
|
await writeFile(join(uploadDir, "articles", `${articleFileName}.html`), articleFileContents);
|
2024-08-03 18:07:27 +02:00
|
|
|
}
|
2024-08-17 20:21:23 +02:00
|
|
|
|
2024-08-20 19:17:05 +02:00
|
|
|
const commonStyles = await readFile(`${process.cwd()}/template-styles/common-styles.css`, {
|
2024-08-17 20:21:23 +02:00
|
|
|
encoding: "utf-8"
|
|
|
|
|
});
|
2024-08-27 16:39:29 +02:00
|
|
|
const specificStyles = await readFile(
|
|
|
|
|
`${process.cwd()}/template-styles/${websiteData.content_type.toLowerCase()}-styles.css`,
|
|
|
|
|
{
|
|
|
|
|
encoding: "utf-8"
|
|
|
|
|
}
|
|
|
|
|
);
|
2024-08-25 16:31:12 +02:00
|
|
|
await writeFile(
|
|
|
|
|
join(uploadDir, "styles.css"),
|
|
|
|
|
commonStyles
|
|
|
|
|
.concat(specificStyles)
|
|
|
|
|
.replace(
|
|
|
|
|
/--color-accent:\s*(.*?);/,
|
|
|
|
|
`--color-accent: ${websiteData.accent_color_dark_theme};`
|
|
|
|
|
)
|
|
|
|
|
.replace(
|
|
|
|
|
/@media\s*\(prefers-color-scheme:\s*dark\)\s*{[^}]*--color-accent:\s*(.*?);/,
|
|
|
|
|
(match) =>
|
|
|
|
|
match.replace(
|
|
|
|
|
/--color-accent:\s*(.*?);/,
|
|
|
|
|
`--color-accent: ${websiteData.accent_color_light_theme};`
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
);
|
2024-08-03 18:07:27 +02:00
|
|
|
};
|