diff --git a/src/routes/blog/new/+page.server.ts b/src/routes/blog/new/+page.server.ts index 0dc5da2..a43baf2 100644 --- a/src/routes/blog/new/+page.server.ts +++ b/src/routes/blog/new/+page.server.ts @@ -1,61 +1,76 @@ -import { BlogController } from '$lib/blog/BlogController.js'; -import type { Actions } from '@sveltejs/kit'; -import { error, redirect } from '@sveltejs/kit'; -import { dump as dumpYaml } from 'js-yaml'; -import { resolve } from 'path'; +import { BlogController } from "$lib/blog/BlogController.js"; +import type { Actions } from "@sveltejs/kit"; +import { error, redirect } from "@sveltejs/kit"; +import { dump as dumpYaml } from "js-yaml"; +import { resolve } from "path"; export const prerender = false; const thisDirectory = import.meta.url - .replace('file://', '') - .split('/') - .filter((part) => part !== '+server.ts') - .join('/'); + .replace("file://", "") + .split("/") + .filter((part) => part !== "+server.ts") + .join("/"); export const actions = { - default: async ({ getClientAddress, request }) => { - console.log(`Received request to create new blog post.`); - const address = await getClientAddress(); - let markdownContent: string; - let title: string; - let slug: string; - let author: string; + default: async ({ getClientAddress, request }) => { + console.log(`Received request to create new blog post.`); + const address = await getClientAddress(); + let markdownContent: string; + let title: string; + let slug: string; + let author: string; + let commaSeparatedTags: string; - try { - const requestBody = await request.formData(); - markdownContent = requestBody.get('content') as string; - title = requestBody.get('title') as string; - slug = requestBody.get('slug') as string; - author = requestBody.get('author') as string; - } catch (e: any) { - console.log(`Caught error destructuring request body`); - console.error(e); - error(400, 'Error in request body.'); - } + try { + const requestBody = await request.formData(); + markdownContent = requestBody.get("content") as string; + title = requestBody.get("title") as string; + slug = requestBody.get("slug") as string; + author = requestBody.get("author") as string; + commaSeparatedTags = requestBody.get("tags") as string; + } catch (e: any) { + console.log(`Caught error destructuring request body`); + console.error(e); + error(400, "Error in request body."); + } - if ([markdownContent, title, slug, author].includes(undefined)) { - error(400, `Missing parameters.`); - } else if (!['127.0.0.1', '::1'].includes(address)) { - console.log(address); - error(403, `Forbidden.`); - } + if ([markdownContent, title, slug, author].includes(undefined)) { + error(400, `Missing parameters.`); + } else if (!["127.0.0.1", "::1"].includes(address)) { + console.log(address); + error(403, `Forbidden.`); + } - const controller = await BlogController.singleton(); + const controller = await BlogController.singleton(); - const worryinglyManualFrontMatter = [`---`, dumpYaml({ title, date: new Date(), slug, author }), `---`].join( - `\n` - ); + const tags = commaSeparatedTags + .split(",") + .map((s) => s.trim()) + .filter((s) => s.length > 0); - const escapedMarkdown = markdownContent.replaceAll(/\\n/g, '\n'); + const worryinglyManualFrontMatter = [ + `---`, + dumpYaml({ title, date: new Date(), slug, author, tags }), + `---`, + ].join(`\n`); - const contentWithFrontmatter = [worryinglyManualFrontMatter, escapedMarkdown].join(`\n`); + const escapedMarkdown = markdownContent.replaceAll(/\\n/g, "\n"); - const resolvedFileName = resolve(thisDirectory, `../../../../content/blog/${slug}.md`); + const contentWithFrontmatter = [ + worryinglyManualFrontMatter, + escapedMarkdown, + ].join(`\n`); - console.log({ resolvedFileName }); - console.log(`\n${contentWithFrontmatter}\n`); + const resolvedFileName = resolve( + thisDirectory, + `../../../../content/blog/${slug}.md`, + ); - await controller.createBlogPost(resolvedFileName, contentWithFrontmatter); + console.log({ resolvedFileName }); + console.log(`\n${contentWithFrontmatter}\n`); - redirect(307, `/blog/${slug}`); - }, + await controller.createBlogPost(resolvedFileName, contentWithFrontmatter); + + redirect(307, `/blog/${slug}`); + }, } satisfies Actions; diff --git a/src/routes/blog/new/+page.svelte b/src/routes/blog/new/+page.svelte index 98a2e6b..dd9580a 100644 --- a/src/routes/blog/new/+page.svelte +++ b/src/routes/blog/new/+page.svelte @@ -6,6 +6,7 @@ let date = new Date(); let content = $state(""); let slug = $state(""); + let tags = $state(""); let blogPost: BlogPost | null = null; function slugifyString(originalString: string): string { @@ -63,6 +64,11 @@ +
+ + +
+