diff --git a/src/components/LeetCodeDifficulty.astro b/src/components/LeetCodeDifficulty.astro index 0140e87..73f9e4f 100644 --- a/src/components/LeetCodeDifficulty.astro +++ b/src/components/LeetCodeDifficulty.astro @@ -1,11 +1,21 @@ --- +import type { HTMLAttributes } from 'astro/types' import kebabCase from 'lodash.kebabcase' -export interface Props { +export interface Props extends HTMLAttributes<'a' | 'span'> { + tag?: string difficulty: string } -const { difficulty } = Astro.props +const { difficulty, tag = 'a', ...props } = Astro.props + +const Tag = tag + +const tagProps: Record = {} +if (tag === 'a') { + tagProps.href = `/difficulties/${kebabCase(difficulty)}` +} + const difficultyStyleMap: Record = { Easy: 'difficulty-easy', Medium: 'difficulty-medium', @@ -21,12 +31,17 @@ const getDifficultyCssClass: (difficulty: string) => string = ( } --- - {difficulty} - + diff --git a/src/pages/difficulties/[slug].astro b/src/pages/difficulties/[slug].astro new file mode 100644 index 0000000..01d4240 --- /dev/null +++ b/src/pages/difficulties/[slug].astro @@ -0,0 +1,52 @@ +--- +import { getCollection } from 'astro:content' +import kebabCase from 'lodash.kebabcase' + +import LeetCodeDifficulty from '@/components/LeetCodeDifficulty.astro' +import PostList from '@/components/post/PostList.astro' +import siteConfig from '@/configs/site' +import AppLayout from '@/layouts/AppLayout.astro' + +export async function getStaticPaths() { + const allDifficulties = new Set() + const allPosts = await getCollection( + 'leetcode-solutions', + ({ data }) => data.difficulty?.length > 0 + ) + allPosts.forEach((post) => allDifficulties.add(post.data.difficulty)) + + return Array.from(allDifficulties).map((difficulty) => { + const slug = kebabCase(difficulty) + const filteredPosts = allPosts.filter( + (post) => post.data.difficulty === difficulty + ) + return { + params: { slug }, + props: { + posts: filteredPosts, + difficulty, + }, + } + }) +} + +const { difficulty, posts } = Astro.props +const { title, description, author } = siteConfig +--- + + +
+
+

+ Posts by difficulty: + +

+
+ +
+