Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
el3um4s committed Mar 10, 2024
1 parent 316012a commit af88dd9
Show file tree
Hide file tree
Showing 47 changed files with 125 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/lib/components/custom/post-preview.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
{#if post.cover}
<a href="{base}/{post.slug}" class="cover">
{#if dev}
{#await import(/* @vite-ignore */ `/src/posts/${post.slug}${post.cover}`) then { default: src }}
{#await import(/* @vite-ignore */ `/src/posts/${post.src}/${post.cover}`) then { default: src }}
<img {src} alt={post.title} loading="lazy" />
{/await}
{:else}
<img
src="{config.repository}/raw/main/src/posts/{post.slug}{post.cover}"
src="{config.repository}/raw/main/src/posts/{post.src}/{post.cover}"
alt={post.title}
loading="lazy"
/>
Expand Down
2 changes: 2 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ export type Categories = 'sveltekit' | 'svelte';
export type Post = {
title: string;
slug: string;
src: string;
srcP: string[];
description: string;
date: string;
categories: Categories[];
Expand Down
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes
2 changes: 1 addition & 1 deletion src/posts/libri/post.md → src/posts/b/libri/post.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: Libri
description: Libri da leggere
date: '2024-09-01'
date: '2023-09-01'
categories:
- Filosofia della Mente
- Coscienza
Expand Down
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes.
6 changes: 3 additions & 3 deletions src/routes/[slug]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
{#if data.meta.cover}
<meta
property="og:image"
content="{config.repository}/raw/main/src/posts/{data.url}/{data.meta.cover}"
content="{config.repository}/raw/main/src/posts/{data.src}/{data.meta.cover}"
/>
{/if}
<meta name="author" content="Samuele C. De Tomasi" data-rh="true" />
Expand Down Expand Up @@ -52,12 +52,12 @@
{#if data.meta.cover}
<div class="cover">
{#if dev}
{#await import(/* @vite-ignore */ `/src/posts/${data.url}/${data.meta.cover}`) then { default: src }}
{#await import(/* @vite-ignore */ `/src/posts/${data.src}/${data.meta.cover}`) then { default: src }}
<img {src} alt={data.meta.title} loading="lazy" />
{/await}
{:else}
<img
src="{config.repository}/raw/main/src/posts/{data.url}/{data.meta.cover}"
src="{config.repository}/raw/main/src/posts/{data.src}/{data.meta.cover}"
alt={data.meta.title}
loading="lazy"
/>
Expand Down
108 changes: 105 additions & 3 deletions src/routes/[slug]/+page.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,116 @@
import { error } from '@sveltejs/kit';

export async function load({ params }) {
export const prerender = 'auto';

export async function load({ fetch, params }) {
try {
const post = await import(`../../posts/${params.slug}/post.md`);
// console.log('TRY');
const { slug } = params;
// console.log('SLUG', slug);

const response = await fetch('../api/posts');
// console.log('response');
// console.log(response);
const allPosts = await response.json();
// console.log('allPosts');
// console.log(allPosts);

const posts = allPosts.filter((post: { slug: string }) => {
return post.slug.toLowerCase() == slug.toLowerCase();
});

// console.log('posts');
// console.log(posts);

const src = posts[0].src;
// console.log(src);
const srcP = posts[0].srcP;
// console.log(srcP);
const len = srcP.length;
// console.log(len);
// console.log(src, srcP, slug, len);
// const srcContent = posts[0].srcContent;
// const url = `../../${src}post.md`;
// console.log('SRC', src);
// const url = `../../posts/${src}/post.md`;

let post;

if (len === 1) {
post = await import(`../../posts/${params.slug}/post.md`);
} else if (len === 2) {
const x = srcP.at(0);
post = await import(`../../posts/${x}/${params.slug}/post.md`);
} else if (len === 3) {
const x = srcP.at(0);
const y = srcP.at(1);
post = await import(`../../posts/${x}/${y}/${params.slug}/post.md`);
} else if (len === 4) {
const x = srcP.at(0);
const y = srcP.at(1);
const z = srcP.at(2);
post = await import(`../../posts/${x}/${y}/${z}/${params.slug}/post.md`);
} else {
error(404, `Could not find ${params.slug}`);
}

// console.log(post);

// console.log('URL', url);
// console.log('srcContent', srcContent);

// const post = await import(`../../posts/${params.slug}/post.md`);
// const post = await import(url);
// const post = await import(`../../${src}/post.md`);
// console.log('post');
// console.log(post);
return {
content: post.default,
meta: post.metadata
meta: post.metadata,
src
};
} catch (e) {
error(404, `Could not find ${params.slug}`);
}
}

// export const prerender = 'auto';

// export async function load({ fetch, params }) {
// try {
// const { column } = params;

// const response = await fetch('../../../api/posts');
// const allPosts = await response.json();

// let posts = allPosts.filter((post: { column: string }) => {
// return post?.column ? post.column.toLowerCase() == column.toLowerCase() : false;
// });

// let currentPage = 0;
// if (params.page) {
// currentPage = parseInt(params.page);
// }

// const totalPages = Math.ceil(posts.length / config.pagination);
// const start = currentPage * config.pagination;
// const end = currentPage === totalPages - 1 ? posts.length - 1 : start + config.pagination - 1;
// const nextPage = currentPage === totalPages - 1 ? -1 : currentPage + 1;
// const previousPage = currentPage === totalPages - 1 ? -1 : currentPage + 1;

// posts = posts.slice(start, end + 1);

// return {
// column,
// posts,
// start,
// end,
// currentPage,
// previousPage,
// nextPage,
// totalPages
// };
// } catch (e) {
// error(404, `Could not find ${params.column} (${params?.page})`);
// }
// }
12 changes: 9 additions & 3 deletions src/routes/api/posts/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@ async function getPosts() {

for (const path in paths) {
const file = paths[path];
// console.log(path);
// const srcContent = path.replace('/src/', '');
const src = path.replace('/src/posts/', '').replace('/post.md', '');
const srcP = src.split('/');
// console.log(srcP);
// console.log(src);
// const slug = path.split('/').at(-1)?.replace('.md', '');
const slug = path.split('/').at(-2) + '/';
// const slug = path.split('/').at(-2) + '/';
const slug = path.split('/').at(-2);
// console.log(slug);

if (file && typeof file === 'object' && 'metadata' in file && slug) {
const metadata = file.metadata as Omit<Post, 'slug'>;
const post = { ...metadata, slug } satisfies Post;
const post = { ...metadata, slug, src, srcP } satisfies Post;
post.published && posts.push(post);
}
}
Expand Down
3 changes: 3 additions & 0 deletions svelte.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ const config = {
adapter: adapter({
fallback: '404.html'
}),
prerender: {
handleHttpError: 'warn'
},
paths: {
base: process.argv.includes('dev') ? '' : ''
},
Expand Down

0 comments on commit af88dd9

Please sign in to comment.