Skip to content

Commit

Permalink
Sort & search notes
Browse files Browse the repository at this point in the history
  • Loading branch information
Einlar committed May 19, 2024
1 parent 6f523d3 commit 8f93ab4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/components/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { CollectionEntry } from "astro:content";
export type SearchItem = {
title: string;
description: string;
data: CollectionEntry<"blog">["data"];
data: CollectionEntry<"notes">["data"];
slug: string;
};

Expand Down Expand Up @@ -112,7 +112,7 @@ export default function SearchBar({ searchList }: Props) {
{searchResults &&
searchResults.map(({ item, refIndex }) => (
<Card
href={`/posts/${item.slug}/`}
href={`/notes/${item.slug}/`}
frontmatter={item.data}
key={`${refIndex}-${item.slug}`}
/>
Expand Down
4 changes: 3 additions & 1 deletion src/pages/notes/index.astro
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
---
import Notes from "@layouts/Notes.astro";
import getPagination from "@utils/getPagination";
import getSortedNotes from "@utils/getSortedNotes";
import { getCollection } from "astro:content";
const notes = await getCollection("notes");
const sortedNotes = getSortedNotes(notes);
const pagination = getPagination({
posts: notes,
posts: sortedNotes,
page: 1,
isIndex: true,
});
Expand Down
10 changes: 5 additions & 5 deletions src/pages/search.astro
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import Main from "@layouts/Main.astro";
import Header from "@components/Header.astro";
import Footer from "@components/Footer.astro";
import SearchBar from "@components/Search";
import getSortedPosts from "@utils/getSortedPosts";
import getSortedNotes from "@utils/getSortedNotes";
// Retrieve all published articles
const posts = await getCollection("blog", ({ data }) => !data.draft);
const sortedPosts = getSortedPosts(posts);
const notes = await getCollection("notes");
const sortedNotes = getSortedNotes(notes);
// List of items to search in
const searchList = sortedPosts.map(({ data, slug }) => ({
const searchList = sortedNotes.map(({ data, slug }) => ({
title: data.title,
description: data.description,
data,
Expand All @@ -23,7 +23,7 @@ const searchList = sortedPosts.map(({ data, slug }) => ({

<Layout title={`Search | ${SITE.title}`}>
<Header activeNav="search" />
<Main pageTitle="Search" pageDesc="Search any article ...">
<Main pageTitle="Search" pageDesc="Search any note...">
<SearchBar client:load searchList={searchList} />
</Main>
<Footer />
Expand Down
15 changes: 15 additions & 0 deletions src/utils/getSortedNotes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { CollectionEntry } from "astro:content";

const getSortedNotes = (notes: CollectionEntry<"notes">[]) => {
return notes.sort(
(a, b) =>
Math.floor(
new Date(b.data.modDatetime ?? b.data.pubDatetime).getTime() / 1000
) -
Math.floor(
new Date(a.data.modDatetime ?? a.data.pubDatetime).getTime() / 1000
)
);
};

export default getSortedNotes;

0 comments on commit 8f93ab4

Please sign in to comment.