mirror of
https://github.com/DefectingCat/DefectingCat.github.io
synced 2025-07-15 00:31:36 +00:00
feat: add tags page
This commit is contained in:
27
app/tags/layout.tsx
Normal file
27
app/tags/layout.tsx
Normal file
@ -0,0 +1,27 @@
|
||||
import clsx from 'clsx';
|
||||
import { ReactNode } from 'react';
|
||||
|
||||
const Layout = ({ children }: { children: ReactNode }) => {
|
||||
return (
|
||||
<>
|
||||
<main
|
||||
className={clsx(
|
||||
'flex-1 max-w-4xl px-8 py-8 mx-auto',
|
||||
'lg:px-0 w-full flex-1',
|
||||
)}
|
||||
>
|
||||
<h1
|
||||
className={clsx(
|
||||
'text-5xl font-bold text-center font-Barlow',
|
||||
'mt-8 mb-20 text-gray-800 dark:text-gray-200',
|
||||
)}
|
||||
>
|
||||
Tags
|
||||
</h1>
|
||||
{children}
|
||||
</main>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default Layout;
|
60
app/tags/page.tsx
Normal file
60
app/tags/page.tsx
Normal file
@ -0,0 +1,60 @@
|
||||
import clsx from 'clsx';
|
||||
import { postLists } from 'lib/posts';
|
||||
import { Metadata } from 'next';
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: 'RUA - Tags',
|
||||
};
|
||||
|
||||
const Page = async () => {
|
||||
const posts = await postLists();
|
||||
// tag ["Rust", "React"] or "Rust"
|
||||
// tag object {"Rust": 1, "React": 1}
|
||||
const tags = posts.reduce<{
|
||||
[key: string]: number;
|
||||
}>((prev, cur) => {
|
||||
if (cur.tags?.length <= 0) {
|
||||
return prev;
|
||||
}
|
||||
if (typeof cur.tags === 'string') {
|
||||
const tag = prev[cur.tags];
|
||||
if (tag) {
|
||||
prev[cur.tags] += 1;
|
||||
} else {
|
||||
prev[cur.tags] = 1;
|
||||
}
|
||||
}
|
||||
if (Array.isArray(cur.tags)) {
|
||||
cur.tags.forEach((tag) => {
|
||||
const tagCount = prev[tag];
|
||||
if (tagCount) {
|
||||
prev[tag] += 1;
|
||||
} else {
|
||||
prev[tag] = 1;
|
||||
}
|
||||
});
|
||||
}
|
||||
return prev;
|
||||
}, {});
|
||||
|
||||
return (
|
||||
<div className={clsx('flex gap-4 flex-wrap')}>
|
||||
{Object.entries(tags).map(([tag, count]) => (
|
||||
<div
|
||||
key={tag}
|
||||
className={clsx(
|
||||
'px-4 py-2 bg-gray-200',
|
||||
'rounded-lg flex items-center',
|
||||
'gap-2 cursor-pointer',
|
||||
'dark:bg-rua-gray-800',
|
||||
)}
|
||||
>
|
||||
<div>{tag}</div>
|
||||
<div>{count}</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default Page;
|
@ -25,8 +25,8 @@ const txtMenu = [
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
name: 'Gists',
|
||||
path: '/gists',
|
||||
name: 'Tags',
|
||||
path: '/tags',
|
||||
},
|
||||
{
|
||||
id: 4,
|
||||
|
Reference in New Issue
Block a user