Broadcasting section

This commit is contained in:
Michal Vanko 2023-02-18 06:41:07 +01:00
parent dac3e13520
commit 3640a5f13a
13 changed files with 79 additions and 53 deletions

View File

@ -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<ArticleAttributes>(postSource)
const post = parseField<ArticleAttributes>('body')({
...parsedPost.attributes,
body: parsedPost.body,
})
return post
}

View File

@ -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<ArticleContent>(prop('date')),
sortBy<ArticlePreviewAttributes>(prop('date')),
(items) => reverse(items),
filter<(typeof contents)[0]>((article) => article.published),
filterAndCount(paginationQuery)

View File

@ -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

View File

@ -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<ArticleContent>,
posts: articleResponse.posts as PaginationResult<ArticlePreviewAttributes >,
page: Number(page),
pageSize,
filters,

View File

@ -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<ArticleAttributes>(postSource)
const post = parseField<SinglePost>('body')({
...parsedPost.attributes,
body: parsedPost.body,
})
const post = await getArticleContent(slug);
return post
}) satisfies PageServerLoad

View File

@ -1,7 +1,7 @@
<script lang="ts">
import ArticleFooter from '$lib/components/articles/ArticlePreviewFooter/ArticlePreviewFooter.svelte'
import type { PageData } from './$types'
import { contentClass } from './page.css'
import { contentClass } from '$lib/styles/article/article.css'
export let data: PageData
</script>

View File

@ -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<ArticleContent>,
posts: articleResponse.posts as PaginationResult<ArticlePreviewAttributes>,
page: Number(page),
pageSize,
filters,

View File

@ -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

View File

@ -0,0 +1,18 @@
<script lang="ts">
import ArticleFooter from '$lib/components/articles/ArticlePreviewFooter/ArticlePreviewFooter.svelte'
import type { PageData } from './$types'
import { contentClass } from '$lib/styles/article/article.css'
export let data: PageData
</script>
<svelte:head>
<title>{data.title}</title>
</svelte:head>
<h1>{data.title}</h1>
<div class="content {contentClass}">
{@html data.body}
</div>
<ArticleFooter article={data} segment="broadcasts" />

View File

@ -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