From 3640a5f13af7b6f46ee11842263ce11b6a3ab267 Mon Sep 17 00:00:00 2001 From: Michal Vanko Date: Sat, 18 Feb 2023 06:41:07 +0100 Subject: [PATCH] Broadcasting section --- src/lib/articleContent/articleContent.ts | 37 +++++++++++++++++++ .../articleContentListing.ts | 24 +++--------- src/{ => lib}/markdown/parse-markdown.ts | 0 src/{ => lib}/markdown/renderer-extension.ts | 0 .../styles/article/article.css.ts} | 0 .../articles/[...params].json/+server.ts | 2 +- src/routes/blog/[...params]/+page.ts | 4 +- .../blog/[slug=blogPage]/+page.server.ts | 30 +-------------- src/routes/blog/[slug=blogPage]/+page.svelte | 2 +- src/routes/broadcasts/[...params]/+page.ts | 4 +- .../[slug=blogPage]/+page.server.ts | 9 +++++ .../broadcasts/[slug=blogPage]/+page.svelte | 18 +++++++++ src/routes/portfolio/+page.server.ts | 2 +- 13 files changed, 79 insertions(+), 53 deletions(-) create mode 100644 src/lib/articleContent/articleContent.ts rename src/lib/{content => articleContent}/articleContentListing.ts (82%) rename src/{ => lib}/markdown/parse-markdown.ts (100%) rename src/{ => lib}/markdown/renderer-extension.ts (100%) rename src/{routes/blog/[slug=blogPage]/page.css.ts => lib/styles/article/article.css.ts} (100%) create mode 100644 src/routes/broadcasts/[slug=blogPage]/+page.server.ts create mode 100644 src/routes/broadcasts/[slug=blogPage]/+page.svelte diff --git a/src/lib/articleContent/articleContent.ts b/src/lib/articleContent/articleContent.ts new file mode 100644 index 0000000..0fa57fe --- /dev/null +++ b/src/lib/articleContent/articleContent.ts @@ -0,0 +1,37 @@ +import { error } from "@sveltejs/kit"; +import fm from "front-matter"; +import { readFile } from "fs"; +import { parseField } from "$lib/markdown/parse-markdown"; +import { promisify } from "util"; + +export interface ArticleAttributes { + slug: string + layout: string + title: string + published: boolean + date: string + thumbnail: string + tags: string[] + body: string +} + +export async function getArticleContent(slug: string) { + 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 +} diff --git a/src/lib/content/articleContentListing.ts b/src/lib/articleContent/articleContentListing.ts similarity index 82% rename from src/lib/content/articleContentListing.ts rename to src/lib/articleContent/articleContentListing.ts index e4b5626..b319f69 100644 --- a/src/lib/content/articleContentListing.ts +++ b/src/lib/articleContent/articleContentListing.ts @@ -8,24 +8,13 @@ import { filterAndCount, type PaginationQuery, } from '$lib/pagination/pagination' +import type { ArticleAttributes } from './articleContent' + +export interface ArticlePreviewAttributes extends ArticleAttributes { + preview: string +} const { NODE_ENV } = process.env - -export interface ArticleAttributes { - layout: string - title: string - published: boolean - date: string - thumbnail: string - tags: string[] -} - -export interface ArticleContent extends ArticleAttributes { - preview: string - slug: string - published: boolean -} - export async function getBlogListing(paginationQuery: PaginationQuery) { const files = await promisify(readdir)(`_posts/blog/`, 'utf-8') const filteredFiles = filterDevelopmentFiles(files) @@ -52,9 +41,8 @@ export async function getBlogListing(paginationQuery: PaginationQuery) { } }) ) - console.log(paginationQuery); const filteredContents = pipe( - sortBy(prop('date')), + sortBy(prop('date')), (items) => reverse(items), filter<(typeof contents)[0]>((article) => article.published), filterAndCount(paginationQuery) diff --git a/src/markdown/parse-markdown.ts b/src/lib/markdown/parse-markdown.ts similarity index 100% rename from src/markdown/parse-markdown.ts rename to src/lib/markdown/parse-markdown.ts diff --git a/src/markdown/renderer-extension.ts b/src/lib/markdown/renderer-extension.ts similarity index 100% rename from src/markdown/renderer-extension.ts rename to src/lib/markdown/renderer-extension.ts diff --git a/src/routes/blog/[slug=blogPage]/page.css.ts b/src/lib/styles/article/article.css.ts similarity index 100% rename from src/routes/blog/[slug=blogPage]/page.css.ts rename to src/lib/styles/article/article.css.ts diff --git a/src/routes/articles/[...params].json/+server.ts b/src/routes/articles/[...params].json/+server.ts index 863a619..2bc27fd 100644 --- a/src/routes/articles/[...params].json/+server.ts +++ b/src/routes/articles/[...params].json/+server.ts @@ -3,7 +3,7 @@ import { parseParams, } from '$lib/pagination/dropTakeParams' import { json } from '@sveltejs/kit' -import { getBlogListing } from '$lib/content/articleContentListing' +import { getBlogListing } from '$lib/articleContent/articleContentListing' import type { RequestHandler } from './$types' export const prerender = true diff --git a/src/routes/blog/[...params]/+page.ts b/src/routes/blog/[...params]/+page.ts index 3b1d472..a26029e 100644 --- a/src/routes/blog/[...params]/+page.ts +++ b/src/routes/blog/[...params]/+page.ts @@ -1,6 +1,6 @@ import { parseParams } from '$lib/pagination/dropTakeParams' import type { PageLoad } from './$types' -import type { ArticleContent } from '$lib/content/articleContentListing' +import type { ArticlePreviewAttributes } from '$lib/articleContent/articleContentListing' import type { PaginationResult } from '$lib/pagination/pagination' export const load = (async ({ fetch, params }) => { @@ -9,7 +9,7 @@ export const load = (async ({ fetch, params }) => { `/articles/segments/blog${params.params ? `/${params.params}` : ''}.json` ).then((r) => r.json()) return { - posts: articleResponse.posts as PaginationResult, + posts: articleResponse.posts as PaginationResult, page: Number(page), pageSize, filters, diff --git a/src/routes/blog/[slug=blogPage]/+page.server.ts b/src/routes/blog/[slug=blogPage]/+page.server.ts index 96efddb..5636172 100644 --- a/src/routes/blog/[slug=blogPage]/+page.server.ts +++ b/src/routes/blog/[slug=blogPage]/+page.server.ts @@ -1,35 +1,9 @@ -import { readFile } from 'fs' -import { promisify } from 'util' -import fm from 'front-matter' -import { parseField } from '../../../markdown/parse-markdown' -import { error } from '@sveltejs/kit' -import type { ArticleAttributes } from '$lib/content/articleContentListing' import type { PageServerLoad } from './$types' +import { getArticleContent } from '$lib/articleContent/articleContent' export const prerender = true -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, - }) - + const post = await getArticleContent(slug); return post }) satisfies PageServerLoad diff --git a/src/routes/blog/[slug=blogPage]/+page.svelte b/src/routes/blog/[slug=blogPage]/+page.svelte index accc5e5..99c40ca 100644 --- a/src/routes/blog/[slug=blogPage]/+page.svelte +++ b/src/routes/blog/[slug=blogPage]/+page.svelte @@ -1,7 +1,7 @@ diff --git a/src/routes/broadcasts/[...params]/+page.ts b/src/routes/broadcasts/[...params]/+page.ts index 8d87256..f20f5cf 100644 --- a/src/routes/broadcasts/[...params]/+page.ts +++ b/src/routes/broadcasts/[...params]/+page.ts @@ -1,6 +1,6 @@ import { parseParams } from '$lib/pagination/dropTakeParams' import type { PageLoad } from './$types' -import type { ArticleContent } from '$lib/content/articleContentListing' +import type { ArticlePreviewAttributes } from '$lib/articleContent/articleContentListing' import type { PaginationResult } from '$lib/pagination/pagination' export const load = (async ({ fetch, params }) => { @@ -10,7 +10,7 @@ export const load = (async ({ fetch, params }) => { ).then((r) => r.json()) return { - posts: articleResponse.posts as PaginationResult, + posts: articleResponse.posts as PaginationResult, page: Number(page), pageSize, filters, diff --git a/src/routes/broadcasts/[slug=blogPage]/+page.server.ts b/src/routes/broadcasts/[slug=blogPage]/+page.server.ts new file mode 100644 index 0000000..5636172 --- /dev/null +++ b/src/routes/broadcasts/[slug=blogPage]/+page.server.ts @@ -0,0 +1,9 @@ +import type { PageServerLoad } from './$types' +import { getArticleContent } from '$lib/articleContent/articleContent' + +export const prerender = true + +export const load = (async ({ params: { slug } }) => { + const post = await getArticleContent(slug); + return post +}) satisfies PageServerLoad diff --git a/src/routes/broadcasts/[slug=blogPage]/+page.svelte b/src/routes/broadcasts/[slug=blogPage]/+page.svelte new file mode 100644 index 0000000..52ed140 --- /dev/null +++ b/src/routes/broadcasts/[slug=blogPage]/+page.svelte @@ -0,0 +1,18 @@ + + + + {data.title} + + +

{data.title}

+ +
+ {@html data.body} +
+ diff --git a/src/routes/portfolio/+page.server.ts b/src/routes/portfolio/+page.server.ts index 1db109c..602e49e 100644 --- a/src/routes/portfolio/+page.server.ts +++ b/src/routes/portfolio/+page.server.ts @@ -3,7 +3,7 @@ import { promisify } from 'util' import fm from 'front-matter' // TODO Switch marked for unified import marked from 'marked' -import { parseField } from '../../markdown/parse-markdown' +import { parseField } from '$lib/markdown/parse-markdown' import type { PageServerLoad } from './$types' export const prerender = true