|
| 1 | +import { groupBlogSidebarItemsByYear } from '@docusaurus/plugin-content-blog/client'; |
| 2 | +import { useThemeConfig } from '@docusaurus/theme-common'; |
| 3 | +import type { Props } from '@theme/BlogSidebar/Content'; |
| 4 | +import Heading from '@theme/Heading'; |
| 5 | +import type { ReactNode } from 'react'; |
| 6 | +import React, { memo } from 'react'; |
| 7 | +import Markdown from 'react-markdown'; |
| 8 | + |
| 9 | +import styles from './styles.module.css'; |
| 10 | + |
| 11 | +function BlogSidebarYearGroup({ |
| 12 | + year, |
| 13 | + yearGroupHeadingClassName, |
| 14 | + children, |
| 15 | +}: { |
| 16 | + year: string; |
| 17 | + yearGroupHeadingClassName?: string; |
| 18 | + children: ReactNode; |
| 19 | +}): React.JSX.Element { |
| 20 | + return ( |
| 21 | + <div className={styles.blogSidebarContent} role="group"> |
| 22 | + <Heading as="h3" className={yearGroupHeadingClassName}> |
| 23 | + {year} |
| 24 | + </Heading> |
| 25 | + {children} |
| 26 | + </div> |
| 27 | + ); |
| 28 | +} |
| 29 | + |
| 30 | +function BlogSidebarContent({ |
| 31 | + items: itemsRaw, |
| 32 | + yearGroupHeadingClassName, |
| 33 | + ListComponent, |
| 34 | +}: Props): ReactNode { |
| 35 | + const items = itemsRaw.map(item => ({ |
| 36 | + ...item, |
| 37 | + title: (<Markdown>{item.title}</Markdown>) as unknown as string, |
| 38 | + })); |
| 39 | + |
| 40 | + const themeConfig = useThemeConfig(); |
| 41 | + |
| 42 | + if (themeConfig.blog.sidebar.groupByYear) { |
| 43 | + const itemsByYear = groupBlogSidebarItemsByYear(items); |
| 44 | + return ( |
| 45 | + <> |
| 46 | + {itemsByYear.map(([year, yearItems]) => ( |
| 47 | + <BlogSidebarYearGroup |
| 48 | + key={year} |
| 49 | + year={year} |
| 50 | + yearGroupHeadingClassName={yearGroupHeadingClassName} |
| 51 | + > |
| 52 | + <ListComponent items={yearItems} /> |
| 53 | + </BlogSidebarYearGroup> |
| 54 | + ))} |
| 55 | + </> |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + return ( |
| 60 | + <div className={styles.blogSidebarContent}> |
| 61 | + <ListComponent items={items} /> |
| 62 | + </div> |
| 63 | + ); |
| 64 | +} |
| 65 | + |
| 66 | +export default memo(BlogSidebarContent); |
0 commit comments