Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/lib/blog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { error } from '@sveltejs/kit';

const url = 'http://localhost:1337';

export async function list(fetch_fn = fetch) {
// TODO: only get the fields we need.
const res = await fetch_fn(`${url}/api/posts?populate=*`);
const data = await res.json();

return data.data;
}

export async function get_one(slug: string, fetch_fn = fetch) {
const res = await fetch_fn(`${url}/api/posts?filters[slug][$eq]=${slug}`);
if (res.status === 404) {
throw error(404, `The post with slug ${slug} was not found`);
} else {
const response = await res.json();
return response.data[0].attributes;
}
}
3 changes: 3 additions & 0 deletions src/routes/blog/+layout.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<main style="text-align: center; margin-top: 8rem;">
<slot />
</main>
15 changes: 15 additions & 0 deletions src/routes/blog/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<script lang="ts">
export let data;
</script>
<h1>Blog posts</h1>
<div>
{#each data.posts as post}
<div>
<h2>{post.attributes.title}</h2>
<p>
{post.attributes.description}
</p>
<a href="/blog/{post.attributes.slug}">link</a>
</div>
{/each}
</div>
5 changes: 5 additions & 0 deletions src/routes/blog/+page.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { list } from '$lib/blog';

export const load = async ({ fetch }) => {
return { posts: await list(fetch) };
};
14 changes: 14 additions & 0 deletions src/routes/blog/[...slug]/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script>
export let data;
</script>

<svelte:head>
<title>ReVanced | {data.post.title}</title>
<meta content="ReVanced | {data.post.title}" name="og:title" />
<meta content="ReVanced | {data.post.title}" name="twitter:title" />
</svelte:head>
<h1 style="color: var(--accent-color); font-size: 5rem;">
{data.post.title}
</h1>

{@html data.post.content}
8 changes: 8 additions & 0 deletions src/routes/blog/[...slug]/+page.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { get_one } from '$lib/blog';
import { parse } from 'marked';

export const load = async ({ fetch, params }) => {
let post = await get_one(params.slug, fetch);
post.content = parse(post.content);
return { post };
};