get all poems workin, get semantic search workin, get thoughts section working

This commit is contained in:
2024-06-02 19:01:57 -04:00
parent de9cccabda
commit 2faf292aab
168 changed files with 3204 additions and 31 deletions

View File

@@ -7,7 +7,8 @@
let searchQuery = '';
async function handleSearch() {
const response = await fetch(`/api/poetry/search?q=${encodeURIComponent(searchQuery)}`);
const section = window.location.pathname.split('/')[1];
const response = await fetch(`/api/${section}/search?q=${encodeURIComponent(searchQuery)}`);
if (response.ok) {
const data: SearchResult[] = await response.json();
searchResults.set(data);
@@ -45,8 +46,8 @@
<li><a href="/thoughts">Thoughts</a></li>
<li><a href="/poetry">Poetry</a></li>
<li><a href="/projects">Projects</a></li>
<li><a href="/experiments">Experiments</a></li>
<li><a href="/services">Services</a></li>
<li><a href="/experiments">Experiments</a></li>
</ul>
</div>
<a class="link-primary text-xl" href="/">silentsilas</a>
@@ -76,8 +77,8 @@
<li><a href="/thoughts">Thoughts</a></li>
<li><a href="/poetry">Poetry</a></li>
<li><a href="/projects">Projects</a></li>
<li><a href="/experiments">Experiments</a></li>
<li><a href="/services">Services</a></li>
<li><a href="/experiments">Experiments</a></li>
</ul>
</div>
</div>

View File

@@ -45,6 +45,7 @@
onMount(() => {
visible = true;
searchResults.set([]);
const interval = setInterval(getRandomGreeting, 3000);
return () => clearInterval(interval);
});

View File

@@ -3,6 +3,7 @@
import { page } from '$app/stores';
import { searchResults } from '$lib/store';
import type { SearchResult } from '$lib/utils/search';
import { onMount } from 'svelte';
import type { PageData } from '../poetry/$types';
export let data: PageData;
@@ -37,6 +38,10 @@
totalPages = Math.ceil(total / limit);
}
onMount(() => {
searchResults.set([]);
});
function navigate(page: number) {
fetchData(page);
goto(`/poetry/?page=${page}`, { replaceState: true });

View File

@@ -1,12 +1,26 @@
<script lang="ts">
import { searchResults } from '$lib/store';
import type { SearchResult } from '$lib/utils/search';
import { onMount } from 'svelte';
import type { PageData } from './$types';
export let data: PageData;
const { title, date: _date, Content, categories: _ } = data;
let results: SearchResult[] = [];
searchResults.subscribe((value: SearchResult[]) => {
results = value ? value : [];
});
onMount(() => {
searchResults.set([]);
});
</script>
<div class="container mx-auto flex flex-col items-center prose px-4">
<h1 class="py-6">{title}</h1>
<Content />
<a href="/poetry" class="link py-10">Back to Poetry</a>
</div>
{#if results.length <= 0}
<div class="container mx-auto flex flex-col items-center prose px-4">
<h1 class="py-6 mb-0">{title}</h1>
<Content />
<a href="/poetry" class="link py-10">Back to Poetry</a>
</div>
{/if}

View File

@@ -2,7 +2,7 @@ import type { Metadata } from '$lib/utils/index.js';
export async function load({ params }) {
const post = await import(`../../../../posts/poetry/${params.slug}.md`);
const { title, date, categories } = post.metadata as Metadata;
const { title, date, categories, draft } = post.metadata as Metadata;
const Content = post.default;
const parsedDate = new Date(date.slice(0, date.length - 6));
const validDate = `${
@@ -14,6 +14,7 @@ export async function load({ params }) {
title,
date: validDate,
categories,
post
post,
draft
};
}

View File

@@ -0,0 +1,15 @@
export const load = async ({ fetch, url }) => {
const limit = 8;
const page = Number(url.searchParams.get('page')) || 1;
const response = await fetch(`/api/thoughts?limit=${limit}&page=${page}`);
const { posts, total } = await response.json();
return {
posts,
total,
page,
limit
};
};

View File

@@ -0,0 +1,85 @@
<script lang="ts">
import { goto } from '$app/navigation';
import { page } from '$app/stores';
import { searchResults } from '$lib/store';
import type { SearchResult } from '$lib/utils/search';
import { onMount } from 'svelte';
import type { PageData } from '../thoughts/$types';
export let data: PageData;
let results: SearchResult[] = [];
searchResults.subscribe((value: SearchResult[]) => {
results = value ? value : [];
});
const formatDate = (date: string) => {
return new Date(date).toLocaleDateString(undefined, {
year: 'numeric',
month: 'long',
day: 'numeric'
});
};
let { posts, total } = data;
const limit = 8;
let currentPage = Number($page.url.searchParams.get('page')) || 1;
let totalPages = Math.ceil(total / limit);
$: $page.url.searchParams.get('page'),
(currentPage = Number($page.url.searchParams.get('page')) || 1);
async function fetchData(page: number) {
const response = await fetch(`/api/thoughts?limit=${limit}&page=${page}`);
const newData = await response.json();
currentPage = page;
posts = newData.posts;
total = newData.total;
totalPages = Math.ceil(total / limit);
}
onMount(() => {
searchResults.set([]);
});
function navigate(page: number) {
fetchData(page);
goto(`/thoughts/?page=${page}`, { replaceState: true });
}
</script>
{#if results.length <= 0}
<div class="container mx-auto flex flex-col items-center">
<div class="prose">
<h1 class="py-6 pt-10">Thoughts</h1>
</div>
<ul>
{#each posts as post}
<li class="py-4">
<h3 class="pb-1">
<a class="link" href={post.path}>
{post.meta.title}
</a>
</h3>
<p class="text-sm">{formatDate(post.meta.date)}</p>
</li>
{/each}
</ul>
</div>
{#if total > 1}
<nav class="join justify-end py-10">
<button
class="join-item btn-primary btn btn-outline"
on:click={() => navigate(currentPage - 1)}
disabled={currentPage === 1}>Prev</button
>
<button class="join-item btn btn-outline">{currentPage} of {totalPages}</button>
<button
class="join-item btn btn-primary btn-outline"
on:click={() => navigate(currentPage + 1)}
disabled={currentPage === totalPages}>Next</button
>
</nav>
{/if}
{/if}

View File

@@ -0,0 +1,26 @@
<script lang="ts">
import { searchResults } from '$lib/store';
import type { SearchResult } from '$lib/utils/search';
import { onMount } from 'svelte';
import type { PageData } from './$types';
export let data: PageData;
const { title, date: _date, Content, categories: _ } = data;
let results: SearchResult[] = [];
searchResults.subscribe((value: SearchResult[]) => {
results = value ? value : [];
});
onMount(() => {
searchResults.set([]);
});
</script>
{#if results.length <= 0}
<div class="container mx-auto flex flex-col items-center prose px-4">
<h1 class="pt-10">{title}</h1>
<Content />
<a href="/thoughts" class="link py-10">Back to Thoughts</a>
</div>
{/if}

View File

@@ -0,0 +1,20 @@
import type { Metadata } from '$lib/utils/index.js';
export async function load({ params }) {
const post = await import(`../../../../posts/thoughts/${params.slug}.md`);
const { title, date, categories, draft } = post.metadata as Metadata;
const Content = post.default;
const parsedDate = new Date(date.slice(0, date.length - 6));
const validDate = `${
parsedDate.getMonth() + 1
}/${parsedDate.getDate()}/${parsedDate.getFullYear()}`;
return {
Content,
title,
date: validDate,
categories,
post,
draft
};
}

View File

@@ -0,0 +1,15 @@
import { fetchMarkdownPosts } from '$lib/utils';
import { json } from '@sveltejs/kit';
export const GET = async ({ url }) => {
const page = Number(url.searchParams.get('page')) || 1;
const limit = Number(url.searchParams.get('limit')) || 8;
const offset = (page - 1) * limit;
const { posts: allPosts, total: total } = await fetchMarkdownPosts('thoughts', limit, offset);
const sortedPosts = allPosts.sort((a, b) => {
return new Date(b.meta.date).getTime() - new Date(a.meta.date).getTime();
});
return json({ posts: sortedPosts, total: total, page: page });
};