From b3153ae06a1074d94ca48b5ea70fa12e2c69db82 Mon Sep 17 00:00:00 2001 From: Michal Vanko Date: Tue, 31 Jan 2023 20:04:22 +0100 Subject: [PATCH] Blog posts working now --- src/routes/blog/[slug=blogPage].svelte | 17 ------- src/routes/blog/[slug=blogPage].ts | 44 ------------------- .../blog/[slug=blogPage]/+page.server.ts | 33 ++++++++++++++ src/routes/blog/[slug=blogPage]/+page.svelte | 18 ++++++++ .../blog/{ => [slug=blogPage]}/blog.css.ts | 0 5 files changed, 51 insertions(+), 61 deletions(-) delete mode 100644 src/routes/blog/[slug=blogPage].svelte delete mode 100644 src/routes/blog/[slug=blogPage].ts create mode 100644 src/routes/blog/[slug=blogPage]/+page.server.ts create mode 100644 src/routes/blog/[slug=blogPage]/+page.svelte rename src/routes/blog/{ => [slug=blogPage]}/blog.css.ts (100%) diff --git a/src/routes/blog/[slug=blogPage].svelte b/src/routes/blog/[slug=blogPage].svelte deleted file mode 100644 index dc20d5f..0000000 --- a/src/routes/blog/[slug=blogPage].svelte +++ /dev/null @@ -1,17 +0,0 @@ - - - - {post.title} - - -

{post.title}

- -
- {@html post.body} -
- diff --git a/src/routes/blog/[slug=blogPage].ts b/src/routes/blog/[slug=blogPage].ts deleted file mode 100644 index 8ff2020..0000000 --- a/src/routes/blog/[slug=blogPage].ts +++ /dev/null @@ -1,44 +0,0 @@ -import { readFile } from 'fs' -import { promisify } from 'util' -import fm from 'front-matter' -import { parseField } from '../../markdown/parse-markdown' -import type { PostAttributes } from './content' -import type { Request, Response } from '@sveltejs/kit' - -export interface SinglePost { - body: string -} - -export async function get({ params: { slug } }: Request): Promise { - // the `slug` parameter is available because - // this file is called [slug].json.js - - let postSource: string - try { - postSource = await promisify(readFile)(`_posts/blog/${slug}.md`, 'utf-8') - } catch (e) { - if (e.code === 'ENOENT') { - return { - status: 404, - body: 'Post not found \n' + e.toString(), - headers: {}, - } - } - return { - status: 500, - body: 'Error loading post source file. \n' + e.toString(), - headers: {}, - } - } - - const parsedPost = fm(postSource) - - const post = parseField('body')({ - ...parsedPost.attributes, - body: parsedPost.body, - }) - - return { - body: { post }, - } -} diff --git a/src/routes/blog/[slug=blogPage]/+page.server.ts b/src/routes/blog/[slug=blogPage]/+page.server.ts new file mode 100644 index 0000000..58096cd --- /dev/null +++ b/src/routes/blog/[slug=blogPage]/+page.server.ts @@ -0,0 +1,33 @@ +import { readFile } from 'fs' +import { promisify } from 'util' +import fm from 'front-matter' +import { parseField } from '../../../markdown/parse-markdown' +import { error, json } from '@sveltejs/kit' +import type { PostAttributes } from '../content' +import type { PageServerLoad } from './$types' + +export interface SinglePost { + body: string +} + +export const load = (async ({ params: { slug } }) => { + let postSource: string + try { + postSource = await promisify(readFile)(`_posts/blog/${slug}.md`, 'utf-8') + // eslint-disable-next-line @typescript-eslint/no-explicit-any + } catch (e: any) { + if (e.code === 'ENOENT') { + throw error(404, 'Post not found \n' + e.toString()) + } + throw e + } + + const parsedPost = fm(postSource) + + const post = parseField('body')({ + ...parsedPost.attributes, + body: parsedPost.body, + }) + + return post +}) satisfies PageServerLoad diff --git a/src/routes/blog/[slug=blogPage]/+page.svelte b/src/routes/blog/[slug=blogPage]/+page.svelte new file mode 100644 index 0000000..67e4ec7 --- /dev/null +++ b/src/routes/blog/[slug=blogPage]/+page.svelte @@ -0,0 +1,18 @@ + + + + {data.title} + + +

{data.title}

+ +
+ {@html data.body} +
+ diff --git a/src/routes/blog/blog.css.ts b/src/routes/blog/[slug=blogPage]/blog.css.ts similarity index 100% rename from src/routes/blog/blog.css.ts rename to src/routes/blog/[slug=blogPage]/blog.css.ts