Skip to content

feat: sort release blog by semver order #8050

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions apps/site/components/withBlogCrossLinks.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import type { FC } from 'react';
import semver from 'semver';

import { getClientContext } from '#site/client-context';
import CrossLink from '#site/components/Common/CrossLink';
import getBlogData from '#site/next-data/blogData';
import type { BlogCategory } from '#site/types';

const extractVersionFromTitle = (title: string): string | null => {
const match = title.match(/v(\d+\.\d+\.\d+)/);
return match ? match[1] : null;
};

const WithBlogCrossLinks: FC = () => {
const { pathname } = getClientContext();

Expand All @@ -18,14 +24,34 @@ const WithBlogCrossLinks: FC = () => {

const { posts } = getBlogData(category);

const currentItem = posts.findIndex(
// Sort posts by semver for release category
const sortedPosts =
category === 'release'
? posts.toSorted((a, b) => {
const versionA = extractVersionFromTitle(a.title);
const versionB = extractVersionFromTitle(b.title);

if (versionA && versionB) {
// Sort by semver in descending order (newest first)
return semver.rcompare(versionA, versionB);
}

// Fallback to date sorting if version extraction fails
return b.date.getTime() - a.date.getTime();
})
: posts;

const currentItem = sortedPosts.findIndex(
({ slug }) => slug === `/blog/${category}/${postname}`
);

const [previousCrossLink, nextCrossLink] = [
posts[currentItem - 1],
posts[currentItem + 1],
];
// For release posts sorted by semver (descending):
// - Previous should point to an older version (higher index)
// - Next should point to a newer version (lower index)
const [previousCrossLink, nextCrossLink] =
category === 'release'
? [sortedPosts[currentItem + 1], sortedPosts[currentItem - 1]]
: [sortedPosts[currentItem - 1], sortedPosts[currentItem + 1]];

return (
<div className="max-xs:grid-cols-1 mt-4 grid w-full grid-cols-2 gap-4">
Expand Down
Loading