diff --git a/templates/blog/article.html b/templates/blog/article.html new file mode 100644 index 0000000..542e558 --- /dev/null +++ b/templates/blog/article.html @@ -0,0 +1,24 @@ + + + + + + Document + + + +
+ {{cover_image}} + {{title}} + {{publication_date}} +
+
+ {{main_content}} +
+ + + \ No newline at end of file diff --git a/templates/blog/index.html b/templates/blog/index.html new file mode 100644 index 0000000..6cd15d8 --- /dev/null +++ b/templates/blog/index.html @@ -0,0 +1,22 @@ + + + + + + Document + + + +
+ {{title}} +
+
+ {{main_content}} +
+ + + \ No newline at end of file diff --git a/templates/blog/styles.css b/templates/blog/styles.css new file mode 100644 index 0000000..e69de29 diff --git a/web-app/.gitignore b/web-app/.gitignore index a8ac6b5..f2b82b1 100644 --- a/web-app/.gitignore +++ b/web-app/.gitignore @@ -1,5 +1,6 @@ node_modules user-uploads +user-websites # Output .output diff --git a/web-app/src/lib/components/WebsiteEditor.svelte b/web-app/src/lib/components/WebsiteEditor.svelte index a56b781..b013c92 100644 --- a/web-app/src/lib/components/WebsiteEditor.svelte +++ b/web-app/src/lib/components/WebsiteEditor.svelte @@ -1,7 +1,6 @@
@@ -46,7 +31,10 @@
{#if fullPreview} - + {:else} {@html md.render(previewContent)} {/if} diff --git a/web-app/src/lib/utils.ts b/web-app/src/lib/utils.ts index 00647a0..4de3089 100644 --- a/web-app/src/lib/utils.ts +++ b/web-app/src/lib/utils.ts @@ -1,3 +1,6 @@ +import markdownit from "markdown-it"; +import hljs from "highlight.js"; + export const sortOptions = [ { value: "creation-time", text: "Creation time" }, { value: "last-modified", text: "Last modified" }, @@ -6,3 +9,17 @@ export const sortOptions = [ ]; export const ALLOWED_MIME_TYPES = ["image/jpeg", "image/png", "image/svg+xml", "image/webp"]; + +export const md = markdownit({ + linkify: true, + typographer: true, + highlight: (str, lang) => { + if (lang && hljs.getLanguage(lang)) { + try { + return hljs.highlight(str, { language: lang }).value; + } catch (_) {} + } + + return ""; + } +}); diff --git a/web-app/src/routes/(authenticated)/website/[websiteId]/publish/+page.server.ts b/web-app/src/routes/(authenticated)/website/[websiteId]/publish/+page.server.ts index 0b69389..b1b4651 100644 --- a/web-app/src/routes/(authenticated)/website/[websiteId]/publish/+page.server.ts +++ b/web-app/src/routes/(authenticated)/website/[websiteId]/publish/+page.server.ts @@ -1,12 +1,60 @@ -import { randomUUID } from "node:crypto"; -import { mkdir, writeFile } from "node:fs/promises"; -import { extname, join, relative } from "node:path"; +import { readFile, mkdir, writeFile } from "node:fs/promises"; +import { join } from "node:path"; +import { md } from "$lib/utils"; -export const load = async ({ parent }) => { - const { website } = await parent(); +export const load = async ({ params, fetch, cookies, locals }) => { + const websiteOverviewData = await fetch( + `http://localhost:3000/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" + } + } + ); + + const websiteOverview = await websiteOverviewData.json(); + + const templatePath = join( + process.cwd(), + "..", + "templates", + websiteOverview.content_type.toLowerCase() + ); + + const indexFile = await readFile(join(templatePath, "index.html"), { encoding: "utf-8" }); + const articleFileContents = await readFile(join(templatePath, "article.html"), { + encoding: "utf-8" + }); + + const indexFileContents = indexFile + .replace("{{title}}", `

${websiteOverview.title}

`) + .replace("{{main_content}}", md.render(websiteOverview.main_content)) + .replace("{{additional_text}}", md.render(websiteOverview.additional_text)); + + const uploadDir = join( + process.cwd(), + "static", + "user-websites", + locals.user.id, + params.websiteId + ); + await mkdir(uploadDir, { recursive: true }); + + await writeFile(join(uploadDir, "index.html"), indexFileContents); + + await mkdir(join(uploadDir, "articles"), { recursive: true }); + + for (const article of websiteOverview.articles) { + const articleFileName = article.title.toLowerCase().split(" ").join("-"); + + await writeFile(join(uploadDir, "articles", `${articleFileName}.html`), articleFileContents); + } return { - website + websiteOverview }; };