-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: blog sidebar grouped by year (#587)
* feat: add patch package dependency * feat: add patch to docusaurus * feat: add swizzle for BlogSidebar * build: add yarn script to make sure patch builds * docs: update readme for swizzled components * refactor: update blog count to ALL * docs: update README with Erick's feedback <3
- Loading branch information
1 parent
38c0c04
commit 1f8a694
Showing
10 changed files
with
449 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
diff --git a/node_modules/@docusaurus/plugin-content-blog/lib/routes.js b/node_modules/@docusaurus/plugin-content-blog/lib/routes.js | ||
index 3400a26..98b1cd8 100644 | ||
--- a/node_modules/@docusaurus/plugin-content-blog/lib/routes.js | ||
+++ b/node_modules/@docusaurus/plugin-content-blog/lib/routes.js | ||
@@ -41,6 +41,7 @@ async function buildAllRoutes({ baseUrl, content, actions, options, aliasedSourc | ||
title: blogPost.metadata.title, | ||
permalink: blogPost.metadata.permalink, | ||
unlisted: blogPost.metadata.unlisted, | ||
+ date: blogPost.metadata.date, | ||
})), | ||
}; | ||
const modulePath = await createData(`blog-post-list-prop-${pluginId}.json`, sidebar); | ||
diff --git a/node_modules/@docusaurus/plugin-content-blog/src/plugin-content-blog.d.ts b/node_modules/@docusaurus/plugin-content-blog/src/plugin-content-blog.d.ts | ||
index f4d4f13..f56deee 100644 | ||
--- a/node_modules/@docusaurus/plugin-content-blog/src/plugin-content-blog.d.ts | ||
+++ b/node_modules/@docusaurus/plugin-content-blog/src/plugin-content-blog.d.ts | ||
@@ -469,6 +469,7 @@ yarn workspace v1.22.19image` is a collocated image path, this entry will be the | ||
title: string; | ||
permalink: string; | ||
unlisted: boolean; | ||
+ date: Date | string; | ||
}; | ||
|
||
export type BlogSidebar = { | ||
diff --git a/node_modules/@docusaurus/plugin-content-blog/src/routes.ts b/node_modules/@docusaurus/plugin-content-blog/src/routes.ts | ||
index a810ce1..2bf5cee 100644 | ||
--- a/node_modules/@docusaurus/plugin-content-blog/src/routes.ts | ||
+++ b/node_modules/@docusaurus/plugin-content-blog/src/routes.ts | ||
@@ -94,6 +94,7 @@ export async function buildAllRoutes({ | ||
title: blogPost.metadata.title, | ||
permalink: blogPost.metadata.permalink, | ||
unlisted: blogPost.metadata.unlisted, | ||
+ date: blogPost.metadata.date | ||
})), | ||
}; | ||
const modulePath = await createData( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import React from 'react'; | ||
import clsx from 'clsx'; | ||
import Link from '@docusaurus/Link'; | ||
import { translate } from '@docusaurus/Translate'; | ||
import type { Props } from '@theme/BlogSidebar/Desktop'; | ||
import styles from './styles.module.css'; | ||
|
||
const SidebarHeader = ({ title }: { title: string }) => ( | ||
<div className={clsx(styles.sidebarHeader, 'margin-bottom--md')}>{title}</div> | ||
); | ||
|
||
const YearHeader = ({ year }: { year: number }) => ( | ||
<h5 className={styles.sidebarItemTitle}>{year}</h5> | ||
); | ||
|
||
const SidebarItem = ({ item }: { item: Props['sidebar']['items'][number] }) => ( | ||
<li className={styles.sidebarItem}> | ||
<Link | ||
isNavLink | ||
to={item.permalink} | ||
className={styles.sidebarItemLink} | ||
activeClassName={styles.sidebarItemLinkActive} | ||
> | ||
{item.title} | ||
</Link> | ||
</li> | ||
); | ||
|
||
export default function BlogSidebarDesktop({ sidebar }: Props) { | ||
let currentYear = null; | ||
|
||
return ( | ||
<aside className="col col--3"> | ||
<nav | ||
className={clsx(styles.sidebar, 'thin-scrollbar')} | ||
aria-label={translate({ | ||
id: 'theme.blog.sidebar.navAriaLabel', | ||
message: 'Blog recent posts navigation', | ||
description: 'The ARIA label for recent posts in the blog sidebar', | ||
})} | ||
> | ||
<SidebarHeader title={sidebar.title} /> | ||
<ul className={clsx(styles.sidebarItemList, 'clean-list')}> | ||
{sidebar.items.map((item) => { | ||
const itemYear = new Date(item.date).getFullYear(); | ||
const yearHeader = currentYear !== itemYear && ( | ||
<YearHeader key={itemYear} year={itemYear} /> | ||
); | ||
currentYear = itemYear; | ||
|
||
return ( | ||
<React.Fragment key={item.permalink}> | ||
{yearHeader} | ||
<SidebarItem item={item} /> | ||
</React.Fragment> | ||
); | ||
})} | ||
</ul> | ||
</nav> | ||
</aside> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
.sidebar { | ||
max-height: calc(100vh - (var(--ifm-navbar-height) + 2rem)); | ||
overflow-y: auto; | ||
position: sticky; | ||
top: var(--ifm-navbar-height); | ||
padding: 20px 12px 0 0; | ||
margin-left: -20px; | ||
} | ||
|
||
.sidebarHeader { | ||
font-size: var(--ifm-h4-font-size); | ||
font-weight: var(--ifm-font-weight-bold); | ||
padding-left: 12px; | ||
display: block; | ||
} | ||
|
||
.sidebarItemTitle { | ||
margin: 0.75rem 0 0.5rem; | ||
color: var(--subtle); | ||
padding-left: 12px; | ||
border-bottom: 0.01rem solid var(--ifm-table-border-color); | ||
padding-bottom: 4px; | ||
} | ||
|
||
.sidebarItemList { | ||
font-size: 13px; | ||
} | ||
|
||
.sidebarItem { | ||
margin-top: 0.1rem; | ||
line-height: 18px; | ||
} | ||
|
||
.sidebarItemLink { | ||
color: var(--ifm-font-color-base); | ||
padding: 4px 8px; | ||
display: block; | ||
border-left: 4px solid transparent; | ||
border-radius: 0.25rem; | ||
line-height: 18px; | ||
} | ||
|
||
.sidebarItemLink:hover { | ||
background: var(--ifm-menu-color-background-active); | ||
color: var(--ifm-font-color-base); | ||
text-decoration: none; | ||
} | ||
|
||
.sidebarItemLinkActive { | ||
color: var(--ifm-font-color-base); | ||
background: var(--ifm-menu-color-background-active); | ||
border-left-color: var(--ifm-menu-color-active); | ||
font-weight: 700; | ||
} | ||
|
||
@media (max-width: 996px) { | ||
.sidebar { | ||
display: none; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import React from 'react'; | ||
import Link from '@docusaurus/Link'; | ||
import { NavbarSecondaryMenuFiller } from '@docusaurus/theme-common'; | ||
import styles from './styles.module.css'; | ||
import clsx from 'clsx'; | ||
import type { Props } from '@theme/BlogSidebar/Mobile'; | ||
|
||
export const SidebarHeader = ({ title }: { title: string }) => ( | ||
<div className={clsx(styles.sidebarHeader, 'margin-bottom--md')}>{title}</div> | ||
); | ||
|
||
export const YearHeader = ({ year }: { year: number }) => ( | ||
<h5 className={styles.sidebarItemTitle}>{year}</h5> | ||
); | ||
|
||
export const SidebarItem = ({ | ||
item, | ||
}: { | ||
item: Props['sidebar']['items'][number]; | ||
}) => ( | ||
<li className="menu__list-item"> | ||
<Link | ||
isNavLink | ||
to={item.permalink} | ||
className="menu__link" | ||
activeClassName="menu__link--active" | ||
> | ||
{item.title} | ||
</Link> | ||
</li> | ||
); | ||
|
||
function BlogSidebarMobileSecondaryMenu({ sidebar }: Props) { | ||
let currentYear = null; | ||
|
||
return ( | ||
<ul className="menu__list blog-menu__list"> | ||
{sidebar.items.map((item) => { | ||
const itemYear = new Date(item.date).getFullYear(); | ||
const yearHeader = currentYear !== itemYear && ( | ||
<YearHeader key={itemYear} year={itemYear} /> | ||
); | ||
currentYear = itemYear; | ||
return ( | ||
<React.Fragment key={item.permalink}> | ||
{yearHeader} | ||
<SidebarItem item={item} /> | ||
</React.Fragment> | ||
); | ||
})} | ||
</ul> | ||
); | ||
} | ||
export default function BlogSidebarMobile(props) { | ||
return ( | ||
<NavbarSecondaryMenuFiller | ||
component={BlogSidebarMobileSecondaryMenu} | ||
props={props} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.sidebarItemTitle { | ||
margin: 0.75rem 0 0.5rem; | ||
color: var(--subtle); | ||
padding-left: 12px; | ||
border-bottom: 0.01rem solid var(--ifm-table-border-color); | ||
padding-bottom: 4px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import React from 'react'; | ||
import { useWindowSize } from '@docusaurus/theme-common'; | ||
import BlogSidebarDesktop from '@theme/BlogSidebar/Desktop'; | ||
import BlogSidebarMobile from '@theme/BlogSidebar/Mobile'; | ||
import type { Props } from '@theme/BlogSidebar'; | ||
|
||
export default function BlogSidebar({ sidebar }: Props): JSX.Element | null { | ||
const windowSize = useWindowSize(); | ||
if (!sidebar?.items.length) { | ||
return null; | ||
} | ||
// Mobile sidebar doesn't need to be server-rendered | ||
if (windowSize === 'mobile') { | ||
return <BlogSidebarMobile sidebar={sidebar} />; | ||
} | ||
return <BlogSidebarDesktop sidebar={sidebar} />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.