feat: Show tag post count

This commit is contained in:
Leni Aniva 2024-10-10 15:20:28 -07:00
parent 82d6c2f4c3
commit 173f32fb33
Signed by: aniva
GPG Key ID: 4D9B1C8D10EA4C50
4 changed files with 11 additions and 6 deletions

View File

@ -27,7 +27,7 @@ export async function getPosts(tag: string | null = null) {
return posts; return posts;
} }
export async function getTags() { export async function getTags() : Promise<Map<string, number>> {
const allPostFiles = import.meta.glob('$content/post/*.md'); const allPostFiles = import.meta.glob('$content/post/*.md');
const iterablePostFiles = Object.entries(allPostFiles); const iterablePostFiles = Object.entries(allPostFiles);
@ -37,5 +37,7 @@ export async function getTags() {
return metadata.tags; return metadata.tags;
}) })
); );
return new Set(allPosts.flat()); return allPosts.flat().reduce((acc: Map<string, number>, curr: string) => {
return acc[curr] ? ++acc[curr] : acc[curr] = 1, acc
}, new Map());
} }

View File

@ -13,8 +13,11 @@
<hr class="separator" /> <hr class="separator" />
<div id="tags"> <div id="tags">
{#each allTags as tag} {#each Object.entries(allTags) as [tag, occ]}
<a class="tag" href="/tag/{tag}">{tag}</a> <div class="tag">
<a href="/tag/{tag}">{tag}</a>
({occ})
</div>
{/each} {/each}
</div> </div>
<hr class="separator" /> <hr class="separator" />

View File

@ -2,6 +2,6 @@ import type { PageLoad } from './$types';
import { getTags } from '$lib/posts'; import { getTags } from '$lib/posts';
export const load: PageLoad = async (_) => { export const load: PageLoad = async (_) => {
const allTags = await getTags(); let allTags = await getTags();
return { allTags }; return { allTags };
}; };

View File

@ -10,7 +10,7 @@
<title>{name} | {siteMetadata.blogName}</title> <title>{name} | {siteMetadata.blogName}</title>
</svelte:head> </svelte:head>
<h1>{name}</h1> <h1>{name} ({posts.length})</h1>
<hr class="separator" /> <hr class="separator" />
<ul id="catalog" class="content"> <ul id="catalog" class="content">