All files / src/pages tags.astro

0% Statements 0/0
0% Branches 0/0
0% Functions 0/0
0% Lines 0/0

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57                                                                                                                 
---
import { getCollection } from 'astro:content';
import Layout from '../layouts/Layout.astro';
import Tag from '../components/Tag/index.tsx';
import Sidebar from '../components/Sidebar/index.tsx';
 
const allPosts = await getCollection('blog');
const sorted = allPosts.sort(
  (a, b) => new Date(b.data.date).getTime() - new Date(a.data.date).getTime()
);
 
const mapping: { [key: string]: number } = {};
allPosts.forEach((post) => {
  post.data.tags?.forEach((name) => {
    if (name) {
      mapping[name] = (mapping[name] || 0) + 1;
    }
  });
});
 
const tags = Object.keys(mapping).sort((a, b) => mapping[b] - mapping[a]);
 
const latestPosts = sorted.slice(0, 6).map((p) => ({
  title: p.data.title,
  slug: p.slug,
  date: p.data.date.toISOString(),
}));
 
const allPostsForSidebar = sorted.map((p) => ({
  date: p.data.date.toISOString(),
  tags: p.data.tags || [],
}));
---
 
<Layout
  title="Tags"
  description="Tags Page"
  canonicalUrl="https://tubone-project24.xyz/tags/"
  noindex={true}
>
  <div class="container">
    <div class="row">
      <Sidebar
        client:load
        latestPosts={latestPosts}
        allPosts={allPostsForSidebar}
        totalCount={sorted.length}
      />
      <div class="col order-2">
        {tags.map((name) => (
          <Tag client:load name={name} count={mapping[name]} />
        ))}
      </div>
    </div>
  </div>
</Layout>