|
| 1 | +import React from 'react'; |
| 2 | +import Link from '@docusaurus/Link'; |
| 3 | +import {translate} from '@docusaurus/Translate'; |
| 4 | +import {PageMetadata} from '@docusaurus/theme-common'; |
| 5 | +import Layout from '@theme/Layout'; |
| 6 | +function Year({year, posts}) { |
| 7 | + return ( |
| 8 | + <> |
| 9 | + <h3>{year} ({posts.length})</h3> |
| 10 | + <ul> |
| 11 | + {posts.map((post) => ( |
| 12 | + <li key={post.metadata.date}> |
| 13 | + <Link to={post.metadata.permalink}> |
| 14 | + {post.metadata.formattedDate} - {post.metadata.title} |
| 15 | + </Link> |
| 16 | + </li> |
| 17 | + ))} |
| 18 | + </ul> |
| 19 | + </> |
| 20 | + ); |
| 21 | +} |
| 22 | +function YearsSection({years}) { |
| 23 | + return ( |
| 24 | + <section className="margin-vert--lg"> |
| 25 | + <div className="container"> |
| 26 | + <div className="row"> |
| 27 | + {years.map((_props, idx) => ( |
| 28 | + <div key={idx} className="col col--4 margin-vert--lg"> |
| 29 | + <Year {..._props} /> |
| 30 | + </div> |
| 31 | + ))} |
| 32 | + </div> |
| 33 | + </div> |
| 34 | + </section> |
| 35 | + ); |
| 36 | +} |
| 37 | +function listPostsByYears(blogPosts) { |
| 38 | + const postsByYear = blogPosts.reduceRight((posts, post) => { |
| 39 | + const year = post.metadata.date.split('-')[0]; |
| 40 | + const yearPosts = posts.get(year) ?? []; |
| 41 | + return posts.set(year, [post, ...yearPosts]); |
| 42 | + }, new Map()); |
| 43 | + return Array.from(postsByYear, ([year, posts]) => ({ |
| 44 | + year, |
| 45 | + posts, |
| 46 | + })); |
| 47 | +} |
| 48 | +export default function BlogArchive({archive}) { |
| 49 | + const title = translate({ |
| 50 | + id: 'theme.blog.archive.title', |
| 51 | + message: 'Archive', |
| 52 | + description: 'The page & hero title of the blog archive page', |
| 53 | + }); |
| 54 | + const description = translate({ |
| 55 | + id: 'theme.blog.archive.description', |
| 56 | + message: 'Archive', |
| 57 | + description: 'The page & hero description of the blog archive page', |
| 58 | + }); |
| 59 | + const years = listPostsByYears(archive.blogPosts); |
| 60 | + return ( |
| 61 | + <> |
| 62 | + <PageMetadata title={title} description={description} /> |
| 63 | + <Layout> |
| 64 | + <header |
| 65 | + className="hero hero--primary" |
| 66 | + style={{ |
| 67 | + background: "url('https://images.unsplash.com/photo-1571607073129-606b83b78f02?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=2070&q=80')", |
| 68 | + backgroundRepeat: "no-repeat", |
| 69 | + backgroundPosition: "center", |
| 70 | + width: "100%" |
| 71 | + }} |
| 72 | + > |
| 73 | + <div className="container"> |
| 74 | + <h1 className="hero__title">{title}</h1> |
| 75 | + </div> |
| 76 | + </header> |
| 77 | + <main>{years.length > 0 && <YearsSection years={years} />}</main> |
| 78 | + </Layout> |
| 79 | + </> |
| 80 | + ); |
| 81 | +} |
0 commit comments