From 170d61dd964ba8319110d82d117db450117ea27b Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Sat, 21 Aug 2021 11:12:25 +0200 Subject: [PATCH] progress --- src/lib/api/index.ts | 23 +++++++++++++- src/lib/components/PostAttributes.svelte | 27 +++++++++------- src/lib/components/PostPreview.svelte | 26 ++++++++------- src/lib/utils.ts | 5 +++ src/routes/api/[type]/[slug].json.ts | 40 ++++++++++++++++++++++-- src/routes/blog/[slug].svelte | 32 +++++++++++++++++++ src/routes/blog/index.svelte | 30 ++++++++++++++++++ src/routes/contact.svelte | 4 +++ src/routes/index.svelte | 23 +++++--------- src/routes/projects.svelte | 1 + src/routes/search.svelte | 1 + src/routes/support.svelte | 4 +++ 12 files changed, 172 insertions(+), 44 deletions(-) create mode 100644 src/lib/utils.ts create mode 100644 src/routes/blog/[slug].svelte create mode 100644 src/routes/blog/index.svelte create mode 100644 src/routes/search.svelte diff --git a/src/lib/api/index.ts b/src/lib/api/index.ts index 13fcbe0..b9e6b6b 100644 --- a/src/lib/api/index.ts +++ b/src/lib/api/index.ts @@ -36,7 +36,7 @@ export type Page = { status: string } -export const BaseAttributes = gql` +export const BaseAttributes = ` id slug status @@ -44,6 +44,27 @@ export const BaseAttributes = gql` content ` +export interface Post extends Page { + date: string + modified: string + post: { + featured: MediaItem + } +} + +export const PostFragment = gql` + fragment PostFragment on Post { + ${BaseAttributes} + date + modified + post { + featured { + ...MediaItemFragment + } + } + } +` + export interface Work extends Page { work: { date: string diff --git a/src/lib/components/PostAttributes.svelte b/src/lib/components/PostAttributes.svelte index a2a721e..3666702 100644 --- a/src/lib/components/PostAttributes.svelte +++ b/src/lib/components/PostAttributes.svelte @@ -1,11 +1,12 @@ - +
+
+ {created} + {#if full && created !== modified}
Last update: {modified}{/if} +
+ {#if post.content} +
~ {readingTimeInMinutes(post.content)} min
+ {/if} +
+ - -
-
- {created} - {#if full && created !== modified}
Last update: {modified}{/if} -
-
~ {readingTimeInMinutes(post.content)} min
-
diff --git a/src/lib/components/PostPreview.svelte b/src/lib/components/PostPreview.svelte index 9c6fdc2..e4dbfa0 100644 --- a/src/lib/components/PostPreview.svelte +++ b/src/lib/components/PostPreview.svelte @@ -1,10 +1,22 @@ - + + {#if post.post.featured} + + {/if} + +

+ {@html post.title} +

+
+ - - - {#if post.featured} - - {/if} - -

- {@html post.title} -

-
diff --git a/src/lib/utils.ts b/src/lib/utils.ts new file mode 100644 index 0000000..0581a76 --- /dev/null +++ b/src/lib/utils.ts @@ -0,0 +1,5 @@ +export function readingTimeInMinutes(text: string, options: { wpm?: number } = {}): number { + const cleaned = text.replace(/(<.*?>)|(\\n)|(&#\d*?;)/g, '') + const words = cleaned.split(' ').length + return Math.round(words / (options.wpm ?? 200)) +} diff --git a/src/routes/api/[type]/[slug].json.ts b/src/routes/api/[type]/[slug].json.ts index bfc6c46..befcbe6 100644 --- a/src/routes/api/[type]/[slug].json.ts +++ b/src/routes/api/[type]/[slug].json.ts @@ -7,6 +7,8 @@ import { MediaItem, MediaItemFragment, Page, + Post, + PostFragment, Project, ProjectFragment, WorkFragment, @@ -22,7 +24,7 @@ export const get: RequestHandler = async (args) => { if (all) { const data = await Call<{ pages: { nodes: Page[] } }>(gql` query { - pages { + pages(where: {status: PUBLISH}) { nodes { ${BaseAttributes} } @@ -50,7 +52,7 @@ export const get: RequestHandler = async (args) => { ${MediaItemFragment} ${WorkFragment} query { - works { + works(where: { status: PUBLISH }) { nodes { ...WorkFragment } @@ -81,7 +83,7 @@ export const get: RequestHandler = async (args) => { gql` ${ProjectFragment} query { - projects { + projects(where: { status: PUBLISH }) { nodes { ...ProjectFragment } @@ -120,6 +122,38 @@ export const get: RequestHandler = async (args) => { ) return { body: data.mediaItem } } + + case 'posts': { + if (all) { + const data = await Call<{ posts: { nodes: Post[] } }>(gql` + ${PostFragment} + ${MediaItemFragment} + { + posts(where: { status: PUBLISH }, first: 1000000000) { + nodes { + ...PostFragment + } + } + } + `) + return { body: data.posts.nodes } + } else { + const data = await Call<{ post: Post }>( + gql` + ${PostFragment} + ${MediaItemFragment} + query ($slug: ID!) { + post(id: $slug, idType: SLUG) { + ...PostFragment + } + } + `, + { slug } + ) + return { body: data.post } + } + } + default: return { status: 404 } } diff --git a/src/routes/blog/[slug].svelte b/src/routes/blog/[slug].svelte new file mode 100644 index 0000000..6a64bd9 --- /dev/null +++ b/src/routes/blog/[slug].svelte @@ -0,0 +1,32 @@ + + + + + + Works + + + + + {#if data.content} + + {/if} + diff --git a/src/routes/blog/index.svelte b/src/routes/blog/index.svelte new file mode 100644 index 0000000..0422a8c --- /dev/null +++ b/src/routes/blog/index.svelte @@ -0,0 +1,30 @@ + + + + + + Blog + + + + {#each data as post} + + {/each} + diff --git a/src/routes/contact.svelte b/src/routes/contact.svelte index 09f25ad..237874d 100644 --- a/src/routes/contact.svelte +++ b/src/routes/contact.svelte @@ -1,3 +1,7 @@ + + @@ -28,7 +19,7 @@ - Niccolo Borgioli + Niccolò Borgioli
diff --git a/src/routes/projects.svelte b/src/routes/projects.svelte index 25fa566..10ec10d 100644 --- a/src/routes/projects.svelte +++ b/src/routes/projects.svelte @@ -1,6 +1,7 @@ +